jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/485364 )
Change subject: pagegenerators.py: add -querypage parameter ......................................................................
pagegenerators.py: add -querypage parameter
Add -querypage parameter to yield pages provided by a QueryPage-based special page.
-querypage list pages available on wiki.
Bug: T214234 Change-Id: Ica39133ba49dd05aa91dfcf9a33c469abf23a018 --- M pywikibot/pagegenerators.py M tests/pagegenerators_tests.py 2 files changed, 61 insertions(+), 3 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/pagegenerators.py b/pywikibot/pagegenerators.py index 9555c39..d735c1f 100644 --- a/pywikibot/pagegenerators.py +++ b/pywikibot/pagegenerators.py @@ -14,7 +14,7 @@ ¶ms; """ # -# (C) Pywikibot team, 2008-2018 +# (C) Pywikibot team, 2008-2019 # # Distributed under the terms of the MIT license. # @@ -306,6 +306,12 @@
-linter:show just shows available categories.
+-querypage:name Work on pages provided by a QueryPage-based special page, + see https://www.mediawiki.org/wiki/API:Querypage. + (tip: use -limit:n to fetch only n pages). + + -querypage shows special pages available. +
FILTER OPTIONS ============== @@ -720,7 +726,7 @@ for c in _list: txt += '{indent}{cat}\n'.format(indent=2 * _i, cat=c) pywikibot.output('%s' % txt) - return True + sys.exit(0)
if not cat: lint_cats = valid_cats @@ -737,6 +743,27 @@ lint_categories='|'.join(lint_cats), namespaces=self.namespaces, lint_from=lint_from)
+ def _handle_querypage(self, value): + """Handle `-querypage` argument.""" + if value is None: # Display special pages. + pages = self.site._paraminfo.parameter('query+querypage', + 'page') + pages = sorted(pages['type']) + limit = self.site._paraminfo.parameter('query+querypage', + 'limit') + + max_w = max(len(p) for p in pages[::2]) + 4 + txt = 'Available special pages:\n' + for a, b in itertools.zip_longest(pages[::2], pages[1::2], + fillvalue=''): + txt += ' {a:<{max_w}}{b}\n'.format(a=a, b=b, max_w=max_w) + txt += ('\nMaximum number of pages to return is {max} ' + '({highmax} for bots).\n'.format(**limit)) + pywikibot.output('%s' % txt) + sys.exit(0) + + return self.site.querypage(value) + def _handle_unusedfiles(self, value): """Handle `-unusedfiles` argument.""" return UnusedFilesGenerator(total=_int_none(value), site=self.site) diff --git a/tests/pagegenerators_tests.py b/tests/pagegenerators_tests.py index 57b1e15..611bd67 100755 --- a/tests/pagegenerators_tests.py +++ b/tests/pagegenerators_tests.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- """Test pagegenerators module.""" # -# (C) Pywikibot team, 2009-2018 +# (C) Pywikibot team, 2009-2019 # # Distributed under the terms of the MIT license. from __future__ import absolute_import, division, unicode_literals @@ -1215,6 +1215,37 @@ gf = pagegenerators.GeneratorFactory(site=self.site) self.assertRaises(ValueError, gf.handleArg, '-linter:dummy')
+ def test_linter_generator_show(self): + """Test generator of pages with lint errors.""" + gf = pagegenerators.GeneratorFactory(site=self.site) + with self.assertRaises(SystemExit) as cm: + gf.handleArg('-linter:show') + self.assertEqual(cm.exception.code, 0) + + def test_querypage_generator_with_valid_page(self): + """Test generator of pages with lint errors.""" + gf = pagegenerators.GeneratorFactory(site=self.site) + gf.handleArg('-querypage:Ancientpages') + gf.handleArg('-limit:5') + gen = gf.getCombinedGenerator() + self.assertIsNotNone(gen) + pages = list(gen) + self.assertLessEqual(len(pages), 5) + for page in pages: + self.assertIsInstance(page, pywikibot.Page) + + def test_querypage_generator_with_invalid_page(self): + """Test generator of pages with lint errors.""" + gf = pagegenerators.GeneratorFactory(site=self.site) + self.assertRaises(AssertionError, gf.handleArg, '-querypage:dummy') + + def test_querypage_generator_with_no_page(self): + """Test generator of pages with lint errors.""" + gf = pagegenerators.GeneratorFactory(site=self.site) + with self.assertRaises(SystemExit) as cm: + gf.handleArg('-querypage') + self.assertEqual(cm.exception.code, 0) +
class TestFactoryGeneratorNewpages(TestCase):
pywikibot-commits@lists.wikimedia.org