jenkins-bot has submitted this change and it was merged.

Change subject: [FEAT] Advanced link tests
......................................................................


[FEAT] Advanced link tests

With the link tests added in 2a230fc4031e5f116aa954177be22dc1603dd410
this removes all potentional failures added:
- Just the ":"
- If the title contains a section and a namespace prefix but no title
  (this is already working when there is no section)
- If the (non-)talk page of the title is actually a title in another
  namespace.
  Example: Talk:File:Test.png would've the non-talk page File:Test.png
           but it's talk page would be File talk:Test.png

Bug: 72681
Change-Id: I3923de0d2930e44ee656e862405b8d654b896fdf
---
M pywikibot/page.py
M tests/link_tests.py
2 files changed, 26 insertions(+), 11 deletions(-)

Approvals:
  John Vandenberg: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/pywikibot/page.py b/pywikibot/page.py
index eccc9ef..5228395 100644
--- a/pywikibot/page.py
+++ b/pywikibot/page.py
@@ -4055,7 +4055,9 @@
         """
         self._site = self._source
         self._namespace = self._defaultns
+        self._is_interwiki = False
         t = self._text
+        ns_prefix = False
 
         # This code was adapted from Title.php : secureAndSplit()
         #
@@ -4074,11 +4076,8 @@
             if ns:
                 # Ordinary namespace
                 t = t[t.index(u":"):].lstrip(u":").lstrip(u" ")
-                # 'namespace:' is not a valid title
-                if not t:
-                    raise pywikibot.InvalidTitle(
-                        u"'{0}' has no title.".format(self._text))
                 self._namespace = ns
+                ns_prefix = True
                 break
             try:
                 newsite = self._site.interwiki(prefix)
@@ -4100,12 +4099,29 @@
                 elif newsite != self._source:
                     first_other_site = newsite
                 self._site = newsite
+                self._is_interwiki = True
 
         if u"#" in t:
             t, sec = t.split(u'#', 1)
             t, self._section = t.rstrip(), sec.lstrip()
         else:
             self._section = None
+
+        if ns_prefix:
+            # 'namespace:' is not a valid title
+            if not t:
+                raise pywikibot.InvalidTitle(
+                    u"'{0}' has no title.".format(self._text))
+            elif ':' in t and self._namespace >= 0:  # < 0 don't have talk
+                other_ns = self._site.namespaces[self._namespace - 1
+                                                 if self._namespace % 2 else
+                                                 self._namespace + 1]
+                if '' in other_ns:  # other namespace uses empty str as ns
+                    next_ns = t[:t.index(':')]
+                    if self._site.ns_index(next_ns):
+                        raise pywikibot.InvalidTitle(
+                            u"The (non-)talk page of '{0}' is a valid title "
+                            "in another namespace.".format(self._text))
 
         # Reject illegal characters.
         m = Link.illegal_titles_pattern.search(t)
@@ -4139,7 +4155,7 @@
 
         # "empty" local links can only be self-links
         # with a fragment identifier.
-        if not self._text.strip():
+        if not t.strip() and not self._is_interwiki:
             raise pywikibot.InvalidTitle("The link does not contain a page "
                                          "title")
 
diff --git a/tests/link_tests.py b/tests/link_tests.py
index cb99fe1..a378c65 100644
--- a/tests/link_tests.py
+++ b/tests/link_tests.py
@@ -61,7 +61,7 @@
 
     def test_invalid(self):
         self.assertRaises(InvalidTitle, Link('', self.get_site()).parse)
-        # TODO: self.assertRaises(InvalidTitle, Link(':', 
self.get_site()).parse)
+        self.assertRaises(InvalidTitle, Link(':', self.get_site()).parse)
         self.assertRaises(InvalidTitle, Link('__  __', self.get_site()).parse)
         self.assertRaises(InvalidTitle, Link('  __  ', self.get_site()).parse)
         # Bad characters forbidden regardless of wgLegalTitleChars
@@ -74,10 +74,9 @@
         # URL encoding
         # %XX is understood by wikimedia but not %XXXX
         self.assertRaises(InvalidTitle, Link('A%2523B', self.get_site()).parse)
-        # Subject of NS_TALK does not roundtrip to NS_MAIN
-        # TODO: A link is invalid if their (non-)talk page would be in another
-        #       namespace than the link's "other" namespace
-        # TODO: self.assertRaises(InvalidTitle, Link('Talk:File:Example.svg', 
self.get_site()).parse)
+        # A link is invalid if their (non-)talk page would be in another
+        # namespace than the link's "other" namespace
+        self.assertRaises(InvalidTitle, Link('Talk:File:Example.svg', 
self.get_site()).parse)
         # Directory navigation
         self.assertRaises(InvalidTitle, Link('.', self.get_site()).parse)
         self.assertRaises(InvalidTitle, Link('..', self.get_site()).parse)
@@ -97,7 +96,7 @@
         # Namespace prefix without actual title
         self.assertRaises(InvalidTitle, Link('Talk:', self.get_site()).parse)
         self.assertRaises(InvalidTitle, Link('Category: ', 
self.get_site()).parse)
-        # TODO: self.assertRaises(InvalidTitle, Link('Category: #bar', 
self.get_site()).parse)
+        self.assertRaises(InvalidTitle, Link('Category: #bar', 
self.get_site()).parse)
 
 
 # ---- The first set of tests are explicit links, starting with a ':'.

-- 
To view, visit https://gerrit.wikimedia.org/r/170583
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I3923de0d2930e44ee656e862405b8d654b896fdf
Gerrit-PatchSet: 4
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: XZise <[email protected]>
Gerrit-Reviewer: John Vandenberg <[email protected]>
Gerrit-Reviewer: Ladsgroup <[email protected]>
Gerrit-Reviewer: Merlijn van Deen <[email protected]>
Gerrit-Reviewer: Nullzero <[email protected]>
Gerrit-Reviewer: Siebrand <[email protected]>
Gerrit-Reviewer: XZise <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
Pywikibot-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/pywikibot-commits

Reply via email to