Page MenuHomePhabricator

Expose subpage information in the client-side API
Open, Needs TriagePublicFeature

Description

In Scribunto Lua modules, you have access to information about a subpage and its context through properties of Title objects.

  • rootText: If this is a subpage, the title of the root page without prefixes. Otherwise, the same as title.text.
  • baseText: If this is a subpage, the title of the page it is a subpage of without prefixes. Otherwise, the same as title.text.
  • subpageText: If this is a subpage, just the subpage name. Otherwise, the same as title.text.
  • isSubpage: Whether this title is a subpage of some other title.

It would be nice if similar information was made available to user scripts and Gadgets.

At a minimum it would be very good if the information from the breadcrumb navigation on a subpage was made available to JavaScript, but most of the Lua Title properties would make working with subpages easier.

if (mw.config.get("wgIsSubpage")) {
  const path   = mw.config.get("wgSubpagePath");
  const parent = mw.config.get("wgBasePage");
  const root   = mw.config.get("wgRootPage");
  if (parent === root) {
    // Add "[[${parent}]]" to the navigation template the user is currently editing
  } else {
    for (const segment of path) {
      // Walk the path upwards adding "../" as needed
    }
  }
}

With lots of caveats you can kinda fake it today by counting slashes in wgTitle, but since MediaWiki subpage structure is non-deterministic that comes with severe caveats. Figuring out the subpage structure from wgTitle on [[9/11 Commission Report/Notes/Part 1]] requires hitting the Action API three times to see which of those slash-separated segments of the page title corresponds to a wikipage that exists (if you create the page "9" everything else becomes a subpage of it; if "9" is deleted the first slash just becomes a regular character in the page title). The breadcrumb navigation reflects this so the information is available somewhere near the front end around the time a page is being rendered.

Alternately or additionally, mediawiki.Title would be a lot more useful for this if it had the same properties and methods as the Lua Title library (including an active method to check for existence, rather than an unused store cf. T184953).

const title = new mw.Title(mw.config.get("wgTitle"));
if (title.exists()) { // Triggers Action API call behind the scenes
  const path   = title.pathComponents; // Property? Or method?
  const parent = title.basePage;
  const root   = title.rootPage;
  if (parent === root) {
    // Add "[[${parent}]]" to the navigation template the user is currently editing
  } else {
    for (const segment of path) {
      // Walk the path upwards adding "../" as needed
    }
  }
}

This is a general need on Wikisource where subpages are used extensively in the main content namespaces (e.g. "War and Peace/Volume 1/Part 2/Chapter 3" or "9/11 Commission Report/Notes/Part 1"). The immediate use case I had was a Gadget to preload a navigation template when creating new content pages, with relative paths ("../") to the root page and an intermediate page (sometimes the two are the same, sometimes not) pre-filled in the navigation template arguments. It's ancient and does a lot of iffy counting of slashes, calling XMLHttpRequest to the page with &raw to check whether it exists (so it can tell whether it is a subpage or a page with a slash in the title), etc. With some way to get at the above subpage info I could cut out a lot of really grotty code, and with a real .exists() method I could avoid a lot of boilerplate API calls (call query with no props and depend on the returned JSON to have a missing property for missing pages, since there's no API that has the explicit semantics of checking for page existence).