-
Notifications
You must be signed in to change notification settings - Fork 5
/
fake_references.py
187 lines (163 loc) · 6.25 KB
/
fake_references.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
#!/usr/bin/python
from contextlib import suppress
import pywikibot
from pywikibot import pagegenerators
from pywikibot.backports import removeprefix
from query_store import QueryStore
from wikidata import WikidataEntityBot
class FakeReferencesBot(WikidataEntityBot):
item_ids = ['Q2013']
inferred_from = 'P3452'
ref_props = ['P143', 'P248']
url_props = ['P854']
use_from_page = False
whitelist_props = {'P813', 'P4656'}
def __init__(self, generator, **kwargs):
self.available_options.update({
'limit': None,
})
super().__init__(**kwargs)
self.store = QueryStore()
self._generator = generator or self.subgenerator()
self.url_start = self.repo.base_url(self.repo.article_path)
def subgenerator(self):
limit = self.opt['limit']
for ident in self.item_ids:
from_item = pywikibot.ItemPage(self.repo, ident)
for item in pagegenerators.WikibaseItemGenerator(
from_item.backlinks(
total=limit, filterRedirects=False, namespaces=[0])):
yield item
if limit is not None:
limit -= 1
if limit == 0:
return
for prop in self.url_props:
ok = True
while ok and limit != 0:
ok = False
query = self.store.build_query(
'fake_references_url',
limit=500 if limit is None else min(500, limit),
prop=prop)
for item in pagegenerators.WikidataSPARQLPageGenerator(
query, site=self.repo):
ok = True
yield item
if limit is not None:
limit -= 1
for prop in self.ref_props:
ok = True
while ok and limit != 0:
ok = False
query = self.store.build_query(
'fake_references',
limit=100 if limit is None else min(100, limit),
prop=prop)
for item in pagegenerators.WikidataSPARQLPageGenerator(
query, site=self.repo):
ok = True
yield item
if limit is not None:
limit -= 1
@property
def generator(self):
return pagegenerators.PreloadingEntityGenerator(self._generator)
@property
def summary(self):
return ('update reference per [[Wikidata:Requests for permissions/'
'Bot/MatSuBot 8|RfPB]]')
def treat_page_and_item(self, page, item):
changed = False
for prop, claims in item.claims.items():
for claim in claims:
if self.handle_claim(claim):
changed = True
if changed:
self.user_edit_entity(item, summary=self.summary)
def handle_claim(self, claim):
ret = False
if not claim.sources:
return ret
if claim.type == 'wikibase-item':
if claim.id == 'P1343' and 'P805' in claim.qualifiers:
target = claim.qualifiers['P805'][0].getTarget()
else:
target = claim.getTarget()
if target:
for source in claim.sources:
ret = self.handle_source_item(source, target) or ret
for source in claim.sources:
ret = self.handle_source_url(source) or ret
return ret
def handle_source_item(self, source, target):
ret = False
for prop in self.ref_props:
keys = set(source.keys())
if prop not in keys:
continue
if keys - (self.whitelist_props | {prop}):
continue
if len(source[prop]) > 1:
#continue?
return ret
fake = next(iter(source[prop]))
items = list(self.item_ids) + [target]
if any(fake.target_equals(tgt) for tgt in items):
snak = pywikibot.Claim(
self.repo, self.inferred_from, isReference=True)
snak.setTarget(target)
source.setdefault(self.inferred_from, []).append(snak)
source.pop(prop)
ret = True
return ret
def handle_source_url(self, source):
ret = False
for prop in self.url_props:
keys = set(source.keys())
if prop not in keys:
continue
if keys - (self.whitelist_props | {prop}):
continue
if len(source[prop]) > 1:
#continue?
return ret
snak = next(iter(source[prop]))
url = snak.getTarget()
if not url:
continue
target = None
with suppress(pywikibot.InvalidTitle, ValueError):
for prefix in [self.url_start, self.repo.concept_base_uri]:
target_id = removeprefix(url, prefix)
if target_id != url:
target = pywikibot.ItemPage(self.repo, target_id)
break
if target:
if target.isRedirectPage():
target = target.getRedirectTarget()
if target != snak.on_item:
snak = pywikibot.Claim(
self.repo, self.inferred_from, isReference=True)
snak.setTarget(target)
source.setdefault(self.inferred_from, []).append(snak)
source.pop(prop)
ret = True
return ret
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:]] = value if not value.isdigit() else int(value)
else:
options[arg[1:]] = True
generator = genFactory.getCombinedGenerator()
bot = FakeReferencesBot(generator=generator, site=site, **options)
bot.run()
if __name__ == '__main__':
main()