User:TalBot/rm-soft-redir-helper.py

From Wikisource
Jump to navigation Jump to search
#! /usr/bin/env python
# _*_ coding: utf8 _*_
#
# List soft redirects with their targets
#
# Copyright (C) 2007, GrafZahl (en.wikisource.org user)
#
# Licence: GPLv2
#
# run with standard args "-log -putthrottle:xx"
#
# Further arguments:
#
#	-cat:xxx
#		Specifies the category of soft redirects to be examined, for
#		example: -cat:'Soft redirects/August 2006' (replace the
#		single quotes with whatever is appropriate for your shell)
#

import catlib, sys, wikipedia

wikipedia.get_throttle.setDelay(5)

# Handle args

args = wikipedia.handleArgs()

month = False

for arg in args:
	if arg[:5] == u'-cat:':
		month = arg[5:]
	else:
		wikipedia.output(u'(WWW) Ignoring unrecognised argument: %s' % arg)

if not month:
	wikipedia.output(u'(FFF) No category given (-cat:xxx)')
	sys.exit(1)

# basic text tokens, etc.

cattitle = u'Category:%s' % month

# Start operation

site = wikipedia.getSite()
cat = catlib.Category(site, cattitle)
articles = cat.articles()

stlist = []

for page in articles:
	# Check if someone confused soft and hard redirs
        if page.isRedirectPage():
                wikipedia.output(u'(EEE) %s is a hard redirect, not a soft one' % page.title())
                continue
        # Extract new target
        newlist = page.linkedPages()
        ### There should be only one normal link
        if len(newlist) != 1:
                wikipedia.output(u'(EEE) No unambiguous target for soft redirect %s' % page.title())
                continue
        new = newlist[0].title()
	stlist.append((page.title(), new))

stlist = sorted(stlist)

wikipedia.output(u'(III) List of soft redirects:')

for st in stlist:
	wikipedia.output(u'* [[%s]] -> [[%s]]' % ( st[0], st[1] ))