jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/508103 )
Change subject: [IMPR] get commons category from wikibase ......................................................................
[IMPR] get commons category from wikibase
- introduce a new Page method get_best_claim to retrieved the first preferred or the first claim referring to the given page - if a wikibase is connected use the best Claim with id P373 for wikidata to get the commons category for the given page - page parameter must be mandatory in findCommonscatLink method - remove pwb release comment about Page.interwiki method
Bug: T175207 Change-Id: Ic34a49f53a14699bd039e112075998c2f404afb9 --- M pywikibot/page/__init__.py M scripts/commonscat.py 2 files changed, 75 insertions(+), 12 deletions(-)
Approvals: Meno25: Looks good to me, but someone else must approve Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/page/__init__.py b/pywikibot/page/__init__.py index bee913d..1e1ac76 100644 --- a/pywikibot/page/__init__.py +++ b/pywikibot/page/__init__.py @@ -39,6 +39,7 @@ AutoblockUser, NotEmailableError, SiteDefinitionError, + UnknownExtension, UserRightsError, ) from pywikibot.family import Family @@ -2329,6 +2330,45 @@ if save: self.save(**kwargs)
+ def get_best_claim(self, prop: str): + """ + Return the first best Claim for this page. + + Return the first 'preferred' ranked Claim specified by wikibase + property or the first 'normal' one otherwise. + + @param prop: property id, "P###" + @return: Claim object given by wikibase property number + for this page object. + @rtype: pywikibot.Claim or None + + @raises UnknownExtension: site has no wikibase extension + """ + def find_best_claim(claims): + """Find the first best ranked claim.""" + index = None + for i, claim in enumerate(claims): + if claim.rank == 'preferred': + return claim + if index is None and claim.rank == 'normal': + index = i + if index is None: + index = 0 + return claims[index] + + if not self.site.has_data_repository: + raise UnknownExtension( + 'Wikibase is not implemented for {}.'.format(self.site)) + try: + item_page = pywikibot.ItemPage.fromPage(self) + except pywikibot.NoPage: + pass + else: + item_page.get() + if prop in item_page.claims: + return find_best_claim(item_page.claims[prop]) + return None +
class FilePage(Page):
diff --git a/scripts/commonscat.py b/scripts/commonscat.py index 7cbb639..5e01aae 100755 --- a/scripts/commonscat.py +++ b/scripts/commonscat.py @@ -35,7 +35,7 @@ # *Found one template. Add this template # *Found more templates. Ask the user <- still have to implement this # -# (C) Pywikibot team, 2008-2019 +# (C) Pywikibot team, 2008-2020 # # Distributed under the terms of the MIT license. # @@ -52,6 +52,11 @@ '¶ms;': pagegenerators.parameterHelp }
+# wikibase property containing the wikibase category +wikibase_property = { + 'wikidata:wikidata': 'P373', +} + # Primary template, list of alternatives # No entry needed if it is like _default commonscatTemplates = { @@ -293,7 +298,7 @@ return
# Commonscat link is wrong - commonscatLink = self.findCommonscatLink(page) + commonscatLink = self.find_commons_category(page) if commonscatLink: self.changeCommonscat(page, currentCommonscatTemplate, currentCommonscatTarget, @@ -304,7 +309,7 @@ pywikibot.output('Found a template in the skip list. Skipping ' + page.title()) else: - commonscatLink = self.findCommonscatLink(page) + commonscatLink = self.find_commons_category(page) if commonscatLink: if commonscatLink == page.title(): textToAdd = '{{%s}}' % primaryCommonscat @@ -355,14 +360,10 @@ self.userPut(page, page.text, newtext, summary=comment, ignore_save_related_errors=True)
- def findCommonscatLink(self, page=None): + def findCommonscatLink(self, page) -> str: """Find CommonsCat template on interwiki pages.
- In Pywikibot >=2.0, page.interwiki() now returns Link objects, - not Page objects - @return: name of a valid commons category - @rtype: str """ for ipageLink in page.langlinks(): ipage = pywikibot.page.Page(ipageLink) @@ -371,24 +372,46 @@ if (not ipage.exists() or ipage.isRedirectPage() or ipage.isDisambig()): continue + commonscatLink = self.getCommonscatLink(ipage) if not commonscatLink: continue - (currentTemplate, - possibleCommonscat, linkText, Note) = commonscatLink + checkedCommonscat = self.checkCommonscatLink( - possibleCommonscat) + commonscatLink[1]) + if checkedCommonscat: pywikibot.output( 'Found link for {} at [[{}:{}]] to {}.' .format(page.title(), ipage.site.code, ipage.title(), checkedCommonscat)) return checkedCommonscat + except pywikibot.BadTitle: # The interwiki was incorrect - return '' + break return ''
+ def find_commons_category(self, page) -> str: + """Find CommonsCat template on wikibase repository. + + Use wikibase property to get the category if possible. + Otherwise check all langlinks to find it. + + @return: name of a valid commons category + """ + data_repo = page.site.data_repository() + cat_property = wikibase_property.get(data_repo.sitename) + if cat_property: + claim = page.get_best_claim(cat_property) + if claim: + category = claim.getTarget() + if category: + return category + + # fallback to interwiki pages + return self.findCommonscatLink(page) + def getCommonscatLink(self, wikipediaPage=None): """Find CommonsCat template on page.
pywikibot-commits@lists.wikimedia.org