-
Notifications
You must be signed in to change notification settings - Fork 2
/
cfdMultipleMerger.py
72 lines (62 loc) · 2.29 KB
/
cfdMultipleMerger.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
import re
import mwparserfromhell
import pywikibot
pywikibot.config.family = "wikipedia"
pywikibot.config.mylang = "en"
site = pywikibot.Site()
#site.login()
class CFDMultipleMergeBot(object):
def __init__(self):
self.getMerges()
def getMerges(self):
page = pywikibot.Page(site, "Wikipedia:Categories for discussion/Working/Manual")
text = page.get()
code = mwparserfromhell.parse(text)
section = code.get_sections(
levels=[2],
matches="Multiple merge targets",
include_headings=False
)[0]
self.merges = re.findall(
"\[\[:Category:(.*?)\]\] to \[\[:Category:(.*?)\]\] and \[\[:Category:(.*?)\]\]",
unicode(section)
)
def run(self):
for merge in self.merges:
source_cat = pywikibot.Category(site, merge[0])
target_cat1 = pywikibot.Category(site, merge[1])
target_cat2 = pywikibot.Category(site, merge[2])
if not (source_cat.exists() and target_cat1.exists() and target_cat2.exists()):
continue
for page in source_cat.articles():
if page.namespace() != 0:
continue
text = page.get()
page_cats = pywikibot.getCategoryLinks(text)
if source_cat in page_cats:
page_cats.remove(source_cat)
if not target_cat1 in page_cats:
page_cats.append(target_cat1)
if not target_cat2 in page_cats:
page_cats.append(target_cat2)
new_text = pywikibot.replaceCategoryLinks(text, page_cats)
if text == new_text:
continue
pywikibot.showDiff(text, new_text)
page.put(
new_text,
"[[Wikipedia:Bots|Bot]]: Merging from %s to %s and %s per CFD outcome" % (
source_cat.title(asLink=True),
target_cat1.title(asLink=True),
target_cat2.title(asLink=True)
)
)
break
def main():
bot = CFDMultipleMergeBot()
bot.run()
if __name__ == "__main__":
try:
main()
finally:
pywikibot.stopme()