jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1155169?usp=email )
Change subject: IMPR: Use wikidata items broken redirect templates ......................................................................
IMPR: Use wikidata items broken redirect templates
- Use wikidata items speedy deletion templates if sdtemplate option is not given - remove get_sd_template method and move statements into sdtemplate property - update redirect_bot_tests
Bug: T396447 Change-Id: I0f8d3ca156ff03585e0aa627048dc537075bc4da --- M scripts/redirect.py M tests/redirect_bot_tests.py 2 files changed, 36 insertions(+), 43 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/scripts/redirect.py b/scripts/redirect.py index aeee362..12c6a6e 100755 --- a/scripts/redirect.py +++ b/scripts/redirect.py @@ -96,6 +96,7 @@ from pywikibot.textlib import extract_templates_and_params_regex_simple
+BROKEN_REDIRECT_TEMPLATE = 'Q11838699', 'Q4847311', 'Q21528265' docuReplacements = {'¶ms;': pagegenerators.parameterHelp} # noqa: N816
@@ -403,40 +404,29 @@ else: raise NotImplementedError(f'No valid action "{action}" found.')
- def get_sd_template( - self, site: pywikibot.site.BaseSite | None = None - ) -> str | None: - """Look for speedy deletion template and return it. - - :param site: site for which the template has to be given - :return: A valid speedy deletion template. - """ - title = None - if site: - sd = self.opt.sdtemplate - if not sd and i18n.twhas_key(site, - 'redirect-broken-redirect-template'): - sd = i18n.twtranslate(site, - 'redirect-broken-redirect-template') - + @property + def sdtemplate(self) -> str: + """Gives the speedy deletion template for the current_page.""" + title = '' + site = self.current_page.site + if sd := self.opt.sdtemplate: # check whether template exists for this site - if sd: - template = extract_templates_and_params_regex_simple(sd) - if template: - title = template[0][0] - page = pywikibot.Page(site, title, ns=10) - if page.exists(): - return sd + template = extract_templates_and_params_regex_simple(sd) + if template: + title = template[0][0] + page = pywikibot.Page(site, title, ns=10) + if page.exists(): + return sd + else: + for item in BROKEN_REDIRECT_TEMPLATE: + tpl = site.page_from_repository(item) + if tpl: + return f'{{{{{tpl.title(with_ns=False)}}}}}'
pywikibot.warning( 'No speedy deletion template {}available.' .format(f'"{title}" ' if title else '')) - return None - - @property - def sdtemplate(self): - """Gives the speedy deletion template for the current_page.""" - return self.get_sd_template(self.current_page.site) + return ''
def init_page(self, item) -> pywikibot.Page: """Ensure that we process page objects.""" diff --git a/tests/redirect_bot_tests.py b/tests/redirect_bot_tests.py index 44110fb..03767ad 100755 --- a/tests/redirect_bot_tests.py +++ b/tests/redirect_bot_tests.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 """Tests for the redirect.py script.""" # -# (C) Pywikibot team, 2017-2024 +# (C) Pywikibot team, 2017-2025 # # Distributed under the terms of the MIT license. # @@ -12,7 +12,7 @@ from unittest.mock import Mock, patch
import pywikibot -from pywikibot import Page, i18n +from pywikibot import Page from scripts.redirect import RedirectRobot from tests.aspects import DefaultSiteTestCase
@@ -43,20 +43,23 @@ bot = RedirectTestRobot('broken', **options) self.assertEqual(bot.sdtemplate, '{{t}}')
- @patch.object(i18n, 'twhas_key', new=Mock(return_value=True)) - @patch.object(i18n, 'twtranslate', new=Mock(return_value='{{sd_title}}')) def test_with_delete_and_i18n_sd(self) -> None: - """Test with delete and i18n template.""" - bot = RedirectTestRobot('broken', delete=True) - self.assertEqual(bot.sdtemplate, '{{sd_title}}') + """Test with delete and wikibase template.""" + with patch.object( + pywikibot.site.APISite, 'page_from_repository', + new=Mock(return_value=pywikibot.Page(self.site, 'sd_title')) + ): + bot = RedirectTestRobot('broken', delete=True) + self.assertEqual(bot.sdtemplate, '{{Sd title}}')
- @patch.object(i18n, 'twhas_key', new=Mock(return_value=False)) def test_with_delete_no_sd_no_i18n(self) -> None: - """Test with delete and no i18n template.""" - bot = RedirectTestRobot('broken', delete=True) - with patch.object(pywikibot, 'warning') as w: - self.assertIsNone(bot.sdtemplate) - w.assert_called_with('No speedy deletion template available.') + """Test with delete and no wikibase template.""" + with patch.object(pywikibot.site.APISite, 'page_from_repository', + new=Mock(return_value=None)): + bot = RedirectTestRobot('broken', delete=True) + with patch.object(pywikibot, 'warning') as w: + self.assertEqual(bot.sdtemplate, '') + w.assert_called_with('No speedy deletion template available.')
def test_with_delete_and_non_existing_sdtemplate(self) -> None: """Test with delete and non-existing sdtemplate.""" @@ -64,7 +67,7 @@ bot = RedirectTestRobot('broken', **options) with patch.object(Page, 'exists', new=Mock(return_value=False)), \ patch.object(pywikibot, 'warning') as w: - self.assertIsNone(bot.sdtemplate, None) + self.assertEqual(bot.sdtemplate, '') w.assert_called_with('No speedy deletion template "n" available.')
pywikibot-commits@lists.wikimedia.org