jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/775948 )
Change subject: [IMPR] enable all parameters of APISite.imageusage() with FilePage.usingPages()
......................................................................
[IMPR] enable all parameters of APISite.imageusage() with FilePage.usingPages()
Change-Id: Ifda1a4ba119a9f7904f30c48468359dda4e2a288
---
M pywikibot/page/_pages.py
M pywikibot/site/_generators.py
2 files changed, 19 insertions(+), 9 deletions(-)
Approvals:
Matěj Suchánek: Looks good to me, but someone else must approve
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/page/_pages.py b/pywikibot/page/_pages.py
index f095424..3eea4ad 100644
--- a/pywikibot/page/_pages.py
+++ b/pywikibot/page/_pages.py
@@ -2473,14 +2473,19 @@
'| {{int:filehist-dimensions}} || {{int:filehist-comment}}\n'
'|-\n%s\n|}\n' % '\n|-\n'.join(lines))
- def usingPages(self, total: Optional[int] = None, content: bool = False):
+ def usingPages(self, **kwargs):
"""Yield Pages on which the file is displayed.
- :param total: iterate no more than this number of pages in total
- :param content: if True, load the current content of each iterated page
- (default False)
+ For parameters refer
+ :meth:`APISite.imageusage()
+ <pywikibot.site._generators.GeneratorsMixin.imageusage>`
+
+ .. versionchanged:: 7.2
+ all parameters from :meth:`APISite.imageusage()
+ <pywikibot.site._generators.GeneratorsMixin.imageusage>`
+ are available.
"""
- return self.site.imageusage(self, total=total, content=content)
+ return self.site.imageusage(self, **kwargs)
@property
def file_is_used(self) -> bool:
diff --git a/pywikibot/site/_generators.py b/pywikibot/site/_generators.py
index bf0b494..d6ecd73 100644
--- a/pywikibot/site/_generators.py
+++ b/pywikibot/site/_generators.py
@@ -1186,21 +1186,26 @@
namespaces=namespaces,
total=total, g_content=content)
- def imageusage(self, image, namespaces=None, filterredir=None,
- total=None, content: bool = False):
+ def imageusage(self, image: 'pywikibot.FilePage', *,
+ namespaces=None,
+ filterredir: Optional[bool] = None,
+ total: Optional[int] = None,
+ content: bool = False):
"""Iterate Pages that contain links to the given FilePage.
- :see: https://www.mediawiki.org/wiki/API:Imageusage
+ .. seealso:: https://www.mediawiki.org/wiki/API:Imageusage
+ .. versionchanged:: 7.2
+ all parameters except `image` are keyword only.
:param image: the image to search for (FilePage need not exist on
the wiki)
- :type image: pywikibot.FilePage
:param namespaces: If present, only iterate pages in these namespaces
:type namespaces: iterable of str or Namespace key,
or a single instance of those types. May be a '|' separated
list of namespace identifiers.
:param filterredir: if True, only yield redirects; if False (and not
None), only yield non-redirects (default: yield both)
+ :param total: iterate no more than this number of pages in total
:param content: if True, load the current content of each iterated page
(default False)
:raises KeyError: a namespace identifier was not resolved
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/775948
To unsubscribe, or for help writing mail filters, visit https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Ifda1a4ba119a9f7904f30c48468359dda4e2a288
Gerrit-Change-Number: 775948
Gerrit-PatchSet: 2
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: D3r1ck01 <xsavitar.wiki(a)aol.com>
Gerrit-Reviewer: Matěj Suchánek <matejsuchanek97(a)gmail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged
jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/775922 )
Change subject: [fix] Don't raise NoPageError with file_is_shared
......................................................................
[fix] Don't raise NoPageError with file_is_shared
Instead raising NoPageError, FilePage.file_is_shared returns False
if a file neither exists locally nor on shared image repository
(commons)
Bug: T305182
Change-Id: I4d20bbc599619c74285232db31f3e1f4d05aa78e
---
M pywikibot/page/_pages.py
M tests/file_tests.py
2 files changed, 15 insertions(+), 8 deletions(-)
Approvals:
Matěj Suchánek: Looks good to me, but someone else must approve
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/page/_pages.py b/pywikibot/page/_pages.py
index f095424..e9476f9 100644
--- a/pywikibot/page/_pages.py
+++ b/pywikibot/page/_pages.py
@@ -2446,17 +2446,27 @@
return self.latest_file_info.thumburl
def file_is_shared(self) -> bool:
- """Check if the file is stored on any known shared repository."""
+ """Check if the file is stored on any known shared repository.
+
+ .. versionchanged:: 7.0
+ return False if file does not exist on shared image repository
+ instead raising NoPageError.
+ """
# as of now, the only known repositories are commons and wikitravel
# TODO: put the URLs to family file
if not self.site.has_image_repository:
return False
+ try:
+ info = self.latest_file_info
+ except NoPageError:
+ return False
+
if 'wikitravel_shared' in self.site.shared_image_repository():
- return self.latest_file_info.url.startswith(
- 'https://wikitravel.org/upload/shared/')
+ return info.url.startswith('https://wikitravel.org/upload/shared/')
+
# default to commons
- return self.latest_file_info.url.startswith(
+ return info.url.startswith(
'https://upload.wikimedia.org/wikipedia/commons/')
def getFileVersionHistoryTable(self):
diff --git a/tests/file_tests.py b/tests/file_tests.py
index ef81ba4..8aed05a 100755
--- a/tests/file_tests.py
+++ b/tests/file_tests.py
@@ -91,13 +91,10 @@
self.assertFalse(commons_file.exists())
self.assertFalse(enwp_file.file_is_shared())
+ self.assertFalse(commons_file.file_is_shared())
page_doesnt_exist_exc_regex = re.escape(
"Page [[commons:{}]] doesn't exist.".format(title))
- with self.assertRaisesRegex(
- NoPageError,
- page_doesnt_exist_exc_regex):
- commons_file.file_is_shared()
with self.assertRaisesRegex(
NoPageError,
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/775922
To unsubscribe, or for help writing mail filters, visit https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I4d20bbc599619c74285232db31f3e1f4d05aa78e
Gerrit-Change-Number: 775922
Gerrit-PatchSet: 1
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: Matěj Suchánek <matejsuchanek97(a)gmail.com>
Gerrit-Reviewer: Multichill <maarten(a)mdammers.nl>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged
jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/773930 )
Change subject: [IMPR] Use itertools.zip_longest to find the most important image
......................................................................
[IMPR] Use itertools.zip_longest to find the most important image
- important_image uses usingPages() to find the most important image
but retrieving all pages need a long time. Now we collect all
usingPages from all images in list_given parallel and stop if the
generator produces only one image and the other tuple entries are
False.
- make important_image a static method
- test it
Change-Id: Idd8354469d444b72357a5a55b111cc9810fd6715
---
M scripts/checkimages.py
M tests/checkimages_tests.py
2 files changed, 37 insertions(+), 14 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/scripts/checkimages.py b/scripts/checkimages.py
index 4ec829b..af18f95 100755
--- a/scripts/checkimages.py
+++ b/scripts/checkimages.py
@@ -84,6 +84,8 @@
import collections
import re
import time
+
+from itertools import zip_longest
from typing import Generator
import pywikibot
@@ -803,26 +805,33 @@
for element in self.load(page_hidden_text):
self.hiddentemplates.add(pywikibot.Page(self.site, element))
- def important_image(self, list_given) -> pywikibot.FilePage:
+ @staticmethod
+ def important_image(
+ list_given: List[Tuple[float, pywikibot.FilePage]]
+ ) -> pywikibot.FilePage:
"""
Get tuples of image and time, return the most used or oldest image.
+ .. versionchanged: 7.2
+ itertools.zip_longest is used to stop `usingPages` as soon as
+ possible.
+
:param list_given: a list of tuples which hold seconds and FilePage
- :type list_given: list
:return: the most used or oldest image
"""
# find the most used image
- inx_found = None # index of found image
- max_usage = 0 # hold max amount of using pages
- for num, element in enumerate(list_given):
- image = element[1]
- image_used = len(list(image.usingPages()))
- if image_used > max_usage:
- max_usage = image_used
- inx_found = num
+ images = [image for _, image in list_given]
+ iterables = [image.usingPages() for image in images]
+ curr_images = []
+ for values in zip_longest(*iterables, fillvalue=False):
+ curr_images = values
+ # bool(FilePage) is True because it is an object subclass
+ if sum(bool(image) for image in values) <= 1:
+ break
- if inx_found is not None:
- return list_given[inx_found][1]
+ for inx, image in enumerate(curr_images):
+ if image is not False:
+ return images[inx]
# find the oldest image
_, image = max(list_given, key=lambda element: element[0])
diff --git a/tests/checkimages_tests.py b/tests/checkimages_tests.py
index b0da988..2666052 100755
--- a/tests/checkimages_tests.py
+++ b/tests/checkimages_tests.py
@@ -1,14 +1,16 @@
#!/usr/bin/python3
"""Unit tests for checkimages script."""
#
-# (C) Pywikibot team, 2015-2021
+# (C) Pywikibot team, 2015-2022
#
# Distributed under the terms of the MIT license.
#
import unittest
+from pywikibot import FilePage
+
from scripts import checkimages
-from tests.aspects import TestCase
+from tests.aspects import DefaultSiteTestCase, TestCase
class TestSettings(TestCase):
@@ -29,5 +31,17 @@
self.assertEqual(item1[1], 'a deprecated template')
+class TestMethods(DefaultSiteTestCase):
+
+ """Test methods of CheckImagesBot."""
+
+ def test_important_image(self):
+ """Test important_image method."""
+ filenames = ('Example.jpg', 'Demo.jpg')
+ images = [(0.0, FilePage(self.site, name)) for name in filenames]
+ self.assertEqual(checkimages.CheckImagesBot.important_image(images),
+ FilePage(self.site, 'Example.jpg'))
+
+
if __name__ == '__main__': # pragma: no cover
unittest.main()
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/773930
To unsubscribe, or for help writing mail filters, visit https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Idd8354469d444b72357a5a55b111cc9810fd6715
Gerrit-Change-Number: 773930
Gerrit-PatchSet: 4
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: D3r1ck01 <xsavitar.wiki(a)aol.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged