jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/843941 )
Change subject: [IMPR] cleanup Site.is_uploaddisabled() ......................................................................
[IMPR] cleanup Site.is_uploaddisabled()
uploadsenabled was added to Siteinfo with 1.27: https://www.mediawiki.org/wiki/API:Siteinfo
- use siteinfo only to determine Site.is_uploaddisabled() - check for is_uploaddisabled() before calling the Uploader - remove site._uploaddisabled settings within Uploader because the ability is already checked. Raise a exception if upload still fails with 'uploaddisabled' error code even the siteinfo says 'uploadsenabled'. - always return True in ClosedSite.is_uploaddisabled() - remove ClosedSite initializer because it calls the super initialzer. - update tests - update documentation
Bug: T306637 Change-Id: I565446e473923c9f45168f4dfe09908ddf224120 --- M pywikibot/site/_apisite.py M pywikibot/site/_obsoletesites.py M pywikibot/site/_upload.py M tests/site_tests.py 4 files changed, 21 insertions(+), 60 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/site/_apisite.py b/pywikibot/site/_apisite.py index f45b6c0..806614c 100644 --- a/pywikibot/site/_apisite.py +++ b/pywikibot/site/_apisite.py @@ -2750,40 +2750,16 @@ def is_uploaddisabled(self) -> bool: """Return True if upload is disabled on site.
- When the version is at least 1.27wmf9, uses general siteinfo. - If not called directly, it is cached by the first attempted - upload action. + **Example:** + + >>> site = pywikibot.Site('commons') + >>> site.is_uploaddisabled() + False + >>> site = pywikibot.Site('wikidata') + >>> site.is_uploaddisabled() + True """ - if self.mw_version >= '1.27wmf9': - return not self._siteinfo.get('general')['uploadsenabled'] - - if hasattr(self, '_uploaddisabled'): - return self._uploaddisabled - - # 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? - req = self._request(throttle=False, - parameters={'action': 'upload', - 'token': self.tokens['csrf']}) - try: - req.submit() - except APIError as error: - if error.code == 'uploaddisabled': - self._uploaddisabled = True - elif error.code == 'missingparam': - # If the upload module is enabled, the above dummy request - # does not have sufficient parameters and will cause a - # 'missingparam' error. - self._uploaddisabled = False - else: - # Unexpected error - raise - return self._uploaddisabled - - raise RuntimeError( - 'Unexpected success of upload action without parameters.') + return not self.siteinfo.get('general')['uploadsenabled']
def stash_info( self, @@ -2824,6 +2800,11 @@ :return: It returns True if the upload was successful and False otherwise. """ + if self.is_uploaddisabled(): + pywikibot.error( + f'Upload error: Local file uploads are disabled on {self}.') + return False + return Uploader(self, filepage, **kwargs).upload()
def get_property_names(self, force: bool = False) -> List[str]: diff --git a/pywikibot/site/_obsoletesites.py b/pywikibot/site/_obsoletesites.py index ee44cbd..cc16046 100644 --- a/pywikibot/site/_obsoletesites.py +++ b/pywikibot/site/_obsoletesites.py @@ -19,10 +19,6 @@ class ClosedSite(APISite): """Site closed to read-only mode."""
- def __init__(self, code, fam, user=None) -> None: - """Initializer.""" - super().__init__(code, fam, user) - def _closed_error(self, notice: str = '') -> None: """An error instead of pointless API call.""" pywikibot.error('Site {} has been closed. {}'.format(self.sitename, @@ -44,10 +40,8 @@ self._closed_error('No recent changes can be returned.')
def is_uploaddisabled(self) -> bool: - """Return True if upload is disabled on site.""" - if not hasattr(self, '_uploaddisabled'): - self._uploaddisabled = True - return self._uploaddisabled + """Upload is disabled on site.""" + return True
def newpages(self, **kwargs) -> None: """An error instead of pointless API call.""" diff --git a/pywikibot/site/_upload.py b/pywikibot/site/_upload.py index 2dd2c32..1438554 100644 --- a/pywikibot/site/_upload.py +++ b/pywikibot/site/_upload.py @@ -318,12 +318,9 @@
try: data = req.submit()['upload'] - self.site._uploaddisabled = False except APIError as error: # TODO: catch and process foreseeable errors - if error.code == 'uploaddisabled': - self.site._uploaddisabled = True - elif error.code == 'stashfailed' \ + if error.code == 'stashfailed' \ and 'offset' in error.other: # TODO: Ask MediaWiki to change this # ambiguous error code. @@ -455,15 +452,7 @@ if not result: request['watch'] = self.watch request['ignorewarnings'] = ignore_all_warnings - try: - result = request.submit() - self.site._uploaddisabled = False - except APIError as error: - # TODO: catch and process foreseeable errors - if error.code == 'uploaddisabled': - self.site._uploaddisabled = True - raise error - result = result['upload'] + result = request.submit()['upload'] pywikibot.debug(result)
if 'result' not in result: diff --git a/tests/site_tests.py b/tests/site_tests.py index 354e4dc..04178a9 100755 --- a/tests/site_tests.py +++ b/tests/site_tests.py @@ -901,12 +901,12 @@ 'wikidatatest': { 'family': 'wikidata', 'code': 'test', - 'enabled': False, + 'disabled': True, }, 'wikipediatest': { 'family': 'wikipedia', 'code': 'test', - 'enabled': True, + 'disabled': False, } }
@@ -915,10 +915,7 @@ def test_is_uploaddisabled(self, key): """Test is_uploaddisabled().""" site = self.get_site(key) - if self.sites[key]['enabled']: - self.assertFalse(site.is_uploaddisabled()) - else: - self.assertTrue(site.is_uploaddisabled()) + self.assertEqual(site.is_uploaddisabled(), self.sites[key]['disabled'])
class TestSametitleSite(TestCase):
pywikibot-commits@lists.wikimedia.org