帮助:开发者文档

Salt lovely留言 | 贡献2020年11月9日 (一) 20:56的版本 (创建页面,内容为“{{施工中|3小时}} {{不完整|一晚上就搬完不可能的}} {{帮助文档难度/困难}} {{浮动帮助菜单}} 考虑到MediaWiki翻译不完全以及部分…”)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
提示:此页面“帮助:开发者文档”正在被其他人编辑中
我们十分不推荐您现在立刻编辑这个页面!这会导致 编辑冲突,您和其他编辑者可能失去编辑数据
  • 这条信息是 User:Salt lovely留下的;
  • 用户 Salt lovely 预计用 持续3小时 完成本次编辑,而本页面的最后编辑时间距现在为
    • 如果计时器没有显示,请强制刷新页面。
    • 如果这个时间已经远远超过 3小时,您可移除此模板并进行您的编辑
  • 您可在 本页面的页面历史 中查看过往编辑版本。
这篇帮助文档的难度为:困难
这篇帮助文档比较复杂,适合资深编辑者阅读。阅读这篇帮助文档前,您需要熟练掌握Wiki知识,对复杂语法有一定的了解。

考虑到MediaWiki翻译不完全以及部分地区难以丝滑顺畅访问的情况,这里搬运MediaWiki核心及常用模块的文档以及示例代码,原文见此

为了方便查阅,本文与原文的格式与内容可能有较大差异。

mediaWiki

MediaWiki会在Wiki的所有页面创建一个名为“mediaWiki”的对象,亦可以使用简称“mw”来访问。

为了查阅与编写方便,本教程一般使用“mw”。

输出此对象,JavaScript代码:

console.log(mediaWiki);
// console.log(mw)

mw.config

为使JS代码编写更加方便,MediaWiki在对象 mw.config 中提供了一些配置值。

其中大部分以 wg 为前缀。

  • For legacy reasons these names are exposed as global JavaScript variables 若$wgLegacyJavaScriptGlobals为真,所以需要一个前缀以防变量名冲突。
  • A subset of these names correspond to $wg_variable in PHP, but others are unrelated.
  • Besides the names in this list, many extensions also set additional JavaScript config variables prefixed with 'wg'.

你可以通过mw.config来访问这些值。

全站

名称 类型 描述 版本
debug 布尔型 (没啥好说的) MW 1.17+
skin 字符串 显示当前皮肤的内部名称,经典皮肤(自带的皮肤)名称为:"standard"。 MW 1.7 (monobook 皮肤)
MW 1.8+: 所有情况
stylepath 字符串 皮肤根目录的完整链接,包括样式表和配套的脚本。这个链接不包括皮肤的子目录,也不以“/”结尾。 MW 1.7 (monobook 皮肤)
MW 1.8+: 所有情况
wgArticlePath 字符串 Local 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 title title, a valid URL may be constructed using wgArticlePath.replace('$1', title). See also $wgArticlePath. MW 1.8+
wgCaseSensitiveNamespaces 数组 The IDs of the namespaces treated as case-sensitive by MediaWiki. Determined by the values of them $wgCapitalLinks and $wgCapitalLinksOverrides configuration variables. MW 1.18+ (90234)
wgContentLanguage 字符串 整个Wiki的默认语言代码。 MW 1.8+
wgContentNamespaces 整数数组 The IDs of the namespaces considered "content namespaces" by MediaWiki. Equivalent to the value of the $wgContentNamespaces configuration variable, 没有加载好的话就是0 MW 1.23+ (Git: 3d87e3a86bcb39d444ef916129dd48bf80b5bb31)
wgDBname 字符串 Wiki的数据库名。 MW 1.13+
wgEnableAPI 布尔型 如果mw的API启用则为true,反之为false MW 1.12+
wgEnableWriteAPI 布尔型 如果mw的写入API启用则为true,反之为false。If wgEnableAPI && !wgEnableWriteAPI, only the read operations of the API are enabled. MW 1.12+
wgExtensionAssetsPath 字符串 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. MW 1.18+ (82247)
wgFormattedNamespaces 对象 一个名字空间ID到翻译过的名字空间的查找表。每一个名字空间都有一个字符串表示的数字作为键映射到名字空间的名字。不包含别名或原名。 MW 1.16+
wgNamespaceIds 对象 一个名字空间到名字空间ID的查找表。每一个名字空间(包括别名)都有一个名字空间的名字作为键映射到名字空间的ID。不包含原名。所有键的字母均小写,用下划线代替空格。 MW 1.16+
wgScript 字符串 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". See also $wgScript. MW 1.11+
wgScriptPath 字符串 wgScript的路径部分,结尾不是"/"。This is the path to use for direct calls to the php access points such as index.phpapi.php. See also $wgScriptPath. MW 1.8+
wgServer 字符串 服务器链接,结尾不是"/"。The combination wgServer + wgScriptPath + "/api.php", for instance, results in a valid URL to the API access point script. MW 1.8+
wgSiteName 字符串 网站名,声明于$wgSitename。 MW 1.16+
wgVariantArticlePath 字符串 或 false 如果Wiki自定义了语言变量(比如中文或塞尔维亚的Wikipedias),这个值将设为语言变量的根路径(除wgContentLanguage以外)。这个路径包含了占位符"$1"需要用页面名替换,"$2"需要用语言代码替换(比如"zh-cn")。如果Wiki没有语言变量,这个值将为false。详见$wgVariantArticlePath。 MW 1.13+
wgVersion 字符串 MediaWiki软件版本。 MW 1.12+


