Jump to content

User:PearBOT/Infobox request/source

From Wikipedia, the free encyclopedia
import pywikibot
import re
from pywikibot import pagegenerators
site = pywikibot.Site('en', 'wikipedia')
checkpage = pywikibot.Page(site, u"User:PearBOT/Infobox request config")
linkpage = pywikibot.Page(site, u"User:PearBOT/linkpage")
logpage =  pywikibot.Page(site, u"User:PearBOT/logpage")
allregex = "{{\s?([iI]nfobox [^|]*)"
generator=pagegenerators.GeneratorFactory()
notinfoboxregex = "\s*[mM]apframe"

def infoboxregex(template,project):
    if project[1].get("infoboxes") is not "" and "infoboxes" in project[1].keys():
        if re.search(re.sub("\&","|",project[1].get(re.escape("infoboxes"))),template[0]):
            return True
        else:
            return False
    else:
        if re.search(allregex,template[0]) and not re.search(allregex+notinfoboxregex,template[0]):
            return True
        else:
            return False

def removerequest(talkpage,project):
    talkpagetext=talkpage.text
    talkpagetemplates = pywikibot.textlib.extract_templates_and_params(talkpage.text,remove_disabled_parts="yes",strip = "yes")
    removeregex = "({{\s?"+re.sub("\&","|",re.escape(project[1].get("template")))+"[^{}]*)\s?\|\s?"+re.sub("\&","|",re.escape(project[1].get("parameter")))+"[^\|}]*"
    print(removeregex)
    if not re.search(page.full_url(),logpage.text):
        for template in talkpagetemplates:
            if re.search(re.sub("\&","|",re.escape(project[1].get("template"))),template[0]):
                talkpagetext=re.sub(removeregex,re.search(removeregex,talkpagetext).group(1),talkpagetext)
                talkpage.text=talkpagetext
                logpage.text+="\n"+page.full_url()
                talkpage.save("Removed infobox request since the article has an infobox. ([[Wikipedia:Bots/Requests for approval/PearBOT 2|Task 2]])")

checkpagetemplates=pywikibot.textlib.extract_templates_and_params(checkpage.text,remove_disabled_parts="yes",strip = "yes")
for project in checkpagetemplates:
    if project[0] == "User:PearBOT/Infobox request/config/template":
        category="Category:"+project[1].get("category")
        for page in generator.getCategoryGen(category):
            article = page.toggleTalkPage()
            talkpage= page
            articletext=re.sub("==(a|[^a])*","",article.text)
            articletemplates = pywikibot.textlib.extract_templates_and_params(articletext,remove_disabled_parts="yes",strip = "yes")
            for template in articletemplates:
                if infoboxregex(template,project):
                    removerequest(talkpage,project)