jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/293626 )
Change subject: Get thumburl information in FilePage() ......................................................................
Get thumburl information in FilePage()
Add get_file_url() to retrieve and generate thumburl information.
Deprecate page.fileUrl(), as file url is returned by get_file_url() by default.
In case a thumburl is selected, it relies directly on the API implementation and makes an API call per each requested thumburl.
Might not be efficient if several thumburls are needed.
Added tests.
Bug: T137011 Change-Id: I3054cd96292f8976f60f3a3f470339305f223efe --- M pywikibot/page.py M pywikibot/site.py M tests/file_tests.py 3 files changed, 115 insertions(+), 4 deletions(-)
Approvals: jenkins-bot: Verified Xqt: Looks good to me, approved
diff --git a/pywikibot/page.py b/pywikibot/page.py index 8e4a40f..78c2f79 100644 --- a/pywikibot/page.py +++ b/pywikibot/page.py @@ -2437,11 +2437,44 @@ self._imagePageHtml = http.request(self.site, path) return self._imagePageHtml
+ @deprecated('get_file_url') def fileUrl(self): """Return the URL for the file described on this page.""" - # TODO add scaling option? return self.latest_file_info.url
+ def get_file_url(self, url_width=None, url_height=None, url_param=None): + """ + Return the url or the thumburl of the file described on this page. + + Fetch the information if not available. + + Once retrieved, thumburl information will also be accessible as + latest_file_info attributes, named as in [1]: + - url, thumburl, thumbwidth and thumbheight + + Parameters correspond to iiprops in: + [1] U{https://www.mediawiki.org/wiki/API:Imageinfo%7D + + Parameters validation and error handling left to the API call. + + @param width: see iiurlwidth in [1] + @param height: see iiurlheigth in [1] + @param param: see iiurlparam in [1] + + @return: latest file url or thumburl + @rtype: unicode + + """ + # Plain url is requested. + if url_width is None and url_height is None and url_param is None: + return self.latest_file_info.url + + # Thumburl is requested. + self.site.loadimageinfo(self, history=not self._file_revisions, + url_width=url_width, url_height=url_height, + url_param=url_param) + return self.latest_file_info.thumburl + @deprecated("fileIsShared") def fileIsOnCommons(self): """ diff --git a/pywikibot/site.py b/pywikibot/site.py index 34a774a..7c778c9 100644 --- a/pywikibot/site.py +++ b/pywikibot/site.py @@ -2987,13 +2987,27 @@ ) self._update_page(page, query)
- def loadimageinfo(self, page, history=False): + 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.
+ Parameters correspond to iiprops in: + [1] U{https://www.mediawiki.org/wiki/API:Imageinfo%7D + + Parameters validation and error handling left to the API call. + @param history: if true, return the image's version history + @param url_width: see iiurlwidth in [1] + @param url_height: see iiurlheigth in [1] + @param url_param: see iiurlparam in [1] + """ title = page.title(withSection=False) - args = {"titles": title} + args = {'titles': title, + 'iiurlwidth': url_width, + 'iiurlheight': url_height, + 'iiurlparam': url_param, + } if not history: args["total"] = 1 query = self._generator(api.PropertyGenerator, diff --git a/tests/file_tests.py b/tests/file_tests.py index 2065bbb..483b484 100644 --- a/tests/file_tests.py +++ b/tests/file_tests.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- """FilePage tests.""" # -# (C) Pywikibot team, 2014 +# (C) Pywikibot team, 2017 # # Distributed under the terms of the MIT license. # @@ -136,6 +136,8 @@ family = 'wikipedia' code = 'test'
+ file_name = 'File:Albert Einstein Head.jpg' + cached = True
def test_file_info_with_no_page(self): @@ -155,6 +157,68 @@ image = image.latest_file_info
+class TestFilePageLatestFileInfo(TestCase): + + """Test FilePage.latest_file_info. + + These tests cover properties and methods in FilePage that rely + on site.loadimageinfo. + + """ + + family = 'commons' + code = 'commons' + + file_name = 'File:Albert Einstein Head.jpg' + + cached = True + + def setUp(self): + """Create File page.""" + super(TestCase, self).setUp() + self.image = pywikibot.FilePage(self.site, self.file_name) + + def test_get_file_url(self): + """Get File url.""" + self.assertTrue(self.image.exists()) + self.assertEqual(self.image.get_file_url(), + 'https://upload.wikimedia.org/wikipedia/commons/' + 'd/d3/Albert_Einstein_Head.jpg') + self.assertEqual(self.image.latest_file_info.url, + 'https://upload.wikimedia.org/wikipedia/commons/' + 'd/d3/Albert_Einstein_Head.jpg') + + def test_get_file_url_thumburl_from_width(self): + """Get File thumburl from width.""" + self.assertTrue(self.image.exists()) + # url_param has no precedence over height/width. + self.assertEqual(self.image.get_file_url(url_width=100, url_param='1000px'), + 'https://upload.wikimedia.org/wikipedia/commons/thumb/' + 'd/d3/Albert_Einstein_Head.jpg/100px-Albert_Einstein_Head.jpg') + self.assertEqual(self.image.latest_file_info.thumbwidth, 100) + self.assertEqual(self.image.latest_file_info.thumbheight, 133) + + def test_get_file_url_thumburl_from_heigth(self): + """Get File thumburl from height.""" + self.assertTrue(self.image.exists()) + # url_param has no precedence over height/width. + self.assertEqual(self.image.get_file_url(url_height=100, url_param='1000px'), + 'https://upload.wikimedia.org/wikipedia/commons/thumb/' + 'd/d3/Albert_Einstein_Head.jpg/75px-Albert_Einstein_Head.jpg') + self.assertEqual(self.image.latest_file_info.thumbwidth, 75) + self.assertEqual(self.image.latest_file_info.thumbheight, 100) + + def test_get_file_url_thumburl_from_url_param(self): + """Get File thumburl from height.""" + self.assertTrue(self.image.exists()) + # url_param has no precedence over height/width. + self.assertEqual(self.image.get_file_url(url_param='100px'), + 'https://upload.wikimedia.org/wikipedia/commons/thumb/' + 'd/d3/Albert_Einstein_Head.jpg/100px-Albert_Einstein_Head.jpg') + self.assertEqual(self.image.latest_file_info.thumbwidth, 100) + self.assertEqual(self.image.latest_file_info.thumbheight, 133) + + class TestDeprecatedFilePage(DeprecationTestCase):
"""Test deprecated parts of FilePage."""
pywikibot-commits@lists.wikimedia.org