-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit.py
288 lines (243 loc) · 10.2 KB
/
edit.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# -*- coding: utf-8 -*-
import argparse
import json
import logging
import os
import re
import sys
from datetime import datetime, timedelta
from typing import Dict, List
os.environ['PYWIKIBOT_DIR'] = os.path.dirname(os.path.realpath(__file__))
import pywikibot
from pywikibot.data.api import Request
from pywikibot.textlib import extract_sections
from config import config_page_name # pylint: disable=E0611,W0614
parser = argparse.ArgumentParser()
parser.add_argument('pagename', nargs='?')
parser.add_argument('-c', '--confirm', action='store_true')
parser.add_argument('-d', '--debug', action='store_const', dest='loglevel', const=logging.DEBUG, default=logging.INFO)
args = parser.parse_args()
logger = logging.getLogger('archive_ar')
formatter = logging.Formatter('%(asctime)s | %(levelname)s | %(message)s')
stdout_handler = logging.StreamHandler(sys.stdout)
stdout_handler.setFormatter(formatter)
logger.addHandler(stdout_handler)
logger.setLevel(args.loglevel)
logger.debug('args: %s', args)
site = pywikibot.Site()
site.login()
config_page = pywikibot.Page(site, config_page_name)
cfg = config_page.text
cfg = json.loads(cfg)
logger.debug('config: %s', json.dumps(cfg, indent=4, ensure_ascii=False))
if not cfg['enable']:
print('disabled')
exit()
normalized_titles = dict()
converted_titles = dict()
redirect_titles = dict()
afd_titles = set()
for page in pywikibot.Category(site, 'Category:所有刪除候選').members():
afd_titles.add(page.title())
logger.debug('afd_titles: %d', len(afd_titles))
def check_title(old_title):
mode = []
# 順序不得更改
if old_title in normalized_titles: # 命名空間等
mode.append('normalized')
old_title = normalized_titles[old_title]
new_title = old_title
if new_title in converted_titles: # 繁簡轉換
mode.append('converted')
new_title = converted_titles[new_title]
if new_title in redirect_titles: # 重定向
mode.append('redirects')
new_title = redirect_titles[new_title]
if 'redirects' in mode:
if old_title in afd_titles or re.search('^MediaWiki:', old_title) or re.search('\.(js|css|json)$', old_title):
mode.append('vfd_on_source')
if new_title in afd_titles or re.search('^MediaWiki:', new_title) or re.search('\.(js|css|json)$', new_title):
mode.append('vfd_on_target')
else:
if new_title in afd_titles or re.search('^MediaWiki:', new_title) or re.search('\.(js|css|json)$', new_title):
mode.append('vfd_on_source')
if 'vfd_on_source' not in mode and 'vfd_on_target' not in mode:
page = pywikibot.Page(site, new_title)
try:
if page.exists():
mode.append('no_vfd')
else:
mode.append('vfd_on_source')
except pywikibot.exceptions.InvalidTitleError:
mode.append('vfd_on_source')
return {'title': new_title, 'mode': mode}
def appendComment(text, mode):
if 'A2093064-bot' not in text:
append_text = []
if 'fix' in mode:
comment = []
if 'redirects' in mode and isinstance(cfg['comment_fix']['redirects'], str):
comment.append(cfg['comment_fix']['redirects'])
logger.debug('\tcomment_fix - redirects')
if 'converted' in mode and isinstance(cfg['comment_fix']['converted'], str):
comment.append(cfg['comment_fix']['converted'])
logger.debug('\tcomment_fix - converted')
if 'normalized' in mode and isinstance(cfg['comment_fix']['normalized'], str):
comment.append(cfg['comment_fix']['normalized'])
logger.debug('\tcomment_fix - normalized')
if len(comment) > 0:
append_text.append(cfg['comment_fix']['main'].format(
''.join(comment)))
logger.debug('\tcomment_fix - redirects')
if 'no_vfd' in mode:
append_text.append(cfg['comment_vfd'])
logger.debug('\tcomment_vfd')
if len(append_text) > 0:
append_text = '\n'.join(append_text)
hr = '\n----'
if hr in text:
temp = text.split(hr)
text = hr.join(temp[:-1]) + '\n' + append_text + hr + temp[-1]
else:
text += append_text + '\n'
return text
def escapeEqualSign(titlelist):
anyEqual = any(['=' in title for title in titlelist])
if anyEqual:
newtitlelist = []
for i, title in enumerate(titlelist, 1):
newtitlelist.append('{}={}'.format(i, title))
return newtitlelist
return titlelist
def fix(pagename):
if re.search(r'\d{4}/\d{2}/\d{2}', pagename):
pagename = 'Wikipedia:頁面存廢討論/記錄/' + pagename
logger.debug('-' * 50)
logger.info('running for ' + pagename)
afdpage = pywikibot.Page(site, pagename)
text = afdpage.text
header, threads, footer = extract_sections(text, site)
section_titles: Dict[int, List[str]] = dict()
for sec_id, section in enumerate(threads):
if re.search(r'{{\s*(delh|TalkendH)\s*(\||}})', section.content, re.IGNORECASE) is not None:
logger.debug('%s closed, skip', section.title.strip('= '))
continue
heading = section.title.strip('= ')
m = re.search(r'^\[\[([^\]]+)\]\]$', heading, re.IGNORECASE)
if m:
section_titles[sec_id] = [m.group(1)]
continue
m = re.search(r'^(\[\[[^\]]+\]\][、, ])+\[\[[^\]]+\]\]$', heading, re.IGNORECASE)
if m:
title_list = re.sub(r'\]\][, ]\[\[', ']]、[[', heading).split('、')
section_titles[sec_id] = []
for title in title_list:
section_titles[sec_id].append(title.strip('[]'))
continue
m = re.search(r'^{{al\|((?:[^\]]+\|)+[^\]]+)}}$', heading, re.IGNORECASE)
if m is not None:
title_list = m.group(1).split('|')
section_titles[sec_id] = []
for title in title_list:
m = re.search(r'^\s*\d+\s*=\s*(.+)$', title)
if m:
section_titles[sec_id].append(m.group(1))
else:
section_titles[sec_id].append(title)
continue
logger.debug('%s unknown format, skip', heading)
all_titles = [title for section in section_titles.values() for title in section] # flatten
BATCH_SIZE = 50
for i in range(0, len(all_titles), BATCH_SIZE):
params = {
'action': 'query',
'titles': '|'.join(all_titles[i:i + BATCH_SIZE]),
'redirects': 1,
'converttitles': 1,
'format': 'json',
'formatversion': 2,
}
logger.debug('params: %s', params)
r = Request(site=site, parameters=params)
data = r.submit()
for item in data['query'].get('normalized', []):
normalized_titles[item['from']] = item['to']
for item in data['query'].get('converted', []):
converted_titles[item['from']] = item['to']
for item in data['query'].get('redirects', []):
redirect_titles[item['from']] = item['to']
new_text = header.strip() + '\n\n'
for sec_id, section in enumerate(threads):
sec_content = section.content.strip()
if sec_content:
sec_content += '\n'
if sec_id not in section_titles:
new_text += section.title + '\n' + sec_content + '\n'
continue
m = re.search(r'^(=+)', section.title)
if m:
old_level = m.group(1)
else:
logger.warning('fail to check section level {}'.format(section.title))
new_text += section.title + '\n' + sec_content + '\n'
continue
new_titles = []
mode = []
for old_title in section_titles[sec_id]:
convert = check_title(old_title)
new_title = old_title
if (('redirects' in convert['mode'] and 'vfd_on_target' in convert['mode'])
or ('redirects' not in convert['mode'])):
new_title = convert['title']
if old_title[0] == ':':
new_title = ':' + new_title
if old_title != new_title:
mode.append('fix')
mode += convert['mode']
new_titles.append(new_title)
old_heading = section.title.strip('= ')
logger.debug('%s: %s', old_heading, ', '.join(mode))
new_titles = escapeEqualSign(new_titles)
new_heading = ''
if len(new_titles) == 1:
new_heading = '[[{}]]'.format(new_titles[0])
else:
new_heading = '{{al|' + '|'.join(new_titles) + '}}'
if old_heading != new_heading:
logger.info('change heading to %s', new_heading)
new_text += '{0} {1} {0}\n'.format(old_level, new_heading)
if len(new_titles) == 1:
old_norm_heading = old_heading.replace('_', ' ').replace(''', "'")
if old_norm_heading != new_heading:
sec_content = '{{formerly|' + old_heading + '}}\n' + sec_content
sec_content = appendComment(sec_content, mode)
new_text += sec_content + '\n'
new_text += footer.strip()
if re.sub(r'\s+', '', afdpage.text) == re.sub(r'\s+', '', new_text):
logger.info('nothing changed')
with open('out.txt', 'w', encoding='utf8') as f:
f.write(new_text)
return
summary = cfg['summary']
if args.confirm or args.loglevel <= logging.DEBUG:
pywikibot.showDiff(afdpage.text, new_text)
logger.info('summary: %s', summary)
save = True
if args.confirm:
save = pywikibot.input_yn('Save changes for main page?', 'Y')
if save:
logger.info('save changes')
afdpage.text = new_text
afdpage.save(summary=summary, minor=False)
else:
with open('out.txt', 'w', encoding='utf8') as f:
f.write(new_text)
logger.info('skip save')
if args.pagename:
fix(args.pagename)
else:
logger.debug('run past %s days', cfg['run_past_days'])
for delta in range(cfg['run_past_days']):
rundate = datetime.now() - timedelta(days=delta)
pagename = rundate.strftime('%Y/%m/%d')
fix(pagename)