Page MenuHomePhabricator

ItemPage.exists() throws pywikibot.exceptions.IsRedirectPage
Open, HighPublic

Description

The normal exists() is defined as:

def exists(self):
    """Return True if page exists on the wiki, even if it's a redirect.

    If the title includes a section, return False if this section isn't
    found.

    @rtype: bool
    """
    return self.site.page_exists(self)

In ItemPage.exists() the code is this:

def exists(self):
    """
    Determine if an entity exists in the data repository.

    @rtype: bool
    """
    if not hasattr(self, '_content'):
        try:
            self.get()
            return True
        except pywikibot.NoPage:
            return False
    return 'lastrevid' in self._content

So if you do this on an existing item that is a redirect you'll get a nice exception. Probably easiest to just update it like this:

except pywikibot.IsRedirectPage
    return True

Some tests would be nice to verify this keeps working.

Event Timeline

Xqt triaged this task as High priority.Sep 18 2016, 3:54 PM
Xqt added a subscriber: Xqt.

Do you have a test sample?

Xqt claimed this task.Sep 18 2016, 6:04 PM

Change 311337 had a related patch set uploaded (by Xqt):
[bugfix] ItemPage.exists() should return True for redirects

https://gerrit.wikimedia.org/r/311337

Do you have a test sample?

Judging from your patchset, you found one. If an item exists, but redirects to a deleted item, than it should probably still give true for exists(), right?

Xqt added a comment.Sep 21 2016, 5:33 AM

Yes because get() is successful.

Dalba closed this task as Resolved.Nov 17 2016, 12:54 PM

Change 311337 merged by jenkins-bot:
[bugfix] ItemPage.exists() should return True for redirects

https://gerrit.wikimedia.org/r/311337

Pasleim reopened this task as Open.Nov 18 2016, 8:58 AM
Pasleim added a subscriber: Pasleim.

This patch causes that PropertyPage.exists() throws an error.

Magul added a subscriber: Magul.Nov 18 2016, 9:53 AM

@Pasleim are we talking about something like this?

>>> import pywikibot
>>> s = pywikibot.Site('en', 'wikipedia')
>>> r = s.data_repository()
>>> p = pywikibot.PropertyPage(r, 'P38')
>>> p.exists()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pywikibot/page.py", line 3457, in exists
    self.get(get_redirect=True)
  File "pywikibot/page.py", line 4263, in get
    'PropertyPage.get only implements "force".')
NotImplementedError: PropertyPage.get only implements "force".

FYI, on my not yet updated 6b8c01d, g7574, 2016/11/10, 09:32:07, n/a :

mdammers@bingo:~/pywikibot$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pywikibot
>>> s = pywikibot.Site('en', 'wikipedia')
>>> r = s.data_repository()
>>> p = pywikibot.PropertyPage(r, 'P38')
>>> p.exists()
True
>>>

Maybe go back to my plan A?:

if not hasattr(self, '_content'):
    try:
        self.get()
        return True
     except pywikibot.IsRedirectPage
         return True
    except pywikibot.NoPage:
        return False
return 'lastrevid' in self._content

Yes, this will also return True if the redirect is to a deleted page, but that's quite an extreme edge case and if you follow a redirect you should do your checks again anyway for the target.

We should probably add tests to " TestPropertyPage" to test exists().

@Multichill or maybe we should accept get_redirect in PropertyPage.get and just ignore it there (see: 1)?

Change 322253 had a related patch set uploaded (by Dalba):
WikibasePage.exists: Don't call self.get(get_redirect=True) for PropertyPage

https://gerrit.wikimedia.org/r/322253

Change 322385 had a related patch set uploaded (by Magul):
Remove unnecessary NotImplementedErrors

https://gerrit.wikimedia.org/r/322385

@Dalba I have upload new patch here. @jayvdb and @Legoktm I'm really interested what do You think about these changes here?

matej_suchanek removed a project: good first bug.
matej_suchanek moved this task from Backlog to Data loading problems on the Pywikibot-Wikidata board.
matej_suchanek added a subscriber: matej_suchanek.
D3r1ck01 moved this task from Backlog to Needs Review on the Pywikibot board.Nov 5 2018, 11:29 AM
Xqt added a comment.Sep 13 2019, 7:57 AM

See also T107537 for Flow

Xqt removed Xqt as the assignee of this task.Sep 13 2019, 7:57 AM