-
Notifications
You must be signed in to change notification settings - Fork 0
/
commons_category_redirect.py
101 lines (88 loc) · 3.69 KB
/
commons_category_redirect.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Script to clean up http://commons.wikimedia.org/wiki/Category:Non-empty_category_redirects
Moves all images, pages and categories in redirect categories to the target category.
"""
#
# (C) Multichill, 2008
# (C) Pywikipedia team, 2008-2011
#
# Distributed under the terms of the MIT license.
#
__version__ = '$Id$'
import wikipedia as pywikibot
import config, catlib
from category import *
from datetime import datetime
from datetime import timedelta
redirect_templates = [u'Category redirect', u'Categoryredirect', u'See cat', u'Seecat', u'Catredirect', u'Cat redirect', u'CatRed', u'Catredir']
move_message = u'Moving from [[%s|%s]] to [[%s|%s]] (following [[Template:Category redirect|category redirect]])'
cooldown = 7 # days
def get_redirect_cat(category=None):
'''
Return the target category
'''
destination = None
site = pywikibot.getSite(u'commons', u'commons')
for template in category.templatesWithParams():
if ((template[0] in redirect_templates) and (len(template[1]) > 0)):
#destination = template[1][0];
destination =catlib.Category(site, template[1][0])
if not destination.exists():
return None
return destination
def readyToEdit(old_category):
''' If the category is edited more recenty than cooldown, return false,
otherwise true.
'''
dateformat ="%Y%m%d%H%M%S"
today = datetime.now()
deadline = today + timedelta(days=-cooldown)
old_category.get()
return (deadline.strftime(dateformat) > old_category.editTime())
def main():
''' Main loop. Loop over all categories of
Category:Non-empty_category_redirects and move all content.
'''
site = pywikibot.getSite(u'commons', u'commons')
dirtycat = catlib.Category(site, u'Category:Non-empty category redirects')
destination = None
catbot = None
for old_category in dirtycat.subcategories():
# We want to wait several days after the last edit before we start moving
# things around. This it to prevent edit wars and vandals.
if(readyToEdit(old_category)):
destination = get_redirect_cat(old_category)
if destination:
pywikibot.output(destination.title())
for page in old_category.articles():
try:
catlib.change_category(
page, old_category, destination, move_message
% (old_category.title(),
old_category.title(withNamespace=False),
destination.title(),
destination.title(withNamespace=False)))
except pywikibot.IsRedirectPage:
pywikibot.output(u'%s is a redirect!' % page.title())
for cat in old_category.subcategories():
try:
catlib.change_category(
cat, old_category, destination, move_message
% (old_category.title(),
old_category.title(withNamespace=False),
destination.title(),
destination.title(withNamespace=False)))
except pywikibot.IsRedirectPage:
pywikibot.output(u'%s is a redirect!' % page.title())
#Dummy edit to refresh the page, shouldnt show up in any logs.
try:
old_category.put(old_category.get())
except:
pywikibot.output(u'Dummy edit at %s failed' % old_category.title())
if __name__ == "__main__":
try:
main()
finally:
pywikibot.stopme()