from wikitools import *
import time
import datetime
import urllib
import json
import userpassbot #Bot password
import warnings
import re
import mwparserfromhell
import datetime
import sys
import SRconfig

site = wiki.Wiki() #Tell Python to use the English Wikipedia's API
site.login(userpassbot.username, userpassbot.password) #login

#routine to autoswitch some of the output - as filenames have accented chars!
def pnt(s):
    try:
        print(s)
    except UnicodeEncodeError:
        print(s.encode('utf-8'))
      
def startAllowed():
    textpage = page.Page(site, "User:RonBot/5/Run").getWikiText()
    if textpage == "Run":
        return "run"
    else:
        return "no"

def allow_bots(text, user):
    user = user.lower().strip()
    text = mwparserfromhell.parse(text)
    for tl in text.filter_templates():
        if tl.name.matches(['bots', 'nobots']):
            break
    else:
        return True
    print "template found" #Have we found one
    for param in tl.params:
        bots = [x.lower().strip() for x in param.value.split(",")]
	if param.name == 'allow':
            print "We have an ALLOW" # allow found
            if ''.join(bots) == 'none': return False
            for bot in bots:
                if bot in (user, 'all'):
                    return True
        elif param.name == 'deny':
            print "We have a DENY" # deny found
            if ''.join(bots) == 'none':
                print "none - true"
                return True
	    for bot in bots:
                if bot in (user, 'all'):
                    pnt(bot)
                    pnt(user)
                    print "all - false"
                    return False
    if (tl.name.matches('nobots') and len(tl.params) == 0):
        print "match - false"
        return False
    return True

