User:SlaungerBot/Jean-Pol GRANDMONT src

This is the source code, which has been used to do the Jean-Pol GRANDMONT job <source lang="python"> import cPickle as pickle 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

if __name__ == "__main__":

   replace_templ_on_file_pages(u"Template:Jean-PolGRANDMONTCredit", 
                               u"{{subst:User:Jean-Pol GRANDMONT/License}}",
                               u"Extracting cc-by-sa-3.0 license from transcluded user template "
                               u"to comply with Commons policy.", 
                               max_files=100)

<source>