Replacetext.py
From MythTV
- The correct title of this article is replacetext.py. It appears incorrectly here due to technical restrictions.
replacetext.py is a pywikipedia script used to scan the entire site for a particular string, and substitute it with another. It intelligently only looks at real pages, not redirects, categories, or images. It only updates a page if there is a substitution.
[edit]
Source code
import re, sys
from pywikipedia import wikipedia
oldtext = sys.argv[1]
newtext = sys.argv[2]
site = wikipedia.getSite()
for page in site.allpages():
if not page.isCategory() and not page.isRedirectPage() and not page.isImage():
wikitext = page.get()
newtext = re.compile(oldtext).sub(newtext, wikitext)
try:
if wikitext <> newtext:
page.put(newtext=newtext, \
comment='[[pywikipedia]] assisted cleanup ([[replacetext.py]])-> replacing ' + \
oldtext + ' with ' + newtext, minorEdit=True)
except Exception:
print "Unable to edit " + page.aslink()
[edit]
Usage
To run the script, it needs two arguments. (Use quotation marks to escape the shell).
% python replacetext.py "old text" "new text"
It will check every article, and if it contains "old text", it will replace it with "new text".
