Movatterモバイル変換


[0]ホーム

URL:


Jump to content
MediaWiki
Search

Manual:Interface/JavaScript

From mediawiki.org
<Manual:Interface
Translate this page
Languages:
MediaWiki and JavaScript
DeliveryJavaScript
Adding JS on-wiki
Extensions, skins and other modules
Further guidance
Category for all things JavaScript
Learning JavaScript
CORS
Broken scripts
Core development:coding conventions andunit testing
Technical docs on doc.wikimedia.org
v·d·e

MediaWiki:Common.js containsJavaScript that will be loaded for all users.Enter "MediaWiki:Common.js" into the search box of your wiki.It will tell you if the page doesn't exist; if so, and you have the necessaryuser rights, just create it with the code you need to execute.There are similar pages affecting only users of specificskins (see below).

If$wgAllowUserJs is set to true, users can customize the interface for only themselves by creating and importing personal scripts in certain user subpages.Extension:Gadgets makes it easier for site admins and users to share JavaScript, seeGadget kitchen.

This page covers the state and configuration variables available.For the general JavaScript API, see thegenerated documentation.

List of JavaScript pages

[edit]

Note that any scripts or gadgets won't load onSpecial:Preferences itself, or ifsafemode is used.

Global scripts

[edit]

Personal scripts

[edit]

Personal scripts are only available if$wgAllowUserJs is set totrue.

mw.config

[edit]

To facilitate JavaScript programming, the MediaWiki software exposes a number of configuration values in an object calledmw.config.

Most of these variables have awg prefix.
  • Their names were previously exposed as global JavaScript variables if$wgLegacyJavaScriptGlobals is true, so they use a prefix to avoid collisions.
  • A subset of these names correspond to $wg variables in PHP, but others are unrelated.
  • Besides the names in this list, many extensions also set additional JavaScript config variables prefixed with 'wg'.

These values can be accessed throughmw.config, documented atResourceLoader/Core modules.

Site-wide

[edit]
NameTypeDescriptionAvailabilityExample output
debugInteger (1.36+) / Boolean (-1.35)Since MediaWiki 1.36+,0 means debug mode is off, and a positive non-zero number means debug mode is on (e.g.1 or2).

In MediaWiki 1.35 and earlier,false andtrue were used (phab:T85805).

