Page MenuHomePhabricator

Incorrect base_url when multiple wikis of the same family are used in a script
Closed, ResolvedPublicBUG REPORT

Description

Steps to reproduce

  1. Create scripts/userscripts/watchlists_core.py with the following content:
import pywikibot

commons = pywikibot.Site(url='https://commons.wikimedia.org')
print('Commons base_url: %s' % commons.base_url('/w/api.php'))
meta = pywikibot.Site(url='https://meta.wikimedia.org')
print('Meta base_url: %s' % meta.base_url('/w/api.php'))
  1. Run the script (e.g. ./pwb.py base_url_repro).

Actual result

Commons base_url: https://commons.wikimedia.org/w/api.php
Meta base_url: https://commons.wikimedia.org/w/api.php

Expected result

Commons base_url: https://commons.wikimedia.org/w/api.php
Meta base_url: https://meta.wikimedia.org/w/api.php

Software version

Latest master of Pywikibot (aa816795099f6eb6d7b381b0b8081c87c20507b9).

Other information

Notice the Meta base_url: in the actual result, it points to Commons. If I swap the two pairs of lines, both start to point to Meta. Clearly some caching is in the play, which may be triggered by the instantiation from URLs rather than from family/code pairs (in the real script I’m working on, I have access only to URLs and database names like commonswiki, so using family/code pairs is not an option).

Event Timeline

The usual way to create a site object is like this:

>>> import pywikibot
>>> commons = pywikibot.Site('commons')
>>> meta = pywikibot.Site('meta')

Then you get the right base_url:

>>> print('Commons base_url:', commons.base_url('/w/api.php'))
Commons base_url: https://commons.wikimedia.org/w/api.php

>>> print('Meta base_url:', meta.base_url('/w/api.php'))
Meta base_url: https://meta.wikimedia.org/w/api.php

Using the url does not work:

>>> commons = pywikibot.Site(url='https://commons.wikimedia.org')
>>> commons
APISite('wikimedia', 'wikimedia')
>>> commons.family
Family("wikimedia")
>>> meta = pywikibot.Site(url='https://meta.wikimedia.org')
>>> meta
APISite('wikimedia', 'wikimedia')
>>> meta.family
Family("wikimedia")

Seems that Family.from_url(url) gives no result and AutoFamily is used here. AutoFamily is good for foreign sites:

>>> mysite = pywikibot.Site(url='https://lobbypedia.de')
>>> mysite
APISite('lobbypedia', 'lobbypedia')
>>> mysite.family
Family("lobbypedia")
>>> mysite.base_url('/w/api.php')
'https://lobbypedia.de/w/api.php'
Xqt triaged this task as Medium priority.Feb 18 2025, 8:33 AM
Xqt changed the task status from Open to In Progress.Feb 18 2025, 5:08 PM

Change #1121419 had a related patch set uploaded (by Xqt; author: Xqt):

[pywikibot/core@master] bugfix: early return the site.code in Family.from_url if no parsed.path is found

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

Change #1121419 merged by jenkins-bot:

[pywikibot/core@master] [bugfix] Enable url without api, requests or script path in Site constuctor

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