John Vandenberg has uploaded a new change for review.

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

Change subject: @disallow_redirect
......................................................................

@disallow_redirect

Change-Id: Ibc01db5858f5c0ab6cb338a1614843a9b3744315
---
M pywikibot/page.py
M scripts/replace.py
M scripts/solve_disambiguation.py
M tests/page_tests.py
4 files changed, 82 insertions(+), 12 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/08/175408/1

diff --git a/pywikibot/page.py b/pywikibot/page.py
index 2d71d8a..3a56d38 100644
--- a/pywikibot/page.py
+++ b/pywikibot/page.py
@@ -49,7 +49,8 @@
     SiteDefinitionError
 )
 from pywikibot.tools import (
-    ComparableMixin, deprecated, deprecate_arg, deprecated_args
+    ComparableMixin, deprecated, deprecate_arg, deprecated_args,
+    add_decorated_full_name,
 )
 from pywikibot import textlib
 
@@ -58,6 +59,34 @@
 
 # Pre-compile re expressions
 reNamespace = re.compile("^(.+?) *: *(.*)$")
+
+
+def disallow_redirect(fn):
+    """ Decorator to raise IsRedirectPage exception by default.
+
+    compat often include an argument get_redirect, always default False.
+    It is confusingly named, as 'get' here means get the raw wikitext which
+    includes the #REDIRECT, and not that the redirect should be followed.
+
+    This decorator looks for argument 'allow_redirect' and 'get_redirect',
+    which if set will skip raising IsRedirectPage when used with a redirect.
+
+    @return: decorated method
+    """
+    def callee(self, *args, **kwargs):
+        allow_redirect = kwargs.pop('allow_redirect',
+                                    kwargs.pop('get_redirect', False))
+        if not allow_redirect and self.isRedirectPage():
+            raise pywikibot.IsRedirectPage(self)
+        return fn(self, *args, **kwargs)
+
+    callee.__name__ = fn.__name__
+    callee.__doc__ = fn.__doc__
+    callee.__module__ = callee.__module__
+    if not hasattr(fn, '__full_name__'):
+        add_decorated_full_name(fn)
+    callee.__full_name__ = fn.__full_name__
+    return callee
 
 
 # Note: Link objects (defined later on) represent a wiki-page's title, while
@@ -303,6 +332,7 @@
         return self.autoFormat()[0] is not None
 
     @deprecated_args(throttle=None, change_edit_time=None)
+    # FIXME: rename get_redirect to allow_redirect after using warnings module.
     def get(self, force=False, get_redirect=False, sysop=False):
         """Return the wiki-text of the page.
 
@@ -367,13 +397,17 @@
             self._getexception = pywikibot.IsRedirectPage(self)
             raise self._getexception
 
-    @deprecated_args(throttle=None, change_edit_time=None)
-    def getOldVersion(self, oldid, force=False, get_redirect=False,
-                      sysop=False):
-        """Return text of an old revision of this page; same options as get().
+    @deprecated_args(throttle=None, change_edit_time=None, get_redirect=None)
+    def getOldVersion(self, oldid, force=False, sysop=False):
+        """Return text of an old revision of this page.
 
         @param oldid: The revid of the revision desired.
+        @param force: reload all page attributes, including errors
+        @param sysop: if the user has a sysop account, use it to
+                      retrieve this page
 
+        @return: old revision page text
+        @rtype: unicode
         """
         if force or oldid not in self._revisions \
                 or self._revisions[oldid].text is None:
@@ -381,7 +415,6 @@
                                     getText=True,
                                     revids=oldid,
                                     sysop=sysop)
-        # TODO: what about redirects, errors?
         return self._revisions[oldid].text
 
     def permalink(self, oldid=None):
@@ -1227,6 +1260,7 @@
         """DEPRECATED. Use templates()."""
         return self.templates()
 
+    @disallow_redirect
     def templates(self, content=False):
         """Return a list of Page objects for templates used on this Page.
 
@@ -1277,7 +1311,8 @@
         return self.site.pageimages(self, step=step, total=total,
                                     content=content)
 
-    @deprecate_arg("get_redirect", None)
+    @disallow_redirect
+    @deprecated_args(thistxt=None)
     def templatesWithParams(self):
         """Iterate templates used on this Page.
 
