-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathshift_ranks.py
96 lines (81 loc) · 3 KB
/
shift_ranks.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
#!/usr/bin/python
import pywikibot
from pywikibot import pagegenerators
from query_store import QueryStore
from wikidata import WikidataEntityBot
class RanksShiftingBot(WikidataEntityBot):
end_prop = 'P582'
reason_prop = 'P2241'
use_from_page = False
def __init__(self, generator, **kwargs):
self.available_options.update({
'limit': 500,
})
super().__init__(**kwargs)
self.store = QueryStore()
self._generator = generator or self.custom_generator()
def custom_generator(self):
query = self.store.build_query(
'shift_ranks',
limit=self.opt['limit'],
prop=self.end_prop
)
return pagegenerators.WikidataSPARQLPageGenerator(query, site=self.repo)
@property
def generator(self):
return pagegenerators.PreloadingEntityGenerator(self._generator)
@property
def summary(self):
return ('undeprecate claims and shift other ranks, see '
'[[Special:MyLanguage/Help:Ranking|Help:Ranking]]')
def treat_page_and_item(self, page, item):
changed = False
for claims in item.claims.values():
by_rank = {
'preferred': [],
'normal': [],
'deprecated': [],
}
ok = False
for claim in claims:
by_rank[claim.rank].append(claim)
if claim.rank == 'preferred':
if claim.qualifiers.get(self.end_prop):
ok = False
break
elif claim.rank == 'deprecated':
if claim.qualifiers.get(self.reason_prop):
ok = False
break
if not ok:
ok = bool(claim.qualifiers.get(self.end_prop))
if not ok:
continue
for claim in by_rank['deprecated']:
if claim.qualifiers.get(self.end_prop):
claim.setRank('normal')
changed = True
if not by_rank['preferred']:
for claim in by_rank['normal']:
if not claim.qualifiers.get(self.end_prop):
claim.setRank('preferred')
changed = True
if changed:
self.user_edit_entity(item, summary=self.summary)
def main(*args):
options = {}
local_args = pywikibot.handle_args(args)
site = pywikibot.Site()
genFactory = pagegenerators.GeneratorFactory(site=site)
for arg in genFactory.handle_args(local_args):
if arg.startswith('-'):
arg, sep, value = arg.partition(':')
if value != '':
options[arg[1:]] = int(value) if value.isdigit() else value
else:
options[arg[1:]] = True
generator = genFactory.getCombinedGenerator()
bot = RanksShiftingBot(generator=generator, site=site, **options)
bot.run()
if __name__ == '__main__':
main()