Page MenuHomePhabricator

User.isAnonymous() gives wrong result for IP ranges
Open, MediumPublic

Description

>>> import pwb, pywikibot as py
>>> s = py.Site()
>>> u = py.User(s, '192.168.5.4')
>>> u.isAnonymous()
True
>>> u.isRegistered()
False
>>> u = py.User(s, '192.168.5.4/16')
>>> u.isRegistered()
False
>>> u.isAnonymous()
False
>>> u.username
u'192.168.5.4/16'
>>>

Event Timeline

Change 288915 had a related patch set uploaded (by Xqt):
[bugfix] fix isAnonymous result for ip ranges

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

Xqt triaged this task as Medium priority.

I suspect that you're calling this a bug because some log entries types have IP ranges, but the bug is that logentries.py creates a User class for them. A page.User object is a subclass of page.Page (with an implicit User: namespace), so it only reasonable if it can have real page named User:..... Anything else is not a page.User object.

Yes I agree it is a problem because "user:212.65.1.102/16" may be a valid subpage of "user:212.65.1.102" and is not a range. On the other hand we need some sort of tools to handle IP ranges coming from log entries which acts like a user for some methods. No idea yet.

Change 288915 abandoned by Xqt:
[pywikibot/core@master] [bugfix] fix isAnonymous result for ip ranges

Reason:
Not an appropriate patch

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

My 2 cents:
Pywikibot does not have the mission to correct ambigiousness of MediaWiki.

u.isAnonymous() should first examine isinstance(u, pywikibot.User).
If it is instantiated as Page, it should act as Page.
If it is instantiated as User, we should check the string after /. If it is a valid range (ie. isnumeric() and is between 1 and 32 for IPv4, and the appropriate range for IPv6), then handle it as user, and return True. Otherwise not.
Bot owners have to have the knowledge to handle this properly. If somebody works with users in a script, and does not know, what he is doing, let him blame himself.

I think a numeric-titled subpage of an anon user page is very rare, and much less likely than a CIDR range.