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?
How to get the current Skin's file path in Mediawiki?
726 Views Asked by MidnightLightning At
2
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: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).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:There's also a
getCommonStylePath()
method which does exactly the same thing, except that it uses the string"common"
instead of$stylename
.