jenkins-bot has submitted this change and it was merged.
Change subject: [IMPROV] Implement some compat methods ......................................................................
[IMPROV] Implement some compat methods
This adds and implements some methods which were in compat but not in core.
Change-Id: Ib6c80c6e6ce5bcd711b62d39e30b75789264f977 --- M pywikibot/site.py M tests/site_tests.py 2 files changed, 95 insertions(+), 0 deletions(-)
Approvals: John Vandenberg: Looks good to me, approved XZise: Looks good to me, but someone else must approve jenkins-bot: Verified
diff --git a/pywikibot/site.py b/pywikibot/site.py index 90897fe..f96a215 100644 --- a/pywikibot/site.py +++ b/pywikibot/site.py @@ -2758,6 +2758,31 @@ """ return self.tokens[tokentype]
+ @deprecated("the 'tokens' property") + def getToken(self, getalways=True, getagain=False, sysop=False): + """DEPRECATED: Get edit token.""" + if self.username(sysop) != self.user(): + raise ValueError('The token for {0} was requested but only the ' + 'token for {1} can be retrieved.'.format( + self.username(sysop), self.user())) + if not getalways: + raise ValueError('In pywikibot/core getToken does not support the ' + 'getalways parameter.') + token = self.validate_tokens(['edit'])[0] + if getagain and token in self.tokens: + # invalidate token + del self.tokens._tokens[self.user()][token] + return self.tokens[token] + + @deprecated("the 'tokens' property") + def getPatrolToken(self, sysop=False): + """DEPRECATED: Get patrol token.""" + if self.username(sysop) != self.user(): + raise ValueError('The token for {0} was requested but only the ' + 'token for {1} can be retrieved.'.format( + self.username(sysop), self.user())) + return self.tokens['patrol'] + # following group of methods map more-or-less directly to API queries
def pagebacklinks(self, page, followRedirects=False, filterRedirects=None, @@ -5288,6 +5313,35 @@ step=step, total=total) return lrgen
+ @deprecated_args(lvl='level') + def protectedpages(self, namespace=0, type='edit', level=False, total=None): + """ + Return protected pages depending on protection level and type. + + For protection types which aren't 'create' it uses L{APISite.allpages}, + while it uses for 'create' the 'query+protectedtitles' module. + + @param namespaces: The searched namespace. + @type namespaces: int or Namespace or str + @param type: The protection type to search for (default 'edit'). + @type type: str + @param level: The protection level (like 'autoconfirmed'). If False it + shows all protection levels. + @type level: str or False + @return: The pages which are protected. + @rtype: generator of Page + """ + namespaces = Namespace.resolve(namespace, self.namespaces) + # always assert that, so we are be sure that type could be 'create' + assert('create' in self.protection_types()) + if type == 'create': + return self._generator( + api.PageGenerator, type_arg='protectedtitles', + namespaces=namespaces, gptlevel=level, total=total) + else: + return self.allpages(namespace=namespaces[0], protect_level=level, + protect_type=type, total=total) +
class DataSite(APISite):
diff --git a/tests/site_tests.py b/tests/site_tests.py index b3ac597..bcc2e51 100644 --- a/tests/site_tests.py +++ b/tests/site_tests.py @@ -692,6 +692,43 @@ site.lock_page(page=p1, block=False) site.unlock_page(page=p1)
+ def test_protectedpages_create(self): + """Test that protectedpages returns protected page titles.""" + pages = list(self.get_site().protectedpages(type='create', total=10)) + for page in pages: + self.assertFalse(page.exists()) + self.assertLessEqual(len(pages), 10) + + def test_protectedpages_edit(self): + """Test that protectedpages returns protected pages.""" + site = self.get_site() + pages = list(site.protectedpages(type='edit', total=10)) + for page in pages: + self.assertTrue(page.exists()) + self.assertIn('edit', page.protection()) + self.assertLessEqual(len(pages), 10) + + def test_protectedpages_edit_level(self): + site = self.get_site() + levels = set() + all_levels = site.protection_levels().difference(['']) + for level in all_levels: + if list(site.protectedpages(type='edit', level=level, total=1)): + levels.add(level) + # select one level which won't yield all pages from above + level = next(iter(levels)) + if len(levels) == 1: + # if only one level found, then use any other except that + level = next(iter(all_levels.difference([level]))) + invalid_levels = all_levels.difference([level]) + pages = list(site.protectedpages(type='edit', level=level, total=10)) + for page in pages: + self.assertTrue(page.exists()) + self.assertIn('edit', page.protection()) + self.assertEqual(page.protection()['edit'][0], level) + self.assertNotIn(page.protection()['edit'][0], invalid_levels) + self.assertLessEqual(len(pages), 10) +
class TestImageUsage(DefaultSiteTestCase):
@@ -1460,6 +1497,10 @@ def testInvalidToken(self): self.assertRaises(pywikibot.Error, lambda t: self.mysite.tokens[t], "invalidtype")
+ def test_deprecated_token(self): + self.assertEqual(self.mysite.getToken(), self.mysite.tokens['edit']) + self.assertEqual(self.mysite.getPatrolToken(), self.mysite.tokens['patrol']) +
class TestSiteExtensions(WikimediaDefaultSiteTestCase):
pywikibot-commits@lists.wikimedia.org