jenkins-bot has submitted this change and it was merged.
Change subject: [FEAT] Site: Return the default searched namespaces ......................................................................
[FEAT] Site: Return the default searched namespaces
This returns a set of the namespaces which are searched when the namespaces are not given explicitly. If not logged in they'll return the default namespaces set for the server and otherwise the namespaces selected in the user preferences.
Change-Id: Ieed1f9890d4e7dc631d6e2d33c776592eda2c17f --- M pywikibot/site.py 1 file changed, 34 insertions(+), 0 deletions(-)
Approvals: John Vandenberg: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/site.py b/pywikibot/site.py index 58a76bf..1a16e06 100644 --- a/pywikibot/site.py +++ b/pywikibot/site.py @@ -1473,6 +1473,40 @@ # User blocked raise UserBlocked('User is blocked in site %s' % self)
+ def get_searched_namespaces(self, force=False): + """ + Retrieve the default searched namespaces for the user. + + If no user is logged in, it returns the namespaces used by default. + Otherwise it returns the user preferences. It caches the last result + and returns it, if the username or login status hasn't changed. + + @param force: Whether the cache should be discarded. + @return: The namespaces which are searched by default. + @rtype: C{set} of L{Namespace} + """ + # TODO: Integrate into _userinfo + if (force or not hasattr(self, "_useroptions") + or self.user() != self._useroptions['_name']): + uirequest = api.Request( + site=self, + action="query", + meta="userinfo", + uiprop="options" + ) + uidata = uirequest.submit() + assert 'query' in uidata, \ + "API userinfo response lacks 'query' key" + assert 'userinfo' in uidata['query'], \ + "API userinfo response lacks 'userinfo' key" + self._useroptions = uidata['query']['userinfo']['options'] + # To determine if user name has changed + self._useroptions['_name'] = ( + None if 'anon' in uidata['query']['userinfo'] else + uidata['query']['userinfo']['name']) + return set(ns for ns in self.namespaces().values() if ns.id >= 0 + and self._useroptions['searchNs{0}'.format(ns.id)] in ['1', True]) + def assert_valid_iter_params(self, msg_prefix, start, end, reverse): """Validate iterating API parameters.""" if reverse:
pywikibot-commits@lists.wikimedia.org