jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1154384?usp=email )
Change subject: [FEAT] exturlusage: search for http and https by default ......................................................................
[FEAT] exturlusage: search for http and https by default
Bug: T396280 Change-Id: Iec349bd21f3f0d04a43cf8d2d28d1735e8b5643e --- M pywikibot/site/_generators.py M tests/site_generators_tests.py 2 files changed, 29 insertions(+), 11 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/site/_generators.py b/pywikibot/site/_generators.py index 5deb56d..debd456 100644 --- a/pywikibot/site/_generators.py +++ b/pywikibot/site/_generators.py @@ -1317,8 +1317,8 @@ def exturlusage( self, url: str | None = None, - protocol: str | None = None, - namespaces: list[int] | None = None, + protocol: str | list[str] | None = None, + namespaces: NamespaceArgType = None, total: int | None = None, content: bool = False, ) -> Iterable[pywikibot.Page]: @@ -1331,9 +1331,11 @@ of the hostname :param namespaces: list of namespace numbers to fetch contribs from :param total: Maximum number of pages to retrieve in total - :param protocol: Protocol to search for, likely http or https, http by + :param protocol: list of protocols to search for, http and https by default. Full list shown on Special:LinkSearch wikipage """ + if isinstance(protocol, str): + protocol = [protocol] if url is not None: found_protocol, _, url = url.rpartition('://')
@@ -1343,13 +1345,18 @@ url = None
if found_protocol: - if protocol and protocol != found_protocol: - raise ValueError('Protocol was specified, but a different ' - 'one was found in searched url') - protocol = found_protocol - - if not protocol: - protocol = 'http' + if protocol: + if len(protocol) > 1: + raise ValueError( + 'More than one protocol was specified and a ' + 'protocol was found in searched url' + ) + if protocol[0] != found_protocol: + raise ValueError( + f'Protocol {protocol!r} was specified, but ' + f'{found_protocol!r} was found in searched url' + ) + protocol = [found_protocol]
return self._generator(api.PageGenerator, type_arg='exturlusage', geuquery=url, geuprotocol=protocol, diff --git a/tests/site_generators_tests.py b/tests/site_generators_tests.py index 0d3ab20..7bf8655 100755 --- a/tests/site_generators_tests.py +++ b/tests/site_generators_tests.py @@ -578,9 +578,10 @@ self.assertRaises(AssertionError): mysite.blocks(total=5, starttime=high, endtime=low, reverse=True)
- def test_exturl_usage(self) -> None: + def test_exturlusage(self) -> None: """Test the site.exturlusage() method.""" mysite = self.get_site() + url = 'www.google.com' eu = list(mysite.exturlusage(url, total=10)) self.assertLessEqual(len(eu), 10) @@ -591,6 +592,16 @@ self.assertIsInstance(link, pywikibot.Page) self.assertIn(link.namespace(), (2, 3))
+ with self.assertRaises(ValueError): + mysite.exturlusage('https://www.google.com', protocol='http') + with self.assertRaises(ValueError): + mysite.exturlusage('http://www.google.com', protocol='https') + with self.assertRaises(ValueError): + mysite.exturlusage( + 'https://www.google.com', + protocol=['http', 'https'], + ) + def test_protectedpages_create(self) -> None: """Test that protectedpages returns protected page titles.""" pages = list(self.get_site().protectedpages(protect_type='create',
pywikibot-commits@lists.wikimedia.org