def SearchReplace(search, size):
    params = {'action':'query',
             'list':'search',
             'srsearch':search, 
             'srnamespace':0,
             'srlimit':size
             }
    print search
    #print "SR.params"
    result="" #clear out previous run
    request = api.APIRequest(site, params) #Set the API request
    #print "SR.request"
    result = request.query(False)
    #print result
    totalhits=result['query']['searchinfo']['totalhits']
    print "search", search
    print "TotalHits this search", totalhits
    if size=="max":
        size=totalhits
    if totalhits>0:
        for loopvar in range(0, size):
            #print result
            #print ""
            pagetitle = result['query']['search'][loopvar]['title']
            pagetitletext = pagetitle.encode('utf-8')
            pnt(pagetitletext)
            pagepage = page.Page(site, pagetitle)
            #print "main.pagepage"
            pagetext = pagepage.getWikiText() 
            pagetext=re.sub(r'{{[Ss]tart\s*?box}}','{{S-start}}', pagetext)
            pagetext=re.sub(r'{{[Ee]nd\s*?box}}','{{S-end}}',pagetext)
            #Stop if there's nobots
            stop = allow_bots(pagetext, "RonBot")
            if not stop:
                continue
            print "Bot allowed to edit page"
            #if SRconfig.tagged>=50:
            #    sys.exit('done for now') #Code to limit number of items
            #print "============================================TOP OF ORIG======================"
            #pnt(pagetext)
            #print "============================================BOTTOM OF ORIG======================"
            pagelower=pagetext.lower()
            Infoboxes=pagelower.count('{{infobox')
            InfoAlbum=pagelower.count('{{infobox album')
            InfoSong=pagelower.count('{{infobox song')
            InfoSingle=pagelower.count('{{infobox single')
            print "Infobox check", Infoboxes, InfoAlbum, InfoSong, InfoSingle
            Infobox=InfoAlbum+InfoSong+InfoSingle
            #if pagetitletext<>"User:Ronhjones/Sandbox4":  #OUT
            #    Infoboxes=99                              #OUT
            if Infoboxes==Infobox:
                Manual=pagetext.count('Category:Music pages for manual succession box removal')
                if 's-start' in pagelower:
                    SStart=1
                else:
                    SStart=0
                print "SStart", SStart
                print "Manual", Manual
                if Manual==0:
                    if SStart==1:
                        Nav1=pagelower.count('{{navbox')
                        Nav2=pagelower.count('{{navbox.?}}')+pagetext.count('{{navboxes.?}}')
                        print "Navboxes Total",Nav1
                        print "Navboxex Closed", Nav2
                        if Nav1==Nav2: #If Nav1 <> Nav2 then there is a Navbox constructed in the page.
                            #Look for a box With header = then whitespace then {{S-start) to {{S-end}} or {{end}
                            #If found repace with the = found and add BOTREM
                            #Both types of replace done twice - first time with extra blank line
                            pagetext = re.sub(r'==\s{{[Ss]-start[\S\s]*?{{[Ss][\s\S]*?[Ss{][-{][Ee]nd}}\n*','==BOTREM', pagetext)
                            #Is there a header to the header?
                            pagetext = re.sub(r'==\n.*?=*?BOTREM','==BOTREM', pagetext)
                            #Is there a header to the header again? Three headers in a row would be extreme!
                            pagetext = re.sub(r'==\n.*?=*?BOTREM','==BOTREM', pagetext)
                            #Remove whole line if a header with BOTREM at end
                            pagetext = re.sub(r'.*?=*?BOTREM', '', pagetext)
                            #Look for a box {{S-start) to {{S-end}} or {{end}
                            pagetext = re.sub(r'{{[Ss]-start[\S\s]*?{{[Ss][\s\S]*?[Ss{][-{][Ee]nd}}\n*', '', pagetext)
                            try:
                                pagepage.edit(text=pagetext, bot=True, summary="(Task 5 - Removal of succession box as per [[Wikipedia:Manual of Style/Record charts]]") #(DO NOT UNCOMMENT UNTIL BOT IS APPROVED)
                                SRconfig.tagged += 1
                                print "writing changed page"
                            except:
                                print"Failed to write"
                        else:
                            #Tag the Page for manual navbox removal
                            pagetext=pagetext+"\r\n[[Category:Music pages for manual succession box removal]]"
                            try:
                                pagepage.edit(text=pagetext, bot=True, summary="(Task 5 - Tagging for manual removal of succession box as per [[Wikipedia:Manual of Style/Record charts]]") #(DO NOT UNCOMMENT UNTIL BOT IS APPROVED)
                                SRconfig.tagged += 1
                                print "writing page, tagged manual"
                            except:
                                print "failed to write manual"
                    else:
                        print "No S-starts"
                else:
                    print "Manual cat alredy done"
            else:
                print "Bad Infoxes Found"
            #print "++++++++++++++++++++++++++++++++++++++++++++TOP OF NEW+++++++++++++++++++++++++" 
            #pnt(pagetext)
            #print "++++++++++++++++++++++++++++++++++++++++++++BOTTOM OF NEW++++++++++++++++++++"
            print "End of Page"
            print "Pages done so far", SRconfig.tagged
            print ""
    else:
        print "no pages to do"
        print ""
    return


def main():
    go = startAllowed() #Check if task is enabled
    if go == "no":
        sys.exit('Disabled Task')
    #parameters for API request
    SRconfig.tagged=0
    search='hastemplate:"Infobox song" insource:"{{s-start}}"'
    SearchReplace(search,"max")
    search='hastemplate:"Infobox album" insource:"{{s-start}}"'
    SearchReplace(search,"max")
    search='hastemplate:"Infobox single" insource:"{{s-start}}"'
    SearchReplace(search,"max")
    search='hastemplate:"Infobox song" insource:"{{start box}}"'
    SearchReplace(search,"max")
    search='hastemplate:"Infobox album" insource:"{{start box}}"'
    SearchReplace(search,"max")
    search='hastemplate:"Infobox single" insource:"{{start box}}"'
    SearchReplace(search,"max")
        
if __name__ == "__main__":
    with warnings.catch_warnings():
        warnings.simplefilter("ignore", FutureWarning)
        main()