Manual:Interface/JavaScript/ja

MediaWiki:Common.js にはすべての利用者がアクセスした際に読み込まれる JavaScript が書かれています。 利用しているウィキの検索ボックスに「MediaWiki:Common.js」を入力してください。 ページが存在しない場合はページが存在しないと表示されます。その場合でかつ必要な利用者権限を持っている場合は、実行する必要のあるコードを書いたページを作成してください。 また、特定の外装を使用している利用者のみに影響する類似のページ群があります (下記参照)。

$wgAllowUserJs を true に設定すると、利用者のある下位ページに個人用スクリプトを作って書き込んでおくことにより、利用者は自分だけのインターフェイスをカスタマイズできます。 Extension:Gadgets は、サイト管理者や利用者が JavaScript をより簡単に共有できるようにします (ガジェット キッチン を参照してください)。

このページでは状態と設定に関する利用可能な変数について説明します。 一般的な JavaScript API については、生成された説明文書を参照してください。

JavaScript ページ一覧

Note that any scripts or gadgets won't load on Special:Preferences itself, or if safemode is used.

グローバル スクリプト

個人スクリプト

$wgAllowUserJs trueに設定されている場合のみ利用可能です。

mw.config

JavaScript プログラミングを容易にするために、MediaWiki ソフトウェアは mw.config と呼ばれるオブジェクト内のいくつかの構成値を公開します。

ほとんどの変数には wg というプレフィックスが付けられています。
  • 以前は $wgLegacyJavaScriptGlobals が true の場合に、これらの名前がJavaScriptのグローバル変数として定義されていたため、衝突を防ぐためにプレフィックスが用いられています。
  • この名前の一部分は、PHPの$wg変数に対応していますが、それ以外は無関係です。
  • このリスト上にある名前意外にも、多くの拡張機能では「wg」をプレフィックスとする追加のJavaScript設定変数が定義されています。

これらの値には ResourceLoader/Core modules に記載されている mw.config を通じてアクセスすることができます。

サイト全体

名前 説明 利用可能バージョン 出力例
debug 整数 (1.36+) / 真偽値 (-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 or 2).

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

