User:TalBot/un-res.py

From Wikisource
Jump to: navigation, search
#! /usr/bin/env python
# _*_ coding: utf8 _*_
#
# Move page names of UN Security Council resolutions
#
# 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:
        wikipedia.output(u'(WWW) Ignoring unrecognised argument: %s' % arg)

# basic text tokens, etc.

base_targetname = u'United Nations Security Council Resolution %d'
base_redirname = u'UN Security Council Resolution %d'
base_softname = u'UN Security Council Resolution/%d'

base_softoverwrite = u'{{subst:dated soft redirect|"[[%s]]"}}'
base_rediroverwrite = u'#REDIRECT[[%s]]'

movesumm = u'[bot] new name according to community consensus'
softsumm = u'[bot] dated soft redirect'
redirsumm = u'[bot] establishing redirect'

# Some constants

TYPE_EMPTY = 0x00
TYPE_TARGET = 0x01
TYPE_REDIR = 0x04
TYPE_SOFT = 0x10

# Function to check what kind of page we're dealing with

def get_page_type(page):
        if not page.exists():
                return TYPE_EMPTY
        if page.isRedirectPage():
                return TYPE_REDIR
        text = page.get()
        if (text.find(u'oft redirect') != -1):
                return TYPE_SOFT
        return TYPE_TARGET

# Start operation

wikipedia.get_throttle.setDelay(5)
site = wikipedia.getSite()
pagenos = range(1, 1730)

for n in pagenos:
        # Render strings
        targetname = base_targetname % n
        redirname = base_redirname % n
        softname = base_softname % n
        softoverwrite = base_softoverwrite % targetname
        rediroverwrite = base_rediroverwrite % targetname
        # Step 1: Check current situation
        wikipedia.output(u'(III) Checking current situation for Resolution No. %d' % n)
        targetpage = wikipedia.Page(site, targetname)
        redirpage = wikipedia.Page(site, redirname)
        softpage = wikipedia.Page(site, softname)
        targettype = get_page_type(targetpage)
        redirtype = get_page_type(redirpage)
        softtype = get_page_type(softpage)
        # Step 2: Sanity checks
        sum = targettype + redirtype + softtype
        if sum == 0:
                # Pages do not exist
                wikipedia.output(u'(III) Resolution No. %d does not exist, skipping' % n)
                continue
        if sum & 0x03 != 1:
                # more than one target (requires manual intervention)
                # (or no target at all which would be weird)
                wikipedia.output(u'(EEE) More than one target for Resolution No. %d, skipping' % n)
                continue
        if targettype == TYPE_SOFT or targettype == TYPE_REDIR:
                # Move blocked: bot with admin privileges needed (Xenophon)
                wikipedia.output(u'(EEE) Insufficient privileges to delete obsolete redirect page for Resolution No. %d, skipping' % n)
                continue
        # Step 3: Move target page if necessary
        if targettype == TYPE_EMPTY:
                wikipedia.output(u'(III) Moving target page')
                wikipedia.put_throttle()
                # Where's the target?
                if softtype == TYPE_TARGET:
                        if (softpage.move(targetname, movesumm) == False):
                                wikipedia.output(u'(EEE) Resolution %d: Page move FAILED!' % n)
                                continue
                if redirtype == TYPE_TARGET:
                        if (redirpage.move(targetname, movesumm) == False):
                                wikipedia.output(u'(EEE) Resolution %d: Page move FAILED!' % n)
                                continue
        else:
                wikipedia.output(u'(III) Target already in place')
        # Step 4: Set redirects
        wikipedia.output(u'(III) Setting redirects')
        try:
                softpage.put(softoverwrite, softsumm, None, False)
                redirpage.put(rediroverwrite, redirsumm, None, False)
        except wikipedia.Error:
                wikipedia.output(u'(EEE) Resolution %d: Placing redirects FAILED!' % n)
                continue