Changelink.py

From MythTV Official Wiki
Jump to: navigation, search

Important.png Note: The correct title of this article is changelink.py. It appears incorrectly here due to technical restrictions.


This is a pywikipedia script, developed to support maintenance of this site.

There are many page links that appear as Foo Bar, where the article is titled FooBar. This script will update all the pages correctly.

Source

PythonIcon.png changelink.py

import re, sys
from pywikipedia import wikipedia

oldname = sys.argv[1]
newname = sys.argv[2]

site = wikipedia.getSite()
page = wikipedia.Page(site=site, title=oldname)
links = page.getReferences()
for eachPage in links:
        wikitext = eachPage.get()
        newtext = re.compile("\[\[" + oldname + "\]\]").sub("[[" + newname + "]]", wikitext)
        try:
                eachPage.put(newtext=newtext, \
                        comment='[[pywikipedia]] assisted cleanup -> replacing ' + \
                        oldname + ' with [[' + newname + ']]', minorEdit=True)
        except Exception:
                print "Unable to edit " +eachPage.aslink()

Usage

To run the script, it needs two arguments. (Use quotation marks to escape the shell).

% python changelink.py "Foo Bar" "FooBar"

It will find every article pointed at Foo Bar, substitue [[FooBar]], and then write it as a minor edit. This helps reduce the number of Special:Wantedpages.