MediaWiki Developer's Handbook/Add JavaScript/Predefined variables

From Wikibooks, open books for an open world
Jump to navigation Jump to search

URL constructors and server information[edit | edit source]

var wgScript = "/w/index.php";
var wgArticlePath = "/wiki/$1":
var wgServer = "//";
var wgCookiePrefix = "enwikibooks";

Using wgServer and wgScript, you can build a valid URL for the wiki. This is most useful for diffs and other action links.

To go to a page on Wikimedia wikis, you know that will take you where you want to go, but not all wikis have short URLs - in that case, use wgArticlePath to know how you construct an article path, or just use wgServer and wgScript.

Variable wgArticlePath can be used by replacing $1 by the page title: wgArticlePath.replace(/$1/,"Main Page").

Page information[edit | edit source]

var wgCanonicalNamespace = "";
var wgCanonicalSpecialPageName = false;
var wgNamespaceNumber = 0;
var wgPageName = "MediaWiki_Developer\'s_Handbook/Add_JavaScript";
var wgTitle = "MediaWiki Developer\'s Handbook/Add JavaScript";
var wgAction = "view";
var wgIsArticle = true;
var wgCanonicalNamespace = "Special";
var wgCanonicalSpecialPageName = "Specialpages";
var wgNamespaceNumber = -1;
var wgPageName = "Special:SpecialPages";
var wgTitle = "SpecialPages";

The canonical namespace never changes based on localization. For example, on kmwiki, their meta or project namespace has wgCanonicalNamespace == "Project", and on km:ជំនួយ:Editing, wgCanonicalNamespace == "Help".

As well, each namespace has a number, beginning with zero for the main namespace. These numbers are customizable.

This is helpful to make scripts portable - you don't have to worry about knowing the localized name for a namespace - just use the canonical name, or the number.

Since this page is in the main namespace, wgPageName == wgTitle, but on Wikibooks:About, that is not the case:

var wgPageName = "Help:About";
var wgTitle = "About";

wgTitle never includes the namespace prefix, while wgPageName does. Note also that wgPageName replaces spaces with underscores.

wgAction is one of:

  • view
  • submit (after clicking Save page, Show preview, or Show changes)
  • history
  • edit
  • delete
  • protect
  • purge (when doing ?action=purge to purge the server cache, forcing the page to be re-rendered)

This exposes what the user is doing on the current page. Be careful: to the user, view, submit and purge are probably not differentiated in many cases. In some cases, you will want your script to run on all of those.

wgAction is often useful to avoid running your whole script when it isn't needed. If your script changes the page history form, then you can do

if ( wgAction == 'history' ) {
	addOnloadHook( changeHistoryForm() );
function changeHistoryForm() {

wgIsArticle tells you whether the page is an article as opposed to a special page.

Special Pages[edit | edit source]

var wgCanonicalNamespace = "Special";
var wgCanonicalSpecialPageName = "Specialpages";
var wgNamespaceNumber = -1;
var wgPageName = "Special:SpecialPages";
var wgTitle = "SpecialPages";

For special pages, the canonical namespace is "Special", and wgCanonicalSpecialPageName is the non-CamelCase name of the special page in English. Again, the canonical form doesn't change with localization, so it's better to use that. As well, all special pages have the namespace number -1.

User information[edit | edit source]

var wgUserName = "Mike.lifeguard";
var wgUserGroups = ["bureaucrat", "checkuser", "editor", "sysop", "*", "user", "autoconfirmed"];
var wgGlobalGroups = ["Global_rollback", "steward"];

The user's username is set as wgUserName, and their local and global groups are in arrays. Note that the global group global rollback has an underscore, not a space.

This is useful when part of a script requires that the user be in a certain user group, typically sysop:

function userIsInGroup( group ) {
	// are they in a given local group?
	if ( wgUserGroups ) {
		if ( !group || group.length == 0 ) {
			group = '*';
		for(var i in wgUserGroups)
			if (wgUserGroups[i]==group) return true;
	return false;
if ( userIsInGroup( 'sysop' ) ) {