jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/370359 )
Change subject: Page: add support for retrieving the page image ......................................................................
Page: add support for retrieving the page image
Bug: T172625 Change-Id: I1fea9c17aaea560765dc76787ac9475d6cd5d4e4 --- M pywikibot/data/api.py M pywikibot/page.py M pywikibot/site.py 3 files changed, 38 insertions(+), 1 deletion(-)
Approvals: jenkins-bot: Verified Xqt: Looks good to me, approved
diff --git a/pywikibot/data/api.py b/pywikibot/data/api.py index 7f4c80b..f905bc6 100644 --- a/pywikibot/data/api.py +++ b/pywikibot/data/api.py @@ -3242,6 +3242,9 @@ coords.append(coord) page._coords = coords
+ if 'pageimage' in pagedict: + page._pageimage = pywikibot.FilePage(page.site, pagedict['pageimage']) + if "pageprops" in pagedict: page._pageprops = pagedict['pageprops']
diff --git a/pywikibot/page.py b/pywikibot/page.py index bb8b224..3fbcf79 100644 --- a/pywikibot/page.py +++ b/pywikibot/page.py @@ -162,7 +162,8 @@ '_text', '_pageid', '_catinfo', '_templates', '_protection', '_contentmodel', '_langlinks', '_isredir', '_coords', '_preloadedtext', '_timestamp', '_applicable_protections', - '_flowinfo', '_quality', '_pageprops', '_revid', '_quality_text' + '_flowinfo', '_quality', '_pageprops', '_revid', '_quality_text', + '_pageimage' )
def __init__(self, source, title=u"", ns=0): @@ -1622,6 +1623,22 @@ else: return self._coords
+ @need_version('1.20') + def page_image(self): + """ + Return the most appropriate image on the page. + + Uses the MediaWiki extension PageImages. + + @return: A FilePage object + @rtype: FilePage + """ + if not hasattr(self, '_pageimage'): + self._pageimage = None + self.site.loadpageimage(self) + + return self._pageimage + def getRedirectTarget(self): """ Return a Page object for the target this Page redirects to. diff --git a/pywikibot/site.py b/pywikibot/site.py index e2bc6cc..2e282d1 100644 --- a/pywikibot/site.py +++ b/pywikibot/site.py @@ -2978,6 +2978,23 @@ coprimary='all') self._update_page(page, query)
+ @need_extension('PageImages') + def loadpageimage(self, page): + """ + Load [[mw:Extension:PageImages]] info. + + @param page: The page for which to obtain the image + @type page: Page class + + @raises APIError: PageImages extension is not installed + """ + title = page.title(withSection=False) + query = self._generator(api.PropertyGenerator, + type_arg='pageimages', + titles=title.encode(self.encoding()), + piprop=['name']) + self._update_page(page, query) + def loadpageprops(self, page): """Load page props for the given page.""" title = page.title(withSection=False)
pywikibot-commits@lists.wikimedia.org