jenkins-bot has submitted this change and it was merged.
Change subject: Support querying for commonsMedia via WDQ ......................................................................
Support querying for commonsMedia via WDQ
commonsMedia is a normal STRING query but since it is not stored as a plain string in pywikibot it must be handled separately.
This follows up on the comments in commit If81659f2a17442cf182c8402a30e881d7851db17
Bug: T128987 Change-Id: Ibda2db95e83c5deae94bde422f76191f3c65a23a --- M pywikibot/data/wikidataquery.py M tests/wikidataquery_tests.py 2 files changed, 13 insertions(+), 1 deletion(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/data/wikidataquery.py b/pywikibot/data/wikidataquery.py index 8de89bd..c52f3a1 100644 --- a/pywikibot/data/wikidataquery.py +++ b/pywikibot/data/wikidataquery.py @@ -454,7 +454,10 @@
if claim.type == 'wikibase-item': return HasClaim(claim.getID(numeric=True), claim.getTarget().getID(numeric=True)) - if Claim.types.get(claim.type) == basestring: + if claim.type == 'commonsMedia': + return StringClaim(claim.getID(numeric=True), + claim.getTarget().title(withNamespace=False)) + if claim.type in ('string', 'url', 'math', 'external-id'): return StringClaim(claim.getID(numeric=True), claim.getTarget()) else: raise TypeError("Cannot construct a query from a claim of type %s" diff --git a/tests/wikidataquery_tests.py b/tests/wikidataquery_tests.py index 8ab9ef3..f9a76b8 100644 --- a/tests/wikidataquery_tests.py +++ b/tests/wikidataquery_tests.py @@ -171,6 +171,15 @@ q = query.fromClaim(claim) self.assertEqual(str(q), 'string[268:"somestring"]')
+ # Datatype: commonsMedia + claim = Claim(self.repo, 'P18') + claim.setTarget( + pywikibot.FilePage( + pywikibot.Site(self.family, self.code), + 'Foo.jpg')) + q = query.fromClaim(claim) + self.assertEqual(str(q), 'string[18:"Foo.jpg"]') + def testQuerySets(self): """Test that we can join queries together correctly.""" # construct via queries
pywikibot-commits@lists.wikimedia.org