@@ -1328,7 +1363,10 @@
             result.append((pywikibot.Page(link, self.site), positional))
         return result
 
-    @deprecated_args(nofollow_redirects=None, get_redirect=None)
+    # nofollow_redirects was removed (not deprecated) from compat in mid-2008
+    # (7cb7143).  However it was still used in core until late 2014.
+    @deprecated_args(nofollow_redirects='allow_redirect')
+    @disallow_redirect
     def categories(self, withSortKey=False, step=None, total=None,
                    content=False):
         """Iterate categories that the article is in.
diff --git a/scripts/replace.py b/scripts/replace.py
index cd9fcfb..8f276b4 100755
--- a/scripts/replace.py
+++ b/scripts/replace.py
@@ -391,7 +391,7 @@
                         new_text = newest_text
                         newest_text = self.doReplacements(new_text)
                 if hasattr(self, "addedCat"):
-                    cats = page.categories(nofollow_redirects=True)
+                    cats = page.categories(allow_redirect=True)
                     if self.addedCat not in cats:
                         cats.append(self.addedCat)
                         new_text = textlib.replaceCategoryLinks(new_text,
diff --git a/scripts/solve_disambiguation.py b/scripts/solve_disambiguation.py
index 9f26db8..c4dd84f 100644
--- a/scripts/solve_disambiguation.py
+++ b/scripts/solve_disambiguation.py
@@ -851,10 +851,10 @@
         if disambPage.isRedirectPage() and not self.primary:
             if (disambPage.site.lang in self.primary_redir_template
                 and self.primary_redir_template[disambPage.site.lang]
-                    in disambPage.templates(get_redirect=True)):
+                    in disambPage.templates(allow_redirect=True)):
                 baseTerm = disambPage.title()
                 for template in disambPage.templatesWithParams(
-                        get_redirect=True):
+                        allow_redirect=True):
                     if template[0] == self.primary_redir_template[
                         disambPage.site.lang] \
                             and len(template[1]) > 0:
diff --git a/tests/page_tests.py b/tests/page_tests.py
index 1bb1fde..6a8bcdf 100644
--- a/tests/page_tests.py
+++ b/tests/page_tests.py
@@ -13,7 +13,12 @@
 from pywikibot import InvalidTitle
 import pywikibot.page
 
-from tests.aspects import unittest, TestCase, DefaultSiteTestCase
+from tests.aspects import (
+    unittest,
+    TestCase,
+    DefaultSiteTestCase,
+    DeprecationTestCase,
+)
 
 if sys.version_info[0] > 2:
     basestring = (str, )
@@ -601,6 +606,33 @@
         self.assertIsInstance(mainpage.purge(), bool)
 
 
+class TestDeprecation(DeprecationTestCase):
+
+    """Test page method deprecation."""
+
+    family = 'wikipedia'
+    code = 'en'
+
+    cached = True
+
+    # FIXME: this should be a DefaultSiteTestCase, however mainpage.backlinks
+    # returns incorrect results, on enwp at least.
+
+    def test_categories_nofollow_redirects(self):
+        # 'Main page' is a redirect to 'Main Page', and it is in category
+        # 'Category:Main Page' among others.
+        redirect = pywikibot.Page(self.get_site(), 'Main page')
+
+        self.assertRaises(pywikibot.IsRedirectPage, redirect.categories,
+                                                    allow_redirect=False)
+
+        self.assertNoDeprecation()
+
+        cats = list(redirect.categories(nofollow_redirects=True))
+        self.assertTrue(cats)
+        self.assertDeprecation('nofollow_redirects argument of 
pywikibot.page.BasePage.categories is deprecated; use allow_redirect instead.')
+
+
 if __name__ == '__main__':
     try:
         unittest.main()

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ibc01db5858f5c0ab6cb338a1614843a9b3744315
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: John Vandenberg <jay...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to