所有页面

(根据用户/页面不同,这些值会变动。)

名称 类型 描述 版本
wgAction 字符串 当前操作,如正在编辑页面时为"edit",正在浏览页面时为"view",详见[1] MW 1.10+
wgArticleId 整数 页面的内部ID。不存在的页面和特殊页面的此项是0。 MW 1.8+
wgCanonicalNamespace 字符串 此页面的真实名字空间(未翻译,不是别名)。 MW 1.8+
wgCanonicalSpecialPageName 字符串 或 false 或 未定义 在特殊页面,显示真实页面名(未翻译,不是别名);否则为未定义(MW 1.15及以下)或false(MW 1.16及以上)。 MW 1.9+
wgCategories 字符串数组 当前页面的分类列表。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 整数 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 布尔型 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).
  • 这个变量名不太好 – 我们要做的事可能和这个页面是不是"article"无关。
MW 1.8+
wgIsProbablyEditable 布尔型 如果你可以编辑这个页面的话(基于Title::quickUserCan)则为True。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: 0bbc358951e56fbb3e2e121a02cf213e280ffdc1)
wgNamespaceNumber 整数 当前页面的名字空间ID。 MW 1.8+
wgPageContentLanguage 字符串 当前页面的语言代码(获取:$context->getTitle()->getPageLanguage())。 MW 1.19+ (104483)
wgPageContentModel 字符串 标准Wiki页面:'wikitext',JavaScript页面:'javascript',CSS页面:'css',Scribunto(如Lua模块)页面:'Scribunto' MW 1.22+ (gerrit:62178)
wgPageName 字符串 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 blanks replaced by underscores. To get only the title without the namespace, use wgTitle. MW 1.8+
wgRedirectedFrom 字符串 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+ (104668)
wgRelevantPageName 字符串 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+ (113737)
wgRelevantUserName 字符串 或 无 The relevant name of the user to which content actions and some extra navigation links (e.g. link to user rights 或 user contributions) apply. MW 1.23+ (git: 88773)
wgRelevantPageIsProbablyEditable 布尔型 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)
wgRestrictionEdit 字符串数组 或 无 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, 或 "protections" brought about by the TitleBlacklist extensions "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 字符串数组 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, 或 "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, 或 the right revision for diff views (But 0 when diffonly=yes, 模板:bug). Also set on diff pages; zero for special pages, history pages, 或 anywhere else inapplicable. MW 1.22+ (git:7fa7b71eb3a5dd24e75865162e4c06a4a6012524)
wgSearchType 字符串 或 无 The name of the search backend used to execute search requests. MW 1.23+ (git: 118655)
wgTitle 字符串 不带名字空间的页面标题。可能包含空格,同时不包含下划线。想要获取包含名字空间的页面名,请使用wgPageName MW 1.8+

这些值来自访问页面的用户:

名称 类型 描述 版本
wgUserEditCount 整数 当前用户的编辑数(未登录则为null)。 MW 1.21+
wgUserGroups 字符串数组 列出当前用户所属用户组的列表(未登录则为null)。用户组以内部名字标识,比如"sysop"和"autoconfirmed"。默认用户组名字是"*"。 MW 1.10+
wgUserId 整数 当前用户的数字ID(未登录则为null)。 MW 1.21+
wgUserLanguage 字符串 用户的语言代码, 在特殊:参数设置中设置(可以被URL中的uselang=参数覆盖)。 MW 1.8+
wgUserName 字符串 当前用户的用户名(未登录则为null)(不是用户IP地址,与服务端PHP的 $wgUser->getName() 不同)。 MW 1.8+
wgUserRegistration 整数 当前用户的注册时间,显示为 1970-01-01 00:00:00 UTC 以来的毫秒数。(未登录则为null)。 MW 1.21+


部分页面

部分附加的变量只在特定名字空间、Wiki设置或用户设置时出现。

名称 类型 描述 版本
主页
wgIsMainPage 布尔型 如果当前页面是主页则为true。否则不存在这项(null)。 MW 1.18+
如果页面语言是个变量
wgUserVariant 字符串 如果Wiki有语言变量,则为用户的配置。如果没有语言变量,则没有这一项,代码说明:
mw.config.exists( 'wgUserVariant' ); // false
mw.config.get( 'wgUserVariant' ); // null.
MW 1.16+
保存编辑之后
wgPostEdit 字符串 如果用户刚刚保存了这个页面,则为"saved";
如果用户刚刚创建了这个页面,则为"created";
如果用户刚刚从历史记录中恢复了(编辑一个旧版本后保存)这个页面,则为"restored"。
其他情况下为null
请注意:
  1. 没有改动的编辑则为null,不是"saved"。
  2. 撤销后,显示为"saved"。
  3. 回退后,显示为null
MW 1.21+ (gerrit:50480)
页面版本比对时
wgDiffOldId 整数 "旧"版本的版本ID。仅在页面版本比对时有。 MW 1.30+ (git: a469795efc5bc1c585c9f84ee289ee099833661d)
wgDiffNewId 整数 "新"版本的版本ID。仅在页面版本比对时有。 MW 1.30+ (git: a469795efc5bc1c585c9f84ee289ee099833661d)

mw.hook

mw.html

mw.msg

mw.loader

mw.log

mw.map

mw.user

mw.Api

mw的其他子项

jQuery与插件