jenkins-bot has submitted this change and it was merged.
Change subject: APISite: simplify _userinfo caching
......................................................................
APISite: simplify _userinfo caching
Removed two checks in getuserinfo:
- The old code compared the username in _userinfo
to the username //based on the groups in _userinfo//,
which caused repetitive requests in two cases:
1) no username configured
2) no sysopname configured and the normal username
has sysop rights
- The code checked if _userinfo contained 'rights',
but the only code that sets 'rights' is getuserinfo(),
which explicitly requests 'rights'. If the API doesn't
return those, requesting it again won't solve the issue.
Removed code in api.py that wrote to site._userinfo. Updates
should only be done by self.getuserinfo.
Reworked the 'logged out' check to use Site functions instead
of fiddling with Site internals directly.
Moved LoginStatus into a seperate module.
[1]
https://www.mediawiki.org/wiki/API:Errors_and_warnings
The original code for getuserinfo was added in 697a20d
The original code for api.php was added in 5b0fc90
Bug: T57192
Change-Id: I409bd268bf655c2a05d0f206e164d8db334b1311
---
M pywikibot/data/api.py
M pywikibot/site.py
2 files changed, 33 insertions(+), 22 deletions(-)
Approvals:
John Vandenberg: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/data/api.py b/pywikibot/data/api.py
index f3772c7..6283343 100644
--- a/pywikibot/data/api.py
+++ b/pywikibot/data/api.py
@@ -1989,26 +1989,16 @@
"Unable to process query response of type %s."
% type(result),
data=result)
- if self.action == 'query':
- if 'userinfo' in result.get('query', ()):
- if hasattr(self.site, '_userinfo'):
-
self.site._userinfo.update(result['query']['userinfo'])
- else:
- self.site._userinfo =
result['query']['userinfo']
- status = self.site._loginstatus # save previous login status
- if (('error' in result and
- result['error']['code'].endswith('limit'))
or
- (status >= 0 and
- self.site._userinfo['name'] !=
self.site._username[status])):
- # user is no longer logged in (session expired?)
- # reset userinfo, then make user log in again
- del self.site._userinfo
- self.site._loginstatus = -1
- if status < 0:
- status = 0 # default to non-sysop login
- self.site.login(status)
- # retry the previous query
+
+ if self.action == 'query' and 'userinfo' in
result.get('query', ()):
+ # if we get passed userinfo in the query result, we can confirm
+ # that we are logged in as the correct user. If this is not the
+ # case, force a re-login.
+ username = result['query']['userinfo']['name']
+ if self.site.user() is not None and self.site.user() != username:
+ self.site._relogin()
continue
+
self._handle_warnings(result)
if "error" not in result:
@@ -2028,6 +2018,17 @@
result['error']['help'] =
result['error'].pop("*")
code = result['error'].setdefault('code', 'Unknown')
info = result['error'].setdefault('info', None)
+
+ if code.endswith('limit'):
+ # Older wikis returned an error instead of a warning when
+ # the request asked for too many values. If we get this
+ # error, assume we are not logged in (we can't check this
+ # because the userinfo data is not present) and force
+ # a re-login
+ pywikibot.error("Received API limit error. Forcing re-login")
+ self.site.relogin()
+ continue
+
if code == "maxlag":
lag = lagpattern.search(info)
if lag:
diff --git a/pywikibot/site.py b/pywikibot/site.py
index e9d1100..1ebbfc9 100644
--- a/pywikibot/site.py
+++ b/pywikibot/site.py
@@ -1924,6 +1924,18 @@
forceLogin = redirect_func(login, old_name='forceLogin',
class_name='APISite')
+ def _relogin(self):
+ """Force a login sequence without logging out, using the current
user.
+
+ This is an internal function which is used to re-login when
+ the internal login state does not match the state we receive
+ from the site.
+ """
+ del self._userinfo
+ old_status = self._loginstatus
+ self._loginstatus = LoginStatus.NOT_LOGGED_IN
+ self.site.login(old_status)
+
def logout(self):
"""Logout of the site and load details for the logged out user.
@@ -1954,9 +1966,7 @@
@param force: force to retrieve userinfo ignoring cache
@type force: bool
"""
- if (not hasattr(self, '_userinfo') or force or
- 'rights' not in self._userinfo or
- self._userinfo['name'] != self._username['sysop' in
self._userinfo['groups']]):
+ if force or not hasattr(self, '_userinfo'):
uirequest = self._simple_request(
action="query",
meta="userinfo",
--
To view, visit
https://gerrit.wikimedia.org/r/231769
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I409bd268bf655c2a05d0f206e164d8db334b1311
Gerrit-PatchSet: 13
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Merlijn van Deen <valhallasw(a)arctus.nl>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Ladsgroup <ladsgroup(a)gmail.com>
Gerrit-Reviewer: Malafaya <malafaya(a)clix.pt>
Gerrit-Reviewer: Merlijn van Deen <valhallasw(a)arctus.nl>
Gerrit-Reviewer: XZise <CommodoreFabianus(a)gmx.de>
Gerrit-Reviewer: jenkins-bot <>