jenkins-bot has submitted this change and it was merged.
Change subject: Fetch patrol token for MW < 1.20 ......................................................................
Fetch patrol token for MW < 1.20
Get patrol token for versions < MW 1.20.
Since patrol was added in v1.14, until v1.16, the patrol token is the same as the edit token.
For MW 1.17-19, the patrol token must be obtained from the query list recentchanges.
Tests for patrol token in the different MW versions added.
Incompatibility of site version with requested version added.
Bug: T85727 Change-Id: Ibbba1f8e0093f28d8cefd86a71f225938bc4a8a1 --- M pywikibot/site.py M tests/site_tests.py 2 files changed, 65 insertions(+), 2 deletions(-)
Approvals: XZise: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/site.py b/pywikibot/site.py index 973edaf..c1fd08a 100644 --- a/pywikibot/site.py +++ b/pywikibot/site.py @@ -1470,6 +1470,8 @@
# Constants for token management. # For all MediaWiki versions prior to 1.20. + # 'patrol' is indirectly supported via 'edit' token or recentchanges. + # It will be converted in site.validate_tokens()/site.get_tokens(). TOKENS_0 = set(['edit', 'delete', 'protect', @@ -1479,6 +1481,7 @@ 'email', 'import', 'watch', + 'patrol', ])
# For all MediaWiki versions, with 1.20 <= version < 1.24wmf19 @@ -2608,6 +2611,12 @@ _version = MediaWikiVersion(self.version()) if _version < MediaWikiVersion('1.20'): valid_types = [token for token in types if token in self.TOKENS_0] + + # Pre 1.17, preload token was the same as the edit token. + if _version < MediaWikiVersion('1.17'): + if 'patrol' in types and 'edit' not in valid_types: + valid_types.append('edit') + elif _version < MediaWikiVersion('1.24wmf19'): valid_types = [token for token in types if token in self.TOKENS_1] else: @@ -2630,13 +2639,19 @@ system was introduced which reduced the amount of tokens available. Most of them were merged into the 'csrf' token. If the token type in the parameter is not known it will default to the 'csrf' token. + The other token types available are: - deleteglobalaccount - - patrol + - patrol (*) - rollback - setglobalaccountstatus - userrights - watch + + (*) Patrol was added in v1.14. + Until v1.16, the patrol token is same as the edit token. + For v1.17-19, the patrol token must be obtained from the query + list recentchanges.
@param types: the types of token (e.g., "edit", "move", "delete"); see API documentation for full list of types @@ -2659,6 +2674,9 @@ if all: types.extend(self.TOKENS_0) for tokentype in self.validate_tokens(types): + # 'patrol' token is done later on. + if tokentype == 'patrol': + continue query = api.PropertyGenerator('info', titles='Dummy page', intoken=tokentype, @@ -2670,6 +2688,27 @@ if (tokentype + 'token') in item: user_tokens[tokentype] = item[tokentype + 'token']
+ # patrol token require special handling. + # TODO: try to catch exceptions? + if 'patrol' in types: + if MediaWikiVersion('1.14') <= _version < MediaWikiVersion('1.17'): + user_tokens['patrol'] = user_tokens['edit'] + else: + req = api.Request(site=self, action='query', + list='recentchanges', + rctoken='patrol', rclimit=1) + + req._warning_handler = warn_handler + data = req.submit() + + if 'query' in data: + data = data['query'] + if 'recentchanges' in data: + item = data['recentchanges'][0] + pywikibot.debug(unicode(item), _logger) + if 'patroltoken' in item: + user_tokens['patrol'] = item.get('patroltoken') + else: if _version < MediaWikiVersion('1.24wmf19'): if all is not False: diff --git a/tests/site_tests.py b/tests/site_tests.py index 4d5b4e6..55e3d3f 100644 --- a/tests/site_tests.py +++ b/tests/site_tests.py @@ -1356,7 +1356,14 @@
class TestSiteTokens(DefaultSiteTestCase):
- """Test cases for tokens in Site methods.""" + """Test cases for tokens in Site methods. + + Versions of sites are simulated if actual versions are higher than + needed by the test case. + + Test is skipped if site version is not compatible. + + """
user = True
@@ -1372,6 +1379,11 @@
def _test_tokens(self, version, test_version, additional_token): if version and self._version < MediaWikiVersion(version): + raise unittest.SkipTest( + u'Site %s version %s is too low for this tests.' + % (self.mysite, self._version)) + + if version and self._version < MediaWikiVersion(test_version): raise unittest.SkipTest( u'Site %s version %s is too low for this tests.' % (self.mysite, self._version)) @@ -1394,14 +1406,26 @@ # test __contains__ self.assertIn(tokentype[0], self.mysite.tokens)
+ def test_patrol_tokens_in_mw_116(self): + """Test ability to get patrol token on MW 1.16 wiki.""" + self._test_tokens('1.14', '1.16', 'patrol') + def test_tokens_in_mw_119(self): """Test ability to get page tokens.""" self._test_tokens(None, '1.19', 'delete')
+ def test_patrol_tokens_in_mw_119(self): + """Test ability to get patrol token on MW 1.19 wiki.""" + self._test_tokens('1.14', '1.19', 'patrol') + def test_tokens_in_mw_120_124wmf18(self): """Test ability to get page tokens.""" self._test_tokens('1.20', '1.21', 'deleteglobalaccount')
+ def test_patrol_tokens_in_mw_120(self): + """Test ability to get patrol token.""" + self._test_tokens('1.14', '1.20', 'patrol') + def test_tokens_in_mw_124wmf19(self): """Test ability to get page tokens.""" self._test_tokens('1.24wmf19', '1.24wmf20', 'deleteglobalaccount')
pywikibot-commits@lists.wikimedia.org