Page MenuHomePhabricator

Page.canBeEdited useless as caller must escalate privilege level to edit
Closed, ResolvedPublic

Description

With a sysopname set for test:test, canBeEdited() returns True, but editing the page does not work unless the sysop account is explicitly logged in.

$ python3 pwb.py shell
Welcome to the Pywikibot interactive shell!
>>> import pywikibot
>>> s = pywikibot.Site('test', 'test')
>>> p = pywikibot.Page(s, 'User:John Vandenberg/test protected')
>>> p.canBeEdited()
True
>>> p.text = 'Testing 1 2 3'
>>> p.save()
Logging in to test:test as JVbot
Traceback (most recent call last):
  File "pywikibot/site.py", line 4163, in editpage
    result = req.submit()
  File "pywikibot/data/api.py", line 1734, in submit
    raise APIError(**result['error'])
pywikibot.data.api.APIError: protectedpage: The "editprotected" right is required to edit this page

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.4/code.py", line 90, in runcode
    exec(code, self.locals)
  File "<console>", line 1, in <module>
  File "pywikibot/tools/__init__.py", line 949, in wrapper
    return obj(*__args, **__kw)
  File "pywikibot/page.py", line 1051, in save
    **kwargs)
  File "pywikibot/page.py", line 1075, in _save
    raise err
  File "pywikibot/page.py", line 1062, in _save
    watch=watchval, bot=botflag, **kwargs)
  File "pywikibot/site.py", line 1075, in callee
    return fn(self, *args, **kwargs)
  File "pywikibot/site.py", line 4183, in editpage
    raise self._ep_errors[err.code](page)
pywikibot.exceptions.LockedPage: Page [[test:User:John Vandenberg/test protected]] is locked.

None of these callers explicitly login as sysop, and two dont catch the error.

$ git grep canBeEdited
pywikibot/page.py:    def canBeEdited(self):
pywikibot/page.py:        if not self.canBeEdited():
scripts/blockpageschecker.py:        # if not page.canBeEdited():
scripts/blockpageschecker.py:        if not page.canBeEdited():
scripts/reflinks.py:                if not page.canBeEdited():
scripts/replace.py:                if not page.canBeEdited():
scripts/upload.py:                if potential_file_page.canBeEdited():

$ grep login scripts/blockpageschecker.py scripts/reflinks.py scripts/replace.py scripts/upload.py
scripts/replace.py:    site.login()
scripts/upload.py:        self.targetSite.login()

$ grep LockedPage scripts/blockpageschecker.py scripts/reflinks.py scripts/replace.py scripts/upload.py
scripts/blockpageschecker.py:                    except pywikibot.LockedPage:
scripts/replace.py:                except pywikibot.LockedPage:

Event Timeline

jayvdb raised the priority of this task from to High.
jayvdb updated the task description. (Show Details)
jayvdb added a project: Pywikibot.
jayvdb subscribed.
Restricted Application added subscribers: Aklapper, Unknown Object (MLST). · View Herald TranscriptMay 9 2015, 2:16 AM

reflinks is ok as it uses ignore_save_related_errors

and

Category.change_category catches PageSaveRelatedError

so only upload is a problem

Xqt claimed this task.
Xqt subscribed.

Tested:

>>> import pywikibot
>>> s = pywikibot.Site('test', user='None')
>>> p = pywikibot.Page(s, 'User:John Vandenberg/test protected')
>>> p.canBeEdited()
WARNING: <pyshell#32>:1: FutureWarning: pywikibot.page.BasePage.canBeEdited is deprecated for 23 months and 8 days; use Page.has_permission('edit') instead.

False
>>> s.username()
'None'
>>> s.user()

>>> s.logged_in()
False

Already solved.

Xqt removed Xqt as the assignee of this task.Jan 11 2022, 4:34 PM