From Botwiki
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Bot that change redirect categories on commons.
"""
# Author: Filnik
# License: MIT
# Command: python categoryredirector.py -lang:commons -family:commons
from category import *
import wikipedia, re
def getTrustedUsers(checkPage = None, site = wikipedia.getSite()):
if checkPage != None:
checkPageObj = wikipedia.Page(site, checkPage)
try:
checkText = checkPageObj.get()
except wikipedia.NoPage:
wikipedia.output(u'No text found on the checkpage!')
checkText = ''
trustList = re.findall(r'\* ?\{\{(?:[Tt]emplate:|)[Uu]\|(.*?)\}\}', checkText)
sysopPage = site.getUrl('/w/index.php?title=Special:Listusers&limit=1000&group=sysop')
sysops = re.findall(r'<li><a href=\"/wiki/User:.*?\" title=\"(.*?)\">', sysopPage)
# Adding sysops to the trust list
for user in sysops:
trustList.append(user.split('User:')[1])
return trustList
def main():
arg = wikipedia.handleArgs()
fromGiven = False
toGiven = False
batchMode = False
inPlace = False
overwrite = False
showImages = False
talkPages = False
recurse = False
titleRegex = None
catDB = CategoryDatabase()
action = None
sort_by_last_name = False
restore = False
action = 'move'
fromGiven = True
toGiven = True
site = wikipedia.getSite()
# Get the trusted users
trustList = getTrustedUsers('Commons:CategoryRedirectBot/CheckPage', site)
for xPage in ['Template:Seecat', 'Template:Category redirect']:
templatePage = wikipedia.Page(site, xPage)
for Catpage in templatePage.getReferences():
wikipedia.output(u'Loading %s...' % Catpage.title())
if 'categor' not in Catpage.title().lower():
wikipedia.output(u"%s is not a category... skip!" % Catpage.title())
try:
text = Catpage.get(get_redirect = True)
except wikipedia.NoPage:
wikipedia.output(u"%s doesn't exist! Skip.")
continue
target = re.findall('\{\{(?:[Tt]emplate:|)[Cc]ategory[ _]redirect\|(.*?)\}\}', text)
if target == []:
target = re.findall('\{\{(?:[Tt]emplate:|)[Ss]eecat\|(.*?)\}\}', text)
if target == []:
wikipedia.output(u'No template found! Skip!')
continue
if target != []:
wikipedia.output(u'Found the template!')
oldCatTitle = Catpage.title().split('egory:')[1]
oldCat = wikipedia.Page(site, 'Category:%s' % oldCatTitle)
# Only to prevent errors
oldCat.latestRevision()
# Load the latest user that has edit the source category
latestUser = oldCat.userName()
if latestUser in trustList:
wikipedia.output(u'%s is in the Trust List! Moving category...' % latestUser)
else:
wikipedia.output(u'%s is not in the Trust List! Skip...' % latestUser)
continue
newCatTitle = target[0]
if wikipedia.Page(site, 'Category:' + newCatTitle).exists():
#If this is set to true then the custom edit summary given for removing
#categories from articles will also be used as the deletion reason.
useSummaryForDeletion = False
if (fromGiven == False):
oldCatTitle = wikipedia.input(u'Please enter the old name of the category:')
if (toGiven == False):
newCatTitle = wikipedia.input(u'Please enter the new name of the category:')
editSummary = 'Bot: Moving from %s to %s' % ('Category:%s' % oldCatTitle, 'Category:%s' % newCatTitle)
bot = CategoryMoveRobot(oldCatTitle, newCatTitle, batchMode, editSummary, inPlace, titleRegex = titleRegex)
bot.run()
else:
wikipedia.output(u"The new cat, doesn't exist! Skip!")
continue
return catDB
if __name__ == "__main__":
try:
catDB = main()
finally:
catDB.dump()
wikipedia.stopme()