Jean-Frédéric has submitted this change and it was merged.

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.

Bug: T110003
Change-Id: Ie5b66d175a03c8e325a86d6eddb607057dca1307
---
M erfgoedbot/categorize_images.py
1 file changed, 25 insertions(+), 5 deletions(-)

Approvals:
  Jean-Frédéric: Verified; Looks good to me, approved



diff --git a/erfgoedbot/categorize_images.py b/erfgoedbot/categorize_images.py
index 8e859ad..29d272a 100644
--- a/erfgoedbot/categorize_images.py
+++ b/erfgoedbot/categorize_images.py
@@ -23,6 +23,10 @@
 import re
 import MySQLdb
 
+
+class 
NoCommonsCatFromWikidataItemException(pywikibot.exceptions.PageRelatedError):
+    pass
+
 # Contains the commonscat templates for most Wikipedia's (taken from 
ex-commonscat.py)
 commonscatTemplates = {
     '_default': (u'Commonscat', []),
@@ -389,22 +393,38 @@
     '''
     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 NoCommonsCatFromWikidataItemException:
+                    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.
+
+    Raises: NoCommonsCatFromWikidataItemException if either there is no linked 
item
+            or it does not bear P373
+    '''
+    try:
+        data_item = page.data_item()
+        claims = data_item.get()['claims']
+        return claims['P373'][0].getTarget()
+    except (pywikibot.NoPage, KeyError):
+        raise NoCommonsCatFromWikidataItemException(page)
+
+
 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: merged
Gerrit-Change-Id: Ie5b66d175a03c8e325a86d6eddb607057dca1307
Gerrit-PatchSet: 3
Gerrit-Project: labs/tools/heritage
Gerrit-Branch: master
Gerrit-Owner: Jean-Frédéric <[email protected]>
Gerrit-Reviewer: Jean-Frédéric <[email protected]>
Gerrit-Reviewer: Multichill <[email protected]>
Gerrit-Reviewer: Ricordisamoa <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to