手册:界面/JavaScript

From mediawiki.org
This page is a translated version of the page Manual:Interface/JavaScript and the translation is 90% complete.

MediaWiki:Common.js 包含将为所有用户加载的 JavaScript。 在搜索框中輸入「MediaWiki:Common.js」, 然後系統會告訴您該頁面是否不存在。如果該頁不存在,你需要有權限(user rights )才能創建它。 有的頁面僅影響特定skins 的用戶(請參閱下文)。

如果 $wgAllowUserJs 被設為真,用戶可以通過在某些用戶子頁面中創建和導入個人腳本來為自己定製界面。 Extension:小工具 讓網站管理員和用戶更容易共享 JavaScript ,請參閱 Gadget kitchen

本頁將涵蓋可以使用的狀態和配置變數。 有關常用的 JavaScript API ,請參見 JSDuck documentation

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变量,但其他名称则无关。
  • 除了此列表中的名称外,许多扩展还设置了附加的JavaScript配置变量,前缀为“wg”。

这些值可以通过mw.config访问,记录在ResourceLoader/Core modules中。

站点范围

名稱 类型 描述 可使用情況 Example output
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+ 0
skin String 当前使用的皮肤的内部名称。 1.18MW <1.7 (monobook skin)
MW 1.8+: all skins and pages
vector
stylepath String 皮肤根目录的完整URL,包含样式表和皮肤特定的脚本。 该路径不包含皮肤子目录,并且不会以“/”结尾。 1.18MW <1.7 (monobook skin)
MW 1.8+: all skins and pages
/w/skins
wgArticlePath String 从根开始的引用文章的本地路径,包含一个“$1”占位符,该占位符可以被页面标题替换,以获得该页面的有效URL。 给定有效的页面标题title,可以使用wgArticlePath.replace('$1', title)来构造有效的URL。 另请参阅$wgArticlePath 1.08MW 1.8+ /wiki/$1
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+ en
wgContentNamespaces 整数数组 被MediaWiki视为“内容命名空间”的命名空间的ID。 相当于$wgContentNamespaces 配置变量的值,如果还没有,则包括0 1.23MW 1.23+ (git #3d87e3a8) [100, 102, 104, 106, 0]
wgDBname String wiki数据库的名称。 1.13MW 1.13+ mediawikiwiki
wgWikiID String wiki标识符。 应该优先于wgDBname 1.34MW 1.34+ (d45baf7f0734) mediawikiwiki
wgExtensionAssetsPath String 用于扩展静态资产的根路径(例如images)。 附加“/”,然后附加扩展名,以获取给定扩展的根路径。 1.18MW 1.18+ (r82247) /w/extensions
wgFormattedNamespaces Object 提供从命名空间ID到本地化命名空间名称的映射。 对于每个名称空间,对象都有一个条目,该条目以字符串化的名称空间编号为键,名称空间名称为值。 别名或规范名称不包括在内。 1.16MW 1.16+ {0: '', 1: 'Talk:', 2: 'User', ... }
wgNamespaceIds Object 提供从命名空间名称到命名空间ID的映射。 对于每个名称空间名称(包括本地化名称和规范名称以及别名),对象都有一个条目,该条目将名称空间名称作为关键字,将名称空间ID作为其整数值。 键都是小写的,空格用下划线代替。 1.16MW 1.16+ {media: -2, special: -1, "": 0, talk: 1, user: 2, ... }
wgScript String 主访问点脚本的完整路径,从根开始,包括带扩展名的完整脚本名称。 在WMF Wiki上,通常为“/w/index.php”。 另请参阅$wgScript 1.11MW 1.11+ /w/index.php
wgScriptPath String wgScript的路径部分,后面没有“/”。 这是用于直接调用php访问点(如index.phpapi.php)的路径。 另请参阅$wgScriptPath 1.08MW 1.8+ /w
wgServer String 服务器URL,未以“/”结尾。 例如,组合wgServer + wgScriptPath + "/api.php"导致API访问点脚本的有效URL。 1.08MW 1.8+ //www.mediawiki.org
wgServerName String The name of the server, without the protocol or trailing slash (e.g. "en.wikipedia.org"). 1.24MW 1.24+ (72c0ce43a854) www.mediawiki.org
wgSiteName String 站点的名称,由$wgSitename 定义。 1.16MW 1.16+ MediaWiki
wgVariantArticlePath String or false 如果wiki有语言变体(如中文和塞尔维亚语Wikipedias),则为wgContentLanguage以外的语言变体设置一个从根开始的路径。 路径包含两个占位符:“$1”将被页面标题替换,“$2”将由语言变体的语言代码替换(例如“zh-tw”)。 如果wiki没有语言变体,请设置为false。 另请参阅$wgVariantArticlePath 1.13MW 1.13+ false
wgVersion String 标识为页面提供服务的MediaWiki的版本。 1.12MW 1.12+ 1.42.0-wmf.26


所有页面(特定于用户/页面)

名稱 类型 描述 可利用性 Example output
wgAction String 执行的操作,例如,编辑页面为“edit”,页面视图为“view”。 请参阅Manual:Parameters to index.php#Actions MW 1.10+ view
wgArticleId Integer 页面的内部ID(页面ID)。对于不存在的页面和特殊页面,它为零。 MW 1.8+ 17317
wgCanonicalNamespace String 页面的规范名称(即未本地化或别名)namespace MW 1.8+ Manual
wgCanonicalSpecialPageName 字符串或false或未定义 在特殊页面上,特殊页面的规范名称(即非本地化或别名);否则,它根本没有定义(至少1.15MW(含1.15MW))或设置为false(自1.16MW起)。 MW 1.9+ false
wgCategories Array of strings 页面所属的所有类别的列表。 这本质上是页面上显示的类别框的JavaScript版本(Monobook/Vvector中页面底部的灰色框)。 如果类别框未显示在当前页面上(编辑/查看历史记录时也是如此),wgCategories将是一个空数组。 MW 1.16+ ['JavaScript']
wgCurRevisionId Integer 提供页面时当前查看页面的最高修订ID。 也设置在差异和历史页面上;特殊页面为零。 MW 1.9+ 6400980
wgIsArticle boolean 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 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) true
wgIsRedirect boolean true,如果页面是使用#REDIRECT [[目标页面名称]]重定向到wiki页面。 其他任何东西(普通页面、特殊页面、大多数生成的页面等)都是false 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). 1.16MW 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 整数 当前查看的修订的修订ID,或diff视图的正确修订(但diffonly=yes时为0,工單T231744)。 也设置在不同的页面上;零表示特殊页面、历史页面或其他不适用的页面。 MW 1.22+ git #7fa7b71e 6400980
wgSearchType 字符串或unset 用于执行搜索请求的搜索后端的名称。 MW 1.23+ (git #118655) CirrusSearch
wgTitle 字符串 页面标题,不包含命名空间。 可以包含空格–不包含下划线。 要获得包含名称空间的标题,请使用wgPageName MW 1.8+ Interface/JavaScript

与当前查看页面的用户相关:

名稱 类型 描述 可利用性 Example output
wgUserEditCount 整数 当前用户所做的编辑次数(如果未登录则为null)。 1.21MW 1.21+ 556
wgUserGroups 字符串数组 一个数组,包含当前用户所属的所有(本地)用户组,对于未登录的用户,则为null。 用户组由内部用户组名称标识,例如"sysop"、"autoconfirmed"、"bureaucrat"等。 默认用户组名为"*"。 MW 1.10+ ['autopatrolled', '*', 'user', 'autoconfirmed']
wgUserId 整数 当前用户的数字标识符(如果未登录是空(null)) 1.21MW 1.21+ 12311063
wgUserLanguage 字符串 在Special→Preferences中设置的用户界面语言的语言代码(可能被URL中的uselang=参数覆盖)。 1.08MW 1.8+ en
wgUserName 字符串 查看当前页面的用户的用户名(如果是已登录用户)。 对于未登录的用户则为null(“不是”用户的IP地址,与服务器上的PHP $wgUser->getName()不同)。 MW 1.8+ Your Username
wgUserRegistration 整数 当前用户注册的时间和日期,表示为自纪元以来的毫秒数。 如果未登录,则为Null。 1.21MW 1.21+ 1514273653000


一些页面

有些额外的变量仅取决于页面命名空间、wiki配置和/或用户设置。

名称 类型 描述 可利用性
Main Page
wgIsMainPage 布尔值 如果当前页面是wiki的主页,则为true。 否则完全省略(在mw.config中默认为null)。 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)

页面指定(Wikibase扩展)

当网站支持Wikibase 时,将提供一些附加信息:

名称 类型 描述 可利用性
wgWikibaseItemId 字符串 当前页面的Wikibase条目ID。 目前在编辑模式下此值未定义。 另请参阅phab:T185437 ?

提供给扩展的钩子

另请参阅