User:SlaungerBot/Fcb981

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search

To substitute User:Fcb981/license, the following small script was run

'''
Created on 01/08/2009

@author: Kim Hansen
'''
import userlicensetemplates as ult

if __name__ == '__main__':
    ult.replace_templ_on_file_pages(u"Template:Fcb981c", 
                                    u"{{subst:User:Fcb981/license}}", 
                                    u"Extracting relicensed GFDL license from transcluded user template "
                                    u"to comply with [[Commons:User-specific galleries, templates and categories policy|Commons policy]].",
                                    max_files = 100)

where the userlicensetemplates.py module was almost exactly identical to the script used for the Jean-Pol GRANDMONT job:

import re

import wikipedia as wiki
import pagegenerators as pg

def replace_template_in_text(templ_name, repl, text):
    stripped = templ_name.lstrip("Template:")
    c = stripped[0]
    regex = "\{\{:?([Tt]emplate:)?[" + c.upper() + c.lower() + "]" + stripped[1:] + "\}\}"
    return re.sub(regex, repl, text)

def replace_templ_on_file_pages(old_template_name, new_subst, comment, max_files=1000):
    file_ns = 6
    site = wiki.getSite()
    old_templ_page = wiki.Page(site, old_template_name)
    where_transcluded_gen = pg.ReferringPageGenerator(old_templ_page, onlyTemplateInclusion=True)
    file_pages_transcluded_gen = pg.NamespaceFilterPageGenerator(where_transcluded_gen, [file_ns])
    skipped = []
    for i, p in enumerate(file_pages_transcluded_gen):
        if i >= max_files:
            break
        if p.canBeEdited():
            text = p.get()
            new_text = replace_template_in_text(old_template_name, new_subst, text)
            if new_text == text:
                print "%s could not be found in %s" % (old_template_name, repr(text))
            else:
                p.put(new_text, comment=comment, watchArticle=None, minorEdit=True, sysop=False)
            print "%d of %d processed (%5.1f%%)" % (i+1, max_files, 100.0 * (i+1) / max_files)
        else:
            new_skip = p.aslink()
            print "%s could not be edited." % new_skip
            skipped.append(new_skip)
    if len(skipped) > 0:
        print "Skipped:"
        print skipped