/
new_interwiki.py
359 lines (314 loc) · 13.2 KB
/
new_interwiki.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# -*- coding: utf-8 -*-
import re
from operator import itemgetter
import pywikibot
from pywikibot import pagegenerators, textlib
import mwparserfromhell
from wikidata_summary import summary as wikidata_summary
import_summary = {
'en': u'import %(sitelinks)s sitelink{{PLURAL:%(count)d||s}} from %(source)s'
}
# language codes for which the WMF version differs from the correct one
labels = {
'no': 'nb',
'als': 'gsw',
'fiu-vro': 'vro',
'bat-smg': 'sgs',
'be-x-old': 'be-tarask',
'roa-rup': 'rup',
'zh-classical': 'lzh',
'zh-min-nan': 'nan',
'zh-yue': 'yue',
'crh': 'crh-latn'
}
class InterwikiBot:
def __init__(self, generator, linked=False, comment=None):
self.generator = generator
self.linked = linked
self.comment = comment
def run(self):
for page in self.generator:
treat(page, comment=self.comment)
if self.linked:
try:
for ll in [pywikibot.Page(ll) for ll in page.langlinks() if ll.site.username()]:
treat(ll, comment=self.comment)
except Exception, e:
pywikibot.warning(e)
def interProjectLinks(page):
config = {
'itwiki': {
'prefixes': ['q', 's', 'voy'],
'remove': ['q', 's', 'voy']
},
'itwikiquote': {
'prefixes': ['w', 's', 'voy']
}
}
if page.site.dbName() in config:
for template in mwparserfromhell.parse(page.text).ifilter_templates():
if template.name[0].upper() + template.name[1:] in [u'Ip', u'Interprogetto']:
prefixes = config[page.site.dbName()]['prefixes']
for param in template.params:
if unicode(param.value).strip() != '':
if unicode(param.name).isnumeric():
if unicode(param.value) in prefixes:
yield pywikibot.Page(page.site, unicode(param.value) + ':' + page.title())
elif unicode(param.name) in prefixes:
lang = (unicode(template.get(unicode(param.name) + '_site').value) + ':') if template.has(unicode(param.name) + '_site') else ''
yield pywikibot.Page(page.site, unicode(param.name) + ':' + lang + unicode(param.value))
def lang(page):
l = page.site.lang
if l in labels:
l = labels[l]
return l
def intelLabel(item, page):
# set of sites to get the label from
priority = ('wikipedia', 'wikivoyage', 'wikiquote', 'wikisource', 'commons')
l = []
for sl in item.iterlinks():
if lang(sl) == lang(page) and sl.site.family.name in priority:
l.append((priority.index(sl.site.family.name), sl.title()))
l.append((priority.index(page.site.family.name) if page.site.family.name in priority else (len(priority) + 1), page.title()))
l = sorted(l, key=itemgetter(0))
return l[0][1]
def treat(page, comment):
pywikibot.output(u'\n\n>>> \03{{lightpurple}}{}\03{{default}} <<<'.format(page.title(asLink=True)))
if page.isRedirectPage():
treat(page.getRedirectTarget(), comment)
return
try:
text = page.get(force=True)
except Exception, e:
pywikibot.warning(e)
return
toRemove = []
importInto = pywikibot.ItemPage.fromPage(page)
items = None
if importInto.exists():
importInto.get(force=True)
else:
# possible targets of link import
items = {}
# import interlanguage links
langlinks = textlib.getLanguageLinks(page.text, insite=page.site)
toImport = langlinks.copy()
toImport[page.site] = page
# import interproject links
iws = list(set(interProjectLinks(page)))
for iw in iws:
if True not in [iw != i and i.site == iw.site for i in iws]:
toImport[iw.site] = iw
for site, link in toImport.iteritems():
if link.isRedirectPage():
toImport[site] = link.getRedirectTarget()
if items is not None:
for site, link in toImport.iteritems():
linkItem = pywikibot.ItemPage.fromPage(link)
if linkItem.exists():
if linkItem.getID() in items:
items[linkItem.getID()] += 1
else:
items[linkItem.getID()] = 1
if len(items) > 0:
# select the most appropriate item
items = list(sorted(items.keys(), key=items.__getitem__))
importInto = pywikibot.ItemPage(page.site.data_repository(), items[-1])
importInto.get(force=True)
if importInto is None or not importInto.exists():
pywikibot.warning(u'no data item to import sitelinks into')
return
for site, langlink in langlinks.iteritems():
if site.dbName() in importInto.sitelinks:
try:
if langlink.site.sametitle(importInto.sitelinks[site.dbName()], langlink.title()) or (
langlink.exists() and langlink.isRedirectPage() and langlink.getRedirectTarget().exists() and langlink.getRedirectTarget() == pywikibot.Page(site, importInto.sitelinks[site.dbName()])):
toRemove.append(langlink)
else:
pywikibot.error(u'interwiki conflict: {} != {}'.format(langlink.title(), importInto.sitelinks[site.dbName()]))
return
except Exception, e:
pywikibot.warning(e)
toImport = [sitelink for site, sitelink in toImport.iteritems() if sitelink.site.dbName() not in importInto.sitelinks or importInto.sitelinks[sitelink.site.dbName()] != sitelink.title()]
if len(toImport) > 0:
toImport = [sitelink for sitelink in toImport if canImport(importInto, sitelink)]
if len(toImport) > 0:
dbNames = [p.site.dbName() for p in toImport]
data = {'sitelinks': [], 'labels': []}
for p in toImport:
data['sitelinks'].append({'site': p.site.dbName(), 'title': p.title()})
lng = lang(p)
if lng not in importInto.labels:
data['labels'].append({'language': lng, 'value': intelLabel(importInto, p)})
summary = pywikibot.i18n.translate(importInto.site, import_summary,
{'sitelinks': u', '.join(dbNames[:-2] + [u' and '.join(dbNames[-2:])]),
'count': len(dbNames),
'source': page.site.dbName()
})
try:
importInto.editEntity(data, summary=summary)
pywikibot.output(u'\03{{lightgreen}}successfully imported {} sitelinks and {} labels'.format(len(data['sitelinks']), len(data['labels'])))
except Exception, e:
pywikibot.warning(e)
return
treat(page=page, comment=comment)
return
toRemove = [link for link in toRemove if link.site.family == page.site.family]
if len(toRemove) > 0:
counter = 0
for langlink in toRemove:
temp = removeLanglink(text, langlink)
if temp != text:
counter += 1
text = temp
if counter == len(toRemove) and text != page.text:
pywikibot.showDiff(page.text, text)
page.text = text
if comment is None:
try:
comment = pywikibot.i18n.translate(page.site, wikidata_summary[0],
{'counter': counter,
'id': importInto.getID(),
'user': page.site.user()
})
except IndexError:
try:
comment = pywikibot.i18n.translate(page.site, wikidata_summary[0],
{'counter': counter,
'id': importInto.getID()
})
except Exception, e:
pywikibot.warning(e)
return
try:
page.save(comment=comment, botflag=True, minor=True)
except Exception, e:
pywikibot.warning(e)
else:
pywikibot.error(u'{} interwikis were to be removed but only {} were removed'.format(len(toRemove), counter))
def canImport(item, sitelink):
item.get()
try:
# otherwise it will throw an API error
if not sitelink.exists():
return False
if sitelink.isTalkPage():
return False
# interwikis containing section links cannot be imported
# and are often source of conflicts
if sitelink.section() is not None:
return False
if sitelink.site.dbName() in item.sitelinks:
return False
if pywikibot.ItemPage.fromPage(sitelink).exists():
return False
# prevents connecting a disambiguation page to a non-disambiguation item
if isDisambig(item) != sitelink.isDisambig():
return False
# a category to a non-category
if isCategory(item) != sitelink.isCategory():
return False
# a template to a non-template
if isTemplate(item) != isTemplate(sitelink):
return False
# a project page to a non-project page
if isProjectPage(item) != isProjectPage(sitelink):
return False
# a person to a non-person
if sitelink.site.family.name not in ['wikipedia', 'wikiquote'] and isPerson(item) != isPerson(sitelink):
return False
# special cases for Commons:
# articles to galleries;
# project pages, templates, help pages and categories to self:
# https://www.wikidata.org/wiki/Wikidata:Requests_for_comment/Commons_links
if sitelink.site.dbName() == 'commonswiki':
if sitelink.namespace() in [0, 4, 10, 12, 14]:
if isPerson(item):
return False
for page in item.iterlinks():
if page.namespace() != sitelink.namespace():
return False
elif not isPerson(sitelink):
return False
return True
except Exception, e:
pywikibot.warning(e)
return False
def allcase(string):
return ''.join([ur'[' + re.escape(c.upper()) + re.escape(c.lower()) + ur']' for c in string])
def capitalized(string):
return allcase(string[0]) + re.escape(string[1:])
def interwikiRegex(page):
regex = ur'\[\[\s*%s\s*\:\s*%s\s*\]\]\s*' % (allcase(page.site.code),
capitalized(page.title()))
regex = ur'(\s*\<noinclude\>[\s\n]*' + regex + ur'[\s\n]*\<\/\s*noinclude\>\s*|' + regex + ur')'
return regex
def removeLanglink(text, langlink):
regex = interwikiRegex(langlink)
exceptions = ['nowiki', 'comment', 'math', 'pre', 'source']
text = textlib.replaceExcept(text, regex, '', exceptions)
return text
def isInstanceOf(item, value):
if 'P31' in item.claims:
for claim in item.claims['P31']:
try:
if claim.getTarget().getID() == value:
return True
except:
pass
return False
def isPerson(arg):
if isinstance(arg, pywikibot.page.ItemPage):
return isInstanceOf(arg, 'Q5')
elif isinstance(arg, pywikibot.page.Page):
if arg.site.family.name == 'wikisource':
ns = arg.site.family.authornamespaces
if arg.namespace() in (ns[arg.site.code] or ns['_default']):
return True
elif arg.site.dbName() == 'commonswiki':
return arg.namespace() == 100 # creator
return False
def isDisambig(item):
if isInstanceOf(item, 'Q4167410'):
return True
for page in item.iterlinks():
if page.isDisambig():
return True
return False
def isCategory(item):
return isInstanceOf(item, 'Q4167836')
def isTemplate(arg):
if isinstance(arg, pywikibot.page.ItemPage):
return isInstanceOf(arg, 'Q11266439')
elif isinstance(arg, pywikibot.page.Page):
return arg.namespace() == 10
return False
def isProjectPage(arg):
if isinstance(arg, pywikibot.page.ItemPage):
return isInstanceOf(arg, 'Q14204246')
elif isinstance(arg, pywikibot.page.Page):
return arg.namespace() == 4
return False
def main():
linked = False
comment = None
genFactory = pagegenerators.GeneratorFactory()
for arg in pywikibot.handleArgs():
if arg.startswith('-comment'):
if len(arg) == 8:
comment = pywikibot.input(u'What comment do you want to use?')
else:
comment = arg[9:]
elif arg.startswith('-linked'):
linked = True
else:
genFactory.handleArg(arg)
generator = genFactory.getCombinedGenerator()
if generator:
bot = InterwikiBot(generator, linked=linked, comment=comment)
bot.run()
if __name__ == "__main__":
try:
main()
finally:
pywikibot.stopme()