jenkins-bot has submitted this change and it was merged.
Change subject: preserve sortKey exactly when (de)serializing category links ......................................................................
preserve sortKey exactly when (de)serializing category links
an empty sortKey and a null one are not considered the same anymore - in Category.aslink() by checking `key` against None - in textlib.getCategoryLinks() by making the regex catch empty sortKeys
TestCategoryRearrangement has been introduced to ensure that the bot doesn't do any undesired changes to the text
Change-Id: Ief9d5f95b37f949d667a0ffd43d79b69d12bd8d7 --- M pywikibot/page.py M pywikibot/textlib.py M tests/textlib_tests.py 3 files changed, 22 insertions(+), 2 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/page.py b/pywikibot/page.py index 3396ed1..c6473bc 100644 --- a/pywikibot/page.py +++ b/pywikibot/page.py @@ -1913,7 +1913,7 @@
""" key = sortKey or self.sortKey - if key: + if key is not None: titleWithSortKey = '%s|%s' % (self.title(withSection=False), key) else: diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py index aff70fa..b673616 100644 --- a/pywikibot/textlib.py +++ b/pywikibot/textlib.py @@ -663,7 +663,7 @@ text = removeDisabledParts(text) catNamespace = '|'.join(site.category_namespaces()) R = re.compile(r'[[\s*(?P<namespace>%s)\s*:\s*(?P<catName>.+?)' - r'(?:|(?P<sortKey>.+?))?\s*]]' + r'(?:|(?P<sortKey>.*))?\s*]]' % catNamespace, re.I) for match in R.finditer(text): cat = pywikibot.Category(pywikibot.Link( diff --git a/tests/textlib_tests.py b/tests/textlib_tests.py index 837dba8..32771f4 100644 --- a/tests/textlib_tests.py +++ b/tests/textlib_tests.py @@ -130,6 +130,26 @@ textlib.categoryFormat(data, self.site))
+class TestCategoryRearrangement(PywikibotTestCase): + + """ + Tests to ensure that sorting keys are not being lost when + using .getCategoryLinks() and .replaceCategoryLinks(). + """ + + @classmethod + def setUpClass(cls): + cls.site = pywikibot.Site('en', 'wikipedia') + cls.old = ('[[Category:Cat1]]%(LS)s[[Category:Cat2|]]%(LS)s' + '[[Category:Cat1| ]]%(LS)s[[Category:Cat2|key]]' + % {'LS': config.LS}) + + def test_replace_category_links(self): + cats = textlib.getCategoryLinks(self.old, site=self.site) + new = textlib.replaceCategoryLinks(self.old, cats, site=self.site) + self.assertEqual(old, new) + + if __name__ == '__main__': try: unittest.main()
pywikibot-commits@lists.wikimedia.org