jenkins-bot has submitted this change and it was merged.
Change subject: Do not expand text by default in getCategoryLinks ......................................................................
Do not expand text by default in getCategoryLinks
666364ca introduced wikitext expansion while getting category links, however this caused magic names like {{PAGENAME}} to be expanded when it should be kept in the wikitext.
Make this new functionality disabled by default.
Bug: T109213 Change-Id: I321ba1715fe7b1a9c24d7f8109db236339e5f4b5 --- M pywikibot/textlib.py M tests/textlib_tests.py 2 files changed, 9 insertions(+), 8 deletions(-)
Approvals: Mpaa: Looks good to me, but someone else must approve Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py index 05e0b8b..0fe92e0 100644 --- a/pywikibot/textlib.py +++ b/pywikibot/textlib.py @@ -993,7 +993,7 @@ # Functions dealing with category links # -------------------------------------
-def getCategoryLinks(text, site=None, include=[]): +def getCategoryLinks(text, site=None, include=[], expand_text=False): """Return a list of category links found in text.
@param include: list of tags which should not be removed by @@ -1013,7 +1013,7 @@ R = re.compile(r'[[\s*(?P<namespace>%s)\s*:\s*(?P<rest>.+?)]]' % catNamespace, re.I) for match in R.finditer(text): - if '{{' in match.group('rest'): + if expand_text and '{{' in match.group('rest'): rest = site.expand_text(match.group('rest')) else: rest = match.group('rest') diff --git a/tests/textlib_tests.py b/tests/textlib_tests.py index 762f62d..6b674a8 100644 --- a/tests/textlib_tests.py +++ b/tests/textlib_tests.py @@ -265,24 +265,25 @@ """Test normal templates inside category links.""" self.site = self.get_site() self.assertEqual(textlib.getCategoryLinks( - '[[Category:{{P1|Foo}}]]', self.site), + '[[Category:{{P1|Foo}}]]', self.site, expand_text=True), [pywikibot.page.Category(self.site, 'Foo')]) self.assertEqual(textlib.getCategoryLinks( - '[[Category:{{P1|Foo}}|bar]]', self.site), + '[[Category:{{P1|Foo}}|bar]]', self.site, expand_text=True), [pywikibot.page.Category(self.site, 'Foo', sortKey='bar')]) self.assertEqual(textlib.getCategoryLinks( - '[[Category:{{P1|{{P2|L33t|Foo}}}}|bar]]', self.site), + '[[Category:{{P1|{{P2|L33t|Foo}}}}|bar]]', + self.site, expand_text=True), [pywikibot.page.Category(self.site, 'Foo', sortKey='bar')]) self.assertEqual(textlib.getCategoryLinks( - '[[Category:Foo{{!}}bar]]', self.site), + '[[Category:Foo{{!}}bar]]', self.site, expand_text=True), [pywikibot.page.Category(self.site, 'Foo', sortKey='bar')]) self.assertEqual(textlib.getCategoryLinks( '[[Category:Foo{{!}}bar]][[Category:Wiki{{P2||pedia}}]]', - self.site), + self.site, expand_text=True), [pywikibot.page.Category(self.site, 'Foo', sortKey='bar'), pywikibot.page.Category(self.site, 'Wikipedia')]) self.assertEqual(textlib.getCategoryLinks( - '[[Category:Foo{{!}}and{{!}}bar]]', self.site), + '[[Category:Foo{{!}}and{{!}}bar]]', self.site, expand_text=True), [pywikibot.page.Category(self.site, 'Foo', sortKey='and|bar')]) self.assertRaises(pywikibot.InvalidTitle, textlib.getCategoryLinks, '[[Category:nasty{{{!}}]]', self.site)