jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/959755 )
Change subject: Give visible error to user when there are no rights.
......................................................................
Give visible error to user when there are no rights.
Currently bot does not give user-visible output when
user is not logged in or user does not have permissions.
Instead of silently failing inform user of a problem.
Bug: T345342
Change-Id: I3b7135a0e6ea6fb5c6280f611610b5c60376bbb2
---
M pywikibot/data/sparql.py
M tests/sparql_tests.py
2 files changed, 66 insertions(+), 6 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/data/sparql.py b/pywikibot/data/sparql.py
index 23ffb1d..3417e32 100644
--- a/pywikibot/data/sparql.py
+++ b/pywikibot/data/sparql.py
@@ -4,7 +4,6 @@
#
# Distributed under the terms of the MIT license.
#
-from contextlib import suppress
from typing import Optional
from urllib.parse import quote
@@ -14,7 +13,7 @@
from pywikibot.backports import Dict, List, removeprefix
from pywikibot.comms import http
from pywikibot.data import WaitingMixin
-from pywikibot.exceptions import Error
+from pywikibot.exceptions import Error, NoUsernameError
try:
@@ -144,17 +143,36 @@
if headers is None:
headers = DEFAULT_HEADERS
+ # force cleared
+ self.last_response = None
+
url = f'{self.endpoint}?query={quote(query)}'
while True:
try:
self.last_response = http.fetch(url, headers=headers)
+ break
except Timeout:
self.wait()
- continue
- with suppress(JSONDecodeError):
- return self.last_response.json()
- break
+ try:
+ return self.last_response.json()
+ except JSONDecodeError:
+ # There is no proper error given but server returns HTML page
+ # in case login isn't valid sotry to guess what the problem is
+ # and notify user instead of silently ignoring it.
+ # This could be made more reliable by fixing the backend.
+ # Note: only raise error when response starts with HTML,
+ # not in case the response otherwise might have it in between
+ strcontent = self.last_response.content.decode()
+ if (strcontent.startswith('<!DOCTYPE html>')
+ and 'https://commons-query.wikimedia.org' in url):
+ if ('Special:UserLogin' in strcontent
+ or 'Special:OAuth' in strcontent):
+ message = ('You need to log in to Wikimedia Commons '
+ 'and give OAUTH permission. '
+ 'Open https://commons-query.wikimedia.org '
+ 'with browser to login and give permission.')
+ raise NoUsernameError('User not logged in. ' + message)
return None
diff --git a/tests/sparql_tests.py b/tests/sparql_tests.py
index 706da60..a14f4f0 100755
--- a/tests/sparql_tests.py
+++ b/tests/sparql_tests.py
@@ -14,6 +14,7 @@
import pywikibot.data.sparql as sparql
from tests.aspects import TestCase, WikidataTestCase
from tests.utils import skipping
+from pywikibot.exceptions import NoUsernameError
# See: https://www.w3.org/TR/2013/REC-sparql11-results-json-20130321/
@@ -181,6 +182,33 @@
self.assertFalse(res)
+class TestCommonsQueryService(TestCase):
+ """Test Commons Query Service auth."""
+
+ family = 'commons'
+ code = 'commons'
+
+ def testLoginAndOauthPermisson(self):
+ """Commons Query Service Login and Oauth permission."""
+ # Define the SPARQL query
+ query = 'SELECT ?a ?b WHERE { ?a wdt:P9478 ?b } LIMIT 4'
+
+ # Set up the SPARQL endpoint and entity URL
+ # Note: https://commons-query.wikimedia.org
+ # requires user to be logged in
+
+ entity_url = 'https://commons.wikimedia.org/entity/'
+ endpoint = 'https://commons-query.wikimedia.org/sparql'
+
+ # Create a SparqlQuery object
+ query_object = sparql.SparqlQuery(endpoint=endpoint,
+ entity_url=entity_url)
+
+ # Execute the SPARQL query and retrieve the data user not logged in
+ with self.assertRaisesRegex(NoUsernameError, 'User not logged in'):
+ query_object.select(query, full_data=False)
+
+
class Shared:
"""Shared test placeholder."""
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/959755
To unsubscribe, or for help writing mail filters, visit https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I3b7135a0e6ea6fb5c6280f611610b5c60376bbb2
Gerrit-Change-Number: 959755
Gerrit-PatchSet: 7
Gerrit-Owner: Ipr1 <ilkka.prusi(a)gmail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-CC: Welcome, new contributor! <ssethi(a)wikimedia.org>
Gerrit-CC: Zache-tool <kimmo.virtanen(a)gmail.com>
Gerrit-MessageType: merged
jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/961414 )
Change subject: [cleanup] Remove rawcontinue code parts
......................................................................
[cleanup] Remove rawcontinue code parts
Bug: T343204
Change-Id: I6c4275cb501cff3fece64738e75e3b0772049a79
---
M tests/site_generators_tests.py
M pywikibot/data/api/_requests.py
M pywikibot/data/api/_generators.py
3 files changed, 30 insertions(+), 25 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/data/api/_generators.py b/pywikibot/data/api/_generators.py
index e483ef3..9e25f52 100644
--- a/pywikibot/data/api/_generators.py
+++ b/pywikibot/data/api/_generators.py
@@ -12,12 +12,14 @@
#
from abc import ABC, abstractmethod
from contextlib import suppress
-from typing import Union
+from typing import Optional, Union
from warnings import warn
import pywikibot
from pywikibot import config
+from pywikibot.backports import List
from pywikibot.exceptions import Error, InvalidTitleError, UnsupportedPageError
+from pywikibot.tools import deprecated
from pywikibot.tools.collections import GeneratorWrapper
@@ -102,6 +104,7 @@
self.limit_name = limit_name
self.data_name = data_name
+ self.query_increment: Optional[int]
if config.step > 0:
self.query_increment = config.step
else:
@@ -213,12 +216,10 @@
_namespaces = None
def __init__(self, **kwargs) -> None:
- """
- Initialize a QueryGenerator object.
+ """Initialize a QueryGenerator object.
kwargs are used to create a Request object; see that object's
documentation for values. 'action'='query' is assumed.
-
"""
if not hasattr(self, 'site'):
kwargs = self._clean_kwargs(kwargs) # hasn't been called yet
@@ -279,6 +280,7 @@
else:
self.request[prefix + 'limit'] = int(param['max'])
+ self.api_limit: Optional[int]
if config.step > 0:
self.api_limit = config.step
else:
@@ -300,17 +302,14 @@
else:
self.resultkey = self.modules[0]
- # usually the (query-)continue key is the same as the querymodule,
- # but not always
- # API can return more than one query-continue key, if multiple
- # properties are requested by the query, e.g.
- # "query-continue":{
- # "langlinks":{"llcontinue":"12188973|pt"},
- # "templates":{"tlcontinue":"310820|828|Namespace_detect"}}
- # self.continuekey is a list
- self.continuekey = self.modules
self._add_slots()
+ @property
+ @deprecated(since='8.4.0')
+ def continuekey(self) -> List[str]:
+ """Return deprecated continuekey which is self.modules."""
+ return self.modules
+
def _add_slots(self) -> None:
"""Add slots to params if the site supports multi-content revisions.
@@ -566,12 +565,14 @@
continue
yield result
- if isinstance(item, dict) and set(self.continuekey) & set(item):
+
+ modules_item_intersection = set(self.modules) & set(item)
+ if isinstance(item, dict) and modules_item_intersection:
# if we need to count elements contained in items in
# self.data["query"]["pages"], we want to count
- # item[self.continuekey] (e.g. 'revisions') and not
+ # item[self.modules] (e.g. 'revisions') and not
# self.resultkey (i.e. 'pages')
- for key in set(self.continuekey) & set(item):
+ for key in modules_item_intersection:
self._count += len(item[key])
# otherwise we proceed as usual
else:
diff --git a/pywikibot/data/api/_requests.py b/pywikibot/data/api/_requests.py
index e13bd6f..e9b9bd7 100644
--- a/pywikibot/data/api/_requests.py
+++ b/pywikibot/data/api/_requests.py
@@ -221,7 +221,7 @@
parameters = kwargs
elif parameters is self._PARAM_DEFAULT:
parameters = {}
- self._params = {}
+ self._params: Dict[str, Any] = {}
if 'action' not in parameters:
raise ValueError("'action' specification missing from Request.")
self.action = parameters['action']
@@ -432,12 +432,6 @@
and self.site.has_extension('ProofreadPage'):
prop = set(self['prop'] + ['proofread'])
self['prop'] = sorted(prop)
- # When neither 'continue' nor 'rawcontinue' is present we add a
- # dummy rawcontinue parameter except for 'tokens' (T284577) and
- # 'siteinfo' (T343204)
- if ('tokens' not in meta and 'siteinfo' not in meta
- and 'continue' not in self._params):
- self._params.setdefault('rawcontinue', [''])
elif self.action == 'help':
self['wrap'] = ''
diff --git a/tests/site_generators_tests.py b/tests/site_generators_tests.py
index a2513b3..ec63ae4 100755
--- a/tests/site_generators_tests.py
+++ b/tests/site_generators_tests.py
@@ -1694,8 +1694,8 @@
self.mysite.loadrevisions(self.mainpage,
revids=130000)
- def test_loadrevisions_querycontinue(self):
- """Test the site.loadrevisions() method with query-continue."""
+ def test_loadrevisions_continue(self):
+ """Test the site.loadrevisions() method with continue."""
self.mysite.loadrevisions(self.mainpage, step=5, total=12)
self.assertLength(self.mainpage._revisions, 12)
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/961414
To unsubscribe, or for help writing mail filters, visit https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I6c4275cb501cff3fece64738e75e3b0772049a79
Gerrit-Change-Number: 961414
Gerrit-PatchSet: 3
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged
jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/964114 )
Change subject: [doc] Update sphinx requirements
......................................................................
[doc] Update sphinx requirements
Change-Id: Ic7af64e97c820eec33b4ca6256d34c031c4c289e
---
M ROADMAP.rst
M docs/requirements.txt
2 files changed, 16 insertions(+), 6 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/ROADMAP.rst b/ROADMAP.rst
index 2e73f2c..42ac05d 100644
--- a/ROADMAP.rst
+++ b/ROADMAP.rst
@@ -1,13 +1,13 @@
Current release
---------------
-*(No changes yet*)
+* **Python 3.6 support will be discontinued** and this is the last version supporting it.
Deprecations
------------
-* 8.4.0: *Python 3.6 support is deprecated* and will be dropped soon with Pywikibot 9
+* 8.4.0: Python 3.6 support is deprecated and will be dropped soon with Pywikibot 9
* 8.4.0: *modules_only_mode* parameter of :class:`data.api.ParamInfo`, its *paraminfo_keys* class attribute
and its preloaded_modules property will be removed
* 8.4.0: *dropdelay* and *releasepid* attributes of :class:`throttle.Throttle` will be removed
diff --git a/docs/requirements.txt b/docs/requirements.txt
index d4f5d0c..9af461b 100644
--- a/docs/requirements.txt
+++ b/docs/requirements.txt
@@ -1,6 +1,7 @@
-# This is a PIP requirements file for building Sphinx documentation of pywikibot
-# requirements.txt is also needed
-sphinx >= 7.2.3
-rstcheck >=6.1.2
+# This is a PIP requirements file for building Sphinx documentation of Pywikibot
+# ../requirements.txt is also needed.
+# Note: Python 3.9 is required for sphinx 7.2.6
+sphinx >= 7.2.6
+rstcheck >=6.2.0
sphinxext-opengraph >= 0.8.2
furo >= 2023.9.10
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/964114
To unsubscribe, or for help writing mail filters, visit https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Ic7af64e97c820eec33b4ca6256d34c031c4c289e
Gerrit-Change-Number: 964114
Gerrit-PatchSet: 1
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged