Page MenuHomePhabricator

globalusage() fails if there is no local user
Open, LowPublicFeature

Description

I am using globalusage() in my code on File objects, such as usage = len(list(page.globalusage())). This fails and generates the following NoUsernameError. Noting that the error message made mention of en.wikiquote, I navigated to enwq in my browser while logged in, to automatically create a local account there. Then I reran the code, making no changes, and it ran as expected with no error. I suppose this happens because globalusage wants to create local page objects for all the results in whichever wiki they occur, so you need a local account.

This is overkill for my use case because I just wanted to count them anyway, and didn't need Page objects, but I couldn't find a better way to do it. Maybe PWB needs to create the local user first when you have an SUL account? (Or document that it is necessary and how it is done.) Or provide a method for counting the number without creating page objects, though that's perhaps a separate issue.

@PAWS:~/pageviews$ python generate.py --cat "Category:Media contributed by George W. Bush Presidential Center"
Starting data fetch for "Category:Media contributed by George W. Bush Presidential Center". (2023-08-29 20:22:23)
WARNING: API error mwoauth-invalid-authorization-invalid-user: The authorization headers in your request are for a user that does not exist here
Traceback (most recent call last):
  File "/home/paws/pageviews/generate.py", line 134, in <module>
    cat(args.cat)
  File "/home/paws/pageviews/generate.py", line 75, in cat
    usage = len(list(page.globalusage()))
  File "/srv/paws/lib/python3.10/site-packages/pywikibot/site/_extensions.py", line 224, in globalusage
    gu_site = pywikibot.Site(url=entry['url'])
  File "/srv/paws/lib/python3.10/site-packages/pywikibot/__init__.py", line 206, in Site
    code, fam = _code_fam_from_url(url, fam)
  File "/srv/paws/lib/python3.10/site-packages/pywikibot/__init__.py", line 117, in _code_fam_from_url
    code = family.from_url(url)
  File "/srv/paws/lib/python3.10/site-packages/pywikibot/family.py", line 608, in from_url
    site = pywikibot.Site(code, self.name)
  File "/srv/paws/lib/python3.10/site-packages/pywikibot/__init__.py", line 253, in Site
    _sites[key] = interface(code=code, fam=fam, user=user)
  File "/srv/paws/lib/python3.10/site-packages/pywikibot/site/_apisite.py", line 127, in __init__
    self.login(cookie_only=True)
  File "/srv/paws/lib/python3.10/site-packages/pywikibot/site/_apisite.py", line 387, in login
    raise e
  File "/srv/paws/lib/python3.10/site-packages/pywikibot/site/_apisite.py", line 378, in login
    if self.userinfo['name'] == self.user():
  File "/srv/paws/lib/python3.10/site-packages/pywikibot/site/_apisite.py", line 536, in userinfo
    uidata = uirequest.submit()
  File "/srv/paws/lib/python3.10/site-packages/pywikibot/data/api/_requests.py", line 974, in submit
    self._add_defaults()
  File "/srv/paws/lib/python3.10/site-packages/pywikibot/data/api/_requests.py", line 442, in _add_defaults
    and self.site.mw_version >= '1.25wmf5'):
  File "/srv/paws/lib/python3.10/site-packages/pywikibot/site/_apisite.py", line 1148, in mw_version
    mw_ver = MediaWikiVersion(self.version())
  File "/srv/paws/lib/python3.10/site-packages/pywikibot/site/_apisite.py", line 1121, in version
    version = self.siteinfo.get('generator', expiry=1).split(' ')[1]
  File "/srv/paws/lib/python3.10/site-packages/pywikibot/site/_siteinfo.py", line 304, in get
    preloaded = self._get_general(key, expiry)
  File "/srv/paws/lib/python3.10/site-packages/pywikibot/site/_siteinfo.py", line 242, in _get_general
    default_info = self._get_siteinfo(props, expiry)
  File "/srv/paws/lib/python3.10/site-packages/pywikibot/site/_siteinfo.py", line 167, in _get_siteinfo
    data = request.submit()
  File "/srv/paws/lib/python3.10/site-packages/pywikibot/data/api/_requests.py", line 1282, in submit
    self._data = super().submit()
  File "/srv/paws/lib/python3.10/site-packages/pywikibot/data/api/_requests.py", line 1079, in submit
    raise NoUsernameError(f'Failed {msg}')
pywikibot.exceptions.NoUsernameError: Failed OAuth authentication for wikiquote:en: The authorization headers in your request are for a user that does not exist here
CRITICAL: Exiting due to uncaught exception NoUsernameError: Failed OAuth authentication for wikiquote:en: The authorization headers in your request are for a user that does not exist here

Event Timeline

Xqt triaged this task as Low priority.Aug 30 2023, 5:29 AM
Xqt changed the subtype of this task from "Task" to "Feature Request".

Instantiation the APISite tries to login with stored cookies but it does not use autocreate [1] for logging in. There might be reasons for it; or should there be a config setting to enable it? A possible way to prevent this issue is to login first via login script: pwb login -all -autocreate -async [2] but I am not really sure whether this works for oauth login because this is quite different from client login.

[1] https://doc.wikimedia.org/pywikibot/master/api_ref/pywikibot.site.html#pywikibot.site._apisite.APISite.login
[2] https://doc.wikimedia.org/pywikibot/master/utilities/scripts.html#login-script

Instantiation the APISite tries to login with stored cookies but it does not use autocreate [1] for logging in. There might be reasons for it; or should there be a config setting to enable it? A possible way to prevent this issue is to login first via login script: pwb login -all -autocreate -async [2] but I am not really sure whether this works for oauth login because this is quite different from client login.

[1] https://doc.wikimedia.org/pywikibot/master/api_ref/pywikibot.site.html#pywikibot.site._apisite.APISite.login
[2] https://doc.wikimedia.org/pywikibot/master/utilities/scripts.html#login-script

I tried this, at least in the PAWS environment, but the behavior was odd:

@PAWS:~$ pwb login -all -autocreate -async
Logged in on commons:commons as DPLA bot.
ERROR: commons:* is not a valid site, please remove it from your user-config
ERROR: wikimedia:* is not a valid site, please remove it from your user-config
ERROR: wikipedia:* is not a valid site, please remove it from your user-config
ERROR: wikiquote:* is not a valid site, please remove it from your user-config
ERROR: wikisource:* is not a valid site, please remove it from your user-config
ERROR: wikiversity:* is not a valid site, please remove it from your user-config
ERROR: wikibooks:* is not a valid site, please remove it from your user-config
ERROR: wiktionary:* is not a valid site, please remove it from your user-config
ERROR: wikidata:* is not a valid site, please remove it from your user-config
ERROR: wikinews:* is not a valid site, please remove it from your user-config
ERROR: wikivoyage:* is not a valid site, please remove it from your user-config
WARNING: /srv/paws/lib/python3.10/site-packages/pywikibot/scripts/login.py:98: UserWarning: Site meta:meta instantiated using different code "*"
  site = pywikibot.Site(code, family)

Logged in on meta:meta as DPLA bot.
WARNING: /srv/paws/lib/python3.10/site-packages/pywikibot/scripts/login.py:98: UserWarning: Site mediawiki:mediawiki instantiated using different code "*"
  site = pywikibot.Site(code, family)

Logged in on mediawiki:mediawiki as DPLA bot.

Execution time: 1 seconds

I tried this, at least in the PAWS environment, but the behavior was odd:

Ah, this is about account settings. Asteriks setting is obviously not fully supported as expected. I think this should be a separate task because it is a malfunctioning behaviour.