Manual:インターフェイス/JavaScript

From mediawiki.org
Jump to navigation Jump to search
This page is a translated version of the page Manual:Interface/JavaScript and the translation is 48% complete.
Other languages:
Bahasa Indonesia • ‎Deutsch • ‎English • ‎dansk • ‎español • ‎français • ‎svenska • ‎עברית • ‎العربية • ‎فارسی • ‎अवधी • ‎中文 • ‎日本語

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

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

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

JavaScript ページ一覧

グローバル スクリプト

個人スクリプト

They're only available if $wgAllowUserJs is set to true.

mw.config

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

Most of these variables have a wg prefix.

  • For legacy reasons these names are 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 through mw.config, documented at ResourceLoader/Core modules.

サイト全体

名前 説明 利用可能バージョン
debug Integer (MW 1.36+) / Boolean (<= MW 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).

1.17MW 1.17+
skin String 現在使用されている外装の内部名。 「クラシック」外装は「標準」外装と呼ばれます。 1.18MW <1.7 (monobook skin)
MW 1.8+: all skins and pages
stylepath String スタイルシートと外装固有のスクリプトを含む、外装のルート ディレクトリへの完全な URL。 パスには外装の下位ディレクトリが含まれておらず、末尾に「/」がありません。 1.18MW <1.7 (monobook skin)
MW 1.8+: all skins and pages
wgArticlePath String 記事を参照するためのルートから始まるローカル パス。ページへの有効な URL を取得するために、ページ名に置換される「$1」プレースホルダーを含みます。 有効なページ名が title の場合、有効な URL は wgArticlePath.replace('$1', title) を使用して作成できます。 $wgArticlePath も参照してください。 1.08MW 1.8+
wgCaseSensitiveNamespaces Array MediaWiki によって大文字/小文字が区別されるものとして扱われる名前空間の ID。 $wgCapitalLinks および $wgCapitalLinksOverrides 構成変数の値によって決定されます。 1.18MW 1.18+ (r90234)
wgContentLanguage String The language code for the default content language of the wiki. 1.08MW 1.8+
wgContentNamespaces 整数の配列 The IDs of the namespaces considered "content namespaces" by MediaWiki. Equivalent to the value of the $wgContentNamespaces configuration variable, with 0 included if it is not already. 1.23MW 1.23+ (git #3d87e3a8)
wgDBname String ウィキのデータベースの名前。 1.13MW 1.13+
wgEnableAPI Boolean true if the MediaWiki API is enabled at all; false otherwise. 1.12MW 1.12+
wgEnableWriteAPI Boolean true if the MediaWiki write API is enabled at all; false otherwise. If wgEnableAPI && !wgEnableWriteAPI, only the read operations of the API are enabled. 1.12MW 1.12+
wgExtensionAssetsPath String Root 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)
wgFormattedNamespaces Object Gives 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+
wgNamespaceIds Object Gives a mapping from namespace names to namespace IDs. For each namespace name, including aliases, the object has one entry that has namespace name as the key and the namespace ID as its integer value. Canonical names are not included. The keys are all lowercase, with spaces replaced by underscores. 1.16MW 1.16+
wgScript String Full 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". $wgScript も参照してください。 1.11MW 1.11+
wgScriptPath String The path part of wgScript, without trailing "/". This is the path to use for direct calls to the php access points such as index.php or api.php. See also $wgScriptPath . 1.08MW 1.8+
wgServer String The server URL, not terminated by "/". The combination wgServer + wgScriptPath + "/api.php", for instance, results in a valid URL to the API access point script. 1.08MW 1.8+
wgSiteName String The name of the site, as defined by $wgSitename . 1.16MW 1.16+
wgVariantArticlePath String or false If 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 than wgContentLanguage. 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 to false. See also $wgVariantArticlePath . 1.13MW 1.13+
wgVersion String Identifies the version of MediaWiki that served the page. 1.12MW 1.12+

All pages (user/page-specific)

名前 説明 利用可能バージョン
wgAction String The action performed, e.g. "edit" for edit pages, or "view" for page views. See Manual:Parameters to index.php#Actions. MW 1.10+
wgArticleId Integer The internal ID of the page. For non-existent pages and special pages, it is zero. MW 1.8+
wgCanonicalNamespace String The canonical (i.e., not localized or aliased) namespace name of the page. MW 1.8+
wgCanonicalSpecialPageName String or false or not defined On special pages, the canonical (i.e., not localized or aliased) name of the special page; otherwise it is not defined at all (up to and including MW 1.15) or is set to false (since MW 1.16). MW 1.9+
wgCategories Array of strings The 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. MW 1.16+
wgCurRevisionId Integer 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+
wgIsArticle boolean true if the content displayed on the page is related to the source of the corresponding article on the wiki. So it is true when viewing a page (regardless of namespace), and also true when viewing a diff that has a rendered revision appended to the bottom of it. It is false for anything else (edit, history, special pages, most generated pages, etc.).
This variable is badly named – it has nothing really to do with a page being an "article" or not.
MW 1.8+
wgIsProbablyEditable boolean 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)
wgNamespaceNumber Integer The number of the namespace the page is in. MW 1.8+
wgPageContentLanguage String Language code of the page content language (according to $context->getTitle()->getPageLanguage()) MW 1.19+ (r104483)
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). 1.16MW 1.22+ (gerrit:62178)
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+
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)
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)
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)
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)
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
wgSearchType 文字列または未設定 The name of the search backend used to execute search requests. MW 1.23+ (git #118655)
wgTitle 文字列 The page title, without the namespace. May contain spaces – does not contain underscores. To get the title including the namespace, use wgPageName. MW 1.8+

Related to the user currently viewing the page:

名前 説明 利用可能バージョン
wgUserEditCount 整数 The number of edits the current user made (null if not logged in). 1.21MW 1.21+
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+
wgUserId 整数 The numeric ID of the current user (null if not logged in). 1.21MW 1.21+
wgUserLanguage 文字列 The language code for the user's interface language, as set in Special→Preferences (which may be overridden by a uselang= parameter in the URL). 1.08MW 1.8+
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+
wgUserRegistration 整数 The time and date on which the current user registered, represented as milliseconds since epoch. Null if not logged in. 1.21MW 1.21+

Some pages

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+
If the content language has variants:
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+
After saving an edit
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)

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

Some additional informations will be available when the site supports ウィキベース :

名前 説明 利用可能バージョン
wgWikibaseItemId 文字列 The Wikibase item ID of the current page. Currently, in the edit mode this variable is not set. phab:T185437 を参照してください。 ?

拡張機能向けのフック