How to get the current Skin's file path in Mediawiki?

718 Views Asked by At

In MediaWiki skin definitions, the BaseTemplate that gets extended has several attributes for creating links to other pages in the wiki, but I've got a situation where I need the path to the skin's directory, to pull some images used to create the UI. By default, that would just be /skins/mySkin/images/foo.png, by default but if someone changes the $wgStylePath variable, or renames the skin, that would be an issue. Is there an existing variable that has that URL build out, hidden somewhere in the BaseTemplate methods?

2

There are 2 best solutions below

1
On

Yes, the SkinTemplate class, which contains the code to set up the template variables before executing the template, provides access to $wgStylePath via the 'stylepath' template variable.

When you subclass SkinTemplate to define your skin's main class, you are also expected to override the $stylename member variable, which specifies the subdirectory under which your skin's own stylesheets and images reside. (This would usually be the same as the name of your skin in lower case, but it doesn't have to be; it's perfectly fine to have, say, two related skins using images from the same directory.) This is also made available as a template variable, surprisingly named 'stylename'. So one way to specify an image path in your template would be something like:

<?php $this->text('stylepath') ?>/<?php $this->text('stylename') ?>/images/foo.png

Another way, (formerly) used e.g. by the Vector skin, is to use the getSkinStylePath() method from the Skin class (which is the superclass of SkinTemplate; it's kind of messy and tangled for historical reasons, but basically you can pretty much treat them as one class split into two files).

Update: As of MediaWiki 1.36, getSkinStylePath() has been deprecated. The recommended alternative, according to the release notes, is to "replace usages with the direct path to the resources."

To use it, you pass in the name of the file as a parameter, and the method automatically prepends $wgStylePath and $stylename to it (and appends $wgStyleVersion as a query string). Note that this is not a template method, so you have to escape and print the returned URL yourself:

<?php echo htmlspecialchars( $this->getSkin()->getSkinStylePath( 'images/foo.png' ) ) ?>

There's also a getCommonStylePath() method which does exactly the same thing, except that it uses the string "common" instead of $stylename.

0
On

Apparently this is the new way:

$this->getSkin()->getConfig()->get( 'StylePath' ) . '/SkinName/images/foo.png';

Source: https://phabricator.wikimedia.org/T270754