jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/675446 )
Change subject: [IMPR] Remove code duplication of Site.delete and Site.undelete ......................................................................
[IMPR] Remove code duplication of Site.delete and Site.undelete
- Site.deletepage and Site.deleteoldimage uses the same code. Drop both implementations and merge the code to Site.delete() method. - Site.undeletepage and Site.undelete_file_versions uses the same code. Drop both implementations and merge the code to Site.undelete() method. - update site_tests accordingly.
Change-Id: I43e1d45566fc8f67e8352b062b44608106afce17 --- M pywikibot/page/__init__.py M pywikibot/site/_apisite.py M tests/site_tests.py 3 files changed, 89 insertions(+), 93 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/page/__init__.py b/pywikibot/page/__init__.py index 19fc629..1d3dea5 100644 --- a/pywikibot/page/__init__.py +++ b/pywikibot/page/__init__.py @@ -1736,7 +1736,7 @@ answer = 'y' self.site._noDeletePrompt = True if answer == 'y': - self.site.deletepage(self, reason) + self.site.delete(self, reason) return
else: # Otherwise mark it for deletion @@ -1861,7 +1861,7 @@ pywikibot.output('Undeleting %s.' % (self.title(as_link=True))) reason = pywikibot.input( 'Please enter a reason for the undeletion:') - self.site.undelete_page(self, reason, undelete_revs) + self.site.undelete(self, reason, revision=undelete_revs)
def protect(self, reason: Optional[str] = None, diff --git a/pywikibot/site/_apisite.py b/pywikibot/site/_apisite.py index 1756a15..f9233c9 100644 --- a/pywikibot/site/_apisite.py +++ b/pywikibot/site/_apisite.py @@ -2050,21 +2050,37 @@ } # other errors shouldn't occur because of pre-submission checks
@need_right('delete') - @deprecate_arg('summary', 'reason') - def deletepage(self, page, reason: str): - """Delete page from the wiki. Requires appropriate privilege level. + def delete(self, page, reason: str, *, oldimage: Optional[str] = None): + """Delete a page or a specific old version of a file from the wiki. + + Requires appropriate privileges.
@see: U{https://www.mediawiki.org/wiki/API:Delete%7D Page to be deleted can be given either as Page object or as pageid. + To delete a specific version of an image the oldimage identifier + must be provided. +
@param page: Page to be deleted or its pageid. @type page: L{pywikibot.page.BasePage} or, for pageid, int or str @param reason: Deletion reason. + @param oldimage: oldimage id of the file version to be deleted. + If a BasePage object is given with page parameter, it has to + be a FilePage. @raises TypeError, ValueError: page has wrong type/value. - """ + if oldimage and isinstance(page, pywikibot.page.BasePage) \ + and not isinstance(page, pywikibot.FilePage): + raise TypeError("'page' must be a FilePage not a '{}'" + .format(page.__class__.__name__)) + token = self.tokens['delete'] - params = {'action': 'delete', 'token': token, 'reason': reason} + params = { + 'action': 'delete', + 'token': token, + 'reason': reason, + 'oldimage': oldimage, + }
if isinstance(page, pywikibot.page.BasePage): params['title'] = page @@ -2086,8 +2102,8 @@ } if err.code in self._dl_errors: raise Error(self._dl_errors[err.code] % errdata) - pywikibot.debug("delete: Unexpected error code '%s' received." - % err.code, + pywikibot.debug('delete: Unexpected error code {!r} received.' + .format(err.code), _logger) raise else: @@ -2095,8 +2111,24 @@ finally: self.unlock_page(page)
- @need_right('delete') - def deleteoldimage(self, page, oldimage, reason: str): + @deprecate_arg('summary', 'reason') + @deprecated('delete()', since='20210330', future_warning=True) + def deletepage(self, page, reason: str): + """Delete page from the wiki. Requires appropriate privilege level. + + @see: U{https://www.mediawiki.org/wiki/API:Delete%7D + Page to be deleted can be given either as Page object or as pageid. + + @param page: Page to be deleted or its pageid. + @type page: L{pywikibot.page.BasePage} or, for pageid, int or str + @param reason: Deletion reason. + @raises TypeError, ValueError: page has wrong type/value. + """ + self.delete(page, reason) + + @deprecated('delete() with oldimage keyword parameter', since='20210330', + future_warning=True) + def deleteoldimage(self, page, oldimage: str, reason: str): """Delete a specific version of a file. Requires appropriate privileges.
@see: U{https://www.mediawiki.org/wiki/API:Delete%7D @@ -2104,23 +2136,37 @@ provided.
@param page: Page to be deleted or its pageid - @type page: Page or, in case of pageid, int or str + @type page: FilePage or, in case of pageid, int or str @param oldimage: oldimageid of the file version to be deleted. @param reason: Deletion reason. @raises TypeError, ValueError: page has wrong type/value. + """ + self.delete(page, reason, oldimage=oldimage)
+ @need_right('undelete') + def undelete(self, page, reason: str, *, revisions=None, fileids=None): + """Undelete page from the wiki. Requires appropriate privilege level. + + @see: U{https://www.mediawiki.org/wiki/API:Undelete%7D + + @param page: Page to be deleted. + @type page: pywikibot.BasePage + @param reason: Undeletion reason. + @param revisions: List of timestamps to restore. + If None, restores all revisions. + @type revisions: list + @param fileids: List of fileids to restore. + @type fileids: list """ token = self.tokens['delete'] - params = {'action': 'delete', 'oldimage': oldimage, - 'token': token, 'reason': reason} - - if isinstance(page, pywikibot.Page): - params['title'] = page - msg = page.title(with_section=False) - else: - pageid = int(page) - params['pageid'] = pageid - msg = pageid + params = { + 'action': 'undelete', + 'title': page, + 'reason': reason, + 'token': token, + 'timestamps': revisions, + 'fileids': fileids, + }
req = self._simple_request(**params) self.lock_page(page) @@ -2129,24 +2175,22 @@ except api.APIError as err: errdata = { 'site': self, - 'title': msg, + 'title': page.title(with_section=False), 'user': self.user(), } if err.code in self._dl_errors: raise Error(self._dl_errors[err.code] % errdata) - pywikibot.debug("delete: Unexpected error code '%s' received." - % err.code, + pywikibot.debug('undelete: Unexpected error code {!r} received.' + .format(err.code), _logger) raise - else: - page.clear_cache() finally: self.unlock_page(page)
- @need_right('undelete') @deprecate_arg('summary', 'reason') + @deprecated('undelete()', since='20210330', future_warning=True) def undelete_page(self, page, reason: str, revisions=None): - """Undelete page from the wiki. Requires appropriate privilege level. + """DEPRECATED. Undelete page from the wiki.
@see: U{https://www.mediawiki.org/wiki/API:Undelete%7D
@@ -2156,36 +2200,13 @@ If None, restores all revisions. @type revisions: list @param reason: Undeletion reason. - """ - token = self.tokens['delete'] - self.lock_page(page) + self.undelete(page, reason, revisions=revisions)
- req = self._simple_request(action='undelete', - title=page, - reason=reason, - token=token, - timestamps=revisions) - try: - req.submit() - except api.APIError as err: - errdata = { - 'site': self, - 'title': page.title(with_section=False), - 'user': self.user(), - } - if err.code in self._dl_errors: - raise Error(self._dl_errors[err.code] % errdata) - pywikibot.debug("delete: Unexpected error code '%s' received." - % err.code, - _logger) - raise - finally: - self.unlock_page(page) - - @need_right('undelete') + @deprecated('undelete() with fileids parameter', since='20210330', + future_warning=True) def undelete_file_versions(self, page, reason: str, fileids=None): - """Undelete page from the wiki. Requires appropriate privilege level. + """DEPRECATED. Undelete page from the wiki.
@see: U{https://www.mediawiki.org/wiki/API:Undelete%7D
@@ -2195,30 +2216,7 @@ @param fileids: List of fileids to restore. @type fileids: list """ - token = self.tokens['delete'] - self.lock_page(page) - - req = self._simple_request(action='undelete', - title=page, - reason=reason, - token=token, - fileids=fileids) - try: - req.submit() - except api.APIError as err: - errdata = { - 'site': self, - 'title': page.title(with_section=False), - 'user': self.user(), - } - if err.code in self._dl_errors: - raise Error(self._dl_errors[err.code] % errdata) - pywikibot.debug("delete: Unexpected error code '%s' received." - % err.code, - _logger) - raise - finally: - self.unlock_page(page) + self.undelete(page, reason, fileids=fileids)
_protect_errors = { 'noapiwrite': 'API editing not enabled on %(site)s wiki', diff --git a/tests/site_tests.py b/tests/site_tests.py index b6c42eb..1b81d5a 100644 --- a/tests/site_tests.py +++ b/tests/site_tests.py @@ -2137,28 +2137,28 @@ page=p1, reason='Pywikibot unit test')
def test_delete(self): - """Test the site.deletepage() and site.undelete_page() methods.""" + """Test the site.delete() and site.undelete() methods.""" site = self.get_site() p = pywikibot.Page(site, 'User:Unicodesnowman/DeleteTestSite') # Verify state if not p.exists(): - site.undelete_page(p, 'pywikibot unit tests') + site.undelete(p, 'pywikibot unit tests')
- site.deletepage(p, reason='pywikibot unit tests') + site.delete(p, reason='pywikibot unit tests') with self.assertRaises(pywikibot.NoPage): p.get(force=True)
- site.undelete_page(p, 'pywikibot unit tests', - revisions=['2014-12-21T06:07:47Z', - '2014-12-21T06:07:31Z']) + site.undelete(p, 'pywikibot unit tests', + revisions=['2014-12-21T06:07:47Z', + '2014-12-21T06:07:31Z'])
revs = list(p.revisions()) self.assertLength(revs, 2) self.assertEqual(revs[0].revid, 219995) self.assertEqual(revs[1].revid, 219994)
- site.deletepage(p, reason='pywikibot unit tests') - site.undelete_page(p, 'pywikibot unit tests') + site.delete(p, reason='pywikibot unit tests') + site.undelete(p, 'pywikibot unit tests') revs = list(p.revisions()) self.assertGreater(len(revs), 2)
@@ -2282,15 +2282,14 @@ break
if fileid is not None: - site.undelete_file_versions(fp, 'pywikibot unit tests', - fileids=[fileid]) + site.undelete(fp, 'pywikibot unit tests', fileids=[fileid])
# Delete the older version of file hist = fp.get_file_history() ts = pywikibot.Timestamp(2021, 3, 8, 2, 38, 57) oldimageid = hist[ts]['archivename']
- site.deleteoldimage(fp, oldimageid, 'pywikibot unit tests') + site.delete(fp, 'pywikibot unit tests', oldimage=oldimageid)
# Undelete the older revision of file gen = site.filearchive(start='T276725.png', end='T276725.pngg') @@ -2302,8 +2301,7 @@
self.assertIsNotNone(fileid)
- site.undelete_file_versions(fp, 'pywikibot unit tests', - fileids=[fileid]) + site.undelet(fp, 'pywikibot unit tests', fileids=[fileid])
class TestUsernameInUsers(DefaultSiteTestCase):