Xqt submitted this change.

View Change

Approvals: Xqt: Verified; Looks good to me, approved
[IMPR] Catch invalid titles in Category.isCategoryRedirect()

And attempt to handle them by expanding the wikitext

Change-Id: I853cb2447a4538cb62a3eb6ea66f3fc66174ed39
---
M pywikibot/page/__init__.py
M tests/category_tests.py
2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/pywikibot/page/__init__.py b/pywikibot/page/__init__.py
index 311a694..12ca00e 100644
--- a/pywikibot/page/__init__.py
+++ b/pywikibot/page/__init__.py
@@ -758,8 +758,18 @@
if template.title(with_ns=False) in catredirs:
if args:
# Get target (first template argument)
+ target_title = args[0].strip()
p = pywikibot.Page(
- self.site, args[0].strip(), Namespace.CATEGORY)
+ self.site, target_title, Namespace.CATEGORY)
+ try:
+ p.title()
+ except pywikibot.exceptions.InvalidTitleError:
+ target_title = self.site.expand_text(
+ text=target_title,
+ title=self.title(),
+ )
+ p = pywikibot.Page(self.site, target_title,
+ Namespace.CATEGORY)
if p.namespace() == Namespace.CATEGORY:
self._catredirect = p.title()
else:
diff --git a/tests/category_tests.py b/tests/category_tests.py
index e94fabc..1dd3dfa 100644
--- a/tests/category_tests.py
+++ b/tests/category_tests.py
@@ -157,6 +157,15 @@
self.NOREDIRECTPAGE_RE):
cat2.getCategoryRedirectTarget()

+ # Invalid title case
+ cat3 = pywikibot.Category(site, '2021 establishments in Orissa')
+ cat4 = pywikibot.Category(site, '2021 establishments in Odisha')
+ self.assertTrue('{{title year}}' in cat3.text)
+ self.assertTrue(cat3.isCategoryRedirect())
+ self.assertFalse(cat4.isCategoryRedirect())
+ tgt = cat3.getCategoryRedirectTarget()
+ self.assertEqual(tgt, cat4)
+

class TestCategoryDryObject(TestCase):


To view, visit change 703048. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I853cb2447a4538cb62a3eb6ea66f3fc66174ed39
Gerrit-Change-Number: 703048
Gerrit-PatchSet: 2
Gerrit-Owner: JJMC89 <JJMC89.Wikimedia@gmail.com>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged