jenkins-bot has submitted this change and it was merged.
Change subject: pagegenerators.py: Allow -random and -randomredirect to work with namespaces ......................................................................
pagegenerators.py: Allow -random and -randomredirect to work with namespaces
Add 'namespaces' argument to the random page generators. Use -namespaces before -random/-randomredirect while giving args to the program.
Bug: T119940 Change-Id: I0df00882a6033d2497f5a5e8b48ea0b252c0a463 --- M pywikibot/pagegenerators.py 1 file changed, 27 insertions(+), 12 deletions(-)
Approvals: John Vandenberg: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/pagegenerators.py b/pywikibot/pagegenerators.py index 0d5afdf..46af859 100644 --- a/pywikibot/pagegenerators.py +++ b/pywikibot/pagegenerators.py @@ -134,12 +134,13 @@ -ns:0,2,4 -ns:Help,MediaWiki
- If used with -newpages, -namepace/ns must be provided - before -newpages. + If used with -newpages/-random/-randomredirect, + -namespace/ns must be provided before + -newpages/-random/-randomredirect. If used with -recentchanges, efficiency is improved if - -namepace/ns is provided before -recentchanges. + -namespace/ns is provided before -recentchanges.
- If used with -start, -namepace/ns shall contain only one + If used with -start, -namespace/ns shall contain only one value.
-interwiki Work on the given page and all equivalent pages in other @@ -601,16 +602,29 @@ self.site)) gen = InterwikiPageGenerator(page) elif arg.startswith('-randomredirect'): + # partial workaround for bug T119940 + # to use -namespace/ns with -randomredirect, -ns must be given + # before -randomredirect + # otherwise default namespace is 0 + namespaces = self.namespaces or 0 if len(arg) == 15: - gen = RandomRedirectPageGenerator(site=self.site) + gen = RandomRedirectPageGenerator(site=self.site, + namespaces=namespaces) else: gen = RandomRedirectPageGenerator(total=int(arg[16:]), - site=self.site) + site=self.site, + namespaces=namespaces) elif arg.startswith('-random'): + # partial workaround for bug T119940 + # to use -namespace/ns with -random, -ns must be given + # before -random + # otherwise default namespace is 0 + namespaces = self.namespaces or 0 if len(arg) == 7: - gen = RandomPageGenerator(site=self.site) + gen = RandomPageGenerator(site=self.site, namespaces=namespaces) else: - gen = RandomPageGenerator(total=int(arg[8:]), site=self.site) + gen = RandomPageGenerator(total=int(arg[8:]), site=self.site, + namespaces=namespaces) elif arg.startswith('-recentchanges'): if len(arg) >= 15: total = int(arg[15:]) @@ -2155,7 +2169,7 @@
@deprecated_args(number="total") -def RandomPageGenerator(total=10, site=None): +def RandomPageGenerator(total=10, site=None, namespaces=None): """ Random page generator.
@@ -2166,12 +2180,12 @@ """ if site is None: site = pywikibot.Site() - for page in site.randompages(total=total): + for page in site.randompages(total=total, namespaces=namespaces): yield page
@deprecated_args(number="total") -def RandomRedirectPageGenerator(total=10, site=None): +def RandomRedirectPageGenerator(total=10, site=None, namespaces=None): """ Random redirect generator.
@@ -2182,7 +2196,8 @@ """ if site is None: site = pywikibot.Site() - for page in site.randompages(total=total, redirects=True): + for page in site.randompages(total=total, namespaces=namespaces, + redirects=True): yield page
pywikibot-commits@lists.wikimedia.org