User:TalBot/nsrw-move.py
From Wikisource
#! /usr/bin/env python
# _*_ coding: utf8 _*_
#
# Move the New Student's Reference Work pages to the Page namespace; place soft
# redirects; remove navigation template
#
# (modified version of an earlier page move script)
#
# run with args "-log -putthrottle:xx"
#
# Copyright (C) 2006, 2007 GrafZahl (en.wikisource.org user)
#
# Licence: GPLv2
#
import wikipedia
# Handle args
args = wikipedia.handleArgs()
for arg in args:
wikipedia.output(u'(WWW) Ignoring unrecognised argument: %s\n' % arg)
# basic text tokens, etc.
numbooks = 5
numpages = [ 570, 579, 554, 557, 532 ]
base_old = u'The New Student\'s Reference Work/%d-%04d'
base_new = u'Page:LA2-NSRW-%d-%04d.jpg'
base_overwrite = u'{{subst:dated soft redirect|"[[Page:LA2-NSRW-%d-%04d.jpg]]"}}'
base_search = u'\\{\\{LA2-NSRW\\|%d-%04d\\|%d-%04d\\|%d-%04d\\}\\}\n'
movesumm = u'[bot] conversion to ProofreadPage naming conventions'
oversumm = u'[bot] conversion to dated soft redirect'
replacesumm = u'[bot] removing navigation template'
# Start operation
site = wikipedia.getSite()
for b in range(1, numbooks + 1):
for n in range(1, numpages[b-1] + 1):
# Get all strings straight
if n > 1:
bminus = b
nminus = n - 1
elif b > 1:
bminus = b - 1
nminus = numpages[b-2]
else:
bminus = 1
nminus = 1
if n < numpages[b-1]:
bplus = b
nplus = n + 1
elif b < numbooks:
bplus = b + 1
nplus = 1
else:
bplus = b
nplus = numpages[b-1]
oldpage = base_old % ( b, n )
newpage = base_new % ( b, n )
overwrite = base_overwrite % ( b, n )
search = base_search % ( bminus, nminus, b, n, bplus, nplus )
# Step 1: Move oldpage to newpage
wikipedia.output(u'(III) Moving %s to %s\n' % ( oldpage, newpage ))
try:
page = wikipedia.Page(site, oldpage)
page.move(newpage, movesumm)
except wikipedia.Error:
wikipedia.output(u'(EEE) Page move [[%s]] -> [[%s]] failed' % ( oldpage, newpage ))
continue
# Step 2: Place soft redirect over oldpage
wikipedia.output(u'(III) Placing soft redirect over %s\n' % oldpage)
try:
page.put(overwrite, oversumm, minorEdit = False)
except wikipedia.Error:
wikipedia.output(u'(EEE) Placing soft redirect over [[%s]] failed' % oldpage)
# Step 3: delete navigation template
wikipedia.output(u'(III) Deleting "%s" in [[%s]]\n' % ( search, newpage ))
try:
page = wikipedia.Page(site, newpage)
text = page.get()
text = wikipedia.replaceExcept(text, search, u'', [ 'comment', 'math', 'nowiki', 'pre', 'startspace' ])
page.put(text, replacesumm, minorEdit = False)
except wikipedia.Error:
wikipedia.output(u'(EEE) Deletion of navigation template failed in [[%s]]' % newpage)