MW 1.17+ 0
skin 文字列 現在使用されている外装の内部名。 MW <1.7 (monobook skin)
MW 1.8+: all skins and pages
vector
stylepath 文字列 スタイルシートと外装固有のスクリプトを含む、外装のルート ディレクトリへの完全な URL。 パスには外装の下位ディレクトリが含まれておらず、末尾に「/」がありません。 MW <1.7 (monobook skin)
MW 1.8+: all skins and pages
/w/skins
wgArticlePath 文字列 記事を参照するためのルートから始まるローカル パス。ページへの有効な URL を取得するために、ページ名に置換される「$1」プレースホルダーを含みます。 有効なページ名が title の場合、有効な URL は wgArticlePath.replace('$1', title) を使用して作成できます。 $wgArticlePath も参照のこと。 MW 1.8+ /wiki/$1
wgCaseSensitiveNamespaces 配列 MediaWiki によって大文字/小文字が区別されるものとして扱われる名前空間の ID。 $wgCapitalLinks および $wgCapitalLinksOverrides 構成変数の値によって決定されます。 MW 1.18+ (r90234) []
wgContentLanguage String The language code for the default content language of the wiki. MW 1.8+ en
wgContentNamespaces 整数の配列 MediaWikiが「コンテンツ名前空間 (content namespaces)」とみなす、すべての名前空間ID。 $wgContentNamespaces 変数で設定された値と等しくなる(存在しない場合は 0 を含む)。 MW 1.23+ (git #3d87e3a8) [100, 102, 104, 106, 0]
wgDBname String ウィキのデータベースの名前。 MW 1.13+ mediawikiwiki
wgWikiID String ウィキの識別子。 wgDBname よりも優先される。 MW 1.34+ (d45baf7f0734) mediawikiwiki
wgExtensionAssetsPath 文字列 拡張機能の静的アセット(例:images)に用いられるルートパス。 特定の拡張機能のルートパスを取得する場合は、拡張機能の名前と「/」を末尾に追加する。 MW 1.18+ (r82247) /w/extensions
wgFormattedNamespaces オブジェクト 名前空間IDからローカライズされた名前空間名へのマッピングを提供する。 オブジェクトにはそれぞれの名前空間ごとに、文字列形式の名前空間IDをキーとした、名前空間名を値として持つエントリを持つ。 エイリアスや既定の名前は含まれない。 MW 1.16+ {0: '', 1: 'Talk:', 2: 'User', ... }
wgNamespaceIds オブジェクト 名前空間名から名前空間IDへのマッピングを提供する。 オブジェクトにはそれぞれの名前空間(ローカライズされた名前、既定の名前、エイリアスを含む)ごとに、名前空間名をキーとした、整数値の名前空間IDを値として持つエントリを持つ。 キーはすべて小文字で、スペースはアンダースコアに置き換えられる。 MW 1.16+ {media: -2, special: -1, "": 0, talk: 1, user: 2, ... }
wgScript 文字列 メインアクセスポイントへのフルパス。ルートから始まり、拡張子を含む完全なスクリプト名を含む。 ウィキメディア財団のウィキでは、通常「/w/index.php」に設定されている。 $wgScript も参照のこと。 MW 1.11+ /w/index.php
wgScriptPath 文字列 「/」を除いた wgScript のパス部分。 これは index.phpapi.php などの、PHPアクセスポイントへの直接呼び出しに用いられるパスである。 $wgScriptPath も参照のこと。 MW 1.8+ /w
wgServer 文字列 「/」で終了しないサーバーのURL。 例えば、wgServer + wgScriptPath + "/api.php"の組み合わせは、APIアクセスポイントスクリプトへの有効なURLとなります。 MW 1.8+ //www.mediawiki.org
wgServerName String プロトコルや末尾のスラッシュを除いたサーバーの名前(例:en.wikipedia.org)。 MW 1.24+ (72c0ce43a854) www.mediawiki.org
wgSiteName String $wgSitename で定義されたサイトの名前。 MW 1.16+ MediaWiki
wgVariantArticlePath 文字列またはfalse 多言語版のウィキ(中国語版やセルビア語版のウィキペディアなど)が存在する場合は、wgContentLanguage 以外の言語版のルートから始まるパスが設定される。 パスには2つのプレースホルダーが含まれる。「$1」はページ名に、「$2」はその言語版の言語コード(例:zh-tw)に置き換えられる。 ウィキが他の言語版を持たない場合は、false が設定される。 $wgVariantArticlePath も参照のこと。 MW 1.13+ false
wgVersion 文字列 ページを提供したMediaWikiのバージョン識別子。 MW 1.12+ 1.42.0-wmf.26

全ページ (利用者/ページ固有)

名前 説明 利用可能バージョン 出力例
wgAction 文字列 実行された操作。例えば、ページを編集した場合は「edit」、閲覧中の場合は「view」。 Manual:index.php のパラメーター#操作も参照。 MW 1.10+ view
wgArticleId 整数 ページの内部ID(Page ID)。存在しないページと特別ページは 0 を返す。 MW 1.8+ 17317
wgCanonicalNamespace 文字列 そのページの既定の名前空間名(ローカライズされた名前やエイリアスではない名前空間名)。 MW 1.8+ Manual
wgCanonicalSpecialPageName 文字列、false、または未定義 特別ページの既定の名前(ローカライズされた名前やエイリアスではない名前)。特別ページ以外の場合は false が設定される(MW 1.16以降、これより前は未定義となっていた)。 MW 1.9+ false
wgCategories Array of strings そのページが属する全てのカテゴリの一覧を返す。 これはページ内に表示されるカテゴリボックス(モノブックやベクターではページ下部に表示される灰色のボックス)のJavaScript版である。 ページ上にカテゴリボックスが表示されていない場合(履歴を表示/編集中など)、wgCategories は空配列となる。
This is not available in MobileFrontend, regardless of the advanced mode being on or off.
MW 1.16+ ['JavaScript']
wgCurRevisionId 整数 The 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
wgIsArticle 真偽値 true 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 is false 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
wgIsProbablyEditable 真偽値 True if the page is probably editable (based on Title::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 like TitleBlacklist 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
wgIsRedirect boolean true if the page is a redirect to a wiki page using #REDIRECT [[Target page name]]. It is false for anything else (normal pages, special pages, most generated pages, etc.). MW 1.22+ (gerrit:75478) false
wgNamespaceNumber Integer The number of the namespace the page is in. MW 1.8+ 100
wgPageContentLanguage String Language code of the page content language (according to $context->getTitle()->getPageLanguage()) MW 1.19+ (r104483) en
wgPageContentModel String 'wikitext' on typical wiki pages, 'javascript' on pages interpreted as JavaScript, 'css' on pages interpreted as CSS, 'Scribunto' on pages interpreted as Scribunto (Lua). MW 1.22+ (gerrit:62178) wikitext
wgPageName String The 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, use wgTitle. MW 1.8+ Manual:Interface/JavasScript
wgPageParseReport Object Parser 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: {...}}
wgRedirectedFrom String When redirected contains the title of the page we were redirected from. If the page was not redirected, the value is omitted entirely (absent in mw.config). Uses the same format as wgPageName. MW 1.19+ (r104668) null
wgRelevantPageName String The 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 as Special:MovePage and Special:WhatLinksHere. MW 1.19+ (r113737) Manual:Interface/JavaScript
wgRelevantUserName String or unset The 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
wgRelevantPageIsProbablyEditable boolean Like wgIsProbablyEditable, but applied to the contextually relevant page name from wgRelevantPageName 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
wgRestrictionEdit Array of strings or unset If 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 only explicit protections. Namespace-wide protections (e.g. MediaWiki namespace, $wgNamespaceProtection), cascading protections, or "protections" brought about by the TitleBlacklist 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+ []
wgRestrictionMove Array of strings If 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 only explicit protections. Namespace-wide protections (e.g. MediaWiki namespace, $wgNamespaceProtection), cascading protections, or "protections" brought about by the TitleBlacklist 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+ []
wgRevisionId 整数 The 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 #7fa7b71e 6400980
wgSearchType 文字列または未定義 検索リクエストを実行するために使用される検索バックエンドの名前。 MW 1.23+ (git #118655) CirrusSearch
wgTitle 文字列 名前空間を除いたページ名。 スペースが含まれる(アンダースコアは含まれない)。 名前空間を含めたページ名を取得する場合は wgPageName を使用する。 MW 1.8+ Interface/JavaScript

ページを閲覧中の利用者に関連する内容:

名前 説明 利用可能バージョン Example output
wgUserEditCount 整数 現在の利用者の編集回数(ログインしていない場合はnull)。 MW 1.21+ 556
wgUserGroups 文字列の配列 An array containing all the (local) user groups the current user is a member of, or null 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']
wgUserId 整数 The numeric ID of the current user (null if not logged in). MW 1.21+ 12311063
wgUserLanguage 文字列 The language code for the user's interface language, as set in SpecialPreferences (which may be overridden by a uselang= parameter in the URL). MW 1.8+ en
wgUserName 文字列 The user name of the user currently viewing the page, if it's a logged-in user. For non-logged-in users, it is null (not the user's IP address, unlike PHP $wgUser->getName() on the server). MW 1.8+ Your Username
wgUserRegistration 整数 現在の利用者が登録した日時(エポックから経過したミリ秒で表される)。 ログインしていない場合はnull。 MW 1.21+ 1514273653000


一部のページ

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

名前 説明 利用可能バージョン
Main Page
wgIsMainPage 真偽値 true if the current page is the main page of the wiki. Omitted entirely otherwise (defaulting to null in mw.config). MW 1.18+
他の言語の内容が存在する場合:
wgUserVariant 文字列 If 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+
編集を保存した後
wgPostEdit 文字列 "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 is null, not "saved"
  2. When using "undo", this is "saved"
  3. On rollback, this is null
MW 1.21+ (gerrit:50480)
When comparing revisions
wgDiffOldId Integer Revision ID of the "old" revision when viewing a diff. Only available when viewing a revision comparison. MW 1.30+ (git #a469795e)
wgDiffNewId Integer Revision ID of the "new" revision when viewing a diff. Only available when viewing a revision comparison. MW 1.30+ (git #a469795e)

ページ固有 (ウィキベース)

ウィキベースをサポートするサイトで利用できる追加情報:

名前 説明 利用可能バージョン
wgWikibaseItemId 文字列 現在のページのウィキベース項目ID。 現時点では、編集中はこの変数は設定されない。 phab:T185437 を参照のこと。 ?

拡張機能向けのフック


関連項目

Category:JavaScript/ja
Category:JavaScript/ja