jenkins-bot has submitted this change and it was merged.
Change subject: Add support for statements in PropertyPage ......................................................................
Add support for statements in PropertyPage
It's possible to make/remove statements in Property pages in wikidata. - People can make changes in claims (add/remove/modify) using editEntity - Add .claims attribute to PropertyPage class as well
Bug: T92669 Change-Id: Ic0ca0de87af017a6356beac2dad793ac81cc7ee8 --- M pywikibot/page.py M tests/wikibase_edit_tests.py 2 files changed, 62 insertions(+), 39 deletions(-)
Approvals: John Vandenberg: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/page.py b/pywikibot/page.py index 446bbe0..17ae0db 100644 --- a/pywikibot/page.py +++ b/pywikibot/page.py @@ -3271,9 +3271,20 @@ self.descriptions[lang] = self._content[ 'descriptions'][lang]['value']
+ # claims + self.claims = {} + if 'claims' in self._content: + for pid in self._content['claims']: + self.claims[pid] = [] + for claim in self._content['claims'][pid]: + c = Claim.fromJSON(self.repo, claim) + c.on_item = self + self.claims[pid].append(c) + return {'aliases': self.aliases, 'labels': self.labels, 'descriptions': self.descriptions, + 'claims': self.claims, }
def _diff_to(self, type_key, key_name, value_name, diffto, data): @@ -3326,6 +3337,35 @@
if aliases: data['aliases'] = aliases + + claims = {} + for prop in self.claims: + if len(self.claims[prop]) > 0: + claims[prop] = [claim.toJSON() for claim in self.claims[prop]] + + if diffto and 'claims' in diffto: + temp = defaultdict(list) + claim_ids = set() + + diffto_claims = diffto['claims'] + + for prop in claims: + for claim in claims[prop]: + if (prop not in diffto_claims or + claim not in diffto_claims[prop]): + temp[prop].append(claim) + + claim_ids.add(claim['id']) + + for prop, prop_claims in diffto_claims.items(): + for claim in prop_claims: + if 'id' in claim and claim['id'] not in claim_ids: + temp[prop].append({'id': claim['id'], 'remove': ''}) + + claims = temp + + if claims: + data['claims'] = claims return data
def getID(self, numeric=False, force=False): @@ -3620,16 +3660,6 @@ """ super(ItemPage, self).get(force=force, *args, **kwargs)
- # claims - self.claims = {} - if 'claims' in self._content: - for pid in self._content['claims']: - self.claims[pid] = list() - for claim in self._content['claims'][pid]: - c = Claim.fromJSON(self.repo, claim) - c.on_item = self - self.claims[pid].append(c) - # sitelinks self.sitelinks = {} if 'sitelinks' in self._content: @@ -3670,34 +3700,6 @@
self._diff_to('sitelinks', 'site', 'title', diffto, data)
- claims = {} - for prop in self.claims: - if len(self.claims[prop]) > 0: - claims[prop] = [claim.toJSON() for claim in self.claims[prop]] - - if diffto and 'claims' in diffto: - temp = defaultdict(list) - claim_ids = set() - - diffto_claims = diffto['claims'] - - for prop in claims: - for claim in claims[prop]: - if (prop not in diffto_claims or - claim not in diffto_claims[prop]): - temp[prop].append(claim) - - claim_ids.add(claim['id']) - - for prop, prop_claims in diffto_claims.items(): - for claim in prop_claims: - if 'id' in claim and claim['id'] not in claim_ids: - temp[prop].append({'id': claim['id'], 'remove': ''}) - - claims = temp - - if claims: - data['claims'] = claims return data
def iterlinks(self, family=None): @@ -3805,7 +3807,8 @@ """ Remove the claims from the item.
- @type claims: list + @param claims: list of claims to be removed + @type claims: list or pywikibot.Claim
""" # this check allows single claims to be removed by pushing them into a diff --git a/tests/wikibase_edit_tests.py b/tests/wikibase_edit_tests.py index 84dc056..f0907e32 100644 --- a/tests/wikibase_edit_tests.py +++ b/tests/wikibase_edit_tests.py @@ -78,6 +78,26 @@ end_date.setTarget(pywikibot.WbTime(year=2012)) item.claims['P115'][0].addQualifier(end_date)
+ # Testing all again but this time in properties + item = pywikibot.PropertyPage(testsite, 'P115') + item.get() + if 'P115' in item.claims: + to_remove = [] + for claim in item.claims['P115']: + to_remove.append({'id': claim.toJSON()['id'], 'remove': ''}) + item.editEntity({'claims': to_remove}) + + claim = pywikibot.page.Claim(testsite, 'P115', datatype='wikibase-item') + target = pywikibot.ItemPage(testsite, 'Q271') + claim.setTarget(target) + item.editEntity({'claims': [claim.toJSON()]}) + + item.get(force=True) + + end_date = pywikibot.page.Claim(testsite, 'P88', isQualifier=True) + end_date.setTarget(pywikibot.WbTime(year=2012)) + item.claims['P115'][0].addQualifier(end_date) + def test_edit_entity_new_item(self): testsite = self.get_repo() ts = str(time.time())
pywikibot-commits@lists.wikimedia.org