1.17MW 1.17+0
skinStringThe internal name of the currently used skin.1.18MW <1.7 (monobook skin)
MW 1.8+: all skins and pages
vector
stylepathStringFull URL to the root directory for skins, containing stylesheets and skin-specific scripts. The path does not contain the skin subdirectory, and is not terminated by a "/".1.18MW <1.7 (monobook skin)
MW 1.8+: all skins and pages
/w/skins
wgArticlePathStringLocal path, starting at the root, to reference articles, containing a "$1" placeholder that may be replaced by a page title to get a valid URL to that page. Given a valid page titletitle, a valid URL may be constructed usingwgArticlePath.replace('$1', title). See also$wgArticlePath.1.08MW 1.8+/wiki/$1
wgCaseSensitiveNamespacesArrayThe IDs of the namespaces treated as case-sensitive by MediaWiki. Determined by the values of them$wgCapitalLinks and$wgCapitalLinksOverrides configuration variables.1.18MW 1.18+ (r90234)[]
wgContentLanguageStringThe language code for the defaultcontent language of the wiki.1.08MW 1.8+en
wgContentNamespacesArray of integersThe IDs of the namespaces considered "content namespaces" by MediaWiki. Equivalent to the value of the$wgContentNamespaces configuration variable, with0 included if it is not already.1.23MW 1.23+ (git #3d87e3a8)[100, 102, 104, 106, 0]
wgDBnameStringThe name of the wiki's database.1.13MW 1.13+mediawikiwiki
wgWikiIDStringThe wiki identifier. Should be preferred overwgDBname.1.34MW 1.34+ (d45baf7f0734)mediawikiwiki
wgExtensionAssetsPathStringRoot path used for extension static assets (e.g. images). Append '/' then the name of the extension to get the root path for a given extension.1.18MW 1.18+ (r82247)/w/extensions
wgFormattedNamespacesObjectGives a mapping from namespace IDs to localized namespace names. For each namespace, the object has one entry that has the stringified namespace number as the key and the namespace name as its value. Aliases or canonical names are not included.1.16MW 1.16+{0: '', 1: 'Talk', 2: 'User', ... }
wgNamespaceIdsObjectGives a mapping from namespace names to namespace IDs. For each namespace name, including localized and canonical names as well as aliases, the object has one entry that has namespace name as the key and the namespace ID as its integer value. The keys are all lowercase, with spaces replaced by underscores.1.16MW 1.16+{media: -2, special: -1, "": 0, talk: 1, user: 2, ... }
wgScriptStringFull path to the main access point script, starting at the root, including the full script name with extension. On WMF wikis, normally "/w/index.php". See also$wgScript.1.11MW 1.11+/w/index.php
wgScriptPathStringThe path part ofwgScript, without trailing "/". This is the path to use for direct calls to the php access points such asindex.php orapi.php. See also$wgScriptPath.1.08MW 1.8+/w
wgServerStringThe server URL, not terminated by "/". The combinationwgServer + wgScriptPath + "/api.php", for instance, results in a valid URL to the API access point script.1.08MW 1.8+//www.mediawiki.org
wgServerNameStringThe name of the server, without the protocol or trailing slash (e.g. "en.wikipedia.org").1.24MW 1.24+ (72c0ce43a854)www.mediawiki.org
wgSiteNameStringThe name of the site, as defined by$wgSitename.1.16MW 1.16+MediaWiki
wgVariantArticlePathString orfalseIf a wiki has language variants (such as the Chinese and the Serbian Wikipedias), set to a path beginning at the root for language variants other thanwgContentLanguage. The path contains two placeholders: "$1" is to be replaced by the page title, and "$2" is to be replaced by the language code of the language variant (e.g. "zh-tw"). If the wiki does not have language variants, set tofalse. See also$wgVariantArticlePath.1.13MW 1.13+false
wgVersionStringIdentifies the version of MediaWiki that served the page.1.12MW 1.12+1.42.0-wmf.26


All pages (user/page-specific)

[edit]
NameTypeDescriptionAvailabilityExample output
wgActionStringThe action performed, e.g. "edit" for edit pages, or "view" for page views. SeeManual:Parameters to index.php#Actions.MW 1.10+view
wgArticleIdIntegerThe internal ID (page ID) of the page. For non-existent pages and special pages, it is zero.MW 1.8+17317
wgCanonicalNamespaceStringThe canonical (i.e., not localized or aliased)namespace name of the page.MW 1.8+Manual
wgCanonicalSpecialPageNameString orfalse or not definedOn special pages, the canonical (i.e., not localized or aliased) name of the special page; otherwise it is set tofalse (since MW 1.16; previously not defined at all in that case).MW 1.9+false
wgCategoriesArray of stringsThe list of all the categories a page belongs to. This is essentially a JavaScript version of the category box shown on the page (grey box at bottom of page, in Monobook/Vector). If the category box is not shown on the current page (as is the case when editing/viewing history),wgCategories will be an empty array.
This is not available in MobileFrontend, regardless of the advanced mode being on or off.
MW 1.16+['JavaScript']
wgCurRevisionIdIntegerThe top revision ID of the currently viewed page at the time the page was served. Also set on diff and history pages; zero for special pages.MW 1.9+6400980
wgIsArticlebooleantrue if the page displays the content of a wiki page, e.g. when viewing a page (regardless of namespace), or when viewing an old revision or diff with rendered content below it. It isfalse for anything else (edit form, history page, special pages, most generated pages, etc.).
This variable is badly named – it is not related to a page being a main namespace "article".
MW 1.8+true
wgIsProbablyEditablebooleanTrue if the page is probably editable (based onTitle::quickUserCan) by the current user. The 'probably' is necessary for performance reasons. An exact editability check is too costly here, due to cascading protection and hook-based extensions likeTitleBlacklist that may be enabled. If this is true, it is likely to be editable. If it is false, it is definitely not editable.(git #0bbc3589)true
wgIsRedirectbooleantrue if the page is a redirect to a wiki page using#REDIRECT [[Target page name]]. It isfalse for anything else (normal pages, special pages, most generated pages, etc.).MW 1.22+ (gerrit:75478)false
wgNamespaceNumberIntegerThe number of thenamespace the page is in.MW 1.8+100
wgPageContentLanguageStringLanguage code of thepage content language (according to$context->getTitle()->getPageLanguage())MW 1.19+ (r104483)en
wgPageContentModelString'wikitext' on typical wiki pages,'javascript' on pages interpreted as JavaScript,'css' on pages interpreted as CSS,'Scribunto' on pages interpreted as Scribunto (Lua).1.16MW 1.22+ (gerrit:62178)wikitext
wgPageNameStringThe full name of the page, including the localized namespace name, if the namespace has a name (the main namespace (number 0) doesn't), and with spaces replaced by underscores. To get only the title without the namespace, usewgTitle.MW 1.8+Manual:Interface/JavaScript
wgPageParseReportObjectParser limit report for the page when parser data is available. Includes data about parset limits, Lua statistics when Scribunto extension is enabled and parser cache information.MW 1.28+ (git #316207){limitreport: {...}, scribunto: {...}, cachereport: {...}}
wgRedirectedFromStringWhen redirected contains the title of the page we were redirected from. If the page was not redirected, the value is omitted entirely (absent inmw.config). Uses the same format aswgPageName.MW 1.19+ (r104668)null
wgRelevantPageNameStringThe full name of the page to which content actions and navigation links (e.g. a skin's tabs) apply. The AJAX watch function uses this to work correctly on special pages such asSpecial:MovePage andSpecial:WhatLinksHere.MW 1.19+ (r113737)Manual:Interface/JavaScript
wgRelevantUserNameString or unsetThe relevant name of the user to which content actions and some extra navigation links (e.g. link to user rights or user contributions) apply.MW 1.23+ (git #88773)null
wgRelevantPageIsProbablyEditablebooleanLikewgIsProbablyEditable, but applied to the contextually relevant page name fromwgRelevantPageName instead of strictly the current page being viewed. For example, when viewing a page "Special:MovePage/Example" this will indicate whether the subject page is editable.MW 1.30+ (git #dbfe9c99)true
wgRestrictionEditArray of strings or unsetIf the page is editable at all (and is not a special page) and editing of the page is restricted to some user groups, the array contains the minimum user group a user must be in in order to edit the page. For semi-protected pages, it'd contain["autoconfirmed"]; for fully protected pages["sysop"]. If there are no explicit restrictions, the value is[] (an array with no elements).

This array contains onlyexplicit protections. Namespace-wide protections (e.g. MediaWiki namespace,$wgNamespaceProtection), cascading protections, or "protections" brought about by theTitleBlacklist extension's "noedit" attribute, are ignored by this array. On such pages, the value is normally[], unless additional protections have been applied specifically to that page.

If the page does not exist, the variable is not set.

MW 1.14+[]
wgRestrictionMoveArray of stringsIf the page is movable at all (and is not a special page) and moving of the page is restricted to some user groups, the array contains the minimum user group a user must be in in order to move the page. For semi-moveprotected pages, it'd contain["autoconfirmed"]; for fully moveprotected pages["sysop"]. If there are no explicit restrictions, the value is[] (an array with no elements).

This array contains onlyexplicit protections. Namespace-wide protections (e.g. MediaWiki namespace,$wgNamespaceProtection), cascading protections, or "protections" brought about by theTitleBlacklist extension's "moveonly" attribute, are ignored by this array. On such pages, the value is normally[], unless additional protections have been applied specifically to that page.

MW 1.14+[]
wgRevisionIdIntegerThe revision ID of the currently viewed revision, or the right revision for diff views (But 0 when diffonly=yes,T231744). Also set on diff pages; zero for special pages, history pages, or anywhere else inapplicable.MW 1.22+git #7fa7b71e6400980
wgSearchTypeString or unsetThe name of the search backend used to execute search requests.MW 1.23+ (git #118655)CirrusSearch
wgTitleStringThe page title, without the namespace. May contain spaces – does not contain underscores. To get the title including the namespace, usewgPageName.MW 1.8+Interface/JavaScript

Related to the user currently viewing the page:

NameTypeDescriptionAvailabilityExample output
wgUserEditCountIntegerThe number of edits the current user made (null if not logged in).1.21MW 1.21+556
wgUserGroupsArray of stringsAn array containing all the (local) user groups the current user is a member of, ornull for non-logged-in users. User groups are identified by the internal user group names, e.g. "sysop", "autoconfirmed", "bureaucrat", and so on. The default user group is named "*".MW 1.10+['autopatrolled', '*', 'user', 'autoconfirmed']
wgUserIdIntegerThe numeric ID of the current user (null if not logged in).1.21MW 1.21+12311063
wgUserLanguageStringThe language code for the user's interface language, as set in Special→Preferences (which may be overridden by auselang= parameter in the URL).1.08MW 1.8+en
wgUserNameStringThe user name of the user currently viewing the page, if it's a logged-in user. For non-logged-in users, it isnull (not the user's IP address, unlike PHP$wgUser->getName() on the server).MW 1.8+Your Username
wgUserRegistrationIntegerThe time and date on which the current user registered, represented as milliseconds since epoch. Null if not logged in.1.21MW 1.21+1514273653000


Some pages

[edit]

Some additional variables are present only depending on the page namespace, wiki configuration and/or user preferences.

NameTypeDescriptionAvailability
Main Page
wgIsMainPageBooleantrue if the current page is the main page of the wiki. Omitted entirely otherwise (defaulting tonull inmw.config).MW 1.18+
If the content language has variants:
wgUserVariantStringIf the wiki has language variants, the language code of the user's preferred variant. If the wiki does not have variants, the variable is not configured (does not exist), i.e.:
mw.config.exists('wgUserVariant');// false
mw.config.get('wgUserVariant');// null.
MW 1.16+
After saving an edit
wgPostEditString"saved" if the user just saved this page. "created" if the user just created this page. "restored" if the user just restored this page by going to the history page, clicked on a timestamp link for an old revision, clicked on edit and then saved.null otherwise. Note that:
  1. On null edits, this isnull, not "saved"
  2. When using "undo", this is "saved"
  3. On rollback, this isnull
MW 1.21+ (gerrit:50480)
When comparing revisions
wgDiffOldIdIntegerRevision ID of the "old" revision when viewing a diff. Only available when viewing a revision comparison.MW 1.30+ (git #a469795e)
wgDiffNewIdIntegerRevision ID of the "new" revision when viewing a diff. Only available when viewing a revision comparison.MW 1.30+ (git #a469795e)

Page specific (Wikibase extension)

[edit]

Some additional information will be available when the site supportsWikibase:

NameTypeDescriptionAvailability
wgWikibaseItemIdStringThe Wikibase item ID of the current page. Currently, in the edit mode, this variable is not set. Seephab:T185437.?

Hooks for extensions

[edit]


See also

[edit]
Retrieved from "https://www.mediawiki.org/w/index.php?title=Manual:Interface/JavaScript&oldid=7735006"
Category:

[8]ページ先頭

©2009-2025 Movatter.jp