Page MenuHomePhabricator

'wgServer' should reflect mobile site or provide a new configuration variable for current site
Closed, InvalidPublicBUG REPORT

Description

Steps to replicate the issue (include links if applicable):

This is regarding the JavaScript variable, mw.config.get('wgServer') which is used in user scripts. When running scripts on en.m.wikipedia.org (e.g., when a user script is running on a mobile phone), mw.config.get('wgServer') is "//en.wikipedia.org" rather than the expected "//en.m.wikipedia.org".

Here's an example from the console:

> location
< Location {href: "https://en.m.wikipedia.org/wiki/Main_Page", protocol: "https:", host: "en.m.wikipedia.org", hostname: "en.m.wikipedia.org", port: "", …}
> mw.config.get('wgServer');
< "//en.wikipedia.org"

What happens?:

I was using wgServer as part of comparing hrefs (on a Wikipedia page) in a user script to make sure I only match local pages. The code didn't work on mobile because lots of page links looked like links to elsewhere.

What I'm doing as a workaround for now is using window.location.origin because I want this code to work on both desktop and mobile.

What should have happened instead?:

It seems like wgServer should just be the current server. If there's a reason it needs to be the non-mobile site then perhaps there should be another variable that tells you the name of the current site, the one expected to be the destination for most links.

Software version (on Special:Version page; skip for WMF-hosted wikis like Wikipedia):

Current English Wikipedia.

Other information (browser name/version, screenshots, etc.):

Tested on Safari.

Event Timeline

DanielQ renamed this task from 'wgServer' is not current server when using mobile site to 'wgServer' should reflect mobile site or provide a new configuration variable for current site.Oct 18 2024, 8:15 PM

Most of the times, scripts don't need this as they can simply be root relative. In the rare cases where it is required (URL constructor for instance), MediaWiki core scripts indeed use window.location or document.location.

Most of the times, scripts don't need this as they can simply be root relative. In the rare cases where it is required (URL constructor for instance), MediaWiki core scripts indeed use window.location or document.location.

It seems like you're addressing a different use case than the one described in the bug report, creating URLs. The original issue is comparing hrefs within the document to determine if they are on the same server. Converting hrefs to root-relative paths before performing other operations is a related use case. Some scripts use wgServer for both of those use cases. Scripts that function correctly on desktop should also function correctly on mobile.

When is there a need to construct urls with wgServer? In JavaScript, HTML, and CSS (with some subtle differences) all URLs are already relative to the current document.

<!--
  When on https://en.wikipedia.org/example/
-->
<a href="./bar">    <!-- https://en.wikipedia.org/example/bar -->
<a href="/foo/bar"> <!-- https://en.wikipedia.org/foo/bar -->
// When on https://en.m.wikipedia.org/example/
// Fetches from https://en.m.wikipedia.org/w/api.php

fetch('/w/api.php');

Whether with wgArticlePath, or mw.util.getUrl, or mw.util.wikiScript, these values are all designed to be used in your HTML or JavaScript as-is.

If you need a full URL for some reason, eg if you're building a string that is going to be displayed to the user and printed in a PDF, then you'd want to use something like the browser built-in location.href and URL#toString (as new URL(myurl, location).toString(), to expand the URL. This is more stable and portable then manually stitching together two strings that aren't designed for that purpose. This also naturally takes care of values like ./ or ../, which you may encounter in Parsoid data.

Example of why concatenating wgServer yourself isn't designed for this purpose:

  • Consider that wgServer may be protocol-relative, which means you still don't have a full url.
  • Consider that mw.config keys like wgArticlePath, wgResourceBasePath, and others paths may include a scheme and hostname already (e.g. if a CDN domain is used, or if there's a non-canonical domain involved such as the auth subdomain for SUL3). So blind concatenation could produce an invalid URL in that case. E.g. you might end up with https://en.wikipedia.org/enwiki/wiki/Banana or some such instead of https://en.wikipedia.org/wiki/Banana or https://auth.wikimedia.org/enwiki/wiki/Banana.

In my experience, when I have encountered gadget code using location.origin or wgServer, it did so not because it is needed, but because the author didn't know how relative URLs work. To my knowledge, there are no documentation examples or MediaWiki tutorials that demonstrate using location.origin or wgServer. It isn't needed.

If it is needed, please let us know so that we can fix this in a way that works automatically for everyone (if possible). You should not have to workaround this in individual scripts.