-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfix_redirects.py
75 lines (62 loc) · 2.31 KB
/
fix_redirects.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
import pywikibot as pw
import toolforge
import helpers
from helpers import sql_to_matrix
conn = toolforge.connect('hywiki')
hywiki = pw.Site('hy', 'wikipedia')
double_sql_query = '''SELECT src.page_namespace,
src.page_title
FROM page AS src
JOIN redirect ON rd_from = src.page_id
JOIN page AS dbl ON dbl.page_namespace = rd_namespace
AND dbl.page_title = rd_title
WHERE dbl.page_is_redirect = 1;'''
broken_sql_query = '''SELECT page_namespace,
page_title
FROM page AS src
JOIN redirect ON rd_from = src.page_id
WHERE rd_interwiki = ''
AND NOT EXISTS
(SELECT 1
FROM page AS redir
WHERE redir.page_namespace = rd_namespace
AND redir.page_title = rd_title);'''
def get_target_or_none(page: pw.Page):
if page.exists() and not page.isRedirectPage():
return page
if not page.exists():
return get_target_or_none(page.moved_target())
if page.isRedirectPage():
return get_target_or_none(page.getRedirectTarget())
def fix_double_redirect(page: pw.Page):
try:
target = get_target_or_none(page)
if target:
page.text = f'#ՎԵՐԱՀՂՈՒՄ [[{target.title(with_ns=True)}]]'
page.save('ուղղում եմ կրկնակի վերահղումները')
except pw.exceptions.NoMoveTargetError:
page.delete(reason='կոտրված վերահղում')
except pw.exceptions.CircularRedirectError:
pass
def fix_broken_redirect(page: pw.Page):
try:
target = get_target_or_none(page)
if target:
page.text = f'#ՎԵՐԱՀՂՈՒՄ [[{target.title(with_ns=True)}]]'
page.save('ուղղում եմ կոտրված վերահղումը')
except pw.exceptions.NoMoveTargetError:
page.delete(reason='ջնջում եմ կոտրված վերահղումը', prompt=False)
except pw.exceptions.CircularRedirectError:
pass
double_data = sql_to_matrix('hywiki', double_sql_query)
for row in double_data:
ns = row[0]
title = row[1]
page = pw.Page(hywiki, title if ns == 0 else f'{helpers.nsMap[ns]}:{title}')
fix_double_redirect(page)
broken_data = sql_to_matrix('hywiki', broken_sql_query)
for row in broken_data:
ns = row[0]
title = row[1]
page = pw.Page(hywiki, title if ns == 0 else f'{helpers.nsMap[ns]}:{title}')
fix_broken_redirect(page)