jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/385773 )
Change subject: [bugfix] Use grnfilterredir for random pages ......................................................................
[bugfix] Use grnfilterredir for random pages
Bug: T178732 Change-Id: I0763cf99fd113cd25a0048d8a760b16fc4828346 --- M pywikibot/site.py M tests/site_tests.py 2 files changed, 28 insertions(+), 5 deletions(-)
Approvals: Dalba: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/site.py b/pywikibot/site.py index ac20cd0..7b09c7f 100644 --- a/pywikibot/site.py +++ b/pywikibot/site.py @@ -4924,7 +4924,7 @@ 'users', ususers=usernames, site=self, usprop=usprop) return usgen
- @deprecated("Site.randompages()") + @deprecated('Site.randompages(total=1)') def randompage(self, redirect=False): """ DEPRECATED. @@ -4934,7 +4934,7 @@ """ return self.randompages(total=1, redirects=redirect)
- @deprecated("Site.randompages()") + @deprecated("Site.randompages(total=1, redirects=True)") def randomredirectpage(self): """ DEPRECATED: Use Site.randompages() instead. @@ -4956,17 +4956,34 @@ @type namespaces: iterable of basestring or Namespace key, or a single instance of those types. May be a '|' separated list of namespace identifiers. - @param redirects: if True, include only redirect pages in results - (default: include only non-redirects) + @param redirects: if True, include only redirect pages in results, + False does not include redirects and None (MW 1.26+) include both + types. (default: False) + @type redirects: bool or None @param content: if True, load the current content of each iterated page (default False) @raises KeyError: a namespace identifier was not resolved @raises TypeError: a namespace identifier has an inappropriate type such as NoneType or bool + @raises AssertError: unsupported redirects parameter """ + mapping = {False: None, True: 'redirects', None: 'all'} + assert redirects in mapping + redirects = mapping[redirects] + params = {} + if redirects is not None: + if MediaWikiVersion(self.version()) < MediaWikiVersion('1.26'): + if redirects == 'all': + warn("parameter redirects=None to retrieve 'all' random" + 'page types is not supported by mw version {0}. ' + 'Using default.'.format(self.version()), + UserWarning) + params['grnredirect'] = redirects == 'redirects' + else: + params['grnfilterredir'] = redirects rngen = self._generator(api.PageGenerator, type_arg="random", namespaces=namespaces, total=total, - g_content=content, grnredirect=redirects) + g_content=content, **params) return rngen
# Catalog of editpage error codes, for use in generating messages. diff --git a/tests/site_tests.py b/tests/site_tests.py index 1b45692..3ef9079 100644 --- a/tests/site_tests.py +++ b/tests/site_tests.py @@ -2006,6 +2006,12 @@ self.assertIsInstance(rndpage, pywikibot.Page) self.assertTrue(rndpage.isRedirectPage())
+ def test_all(self): + """Test site.randompages() with both types.""" + mysite = self.get_site() + for rndpage in mysite.randompages(total=5, redirects=None): + self.assertIsInstance(rndpage, pywikibot.Page) + def test_namespaces(self): """Test site.randompages() with namespaces.""" mysite = self.get_site()
pywikibot-commits@lists.wikimedia.org