Page MenuHomePhabricator

ns parameter of Page object and/or defaultnamespace of Link object should force the namespace
Open, HighPublic

Description

ns parameter of Page object and/or defaultnamespace of Link object should force the Namespace instead of add a Namespace if no one is given.

Especially for categories but also for other namespaces it is possible to name pages which looks like a Namespace e.g. "Kategory:Wikipedia:Hilfe".

site = pywikibot.Site()
pywikibot.Category(pywikibot.Link("Wikipedia:Hilfe"), defaultNamespace=14) or
pywikibot.Category(site, "Wikipedia:Hilfe", ns=14)

fails with an exception error

pywikibot.Page(site, "Wikipedia:Hilfe", ns=14) gives
Page("Wikipedia:Hilfe") instead of the category page as expected.

There are several scripts which fix that point by adding a "category:" in front of a page title which should be obsolete.

See https://gerrit.wikimedia.org/r/#/c/132781/ for example

Event Timeline

bzimport raised the priority of this task from to Needs Triage.Nov 22 2014, 3:24 AM
bzimport set Reference to bz65262.
bzimport added a subscriber: Unknown Object (????).

Change 148337 had a related patch set uploaded (by XZise):
[FEAT] Page: 'force_ns' argument in Page.init

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

Patch-For-Review

Xqt triaged this task as High priority.Mar 18 2016, 2:49 PM
This comment was removed by Dalba.

In my opinion the best way to solve this issue is to make Page(enwiki, 'category:foo', ns=14) return Page('Category:Category:foo'). In my experience, the user almost always knows if the title already contains namespace or not and should use ns only if it doesn't.

Changing the code is relatively easy but it would be a breaking change, and I'm guessing that it won't be approved.

Change 395154 had a related patch set uploaded (by Dvorapa; owner: Dvorapa):
[pywikibot/core@master] [bugfix] Don't handle category prefixes as iw shortcuts

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

Change 148337 abandoned by Dvorapa:
[FEAT] Page: 'force_ns' argument in Page.init

Reason:
In favor of https://gerrit.wikimedia.org/r/#/c/395154/

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

Dvorapa added a project: good first task.
Dvorapa moved this task from Backlog to Doing on the good first task board.
Dvorapa added a subscriber: Dvorapa.

Not actively working on it

Silently ignoring the namespace parameter causes problems. I'm writing a script that pulls results from a database query, passing page_namespace and page_title to the page constructor. This bug caused an issue when it came to a page that had a namespace prefix at the beginning of the title:

Python 3.7.7 (default, Mar 22 2020, 15:52:55) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pywikibot
>>> site = pywikibot.Site('en', 'wikipedia')
>>> expected = pywikibot.Page(site, 'Wikipedia:WP:Athlete is not exclusionary')
>>> print(expected)
[[wikipedia:en:Wikipedia:WP:Athlete is not exclusionary]]
>>> actual = pywikibot.Page(site, 'WP:Athlete is not exclusionary', ns=4)
>>> print(actual)
[[wikipedia:en:Wikipedia:Athlete is not exclusionary]]
>>> actual == expected
False

I would agree with @Dalba's solution, but using a force parameter is probably easier to implement without having to announce and deprecate. Wiki editors can be quite creative with their page names, and for all we know someone depends on this spacebar heating.

It is not as trivial as it sounds, have a look at CR comments for the path given above. I guess we only have namespace chains for categories. It is unusual for Project and Template namespaces and have never seen it for the others. Therefore namespace should be forced for categories only (currently it is done by 'Category:' + title construct) but the behaviour should be kept for the others.