jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/570994 )
Change subject: [bugfix] Rewrite APISite.page_can_be_edited ......................................................................
[bugfix] Rewrite APISite.page_can_be_edited
site.py: - Add a new action parameter to page_can_be_edited which reflects wgRestrictionTypes. - raise a ValueError if an invalid action parameter is given - add a dict of special case protection levels and the needed edit right - the modified method also works for closed wikis
page.py: - rename "canBeEdited" method to "has_permission" and enable the action parameter to give a restiction type - deprecate the old method.
others: - update other scrips to use the new has_permissionmethod
Bug: T244604 Change-Id: If1eb697a1aadbbd8fe76ede6da01637363ecc5a0 --- M pywikibot/page.py M pywikibot/site.py M pywikibot/specialbots.py M scripts/blockpageschecker.py M scripts/reflinks.py M scripts/replace.py M tests/page_tests.py 7 files changed, 51 insertions(+), 42 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/page.py b/pywikibot/page.py index af90868..48ff258 100644 --- a/pywikibot/page.py +++ b/pywikibot/page.py @@ -1141,17 +1141,24 @@ p_types.remove('upload') return p_types
- def canBeEdited(self): - """ - Determine whether the page may be edited. + def has_permission(self, action='edit'): + """Determine whetherthe page can be modified.
- This returns True if and only if: - - page is unprotected, and bot has an account for this site, or - - page is protected, and bot has a sysop account for this site. + Return True if the bot has the permission of needed restriction level + for the given action type.
+ @param action: a valid restriction type like 'edit', 'move' + @type action: str @rtype: bool + + @raises ValueError: invalid action parameter """ - return self.site.page_can_be_edited(self) + return self.site.page_can_be_edited(self, action) + + @deprecated("Page.has_permission('edit')", since='20200208') + def canBeEdited(self): + """DEPRECATED. Determine whether the page may be edited.""" + return self.has_permission()
def botMayEdit(self): """ @@ -2163,7 +2170,7 @@ if cat not in cats: cats.append(cat)
- if not self.canBeEdited(): + if not self.has_permission(): pywikibot.output("Can't edit %s, skipping it..." % self.title(as_link=True)) return False diff --git a/pywikibot/site.py b/pywikibot/site.py index 0ad71fe..1371935 100644 --- a/pywikibot/site.py +++ b/pywikibot/site.py @@ -3115,23 +3115,35 @@ self.loadpageinfo(page) return page._protection
- def page_can_be_edited(self, page): - """ - Determine if the page can be edited. + def page_can_be_edited(self, page, action='edit'): + """Determine if the page can be modified.
- Return True if and only if: - - page is unprotected, and bot has an account for this site, or - - page is protected, and bot has a sysop account for this site. + Return True if the bot has the permission of needed restriction level + for the given action type.
+ @param page: a pywikibot.Page object + @type param: pywikibot.Page + @param action: a valid restriction type like 'edit', 'move' + @type action: str @rtype: bool + + @raises ValueError: invalid action parameter """ - rest = self.page_restrictions(page) - sysop_protected = 'edit' in rest and rest['edit'][0] == 'sysop' - try: - api.LoginManager(site=self, sysop=sysop_protected) - except NoUsername: - return False - return True + if action not in self.siteinfo['restrictions']['types']: + raise ValueError('{}.page_can_be_edited(): Invalid value "{}" for ' + '"action" parameter' + .format(self.__class__.__name__, action)) + prot_rights = { + '': action, + 'autoconfirmed': 'editsemiprotected', + 'sysop': 'editprotected', + 'steward': 'editprotected' + } + restriction = self.page_restrictions(page).get(action, ('', None))[0] + user_rights = self.userinfo['rights'] + if prot_rights.get(restriction, restriction) in user_rights: + return True + return False
def page_isredirect(self, page): """Return True if and only if page is a redirect.""" @@ -7580,16 +7592,6 @@ 'create': ('steward', 'infinity')} return page._protection
- def page_can_be_edited(self, page): - """Determine if the page can be edited.""" - rest = self.page_restrictions(page) - sysop_protected = 'edit' in rest and rest['edit'][0] == 'steward' - try: - api.LoginManager(site=self, sysop=sysop_protected) - except NoUsername: - return False - return True - def recentchanges(self, **kwargs): """An error instead of pointless API call.""" self._closed_error('No recent changes can be returned.') diff --git a/pywikibot/specialbots.py b/pywikibot/specialbots.py index 2bfbed1..2bd4cd1 100644 --- a/pywikibot/specialbots.py +++ b/pywikibot/specialbots.py @@ -3,7 +3,7 @@ """Library containing special bots.""" # # (C) Rob W.W. Hooft, Andre Engels 2003-2004 -# (C) Pywikibot team, 2003-2019 +# (C) Pywikibot team, 2003-2020 # # Distributed under the terms of the MIT license. # @@ -308,7 +308,7 @@ pywikibot.output( 'File exists and you asked to abort. Skipping.') return None - if potential_file_page.canBeEdited(): + if potential_file_page.has_permission(): if overwrite is None: overwrite = not pywikibot.input_yn( 'File with name %s already exists. ' diff --git a/scripts/blockpageschecker.py b/scripts/blockpageschecker.py index 0837ada..ec7488f 100755 --- a/scripts/blockpageschecker.py +++ b/scripts/blockpageschecker.py @@ -44,7 +44,7 @@ # (C) Monobi a.k.a. Wikihermit, 2007 # (C) Filnik, 2007-2011 # (C) Nicolas Dumazet (NicDumZ), 2008-2009 -# (C) Pywikibot team, 2007-2019 +# (C) Pywikibot team, 2007-2020 # # Distributed under the terms of the MIT license. # @@ -309,7 +309,7 @@ # FIXME: This check does not work : # PreloadingGenerator cannot set correctly page.editRestriction # (see bug T57322) - # if not page.canBeEdited(): + # if not page.has_permission(): # pywikibot.output( # "%s is sysop-protected : this account can't edit " # "it! Skipping..." % pagename) @@ -319,7 +319,7 @@ editRestr = restrictions['edit'] except KeyError: editRestr = None - if not page.canBeEdited(): + if not page.has_permission(): pywikibot.output('%s is protected: ' "this account can't edit it! Skipping..." % pagename) diff --git a/scripts/reflinks.py b/scripts/reflinks.py index a147fd6..85c0205 100755 --- a/scripts/reflinks.py +++ b/scripts/reflinks.py @@ -39,7 +39,7 @@ ¶ms; """ # (C) Nicolas Dumazet (NicDumZ), 2008 -# (C) Pywikibot team, 2008-2019 +# (C) Pywikibot team, 2008-2020 # # Distributed under the terms of the MIT license. # @@ -515,7 +515,7 @@ try: # Load the page's text from the wiki new_text = page.get() - if not page.canBeEdited(): + if not page.has_permission(): pywikibot.output("You can't edit page " + page.title(as_link=True)) continue diff --git a/scripts/replace.py b/scripts/replace.py index 1c30090..e3e9f01 100755 --- a/scripts/replace.py +++ b/scripts/replace.py @@ -137,7 +137,7 @@ """ # # (C) Daniel Herding, 2004-2012 -# (C) Pywikibot team, 2004-2019 +# (C) Pywikibot team, 2004-2020 # # Distributed under the terms of the MIT license. # @@ -728,7 +728,7 @@ try: # Load the page's text from the wiki original_text = page.get(get_redirect=True) - if not page.canBeEdited(): + if not page.has_permission(): pywikibot.output("You can't edit page " + page.title(as_link=True)) continue diff --git a/tests/page_tests.py b/tests/page_tests.py index 1b7cbf1..23c13c4 100644 --- a/tests/page_tests.py +++ b/tests/page_tests.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- """Tests for the page module.""" # -# (C) Pywikibot team, 2008-2019 +# (C) Pywikibot team, 2008-2020 # # Distributed under the terms of the MIT license. # @@ -464,7 +464,7 @@ r'use interwiki.page_empty_check(page) instead.'): self.assertIsInstance(mainpage.isEmpty(), bool) self.assertIsInstance(mainpage.isDisambig(), bool) - self.assertIsInstance(mainpage.canBeEdited(), bool) + self.assertIsInstance(mainpage.has_permission(), bool) self.assertIsInstance(mainpage.botMayEdit(), bool) self.assertIsInstance(mainpage.editTime(), pywikibot.Timestamp) self.assertIsInstance(mainpage.permalink(), basestring)
pywikibot-commits@lists.wikimedia.org