jenkins-bot has submitted this change and it was merged.
Change subject: Add mechanism for removing qualifiers ......................................................................
Add mechanism for removing qualifiers
Adds mechanisms for removing qualifiers, similar to existing ones for removing sources, by connecting to the wbremovequalifiers API call.
Also adds tests for this mechanism to wikibase_edit_tests
Bug: T119963 Change-Id: Id69451e5cb3e5b037b2eace61472356fffd40cde --- M pywikibot/page.py M pywikibot/site.py M tests/wikibase_edit_tests.py 3 files changed, 129 insertions(+), 1 deletion(-)
Approvals: John Vandenberg: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/page.py b/pywikibot/page.py index aa90752..a40cca5 100644 --- a/pywikibot/page.py +++ b/pywikibot/page.py @@ -4410,6 +4410,27 @@ else: self.qualifiers[qualifier.getID()] = [qualifier]
+ def removeQualifier(self, qualifier, **kwargs): + """ + Remove the qualifier. Calls removeQualifiers(). + + @param qualifier: the qualifier to remove + @type qualifier: Claim + """ + self.removeQualifiers([qualifier], **kwargs) + + def removeQualifiers(self, qualifiers, **kwargs): + """ + Remove the qualifiers. + + @param qualifiers: the qualifiers to remove + @type qualifiers: list Claim + """ + data = self.repo.remove_qualifiers(self, qualifiers, **kwargs) + self.on_item.latest_revision_id = data['pageinfo']['lastrevid'] + for qualifier in qualifiers: + self.qualifiers[qualifier.getID()].remove(qualifier) + def target_equals(self, value): """ Check whether the Claim's target is equal to specified value. diff --git a/pywikibot/site.py b/pywikibot/site.py index 1f987e6..361bf4c 100644 --- a/pywikibot/site.py +++ b/pywikibot/site.py @@ -7060,7 +7060,7 @@ """ Remove claims.
- @param claims: Claims to be added + @param claims: Claims to be removed @type claims: list of Claim @param bot: Whether to mark the edit as a bot edit @type bot: bool @@ -7123,6 +7123,38 @@ return data
@must_be(group='user') + def remove_qualifiers(self, claim, qualifiers, + bot=True, summary=None, baserevid=None): + """ + Remove qualifiers. + + @param claim: A Claim object to remove the qualifier from + @type claim: Claim + @param qualifiers: Claim objects currently used as a qualifiers + @type qualifiers: list of Claim + @param bot: Whether to mark the edit as a bot edit + @type bot: bool + @param summary: Edit summary + @type summary: str + @param baserevid: Base revision id override, used to detect conflicts. + When omitted, revision of claim.on_item is used. DEPRECATED. + @type baserevid: long + """ + params = dict(action='wbremovequalifiers', + claim=claim.snak, + baserevid=self._get_baserevid(claim, baserevid), + summary=summary, + bot=bot, + ) + + params['qualifiers'] = [qualifier.hash for qualifier in qualifiers] + params['token'] = self.tokens['edit'] + + req = self._simple_request(**params) + data = req.submit() + return data + + @must_be(group='user') def linkTitles(self, page1, page2, bot=True): """ Link two pages together. diff --git a/tests/wikibase_edit_tests.py b/tests/wikibase_edit_tests.py index 9bda4d1..fe9eccc 100644 --- a/tests/wikibase_edit_tests.py +++ b/tests/wikibase_edit_tests.py @@ -172,6 +172,81 @@ self.assertEqual(new_item.getRedirectTarget(), target_item)
+class TestWikibaseRemoveQualifier(WikibaseTestCase): + + """Run wikibase write tests to remove qualifiers.""" + + family = 'wikidata' + code = 'test' + + user = True + write = True + + def setUp(self): + """Add a claim with two qualifiers.""" + super(TestWikibaseRemoveQualifier, self).setUp() + testsite = self.get_repo() + item = pywikibot.ItemPage(testsite, 'Q68') + item.get() + # Create claim with qualifier + if 'P115' in item.claims: + item.removeClaims(item.claims['P115']) + + claim = pywikibot.page.Claim(testsite, 'P115', datatype='wikibase-item') + target = pywikibot.ItemPage(testsite, 'Q271') + claim.setTarget(target) + item.addClaim(claim) + + item.get(force=True) + + qual_1 = pywikibot.page.Claim(testsite, 'P88', isQualifier=True) + qual_1.setTarget(pywikibot.WbTime(year=2012)) + item.claims['P115'][0].addQualifier(qual_1) + + qual_2 = pywikibot.page.Claim(testsite, 'P580', isQualifier=True) + qual_2.setTarget(pywikibot.ItemPage(testsite, 'Q67')) + item.claims['P115'][0].addQualifier(qual_2) + + def test_remove_single(self): + """Test adding a claim with two qualifiers, then removing one.""" + self.setUp() + testsite = self.get_repo() + item = pywikibot.ItemPage(testsite, 'Q68') + item.get(force=True) + + # Remove qualifier + claim = item.claims['P115'][0] + qual_3 = claim.qualifiers[u'P580'][0] + claim.removeQualifier(qual_3) + + # Check P580 qualifier removed but P88 qualifier remains + item = pywikibot.ItemPage(testsite, 'Q68') + item.get(force=True) + claim = item.claims['P115'][0] + self.assertNotIn('P580', claim.qualifiers.keys()) + self.assertIn('P88', claim.qualifiers.keys()) + + def test_remove_multiple(self): + """Test adding a claim with two qualifiers, then removing both.""" + self.setUp() + testsite = self.get_repo() + item = pywikibot.ItemPage(testsite, 'Q68') + item.get(force=True) + + # Remove qualifiers + item.get(force=True) + claim = item.claims['P115'][0] + qual_3 = claim.qualifiers[u'P580'][0] + qual_4 = claim.qualifiers[u'P88'][0] + claim.removeQualifiers([qual_3, qual_4]) + + # Check P580 and P88 qualifiers are removed + item = pywikibot.ItemPage(testsite, 'Q68') + item.get(force=True) + claim = item.claims['P115'][0] + self.assertNotIn('P580', claim.qualifiers.keys()) + self.assertNotIn('P88', claim.qualifiers.keys()) + if __name__ == '__main__': try: unittest.main()
pywikibot-commits@lists.wikimedia.org