jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1013713?usp=email )
Change subject: [bugfix] Use the same iiprop in PageGenerator as in APISite.loadimageinfo() ......................................................................
[bugfix] Use the same iiprop in PageGenerator as in APISite.loadimageinfo()
Bug: T360093 Change-Id: I00e5ea66aa6401c6b9c000e9e19a5a35fbc8f4bc --- M pywikibot/data/api/_generators.py M pywikibot/site/__init__.py M pywikibot/site/_apisite.py M tests/pagegenerators_tests.py M tests/site_generators_tests.py 5 files changed, 67 insertions(+), 48 deletions(-)
Approvals: jenkins-bot: Verified Xqt: Looks good to me, approved
diff --git a/pywikibot/data/api/_generators.py b/pywikibot/data/api/_generators.py index 78df271..e32ed2e 100644 --- a/pywikibot/data/api/_generators.py +++ b/pywikibot/data/api/_generators.py @@ -6,7 +6,7 @@ :class:`tools.collections.GeneratorWrapper` """ # -# (C) Pywikibot team, 2008-2023 +# (C) Pywikibot team, 2008-2024 # # Distributed under the terms of the MIT license. # @@ -675,23 +675,28 @@ g_content: bool = False, **kwargs ) -> None: - """ - Initializer. + """Initializer.
- Required and optional parameters are as for ``Request``, except that - action=query is assumed and generator is required. + Required and optional parameters are as for ``Request``, except + that ``action=query`` is assumed and generator is required. + + .. versionchanged:: 9.1 + retrieve the same imageinfo properties as in + :meth:`APISite.loadimageinfo() + <pywikibot.site._apisite.APISite.loadimageinfo>` with default + parameters.
:param generator: the "generator=" type from api.php :param g_content: if True, retrieve the contents of the current version of each Page (default False) - """ - # If possible, use self.request after __init__ instead of appendParams + # If possible, use self.request after __init__ instead of append_params def append_params(params, key, value) -> None: if key in params: params[key] += '|' + value else: params[key] = value + kwargs = self._clean_kwargs(kwargs) parameters = kwargs['parameters'] # get some basic information about every page generated @@ -704,8 +709,7 @@ if not ('inprop' in parameters and 'protection' in parameters['inprop']): append_params(parameters, 'inprop', 'protection') - append_params(parameters, 'iiprop', - 'timestamp|user|comment|url|size|sha1') + append_params(parameters, 'iiprop', pywikibot.site._IIPROP) append_params(parameters, 'iilimit', 'max') # T194233 parameters['generator'] = generator super().__init__(**kwargs) diff --git a/pywikibot/site/__init__.py b/pywikibot/site/__init__.py index 6824eb1..9cfcbf4 100644 --- a/pywikibot/site/__init__.py +++ b/pywikibot/site/__init__.py @@ -1,6 +1,6 @@ """Library module representing MediaWiki sites (wikis).""" # -# (C) Pywikibot team, 2021-2022 +# (C) Pywikibot team, 2021-2024 # # Distributed under the terms of the MIT license. # @@ -21,3 +21,9 @@
__all__ = ('APISite', 'BaseSite', 'ClosedSite', 'DataSite', 'RemovedSite', 'Namespace', 'NamespacesDict', 'Siteinfo', 'TokenWallet') + +# iiprop file information to get, used in several places +_IIPROP = ( + 'timestamp', 'user', 'comment', 'url', 'size', 'sha1', 'mime', 'mediatype', + 'archivename', 'bitdepth', +) diff --git a/pywikibot/site/_apisite.py b/pywikibot/site/_apisite.py index 33af282..340d82e 100644 --- a/pywikibot/site/_apisite.py +++ b/pywikibot/site/_apisite.py @@ -1488,11 +1488,10 @@
The following properties are loaded: ``timestamp``, ``user``, ``comment``, ``url``, ``size``, ``sha1``, ``mime``, ``mediatype``, - ``archivename`` and ``bitdepth``. - ``metadata``is loaded only if history is False. - If *url_width*, *url_height* or *url_param* is given, additional - properties ``thumbwidth``, ``thumbheight``, ``thumburl`` and - ``responsiveUrls`` are given. + ``archivename`` and ``bitdepth``. ``metadata`` is loaded only if + *history* is False. If *url_width*, *url_height* or *url_param* + is given, additional properties ``thumbwidth``, ``thumbheight``, + ``thumburl`` and ``responsiveUrls`` are given.
.. note:: Parameters validation and error handling left to the API call. @@ -1508,7 +1507,7 @@ :param url_height: get info for a thumbnail with given height :param url_param: get info for a thumbnail with given param :param timestamp: timestamp of the image's version to retrieve. - It has effect only if history is False. + It has effect only if *history* is False. If omitted, the latest version will be fetched. """ args = { @@ -1516,14 +1515,11 @@ 'iiurlwidth': url_width, 'iiurlheight': url_height, 'iiurlparam': url_param, - 'iiprop': [ - 'timestamp', 'user', 'comment', 'url', 'size', 'sha1', 'mime', - 'mediatype', 'archivename', 'bitdepth', - ] + 'iiprop': pywikibot.site._IIPROP } if not history: args['total'] = 1 - args['iiprop'].append('metadata') + args['iiprop'] += ('metadata', ) if timestamp: args['iistart'] = args['iiend'] = timestamp.isoformat()
diff --git a/tests/pagegenerators_tests.py b/tests/pagegenerators_tests.py index ce38f4f..779e859 100755 --- a/tests/pagegenerators_tests.py +++ b/tests/pagegenerators_tests.py @@ -1453,6 +1453,8 @@ with self.assertRaisesRegex(ValueError, 'does not have a valid extension'): pywikibot.FilePage(page) + else: + self.assertIsInstance(page.latest_file_info.mime, str)
def test_wanted_templates(self): """Test wantedtemplates generator.""" diff --git a/tests/site_generators_tests.py b/tests/site_generators_tests.py index e595eed..c0cb13d 100755 --- a/tests/site_generators_tests.py +++ b/tests/site_generators_tests.py @@ -26,6 +26,17 @@ from tests.utils import skipping
+global_expected_params = { + 'action': ['query'], + 'continue': [True], + 'iilimit': ['max'], + 'iiprop': list(pywikibot.site._IIPROP), + 'indexpageids': [True], + 'inprop': ['protection'], + 'prop': ['info', 'imageinfo', 'categoryinfo'], +} + + class TestSiteGenerators(DefaultSiteTestCase): """Test cases for Site methods."""
@@ -179,16 +190,11 @@ def test_pagetemplates(self): """Test Site.pagetemplates.""" tl_gen = self.site.pagetemplates(self.mainpage) - expected_params = { - 'continue': [True], - 'inprop': ['protection'], - 'iilimit': ['max'], - 'iiprop': ['timestamp', 'user', 'comment', 'url', 'size', 'sha1'], - 'indexpageids': [True], - 'generator': ['templates'], 'action': ['query'], - 'prop': ['info', 'imageinfo', 'categoryinfo'], - 'titles': [self.mainpage.title()], - } + expected_params = dict( + global_expected_params, + generator=['templates'], + titles=[self.mainpage.title()], + )
self.assertEqual(tl_gen.request._params, expected_params)
@@ -213,16 +219,11 @@ """Test Site.pagelinks.""" links_gen = self.site.pagelinks(self.mainpage) gen_params = links_gen.request._params.copy() - expected_params = { - 'action': ['query'], 'indexpageids': [True], - 'continue': [True], - 'inprop': ['protection'], - 'iilimit': ['max'], - 'iiprop': ['timestamp', 'user', 'comment', 'url', 'size', - 'sha1'], 'generator': ['links'], - 'prop': ['info', 'imageinfo', 'categoryinfo'], - 'redirects': [False], - } + expected_params = dict( + global_expected_params, + generator=['links'], + redirects=[False], + ) if 'pageids' in gen_params: expected_params['pageids'] = [str(self.mainpage.pageid)] else: @@ -1110,13 +1111,13 @@ """Test site.search() method with 'where' parameter set to title.""" search_gen = self.site.search( 'wiki', namespaces=0, total=10, where='title') - expected_params = { - 'prop': ['info', 'imageinfo', 'categoryinfo'], - 'inprop': ['protection'], - 'iiprop': ['timestamp', 'user', 'comment', 'url', 'size', 'sha1'], - 'iilimit': ['max'], 'generator': ['search'], 'action': ['query'], - 'indexpageids': [True], 'continue': [True], - 'gsrnamespace': [0], 'gsrsearch': ['wiki'], 'gsrwhat': ['title']} + expected_params = dict( + global_expected_params, + generator=['search'], + gsrnamespace=[0], + gsrsearch=['wiki'], + gsrwhat=['title'], + ) self.assertEqual(search_gen.request._params, expected_params) for hit in search_gen: self.assertIsInstance(hit, pywikibot.Page)
pywikibot-commits@lists.wikimedia.org