jenkins-bot has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1354016?usp=email )

Change subject: archivebot: Accept equivalent config template spellings
......................................................................

archivebot: Accept equivalent config template spellings

Match namespace prefixes case-insensitively and accept interchangeable
spaces and underscores in template titles. Allow whitespace after the
opening braces and spacing around the namespace colon.

This keeps the header check and config rewrite consistent with names
accepted by load_config, while preserving the title's case rules.

Change-Id: Id755c1a9f96edc6174a5937d74fb3d72c529cef5
---
M scripts/CHANGELOG.rst
M scripts/archivebot.py
M tests/archivebot_tests.py
3 files changed, 70 insertions(+), 5 deletions(-)

Approvals:
  Xqt: Looks good to me, approved
  jenkins-bot: Verified




diff --git a/scripts/CHANGELOG.rst b/scripts/CHANGELOG.rst
index 502e719..65008d8 100644
--- a/scripts/CHANGELOG.rst
+++ b/scripts/CHANGELOG.rst
@@ -4,6 +4,12 @@
 11.9.0
 ------

+archivebot
+^^^^^^^^^^
+
+* Accept equivalent namespace case, spaces, and underscores when locating
+  the configuration template for rewriting.
+
 category
 ^^^^^^^^

diff --git a/scripts/archivebot.py b/scripts/archivebot.py
index c584d9c..3d9d9cf 100755
--- a/scripts/archivebot.py
+++ b/scripts/archivebot.py
@@ -306,7 +306,8 @@
     """Return a regex that matches to variations of the template title.

     It supports the transcluding variant as well as localized namespaces
-    and case-insensitivity depending on the namespace.
+    and case-insensitivity depending on the namespace. Spaces and
+    underscores are interchangeable.

     :param tpl_page: The template page
     :type tpl_page: pywikibot.page.Page
@@ -314,9 +315,12 @@
     ns = tpl_page.site.namespaces[tpl_page.namespace()]
     marker = '?' if ns.id == 10 else ''
     title = tpl_page.title(with_ns=False)
-    title = case_escape(ns.case, title)
+    title = case_escape(ns.case, title, underscore=True)
+    namespaces = '|'.join(case_escape('case-sensitive', name, underscore=True)
+                          for name in ns)

-    return re.compile(r'(?:(?:{}):){}{}'.format('|'.join(ns), marker, title))
+    return re.compile(r'(?:(?i:{})[ _]*:[ _]*){}{}'.format(
+        namespaces, marker, title))
 

 def calc_md5_hexdigest(txt, salt) -> str:
@@ -929,7 +933,7 @@

         if whys:
             # Search for the marker template
-            rx = re.compile(r'\{\{%s\s*?\n.*?\n\}\}'
+            rx = re.compile(r'\{\{\s*%s\s*?\n.*?\n\}\}'
                             % (template_title_regex(self.tpl).pattern),
                             re.DOTALL)
             if not rx.search(self.page.header):
diff --git a/tests/archivebot_tests.py b/tests/archivebot_tests.py
index 39cd394..79dac90 100755
--- a/tests/archivebot_tests.py
+++ b/tests/archivebot_tests.py
@@ -15,7 +15,7 @@

 import pywikibot
 from pywikibot.exceptions import Error
-from pywikibot.textlib import TimeStripper
+from pywikibot.textlib import TimeStripper, extract_templates_and_params
 from scripts import archivebot
 from tests.aspects import TestCase

@@ -163,6 +163,61 @@
             archivebot.str2size('1234 567')


+class TestArchiveBotConfigSpelling(TestCase):
+
+    """Test equivalent spellings when reading and rewriting config."""
+
+    family = 'wikipedia'
+    code = 'en'
+    dry = True
+
+    def test_config_spelling(self) -> None:
+        """Test that accepted config names can also be rewritten."""
+        for title, spelling in (
+            ('User:MiszaBot/config', 'user:MiszaBot/config'),
+            ('Template:Auto archiving', 'Auto_archiving'),
+            ('Template:Auto archiving', 'template:Auto archiving'),
+            ('User:MiszaBot/config', ' \nUser : MiszaBot/config'),
+        ):
+            with self.subTest(spelling=spelling):
+                header = ('{{' + spelling + '\n|archive = Talk:Test/Archive'
+                          '\n|algo = old(24h)\n}}')
+                archiver = object.__new__(archivebot.PageArchiver)
+                archiver.site = self.site
+                archiver.tpl = pywikibot.Page(self.site, title)
+                archiver.page = Mock(
+                    header=header,
+                    raw_extracted_templates=extract_templates_and_params(
+                        header, strip=True),
+                )
+                archiver.attributes = {}
+                archiver.load_config()
+                archiver.set_attr('counter', '2')
+                archiver.archived_threads = 2
+                archiver.archives = {}
+                archiver.comment_params = {}
+                archiver.asynchronous = False
+                archiver.info = Mock()
+                archiver.analyze_page = Mock(
+                    return_value={('duration', 'one day')})
+                with patch.object(self.site, 'mediawiki_message',
+                                  return_value=', '), \
+                        patch.object(archivebot.i18n, 'twtranslate',
+                                     return_value='Archived'):
+                    archiver.run()
+                self.assertEqual(archiver.page.header, archiver.attr2text())
+                archiver.page.update.assert_called_once_with(
+                    'Archived', asynchronous=False)
+
+    def test_title_case(self) -> None:
+        """Test that namespace case does not relax title case matching."""
+        template = pywikibot.Page(self.site, 'User:MiszaBot/config')
+        pattern = archivebot.template_title_regex(template)
+        self.assertIsNotNone(pattern.fullmatch('user:miszaBot/config'))
+        self.assertIsNone(pattern.fullmatch('User:Miszabot/config'))
+        self.assertIsNone(pattern.fullmatch('MiszaBot/config'))
+
+
 class TestArchiveBot(TestCase):

     """Test archivebot script on 40+ Wikipedia sites."""

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1354016?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.wikimedia.org/r/settings?usp=email

Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Id755c1a9f96edc6174a5937d74fb3d72c529cef5
Gerrit-Change-Number: 1354016
Gerrit-PatchSet: 2
Gerrit-Owner: Mahveotm <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to