Xqt has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1354177?usp=email )

Change subject: textlib: Handle results without category links
......................................................................

textlib: Handle results without category links

Skip placing special templates below categories when replaceCategoryLinks
finds no category links in the result. This avoids IndexError when adding
text to an uncategorized page or removing its final category on Wikipedias
with special template placement rules.

Change-Id: I113d79324ec90b5a195fca022c83ce7e8b351e10
---
M pywikibot/textlib.py
M tests/textlib_tests.py
2 files changed, 21 insertions(+), 1 deletion(-)

Approvals:
  Xqt: Verified; Looks good to me, approved




diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py
index c5d6296..5107520 100644
--- a/pywikibot/textlib.py
+++ b/pywikibot/textlib.py
@@ -1962,7 +1962,10 @@

     if under_categories:
         category = get_regexes('category', site)[0]
-        last_category = deque(category.finditer(newtext), maxlen=1)[0]
+        last = deque(category.finditer(newtext), maxlen=1)
+        if not last:
+            return newtext.strip()
+        last_category = last[0]
         for reg in under_categories:
             special = reg.search(newtext)
             if special and not isDisabled(newtext, special.start()):
diff --git a/tests/textlib_tests.py b/tests/textlib_tests.py
index 5d13512..87b7200 100755
--- a/tests/textlib_tests.py
+++ b/tests/textlib_tests.py
@@ -287,6 +287,23 @@
         self.assertEqual(orig_sortkey, new_dummy.sortKey)


+class TestReplaceCategoryLinks(TestCase):
+
+    """Test replacing category links."""
+
+    family = 'wikipedia'
+    code = 'en'
+
+    dry = True
+
+    def test_remove_last_category(self) -> None:
+        """Test removing the last category leaves the stub template intact."""
+        self.assertEqual(
+            textlib.replaceCategoryLinks(
+                'foo\n[[Category:Foo]]\n{{foo-stub}}', [], site=self.site),
+            'foo\n{{foo-stub}}')
+
+
 class TestTemplatesInCategory(TestCase):

     """Tests to verify that templates in category links are handled."""

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1354177?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: I113d79324ec90b5a195fca022c83ce7e8b351e10
Gerrit-Change-Number: 1354177
Gerrit-PatchSet: 3
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