-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrfd.py
64 lines (56 loc) · 2.71 KB
/
rfd.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
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# licensed under CC-Zero: https://creativecommons.org/publicdomain/zero/1.0
import pywikibot
import re
site = pywikibot.Site('wikidata', 'wikidata')
repo = site.data_repository()
page = pywikibot.Page(site, 'Wikidata:Requests for deletions')
cntDone = 0
cntNotDone = 0
force = False
content = re.findall(r'(?:(?<!=)==([^=]+)==(?!=))?([\s\S]+?(?=$|(?<!=)==[^=]+==(?!=)))', page.get())
for i in range(len(content)):
try:
content[i] = list(map(str.strip, list(content[i])))
res = re.search(r'(Q\d+)', content[i][0])
if res:
entity = pywikibot.ItemPage(repo, res.group(1))
else:
res = re.search(r'(Lexeme:L\d+)', content[i][0])
if res:
entity = pywikibot.Page(repo, res.group(1)) # T189321
if res:
if any(x in content[i][1] for x in ('{{done', '{{deleted', '{{not done', '{{notdone', '{{not deleted', '{{merged')):
continue
if not entity.exists() and not entity.isRedirectPage():
for m in site.logevents(logtype='delete', page=entity, total=1):
content[i][1] += u'\n: {{{{deleted|admin={}}}}} --~~~~'.format(m.user())
cntDone += 1
elif entity.isRedirectPage() and entity.getRedirectTarget().exists():
content[i][1] += (u'\n: {{{{done}}}} Redirect created by [[User:{}]], you can do it ' +
u'[[Special:MyLanguage/Help:Merge|yourself]] next time. --~~~~').format(entity.userName())
cntDone += 1
else:
if '{{on hold' not in content[i][1]:
refs = list(entity.backlinks(follow_redirects=False, filter_redirects=False, namespaces=[0, 120], total=12))
numberOfRefs = len(refs)
if entity in refs:
numberOfRefs -= 1
if numberOfRefs > 0:
force = True
content[i][1] += u'\n: {{{{on hold}}}} This item is linked from {}{} other{}. --~~~~'.format(
min(numberOfRefs, 10), '+' if numberOfRefs > 10 else '', 's' if numberOfRefs > 1 else '')
cntNotDone += 1
except:
pass
text = ''
for section in content:
if section[0] != '':
text += u'== {} ==\n\n'.format(section[0])
text += section[1]+'\n\n'
if cntDone > 0 or force:
comment = 'Bot: marking {} requests as done ({} unactioned requests)'.format(cntDone, cntNotDone)
page.put(text, summary=comment, minor=False)
statspage = pywikibot.Page(site, 'Wikidata:Requests for deletions/count')
statspage.put(cntNotDone, summary='Updating stats: '+str(cntNotDone), minor=False)