XZise has uploaded a new change for review. https://gerrit.wikimedia.org/r/243832
Change subject: [FIX] getLanguageLinks: Skip own site ...................................................................... [FIX] getLanguageLinks: Skip own site When the text contains an langlink to its own site, it should be skipped as it is not an interwiki link. Conflicts: tests/textlib_tests.py Bug: T114608 Change-Id: I0856e198e817eac160b7f2682bffc9e65387ac6e (cherry picked from commit c24650e8f55c74b78270cf66615433c702eac647) --- M pywikibot/textlib.py M tests/textlib_tests.py 2 files changed, 43 insertions(+), 2 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core refs/changes/32/243832/1 diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py index b40aba4..09281a4 100644 --- a/pywikibot/textlib.py +++ b/pywikibot/textlib.py @@ -482,7 +482,8 @@ """ Return a dict of inter-language links found in text. - The returned dict uses language codes as keys and Page objects as values. + The returned dict uses the site as keys and Page objects as values. It does + not contain its own site. Do not call this routine directly, use Page.interwiki() method instead. @@ -524,6 +525,9 @@ pagetitle = pagetitle[:pagetitle.index('|')] # we want the actual page objects rather than the titles site = pywikibot.Site(code=lang, fam=fam) + # skip language links to its own site + if site == insite: + continue try: result[site] = pywikibot.Page(site, pagetitle, insite=insite) except pywikibot.InvalidTitle: diff --git a/tests/textlib_tests.py b/tests/textlib_tests.py index 468d3f2..ba1c3ff 100644 --- a/tests/textlib_tests.py +++ b/tests/textlib_tests.py @@ -19,7 +19,9 @@ from pywikibot import config from pywikibot.tools import OrderedDict -from tests.aspects import unittest, TestCase, DefaultDrySiteTestCase +from tests.aspects import ( + unittest, TestCase, DefaultDrySiteTestCase, SiteAttributeTestCase, +) files = {} dirname = os.path.join(os.path.dirname(__file__), "pages") @@ -541,6 +543,41 @@ r'X\g<bar>X') +class TestGetLanguageLinks(SiteAttributeTestCase): + + """Test L{textlib.getLanguageLinks} function.""" + + sites = { + 'enwp': { + 'family': 'wikipedia', + 'code': 'en', + }, + 'dewp': { + 'family': 'wikipedia', + 'code': 'de', + }, + 'commons': { + 'family': 'commons', + 'code': 'commons', + }, + } + + example_text = '[[en:Site]] [[de:Site|Piped]] [[commons:Site]] [[baden:Site]]' + + @classmethod + def setUpClass(cls): + """Define set of valid targets for the example text.""" + super(TestGetLanguageLinks, cls).setUpClass() + cls.sites_set = set([cls.enwp, cls.dewp]) + + def test_getLanguageLinks(self, key): + """Test if the function returns the correct titles and sites.""" + lang_links = textlib.getLanguageLinks(self.example_text, self.site) + self.assertEqual(set(page.title() for page in lang_links.values()), + set(['Site'])) + self.assertEqual(set(lang_links), self.sites_set - set([self.site])) + + if __name__ == '__main__': try: unittest.main() -- To view, visit https://gerrit.wikimedia.org/r/243832 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I0856e198e817eac160b7f2682bffc9e65387ac6e Gerrit-PatchSet: 1 Gerrit-Project: pywikibot/core Gerrit-Branch: 2.0 Gerrit-Owner: XZise <commodorefabia...@gmx.de> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits