User:MediaWiki Update Bot/commonsmwupdate specific.py

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

Adapted from basic.py (provided in the framework).

I have a bad habit of hardcoding variables and editing the file to change them, rather than making them command line arguments. Sorry about that.

If any element of the sidebar changes, this will need some updating.


# -*- coding: utf-8  -*-
# -*- coding: utf-8  -*-


## section 1: VARIABLES. CHANGE BEFORE RUNNING

#langcode = "lt"
# pt pl es 'it','ja','ru','cs
lcs = ['ko','nl','sk','sv','sr','is','th','ast','be-tarask','bg','da','et','eo','ext','fo','tr','uk','yue','zh-hans','zh-hant','ar','he']

## GENERICS.

prefix = "MediaWiki:"


def getuploadlink(langcode):
        return "Commons:Upload/" + langcode

def getpagenames(langcode):

        fakecodes = ["/"+langcode+s for s in ['ownwork','fromflickr','fromwikimedia','fromgov']]

        # all in the MediaWiki namespace, add that later
        pages = [ 
                   # account links
                  "Mytalk", "Mypreferences", "Mywatchlist", "Mycontris", "Userlogout",
                   # tab label
                  "Nstab-special",
                   # search button labels
                  "Ilsubmit", "Powersearch", "Search",
                   # more menu labels, links
                  "Searcharticle", "Searchbutton", "Toolbox", "Specialpages",
                   # footer
                  "Privacy", "Aboutsite", "Disclaimerpage", "Disclaimers",
                   # from Sidebar: 
                  "Navigation", "Mainpage", "Welcome-url", "Welcome",
                  "Portal-url", "Portal", "Village pump-url", "Village pump",
                  "Participate", "Upload-url", "Upload", "Recentchanges", 
                  "Latestfiles", "Randomimage", "Helppage", "Help", "Contact-url",
                  "Contact", "Sitesupport-url", "Sitesupport",
                  # on form
                  #"Sourcefilename",  - seems to cause edit conflicts????
                  "Destfilename", "License","Nolicense",
                  "Fileuploadsummary","Watchthisupload", "Ignorewarnings", "Uploadbtn",
                  "Watchthisupload", "Upload-permitted", "Upload-maxfilesize"
                  ]

        pagenames = [prefix + page + fc for fc in fakecodes for page in pages]
        return pagenames

uploadpage = "Upload-url"

import wikipedia
import pagegenerators
import sys

class BasicBot:
    # Edit summary message that should be used.
    msg = {        'commons': u'Bot: updating non-English link target (see Commons:Upload)'    }

    def __init__(self, generator, debug):
        self.generator = generator
        self.debug = debug

    def run(self, langcode):
        # Set the edit summary message
        wikipedia.setAction(wikipedia.translate(wikipedia.getSite(), self.msg))
        for page in self.generator:
            self.treat(page, langcode)

    def treat(self, page, langcode):
        text = "{{" + basename(page.title()) + "/" + langcode + "}}"       
        page.put(text)
        # only save if something was changed
        if text : #!= page.get():
            # Show the title of the page where the link was found.
            # Highlight the title in purple.
            colors = [None] * 6 + [13] * len(page.title()) + [None] * 4
            wikipedia.output(u"\n\n>>> %s <<<" % page.title(), colors = colors)

def basename(title):
    return title[:title.find("/")]
            
def main():
    # The generator gives the pages that should be worked upon.
    gen = None
    # If debug is True, doesn't do any real changes, but only show
    # what would have been changed.
    debug = False

    
    # The preloading generator is responsible for downloading multiple
    # pages from the wiki simultaneously.
    for langcode in lcs:
            pagenames = getpagenames(langcode)
            uploadlink = getuploadlink(langcode)
            gen = pagegenerators.PagesFromTitlesGenerator(pagenames)
            bot = BasicBot(gen, debug)
            bot.run(langcode)

            print "done"
            print "Now change", prefix + uploadpage + "/" + langcode,
            print " to this: ", uploadlink


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