-
Notifications
You must be signed in to change notification settings - Fork 5
/
fix_qualifiers.py
110 lines (91 loc) · 4.26 KB
/
fix_qualifiers.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
#!/usr/bin/python
"""This script is obsolete!"""
import pywikibot
from pywikibot import pagegenerators
from query_store import QueryStore
from wikidata import WikidataEntityBot
class QualifiersFixingBot(WikidataEntityBot):
blacklist = frozenset(['P143', 'P248', 'P459', 'P518', 'P577', 'P805',
'P972', 'P1065', 'P1135', 'P1480', 'P1545', 'P1932',
'P2315', 'P2701', 'P3274', ])
whitelist = frozenset(['P17', 'P21', 'P39', 'P155', 'P156', 'P281', 'P580',
'P582', 'P585', 'P669', 'P708', 'P969', 'P1355',
'P1356', ])
good_item = 'Q15720608'
use_from_page = False
def __init__(self, **kwargs):
kwargs.update({
'bad_cache': kwargs.get('bad_cache', []) + list(self.blacklist),
'good_cache': kwargs.get('good_cache', []) + list(self.whitelist),
})
super().__init__(**kwargs)
self.store = QueryStore()
def filterProperty(self, prop_page):
if prop_page.type == 'external-id':
return False
prop_page.get()
if 'P31' not in prop_page.claims:
pywikibot.warning('%s is not classified' % prop_page.getID())
return False
for claim in prop_page.claims['P31']:
if claim.target_equals(self.good_item):
return True
return False
@property
def generator(self):
query = self.store.build_query(
'qualifiers', item=self.good_item,
good=', wd:'.join(self.whitelist),
bad=', wd:'.join(self.blacklist))
return pagegenerators.PreloadingItemGenerator(
pagegenerators.WikidataSPARQLPageGenerator(query, site=self.repo))
def treat_page_and_item(self, page, item):
for prop in item.claims.keys():
for claim in item.claims[prop]:
moved = set()
json = claim.toJSON()
i = -1
for source in claim.sources:
i += 1
for ref_prop in filter(self.checkProperty, source.keys()):
for snak in source[ref_prop]:
json.setdefault('qualifiers', {}).setdefault(ref_prop, [])
for qual in (pywikibot.Claim.qualifierFromJSON(self.repo, q)
for q in json['qualifiers'][ref_prop]):
if qual.target_equals(snak.getTarget()):
break
else:
snak.isReference = False
snak.isQualifier = True
json['qualifiers'][ref_prop].append(snak.toJSON())
json['references'][i]['snaks'][ref_prop].pop(0)
if len(json['references'][i]['snaks'][ref_prop]) == 0:
json['references'][i]['snaks'].pop(ref_prop)
if len(json['references'][i]['snaks']) == 0:
json['references'].pop(i)
i -= 1
moved.add(ref_prop)
if len(moved) > 0:
data = {'claims': [json]}
self.user_edit_entity(item, data, summary=self.makeSummary(prop, moved),
asynchronous=True)
def makeSummary(self, prop, props):
props = ['[[Property:P%s]]' % pid for pid in sorted(
int(pid[1:]) for pid in props)]
return '[[Property:%s]]: moving misplaced reference%s %s to qualifiers' % (
prop, 's' if len(props) > 1 else '', '%s and %s' % (
', '.join(props[:-1]), props[-1]) if len(props) > 1 else props[0])
def main(*args):
options = {}
for arg in pywikibot.handle_args(args):
if arg.startswith('-'):
arg, sep, value = arg.partition(':')
if value != '':
options[arg[1:]] = value if not value.isdigit() else int(value)
else:
options[arg[1:]] = True
site = pywikibot.Site('wikidata', 'wikidata')
bot = QualifiersFixingBot(site=site, **options)
bot.run()
if __name__ == '__main__':
main()