Page MenuHomePhabricator

Port clear_cfd_templates.py to pywikibot-core
Open, Needs TriagePublic5 Estimated Story Points

Description

I have a script called clear_cfd_templates.py that I wrote over half a decade ago and have been running actively on the English Wikipedia ever since. It looks at the CFD Retain working page and clears the CFD templates from category pages whose CFD decision was "Keep". This is one of the few remaining scripts I have left that isn't running in -core. The really strange thing is that I can't find any evidence of it having ever existing in the -core git logs, even though it appears to have code incorporated into it from other contributors. Perhaps it didn't make it in the great transition from SVN?

I'm thinking of, rather than doing a straight port, first taking the existing cfd.py script and adding a mode flag to it (similar to how category.py can be called with add/remove/move modes), and then adding clearing of CFD templates as a new mode. The existing mode can be called process and the new mode can be called retain.

Event Timeline

Here is the existing script that runs on pywikibot-compat:

# -*- coding: utf-8 -*-                                                                                                             
"""                                                                                                                                 
This script processes the Categories for discussion/Retain working page.                                                            
It parses a list of category links and removes the CFD links from each one.                                                         
                                                                                                                                    
Syntax: python clear_cfd_templates.py                                                                                               
                                                                                                                                    
"""
#                                                                                                                                   
# (C) Ben McIlwain, 2011                                                                                                            
# (C) Pywikipedia bot team, 2011                                                                                                    
#                                                                                                                                   
# Distributed under the terms of the MIT license.                                                                                   

import wikipedia as pywikibot
import re
import category, catlib

retainPage = 'Wikipedia:Categories for discussion/Working/Retain'
retainSummary = 'Robot - Result of [[WP:CFD|CFD]] discussion was Keep; removing CFD template(s).'
maintainSummary = 'Robot - Updating list of categories with CFD templates to process.'
retaincat = re.compile(r"^\*\s*\[\[\:Category\:([^\]]*?)\]\](.*)$", re.IGNORECASE)
botStr = 'BOT: '

class Enum(set):
    def __getattr__(self, name):
        if name in self:
            return name
        raise AttributeError

Status = Enum(["REMOVED", "NO_PAGE", "NO_TEMPLATE", "ERROR"])

messages = {
    Status.REMOVED     : "Successfully removed.",
    Status.NO_PAGE     : "The category page could not be found.",
    Status.NO_TEMPLATE : "Could not find CFD template on category page.",
    Status.ERROR       : "Some other error occurred."
}

class ReCheck:
    def __init__(self):
        self.result = None
    def check(self, pattern, text):
        self.result = pattern.search(text)
        return self.result

def main():
    pywikibot.handleArgs()
    page = pywikibot.Page(pywikibot.getSite(), retainPage)
    catname = None
    m = ReCheck()
    cfdTemplates = pywikibot.translate(pywikibot.getSite(), category.cfd_templates)
    oldtext = page.get()
    newtextarr = []
    for line in oldtext.split("\n"):
        if (m.check(retaincat, line)):
            if (botStr in m.result.group(2)):
                newtextarr.append(line)
            else:
                catname = m.result.group(1)
                status = clearCfdTemplates(catname, cfdTemplates)
                if (status == Status.REMOVED) :
                    pass  # Handled successfully, don't re-add to page.                                                             
                else:
                    newtextarr.append(line + " - " + botStr + messages[status] + " --~~~~")
        else:
            newtextarr.append(line)

    newtext = "\n".join(newtextarr)
    if (newtext == oldtext):
        pywikibot.output("No changes were made.")
    else:
        pywikibot.output("Committing changes to working page.")
        page.put(newtext, maintainSummary)

def clearCfdTemplates(catname, cfdTemplates):
    status = Status.ERROR
    page = pywikibot.Page(pywikibot.getSite(), "Category:" + catname)
    try:
        pageSrc = page.get()
        newSrc = catlib.remove_cfd_templates(cfdTemplates, pageSrc)
        if (newSrc == pageSrc):
            pywikibot.output("No CFD templates found on category page: %s" % catname)
            status = Status.NO_TEMPLATE
        else:
            pywikibot.output("Removing CFD templates from category page: %s" % catname)
            page.put(newSrc, retainSummary)
            status = Status.REMOVED
    except pywikibot.NoPage:
        pywikibot.output("Category page not found: %s" % catname)
        status = Status.NO_PAGE
    return status

if __name__ == "__main__":
    try:
        main()
    finally:
        pywikibot.stopme()

@Cyde: Hi! This task has been assigned to you a while ago. Could you maybe share an update? Do you still plan to work on this task?
If this task is not resolved and only if you do not plan to work on this task anymore: Please consider removing yourself as assignee (via Add Action...Assign / Claim in the dropdown menu): That would allow others to work on this (in theory), as others won't think that someone is already working on this. Thanks! :)

Aklapper removed Cyde as the assignee of this task.Mar 6 2020, 7:31 PM

@Cyde: I am resetting the assignee of this task because there has not been progress lately (please correct me if I am wrong!). Resetting the assignee avoids the impression that somebody is already working on this task. It also allows others to potentially work towards fixing this task. Please claim this task again when you plan to work on it (via Add Action...Assign / Claim in the dropdown menu) - it would be welcome. Thanks for your understanding!