-
Notifications
You must be signed in to change notification settings - Fork 11
/
uncompleteMerges.py
200 lines (184 loc) · 5.75 KB
/
uncompleteMerges.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
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# licensed under CC-Zero: https://creativecommons.org/publicdomain/zero/1.0
import pywikibot
from pywikibot.data import api
from datetime import datetime, timedelta
import re
site = pywikibot.Site('wikidata', 'wikidata')
site.login()
repo = site.data_repository()
def addition_check(q1, q2):
# only pass if not linked together
params2 = {
'action': 'query',
'titles': q1,
'prop': 'links',
'pllimit': 500
}
req2 = api.Request(site=site, **params2)
data2 = req2.submit()
for m in data2['query']['pages']:
try:
for m in data2['query']['pages'][m]['links']:
if q2 == m['title']:
return False
except:
pass
# check if q1 has wikimedia in its description but not q2
item1 = pywikibot.ItemPage(repo, q1)
item2 = pywikibot.ItemPage(repo, q2)
item1.get()
item2.get()
if 'en' in item1.descriptions and 'en' in item2.descriptions:
if 'ikimedia' in item1.descriptions['en'] and 'ikimedia' not in item2.descriptions['en']:
return False
return True
def merge(fromId, toId):
if addition_check(fromId, toId) == False:
return 0
fromItem = pywikibot.ItemPage(repo, fromId)
toItem = pywikibot.ItemPage(repo, toId)
fromItem.mergeInto(toItem, ignore_conflicts='description')
if not fromItem.isRedirectPage():
clearItem(fromId)
fromItem.set_redirect_target(toItem, force=True, save=True)
def clearItem(fromId):
# get token
params = {
'action': 'query',
'meta': 'tokens'
}
req = api.Request(site=site, **params)
data = req.submit()
# clear item
params2 = {
'action': 'wbeditentity',
'id': fromId,
'clear': 1,
'data': '{}',
'bot': 1,
'summary': 'clearing item to prepare for redirect',
'token': data['query']['tokens']['csrftoken']
}
req2 = api.Request(site=site, **params2)
data2 = req2.submit()
def check(q1):
item = pywikibot.ItemPage(repo, q1)
# ignore redirects and deleted items
if item.isRedirectPage():
return 0
if not item.exists():
return 0
# ignore items with sitelinks and more than 3 claims
dict = item.get()
if len(dict['sitelinks']) != 0:
return 0
if len(dict['claims']) > 3:
return 0
# ignore items with more than 10 backlinks
params2 = {
'action': 'query',
'list': 'backlinks',
'bltitle': q1,
'blnamespace': 0,
'bllimit': 11
}
req2 = api.Request(site=site, **params2)
data2 = req2.submit()
if len(data2['query']['backlinks']) > 10:
return 0
# ignore items with link to WD namespace
params2 = {
'action': 'query',
'list': 'backlinks',
'bltitle': q1,
'blnamespace': 4,
'bllimit': 1
}
req2 = api.Request(site=site, **params2)
data2 = req2.submit()
if len(data2['query']['backlinks']) > 0:
return 0
# ignore items in Category:Notability policy exemptions
cat = pywikibot.Category(site, 'Category:Notability policy exemptions')
liste = list(cat.articles(recurse=5))
for m in liste:
if q1 == m.title()[5:]:
return 0
# ignore items where all removed sitelinks are not added to the same new item
params1 = {
'action': 'query',
'prop': 'revisions',
'titles': q1,
'rvlimit': 200
}
req1 = api.Request(site=site, **params1)
data1 = req1.submit()
q2 = False
for foo in data1['query']['pages']:
for m in data1['query']['pages'][foo]['revisions']:
res = re.search(
'wbsetsitelink-remove\:1\|(.*)wiki \*\/ (.*)', m['comment'])
if res:
try:
site2 = pywikibot.Site(
res.group(1).replace('_', '-'), "wikipedia")
page = pywikibot.Page(site2, res.group(2))
item2 = pywikibot.ItemPage.fromPage(page)
if item2.exists():
qTem = item2.getID()
if not q2 or q2 == qTem:
q2 = qTem
else:
return 0
except:
return 0
if q2:
try:
merge(q1, q2)
except:
pass
return 1
def main():
time_file = 'uncompleteMerges_time.dat'
f1 = open(time_file, 'r')
old_time_str = str(int(f1.read())+1)
f1.close()
now = datetime.now()
new_time = now - timedelta(minutes=10)
new_time_str = new_time.strftime("%Y%m%d%H%M%S")
rccontinue = old_time_str+'|0'
while True:
params = {
'action': 'query',
'list': 'recentchanges',
'rcprop': 'title|comment|timestamp',
'rcstart': old_time_str,
'rcend': new_time_str,
'rcdir': 'newer',
'rctype': 'edit',
'rcnamespace': 0,
'rcshow': '!bot',
'rclimit': 500,
'rccontinue': rccontinue
}
req = api.Request(site=site, **params)
data = req.submit()
for m in data['query']['recentchanges']:
timestamp = m['timestamp']
if 'comment' not in m:
continue
res = re.search(
'wbsetsitelink-remove\:1\|(.*)wiki \*\/ (.*)', m['comment'])
if res:
check(m['title'])
if 'query-continue' in data:
rccontinue = data['query-continue']['recentchanges']['rccontinue']
else:
break
f3 = open(time_file, 'w')
f3.write(re.sub(r'\:|\-|Z|T', '', timestamp))
f3.close()
if __name__ == "__main__":
main()