-
Notifications
You must be signed in to change notification settings - Fork 0
/
katmove.py
58 lines (45 loc) · 1.44 KB
/
katmove.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
import pywikibot
from scripts.category import CategoryMoveRobot
import toolforge
site = pywikibot.Site("lv", "wikipedia")
conn = toolforge.connect('lvwiki_p')
SQL = """SELECT p.page_title, COUNT(cl.cl_from) AS anz
FROM page p
LEFT JOIN categorylinks cl ON p.page_title = cl.cl_to
WHERE p.page_namespace = 14
AND p.page_is_redirect = 1
GROUP BY p.page_title
having anz>0"""
def encode_if_necessary(b):
if type(b) is bytes:
return b.decode('utf8')
return b
def run_query():
#query = query.encode('utf-8')
#print(query)
try:
cursor = conn.cursor()
cursor.execute(SQL)
rows = cursor.fetchall()
except KeyboardInterrupt:
sys.exit()
return rows
#
def one_cat(OLDCAT):
oldcatObject = pywikibot.Page(site,"Kategorija:{}".format(OLDCAT))
if OLDCAT == 'User_vro': return 0 # doesn't work with babel categories
if not oldcatObject.isRedirectPage():
return 0
#
NEWCAT = oldcatObject.getRedirectTarget().title(with_ns=False)
#pywikibot.output(NEWCAT)
SUMMARY = 'kategorija "{}" → "{}"'.format(OLDCAT.replace('_',' '), NEWCAT.replace('_',' '))
q = CategoryMoveRobot(OLDCAT, NEWCAT, batch=True, comment=SUMMARY, inplace=True, delete_oldcat=False)
q.run()
#
data = run_query()#oldcategory = ['Sanktpēterburgas_"SKA"_spēlētāji','','','']
for categ in data:
categ = encode_if_necessary(categ[0])
#pywikibot.output(categ)
if categ=='': continue
one_cat(categ)