Skip to content

Commit

Permalink
Merge "[bugfix] Use grnfilterredir for random pages"
Browse files Browse the repository at this point in the history
  • Loading branch information
jenkins-bot authored and Gerrit Code Review committed Oct 29, 2017
2 parents 6a0ec17 + 635462f commit e639ff1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
29 changes: 23 additions & 6 deletions pywikibot/site.py
Expand Up @@ -4924,7 +4924,7 @@ def users(self, usernames):
'users', ususers=usernames, site=self, usprop=usprop)
return usgen

@deprecated("Site.randompages()")
@deprecated('Site.randompages(total=1)')
def randompage(self, redirect=False):
"""
DEPRECATED.
Expand All @@ -4934,7 +4934,7 @@ def randompage(self, redirect=False):
"""
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.
Expand All @@ -4956,17 +4956,34 @@ def randompages(self, total=None, namespaces=None,
@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.
Expand Down
6 changes: 6 additions & 0 deletions tests/site_tests.py
Expand Up @@ -2006,6 +2006,12 @@ def test_redirects(self):
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()
Expand Down

0 comments on commit e639ff1

Please sign in to comment.