Extension:TemplateData
TemplateData 发布状态: 稳定版 |
|
---|---|
![]() |
|
实现 | 标签 , API |
描述 | 允許存儲,檢索和可视化有關模板的信息 |
作者 | Timo Tijhof, Moriel Schottlender, James Forrester, Trevor Pascal, Bartosz Dziewoński, Marielle Volz, ... |
最新版本 | 0.1.2 (持续更新) |
兼容性方针 | 发行分支 |
MediaWiki | 1.25+ |
PHP | 5.4+ |
数据庫更改 | 否 |
许可协议 | GNU通用公眾授權條款2.0或更新版本 |
下载 | |
|
|
<templatedata> |
|
翻譯TemplateData擴充功能如在translatewiki.net可用 | |
问题 | 尚未完成的工作 · 回報錯誤 |
TemplateData扩展引入了<templatedata>
标记和API,它们共同允许编辑者指定应如何调用模板。
此信息作为最终用户的格式良好的表提供,并作为JSON API提供,它使其他系统(例如VisualEditor)能够构建用于处理模板及其参数的接口。
有关深入的帮助,请参阅Help:TemplateData 。
安装
- 下载文件,并将其放置在您
extensions/
文件夹中的TemplateData
目录内。 - 将下列代码放置在您的LocalSettings.php的底部:
wfLoadExtension( 'TemplateData' );
完成 – 在您的wiki上导航至Special:Version,以验证扩展已成功安装。
编辑数据
TemplateData扩展通过在模板页面的wikitext中定义<templatedata>
标记来工作(可选地,它也可以从不同的页面中转换)。
This is designed to be compatible with the common layout many wikis have of transcluding template documentation from a separate page (but is not required).
This content of the <templatedata>
tag must be valid JSON using the format described below; note that all descriptions must be in plain text (no wikitext is accepted inside the template data).
When a <templatedata>
block is included on a page, the TemplateData extension performs the following checks when the page is being saved:
内容必须是有效的JSON;
JSON结构中的每一项必须是下文指定的预设类型(例如对象、数组或原始类型);以及
对于那些具有指定的可能的值的列表(例如params.yourParameterName.type
)的项,JSON对象中的值必须与这些值中的某一种相匹配。
如果这些检查中的任何一项失败,服务器将不允许用户保存,并且将在编辑页面的上方显示错误消息。
For autovalue items, bug 2700 prevents subst:
from working in any template that is added inside a <tag>
, including <ref>
and <gallery>
tags.
The TemplateData block will save correctly, but the resulting wikitext will not be parsed correctly when the template is used.
Note that you cannot use {{#tag:templatedata}}
in lieu of <templatedata>
.
格式
The below is a visualised version of the JSON data as defined on the template page enclosed by <templatedata>
</templatedata>
tags.
The formal specification is available in the TemplateData repository. 有关最新版本,请参考Specification.md。
模板数据对象
模板数据对象是一个“JSON根元素”,其被附于在模板页上的<templatedata>
元素中。
键 | 类型 | 描述 |
---|---|---|
description
|
InterfaceText或null
|
模板的简要说明。 必须为纯文本格式。 Once filled, it can be displayed as caption when editing a single template and perhaps in search results when users pick one of many. 默认设置为null 。
|
params
|
Object containing Param objects | An object that maps each parameter name of the template to a corresponding Param object describing the properties of that parameter. |
paramOrder
|
Array containing parameter name strings | The logical order in which parameters should be displayed. The array contains each parameter key exactly once. Each string must be a valid key in the params object. If this key does not exist in the <templatedata></templatedata> structure on the template page, it is added to the structure provided by the API using the order in which the parameters occur within the TemplateData object.
|
sets
|
Array containing Set objects |
An array containing set specifications. A set is a group of parameters that should be used together. 默认值为[]。 请注意,sets 功能仍在开发中。
|
format
|
字符串,inline 或block
|
How the template's wikitext representation SHOULD be laid out. 默认设置为inline 。 See #Custom_formats for other custom formats.
|
maps
|
A Maps object | An object that maps a simplified name of a third party consumer of the template data (i.e. Citoid, Special:MyLanguage/Wikidata, etc.) to an object that in turn maps a name of a consumer parameter to one or more corresponding names of template parameters. |
Param object
键 | 类型 | 默认 | 描述 | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
label |
InterfaceText | null |
参数的(非常)简短的名称。 尽量保持20个字符以下。 | ||||||||||||||||||||||||||||
description
|
InterfaceText | null
|
参数的简短描述,以使用户知道从选项列表中选择哪个。 | ||||||||||||||||||||||||||||
required
|
布尔值 | false
|
Whether the parameter is required for the template to work (true if this parameter must be specified). | ||||||||||||||||||||||||||||
suggested
|
布尔值 | false
|
Whether the parameter is suggested for the template to be useful (true if this parameter should be specified). | ||||||||||||||||||||||||||||
deprecated |
布尔值或字符串 | false |
Whether the parameter is deprecated. The value can be a string of instructions giving the user guidance what instead can be done, or be simply true .
| ||||||||||||||||||||||||||||
aliases |
Array containing strings | [] |
List of aliases. An alias is an alternative name for the parameter that may be used instead of (not in addition to) the primary name. Aliases are not documented in a separate Parameter object. If they need more information, they should be in their own property marked "deprecated". | ||||||||||||||||||||||||||||
default |
InterfaceText | null |
The default value used by the template if no value is assigned to the parameter, or a description thereof. | ||||||||||||||||||||||||||||
autovalue |
字符串 | null |
A dynamically-generated default value in wikitext, such as today's date or the editing user's name; this will often involve wikitext substitution, such as {{subst:CURRENTYEAR}} .
| ||||||||||||||||||||||||||||
example
|
InterfaceText | null
|
An example text for the parameter, to help users fill in the proper value. | ||||||||||||||||||||||||||||
type |
字符串 | "unknown" |
The type of the parameter, for (soft) type hinting. One of:
| ||||||||||||||||||||||||||||
inherits |
字符串 | 无 substituted |
The name key of another parameter (must be a valid key in the params object). The current Parameter Object will inherit properties from the specified Parameter Object, with local properties overriding the inherited ones.
|
Set object
键 | 类型 | 默认 | 描述 | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
— | Array of Arrays | []
|
The type of the parameter, for (soft) type hinting. One of:
|
Maps object
键 | 类型 | 默认 | 描述 |
---|---|---|---|
name of consumer | 对象 | {} | An object that maps a name of a consumer to an object that in turn maps a name of a consumer parameter to one or more corresponding names of template parameters. The names of the template parameters can be specified either as a string (one name), an array of strings (several names), or an array of arrays of strings (several sets of names); each of these strings must be a valid key in the A specific template parameter may be in multiple maps, and it may be used in the values of multiple keys in a given map, but not every template parameter must be in a map. A shortened example from English Wikipedia's Template:Cite_news#TemplateData illustrates the structure of a maps object. { "proveit": { "main": "title", "textarea": [ "quote" ] }, "citoid": { "title": "title", "url": "url", "publisher": "publisher", "publicationTitle": "work", "date": "date", "ISSN": [ "issn" ], "ISBN": [ "isbn" ], "contributor": "others", "author": [ [ "first", "last" ], [ "first2", "last2" ], [ "first9", "last9" ] ], "editor": [ [ "editor-first", "editor-last" ] ] } } |
InterfaceText (string or object)
A free-form plain-text string (i.e. no wikitext or HTML) in the content-language of the wiki, or an object containing those strings keyed by language code.
The default value for all InterfaceText fields is null
.
字符串类型
If it is a string, it must contain a non-localized string in the content language of the local wiki.
Object type
If it is an object, the object must have this mapping:
键 | 类型 | 描述 |
---|---|---|
Wikipedia language code | 字符串 | Localized string for use on the Wikipedia corresponding to the given language code. |
Example of object mapping:
键 | 值 |
---|---|
"en" | "Welcome to Wikipedia" |
"de" | "Willkommen bei Wikipedia" |
"fe" | "Bienvenue sur Wikipédia" |
"nl" | "Welkom op Wikipedia" |
"it" | "Benvenuti su Wikipedia" |
"es" | "Bienvenidos a Wikipedia" |
"ru" | "Добро пожаловать в Википедию" |
示例
An example TemplateData structure as specified within wikitext on a template page.
<templatedata>
{
"description": "Label unsigned comments in a conversation.",
"params": {
"user": {
"label": "User's name",
"type": "wiki-user-name",
"required": true,
"description": "User name of person who forgot to sign their comment.",
"aliases": ["1"]
},
"date": {
"label": "Date",
"suggested": true,
"description": {
"en": "Timestamp of when the comment was posted, in YYYY-MM-DD format."
},
"aliases": ["2"],
"autovalue": "{{subst:#time:Y-m-d}}"
},
"year": {
"label": "Year",
"type": "number"
},
"month": {
"label": "Month",
"inherits": "year"
},
"day": {
"label": "Day",
"inherits": "year"
},
"comment": {
"required": false
}
},
"sets": [
{
"label": "Date",
"params": ["year", "month", "day"]
}
],
"maps": {
"ExampleConsumer": {
"foo": "user",
"bar": ["year", "month", "day"],
"quux": [
"date",
["day", "month"],
["month", "year"],
"year"
]
}
}
}
</templatedata>
Below is how the above example would be displayed on the template page:
Label unsigned comments in a conversation.
Parameter | Description | Type | Status | |
---|---|---|---|---|
User's name | user 1 | User name of person who forgot to sign their comment. | User | required |
Date | date 2 | Timestamp of when the comment was posted, in YYYY-MM-DD format.
| Unknown | suggested |
Year | year | no description | Number | optional |
Month | month | no description | Number | optional |
Day | day | no description | Number | optional |
Comment | comment | no description | Unknown | optional |
API
See the generated help at Special:ApiHelp/templatedata.
A sample invocation (formatted for human readability) is:
This returns the template data for Template:Cite web. The <templatedata>...</templatedata> block for this template is on its transcluded template documentation page, en:Template:Cite web/doc.
Additions and changes to JSON as received from the API
The JSON structure provided in response to an API HTTP get request is significantly different than that which is defined in the <templatedata>...</templatedata> block.
In response to the API request, extension TemplateData makes the following changes to the JSON object:
- Adds two wrapping objects:
- An overall Pages Object containing one or more Page Objects
- An object with a numeric key: a Page Object
- Additions/changes to the actual TemplateData Object
- Add the
title
key with the name of the page from which the data was requested (e.g. "Template:Cite web"). - Add the
paramOrder
key and array using the order in which the parameters occur in theparams
key within the TemplateData Object on the template page. - 添加
sets
键 - Change all TemplateText occurrences that are just strings to objects with a single key for the current wiki's Wikipedia language code
- 删除所有
inherits
键。- Add all properties from parameter from which an inheritance is defined which are not superseded by explicitly defined keys in the inheritor's Parameter Object.
- Add default values for all keys in each Parameter Object which have not been explicitly defined or inherited.
- Add the
Visualised differences in a TemplateData JSON object delivered by the API
| ||||||
|
键 | 类型 | 描述 |
---|---|---|
title
|
字符串 | The page name of the template (e.g. "Template:Cite web"). This key is not contained in the JSON structure on the template page. It is added to the structure by the MediaWiki software in route to delivery via the API. |
paramOrder
|
Array containing parameter name strings | The logical order in which parameters should be displayed. The array contains each parameter key exactly once. Each string must be a valid key in the params object.If this does not exist in the <templatedata></templatedata> structure on the template page, it is added to the structure as provided by the API using the order in which the parameters occur within the TemplateData object.
|
sets
|
Array containing Set Objects | An array containing set specifications. A set is a group of parameters that should be used together. If this does not exist on the template page it is added as an empty array to the structure provided by the API. |
键 | 类型 | 描述 |
---|---|---|
label |
InterfaceText | translated to InterfaceText object, if not already in that format. |
required |
布尔值 | 如果未定义,则添加默认值false。 |
description |
InterfaceText 或 null |
如果定义为字符串,则转换为InterfaceText对象。 If not defined, key is added with a null value.
|
deprecated |
布尔值或字符串 | If not defined, default value of false added. |
aliases |
字符串数组 | If not defined, a default empty array is assigned (i.e. [] ). |
default |
字符串 | 如果未定义,则分配默认值""。 |
type |
字符串 | 如果未定义,则分配默认值"unknown"。 |
inherits |
(已移除) | This key is removed and all inherited keys are added. Keys defined for the current parameter take precedence and any keys not defined on either the current param, or the one from which it inherits are given the default value. |
键 | Object or primitive type |
描述 |
---|---|---|
Object (containing localized strings by Wikipedia language code) If a TemplateData field on the template page contains a string, not an object with localized strings, MediaWiki translates it into an InterfaceText Object with a string assigned to the key name of the source Wikipedia Wikipedia language code. The default for all InterfaceText fields is null .
| ||
(Wiki language code) | 字符串 | Localized string for use on the Wikipedia corresponding to the Wikipedia language code. |
Example of JSON object provided by API
The response to an API request for the TemplateData structure in the Example section (above) can be seen:
- HTML格式:https://www.mediawiki.org/w/api.php?action=templatedata&titles=Extension:TemplateData&format=jsonfm
- 由API提供:https://www.mediawiki.org/w/api.php?action=templatedata&titles=Extension:TemplateData
配置
变量 | 默认 | 描述 |
---|---|---|
$wgTemplateDataUseGUI
|
true
|
实验性对话框界面,用于编辑templatedata JSON |
参见
- 扩展:模板精灵 – user interface for selecting and inserting templates' wikitext, based on their TemplateData.
- GitHub: jeblad/TemplateData – alternate version with some Lua functionality.
![]() | 此扩展被用于一个或多个维基媒体项目上。 这可能意味着扩展稳定且工作良好,足以用在同等高流量的网站上。 请在维基媒体的CommonSettings.php和InitialiseSettings.php配置文件中寻找此扩展名称以查看安装它的网站。 详细的已安装扩展的完整列表可在wiki的Special:Version页面找到。 |
- Extensions bundled with MediaWiki 1.35/zh
- Stable extensions/zh
- Tag extensions/zh
- API extensions/zh
- GPL licensed extensions/zh
- Extensions in Wikimedia version control/zh
- EditPage::showEditForm:fields extensions/zh
- EditPage::showEditForm:initial extensions/zh
- PageContentSave extensions/zh
- ParserFetchTemplateData extensions/zh
- ParserFirstCallInit extensions/zh
- ResourceLoaderRegisterModules extensions/zh
- All extensions/zh
- Extensions used on Wikimedia/zh
- Metadata/zh