Jean-Frédéric has uploaded a new change for review.
https://gerrit.wikimedia.org/r/233340
Change subject: Support for Wikidata while guessing categories from CommonsCat
......................................................................
Support for Wikidata while guessing categories from CommonsCat
Wikipedia lists may have an empty CommonsCat template,
not because the target is the title of the page itself
(which is currently assumed by the bot), but because
the CommonsCat is taken from Wikidata.
This changeset adds support for this use case to ErfgoedBot.
If the bot finds an empty CommonsCat, it will try to access the
linked Wikidata item, and try to extract the target of P373.
Change-Id: Ie5b66d175a03c8e325a86d6eddb607057dca1307
---
M erfgoedbot/categorize_images.py
1 file changed, 16 insertions(+), 5 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/labs/tools/heritage
refs/changes/40/233340/1
diff --git a/erfgoedbot/categorize_images.py b/erfgoedbot/categorize_images.py
index 8e859ad..082f484 100644
--- a/erfgoedbot/categorize_images.py
+++ b/erfgoedbot/categorize_images.py
@@ -389,22 +389,33 @@
'''
Get a Commons category based on a page with a Commonscat template
'''
-
for (template, params) in page.templatesWithParams():
if template.title(withNamespace=False) in commonsCatTemplates:
if len(params) >= 1:
cat_title = params[0]
- break
# commonscat template without parameter
else:
- cat_title = page.title(withNamespace=False)
- break
+ # That may be inferred from Wikidata
+ try:
+ cat_title = get_Commons_category_via_Wikidata(page)
+ except (pywikibot.NoPage, KeyError):
+ # No Wikidata item or No P373 on the Wikidata item
+ cat_title = page.title(withNamespace=False)
+ break
site = pywikibot.Site(u'commons', u'commons')
cat = pywikibot.Category(site, cat_title)
-
return cat
+def get_Commons_category_via_Wikidata(page):
+ '''
+ Get Commons Category from the linked Wikidata item and P373
+ '''
+ data_item = page.data_item()
+ claims = data_item.get()['claims']
+ return claims['P373'][0].getTarget()
+
+
def processCountry(countrycode, lang, countryconfig, commonsCatTemplates,
conn, cursor, overridecat=None):
'''
Work on a single country.
--
To view, visit https://gerrit.wikimedia.org/r/233340
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie5b66d175a03c8e325a86d6eddb607057dca1307
Gerrit-PatchSet: 1
Gerrit-Project: labs/tools/heritage
Gerrit-Branch: master
Gerrit-Owner: Jean-Frédéric <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits