jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/371659 )
Change subject: FilePage: add API query&prop=globalusage support ......................................................................
FilePage: add API query&prop=globalusage support
Raise UnknownExtension if GlobalUsage is not installed on wiki.
Bug: T168317 Change-Id: Id634380c2692411611dfe9414f9be2fe42c55c11 --- M pywikibot/page.py M pywikibot/site.py M tests/page_tests.py 3 files changed, 68 insertions(+), 0 deletions(-)
Approvals: jenkins-bot: Verified Xqt: Looks good to me, approved
diff --git a/pywikibot/page.py b/pywikibot/page.py index 3fbcf79..f815f53 100644 --- a/pywikibot/page.py +++ b/pywikibot/page.py @@ -2693,6 +2693,16 @@ pywikibot.warning('Unsuccesfull request (%s): %s' % (req.status, req.uri)) return False
+ def globalusage(self, total=None): + """ + Iterate all global usage for this page. + + @param total: iterate no more than this number of pages in total + @return: a generator that yields Pages also on sites different from + self.site. + @rtype: generator + """ + return self.site.globalusage(self, total=total)
wrapper = _ModuleDeprecationWrapper(__name__) wrapper._add_deprecated_attr('ImagePage', FilePage) diff --git a/pywikibot/site.py b/pywikibot/site.py index 1debbda..0e5efbd 100644 --- a/pywikibot/site.py +++ b/pywikibot/site.py @@ -3004,6 +3004,45 @@ ) self._update_page(page, query)
+ @need_extension('Global Usage') + def globalusage(self, page, total=None): + """Iterate global image usage for a given FilePage. + + @param page: the page to return global image usage for. + @type image: FilePage + @param total: iterate no more than this number of pages in total. + @raises TypeError: input page is not a FilePage. + @raises SiteDefinitionError: Site could not be defined for a returned + entry in API response. + """ + if not isinstance(page, pywikibot.FilePage): + raise TypeError('Page %s must be a FilePage.' % page) + + title = page.title(withSection=False) + args = {'titles': title, + 'gufilterlocal': False, + } + query = self._generator(api.PropertyGenerator, + type_arg='globalusage', + guprop=['url', 'pageid', 'namespace'], + total=total, # will set gulimit=total in api, + **args) + + self._update_page(page, query) + + for pageitem in query: + assert 'globalusage' in pageitem, \ + "API globalusage response lacks 'globalusage' key" + for entry in pageitem['globalusage']: + try: + gu_site = pywikibot.Site(url=entry['url']) + except SiteDefinitionError: + pywikibot.warning('Site could not be defined for global' + ' usage for {0}: {1}.'.format(page, entry)) + continue + gu_page = pywikibot.Page(gu_site, entry['title']) + yield gu_page + def loadimageinfo(self, page, history=False, url_width=None, url_height=None, url_param=None): """Load image info from api and save in page attributes. diff --git a/tests/page_tests.py b/tests/page_tests.py index 3c74326..9b7377b 100644 --- a/tests/page_tests.py +++ b/tests/page_tests.py @@ -628,6 +628,25 @@ self.site.loadimageinfo.assert_called_once_with(page, history=True)
+class TestFilePage(DefaultSiteTestCase): + + """Test methods of the FilePage class.""" + + family = 'commons' + code = 'commons' + + cached = True + + def test_globalusage(self, key): + """Test globalusage generator.""" + page = pywikibot.FilePage(self.site, 'File:Example.jpg') + gen = page.globalusage(total=3) + pages = list(gen) + self.assertEqual(len(pages), 3) + self.assertTrue(any(isinstance(p), pywikibot.Page) for p in pages) + self.assertTrue(any(p.site != self.site for p in pages)) + + class TestPageRepr(TestPageBaseUnicode):
"""Test for Page's repr implementation."""
pywikibot-commits@lists.wikimedia.org