Mpaa has uploaded a new change for review.
https://gerrit.wikimedia.org/r/156465
Change subject: site.py: check if site is enabled for uploads
......................................................................
site.py: check if site is enabled for uploads
Fix Bug 69090: upload.py attempts uploads on wikis that
don't allow uploads.
Check APIError code to determine if Site is enabled for uploading.
Currently it is done and cached at first upload attempt.
Added also a helper function is_uploaddisabled() to determine if a site
has uploads enabled, based on a dummy upload attempt.
Throttle has been disabled for such request to speed up the query
(implementation taken from I80b2bba9e63832173d5b697db1f4ea419ca1122f).
Change-Id: Id8ae0cd51582d76b7bdd15c53e39b4105995b540
---
M pywikibot/__init__.py
M pywikibot/data/api.py
M pywikibot/exceptions.py
M pywikibot/site.py
M scripts/upload.py
M tests/site_tests.py
6 files changed, 76 insertions(+), 7 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core
refs/changes/65/156465/1
diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py
index 6804291..88c0602 100644
--- a/pywikibot/__init__.py
+++ b/pywikibot/__init__.py
@@ -43,7 +43,7 @@
)
from pywikibot.tools import UnicodeMixin, redirect_func
from pywikibot.i18n import translate
-from pywikibot.data.api import UploadWarning
+from pywikibot.data.api import UploadWarning, UploadError
import pywikibot.textlib as textlib
import pywikibot.tools
diff --git a/pywikibot/data/api.py b/pywikibot/data/api.py
index 8132a27..105ccb1 100644
--- a/pywikibot/data/api.py
+++ b/pywikibot/data/api.py
@@ -77,6 +77,18 @@
return self.info
+class UploadError(APIError):
+
+ """Upload failed with an error message (passed as the argument)."""
+
+ def __init__(self, code, message):
+ super(UploadError, self).__init__(code, message)
+
+ @property
+ def message(self):
+ return self.info
+
+
class TimeoutError(Error):
pass
@@ -144,6 +156,7 @@
except KeyError:
self.site = pywikibot.Site()
self.mime = kwargs.pop("mime", False)
+ self.throttle = kwargs.pop('throttle', True)
self.max_retries = kwargs.pop("max_retries",
pywikibot.config.max_retries)
self.retry_wait = kwargs.pop("retry_wait", pywikibot.config.retry_wait)
self.params = {}
@@ -308,7 +321,8 @@
simulate = self._simulate(action)
if simulate:
return simulate
- self.site.throttle(write=self.write)
+ if self.throttle:
+ self.site.throttle(write=self.write)
uri = self.site.scriptpath() + "/api.php"
ssl = False
if self.site.family.name in config.available_ssl_project:
diff --git a/pywikibot/exceptions.py b/pywikibot/exceptions.py
index 35f7d2d..6fdcdf0 100644
--- a/pywikibot/exceptions.py
+++ b/pywikibot/exceptions.py
@@ -192,3 +192,4 @@
# TODO: Warn about the deprecated usage
import pywikibot.data.api
UploadWarning = pywikibot.data.api.UploadWarning
+UploadError = pywikibot.data.api.UploadError
\ No newline at end of file
diff --git a/pywikibot/site.py b/pywikibot/site.py
index 7ea6f67..5785791 100644
--- a/pywikibot/site.py
+++ b/pywikibot/site.py
@@ -3859,6 +3859,34 @@
def getImagesFromAnHash(self, hash_found=None):
return self.getFilesFromAnHash(hash_found)
+ def is_uploaddisabled(self):
+ """Return True if upload is disabled on site.
+
+ If not called directly, it is cached by the first attempted
+ upload action.
+
+ """
+ if hasattr(self, '_uploaddisabled'):
+ return self._uploaddisabled
+ else:
+ # attempt a fake upload; on enabled sites will fail for:
+ # missingparam: One of the parameters
+ # filekey, file, url, statuskey is required
+ # TODO: is there another way?
+ try:
+ dummy = pywikibot.FilePage(self, title='dummy')
+ token=self.token(dummy, 'edit')
+ req = api.Request(site=self, action="upload",
+ token=token, throttle=False)
+ req.submit()
+ except api.APIError as error:
+ if error.code == u'uploaddisabled':
+ self._uploaddisabled = True
+ self._uploaddisabled_error = error
+ else:
+ self._uploaddisabled = False
+ return self._uploaddisabled
+
@deprecate_arg('imagepage', 'filepage')
def upload(self, filepage, source_filename=None, source_url=None,
comment=None, text=None, watch=False, ignore_warnings=False):
@@ -3880,6 +3908,7 @@
upload (for example, to overwrite an existing file); default False
"""
+
upload_warnings = {
# map API warning codes to user error messages
# %(msg)s will be replaced by message string from API responsse
@@ -3890,6 +3919,13 @@
'duplicate': "Uploaded file is a duplicate of %(msg)s.",
'badfilename': "Target filename is invalid.",
'filetype-unwanted-type': "File %(msg)s type is unwanted type.",
+ }
+
+ upload_errors = {
+ # map API warning codes to user error messages
+ # %(site)s will be replaced by site name.
+ 'uploaddisabled': "Local file uploads are disabled on %(site)s. "
+ "Please upload files to Wikimedia Commons.",
}
# check for required user right
@@ -3939,9 +3975,15 @@
req["ignorewarnings"] = ""
try:
result = req.submit()
- except api.APIError:
+ self._uploaddisabled = False
+ except api.APIError as error:
+ if error.code == u'uploaddisabled':
+ self._uploaddisabled = True
+ raise pywikibot.UploadError(error.code,
upload_errors[error.code]
+ % {'site': self})
# TODO: catch and process foreseeable errors
- raise
+ else:
+ raise error
result = result["upload"]
pywikibot.debug(result, _logger)
if "warnings" in result:
diff --git a/scripts/upload.py b/scripts/upload.py
index 8a9a44d..480bfe5 100755
--- a/scripts/upload.py
+++ b/scripts/upload.py
@@ -240,8 +240,10 @@
else:
pywikibot.output(u"Upload aborted.")
return
-
- except Exception:
+ except pywikibot.data.api.UploadError as error:
+ if error.code == u'uploaddisabled':
+ pywikibot.output("Upload error: %s" % error.info)
+ except Exception as error:
pywikibot.error("Upload error: ", exc_info=True)
else:
diff --git a/tests/site_tests.py b/tests/site_tests.py
index b4e0540..22c4650 100644
--- a/tests/site_tests.py
+++ b/tests/site_tests.py
@@ -11,7 +11,7 @@
from distutils.version import LooseVersion as LV
from collections import Iterable
import pywikibot
-from tests.utils import PywikibotTestCase, unittest
+from tests.utils import SiteTestCase, PywikibotTestCase, unittest
from datetime import datetime
import re
@@ -1167,6 +1167,16 @@
self.assertEquals(ll.site.family.name, 'wikipedia')
+class TestUploadEnabledSite(SiteTestCase):
+
+ def test_is_uploaddisabled(self):
+ site = pywikibot.Site('commons', 'commons')
+ self.assertFalse(site.is_uploaddisabled())
+
+ site = pywikibot.Site('wikidata', 'wikidata')
+ self.assertTrue(site.is_uploaddisabled())
+
+
if __name__ == '__main__':
try:
unittest.main()
--
To view, visit https://gerrit.wikimedia.org/r/156465
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Id8ae0cd51582d76b7bdd15c53e39b4105995b540
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Mpaa <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits