Happy5214 has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/213816

Change subject: Allow relative links to be handled in Link constructor
......................................................................

Allow relative links to be handled in Link constructor

This patch allows a BasePage object to be passed in lieu of a Site
to the Link constructor. In this case, the link text will be handled
relative to the passed Page. This will allow for subpage links to be
parsed when relative links are used on the page. A new test, test_relative,
has been added to tests/link_tests.py to test this change.

Bug: T57113
Change-Id: Idd6366e82216c130b5143c9356245ff840af41ed
---
M pywikibot/page.py
M tests/link_tests.py
2 files changed, 21 insertions(+), 5 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/16/213816/1

diff --git a/pywikibot/page.py b/pywikibot/page.py
index 3f9c93a..240b99f 100644
--- a/pywikibot/page.py
+++ b/pywikibot/page.py
@@ -4462,17 +4462,24 @@
         @type text: unicode
         @param source: the Site on which the link was found (not necessarily
             the site to which the link refers)
-        @type source: Site
+        @type source: Site or BasePage
         @param defaultNamespace: a namespace to use if the link does not
             contain one (defaults to 0)
         @type defaultNamespace: int
 
         """
-        assert source is None or isinstance(source, pywikibot.site.BaseSite), \
-            "source parameter should be a Site object"
+
+        source_is_page = isinstance(source, BasePage)
+
+        assert source is None or source_is_page or isinstance(source, 
pywikibot.site.BaseSite), \
+            "source parameter should be either a Site or Page object"
+
+        if source_is_page:
+            self._source = source.site
+        else:
+            self._source = source or pywikibot.Site()
 
         self._text = text
-        self._source = source or pywikibot.Site()
         self._defaultns = defaultNamespace
 
         # preprocess text (these changes aren't site-dependent)
@@ -4515,6 +4522,9 @@
         t = t.replace(u"\u200e", u"").replace(u"\u200f", u"")
         self._text = t
 
+        if source_is_page:
+            self._text = source.title() + self._text
+
     def __repr__(self):
         """Return a more complete string representation."""
         return "pywikibot.page.Link(%r, %r)" % (self.title, self.site)
diff --git a/tests/link_tests.py b/tests/link_tests.py
index 83020ff..f094ff1 100644
--- a/tests/link_tests.py
+++ b/tests/link_tests.py
@@ -11,7 +11,7 @@
 
 import pywikibot
 from pywikibot import config2 as config
-from pywikibot.page import Link
+from pywikibot.page import Link, Page
 from pywikibot.exceptions import Error, InvalidTitle
 from tests.aspects import (
     unittest,
@@ -108,6 +108,12 @@
         self.assertRaises(InvalidTitle, Link('Category: ', 
self.get_site()).parse)
         self.assertRaises(InvalidTitle, Link('Category: #bar', 
self.get_site()).parse)
 
+    def test_relative(self):
+        """Test that relative links are handled properly."""
+        p = Page(self.get_site(), 'A')
+        l = Link('/B', p)
+        self.assertEquals(l.title, 'A/B')
+        self.assertEquals(l.site, self.get_site())
 
 # ---- The first set of tests are explicit links, starting with a ':'.
 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Idd6366e82216c130b5143c9356245ff840af41ed
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Happy5214 <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to