User:TalBot/atoeam1-fix.py

From Wikisource
Jump to navigation Jump to search
#! /usr/bin/env python
# _*_ coding: utf8 _*_
#
# Fix certain pages of A Treatise on Electricity and Magnetism, Vol. I
#
# run with args "-log -putthrottle:xx"
#
# Copyright (C) 2006, GrafZahl (en.wikisource.org user)
#
# Licence: GPLv2
#

import wikipedia

# Handle args

args = wikipedia.handleArgs()

for arg in args:
	print u'Ignoring unrecognised argument: %s' % arg

# basic text tokens, etc.

numpages = 518
oldbasename = u'A_Treatise_on_Electricity_and_Magnetism:_Volume_I/%03d'
newbasename = u'Page:A_Treatise_on_Electricity_and_Magnetism_Volume_1_%03d.jpg'
baseoverwrite = u'{{subst:dated soft redirect|"[[%s]]"}}'
basesearch = u'\\{\\{JCMatoeamV1\\|%03d\\|%03d\\|%03d\\}\\}'
basereplace = u'<noinclude>{{JCMatoeamV1|%03d|%03d|%03d}}</noinclude>'
oversumm = u'[bot] dated soft redirect'
replacesumm = u'[bot] protecting navigation template from transclusion'

# Pages where only step 3 is missing

site = wikipedia.getSite()
pagenos = range(15, 20)

for n in pagenos:
	# Get all strings straight
	if n > 1:
		nminus = n - 1
	else:
		nminus = 1
	if n < numpages:
		nplus = n + 1
	else:
		nplus = numpages
	newpage = newbasename % n
	search = basesearch % ( nminus, n, nplus )
	replace = basereplace % ( nminus, n, nplus )
	# Step 3: Text replacement
	print 'Replacing %s with %s in %s:' % ( search, replace, newpage )
	page = wikipedia.Page(site, newpage)
	try:
		text = page.get()
		text = wikipedia.replaceExceptMathNowikiAndComments(text, search, replace)
		page.put(text, replacesumm, None, False)
	except wikipedia.Error:
		print 'Text replacement FAILED'

# Pages where steps 2 and 3 are missing

pagenos = range(111, 122)

for n in pagenos:
	# Get all strings straight
	if n > 1:
		nminus = n - 1
	else:
		nminus = 1
	if n < numpages:
		nplus = n + 1
	else:
		nplus = numpages
	oldpage = oldbasename % n
	newpage = newbasename % n
	overwrite = baseoverwrite % newpage
	search = basesearch % ( nminus, n, nplus )
	replace = basereplace % ( nminus, n, nplus )
	# Step 2: Place soft redirect over oldpage
	print 'Placing soft redirect over %s:' % oldpage
	page = wikipedia.Page(site, oldpage)
	try:
		page.put(overwrite, oversumm, None, False)
	except wikipedia.Error:
		print 'Placing soft redirect FAILED!'
		# Go on anyway
	# Step 3: Text replacement
	print 'Replacing %s with %s in %s:' % ( search, replace, newpage )
	page = wikipedia.Page(site, newpage)
	try:
		text = page.get()
		text = wikipedia.replaceExceptMathNowikiAndComments(text, search, replace)
		page.put(text, replacesumm, None, False)
	except wikipedia.Error:
		print 'Text replacement FAILED'