Extension:TemplateData

This page is a translated version of the page Extension:TemplateData and the translation is 40% complete.
Outdated translations are marked like this.
Other languages:
Deutsch • ‎English • ‎Türkçe • ‎dansk • ‎español • ‎français • ‎lietuvių • ‎polski • ‎português • ‎čeština • ‎русский • ‎українська • ‎अवधी • ‎हिन्दी • ‎বাংলা • ‎中文 • ‎日本語
此扩展已绑定在MediaWiki 1.35及以上版本 因此您不需要再次下载。 然而,您仍需要跟随提供的其他指示。
MediaWiki扩展手册
OOjs UI icon advanced.svg
TemplateData
发布状态: 稳定版
Screenshot of TemplateData extension.png
实现 标签 , 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或更新版本
下载
  • $wgTemplateDataUseGUI
<templatedata>
翻译TemplateData扩展如果在translatewiki.net可用
检查使用和版本矩阵。
问题 开放的工作 · 报告错误

TemplateData扩展引入了<templatedata>标记和API,它们共同允许编辑者指定应如何调用模板。 此信息作为最终用户的格式良好的表提供,并作为JSON API提供,它使其他系统(例如VisualEditor)能够构建用于处理模板及其参数的接口。 有关深入的帮助,请参阅Help:TemplateData

安装

  • 下载文件,并将其放置在您extensions/文件夹中的TemplateData目录内。
  • 将下列代码放置在您的LocalSettings.php的底部:
    wfLoadExtension( 'TemplateData' );
    
  •   完成 – 在您的wiki上导航至Special:Version,以验证扩展已成功安装。

致使用MediaWiki 1.24或更早版本的用户:

上面的说明介绍的是安装此扩展的新方法,它使用wfLoadExtension()。 如果您需要在早期版本(MediaWiki 1.24和更早版本)中安装此扩展,而不是wfLoadExtension( 'TemplateData' );,您需要使用:

require_once "$IP/extensions/TemplateData/TemplateData.php";

编辑数据

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 InterfaceTextnull 模板的简要说明。 必须为纯文本格式。 Once filled, it can be displayed as caption when editing a single template and perhaps in search results when users pick one of many. 默认设置为$nul。
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 字符串,inlineblock 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:
字符串值 参数类型
"unknown" Assumed type if not set
"number" Any numerical value (without decimal points or thousand separators)
"string" Any textual value. May contain line breaks.
"line" Short text field – use for names, labels, and other short-form fields. This is supposed to be text that is not expected to contain line breaks.
"boolean" A boolean value ('1' for true, '0' for false, '' for unknown), see three-valued logic.
"date" A date in ISO 8601 format, e.g. "2014-05-09" or "2014-05-09T16:01:12Z"
"url" A URL, including protocol, e.g. "http://www.example.org", "https://example.org", or "//example.org".
"wiki-page-name" A valid MediaWiki page name for the current wiki. Doesn't have to exist, but if not, should be a valid page name which could be created.
"wiki-file-name" A valid MediaWiki file name for the current wiki. Doesn't have to exist, but if not, should be a valid file name which could be uploaded. Should not include the namespace (e.g. "Foo.svg" not "File:Foo.svg" or "Bild:Foo.svg").
"wiki-template-name" A valid MediaWiki Template name for the current wiki.
"wiki-user-name" A valid MediaWiki user name for the current wiki. Doesn't have to exist, but if not, should be a valid user name which could be created. Should not include the namespace (e.g. "Foo" not "User:Foo" or "Usario:Foo").
"content" Page content in wikitext, such as text style, links, images, etc.
"unbalanced-wikitext" Raw wikitext that should not be treated as standalone content because it is unbalanced – for example, templates concatenating incomplete wikitext as a bigger whole, such as {{echo|before=<u>|after=</u>}}
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:
Set Object

Object or primitive type

描述

label InterfaceText A (very) brief name for the parameter set. Try to keep under 20-ish characters.
params 字符串数组 One or more names of parameters to include in the set (each must be a valid key in the params object). A parameter may be in multiple sets. Not every parameter must be in a set.

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 params object elsewhere in the same template's TemplateData.

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.

Template parameters

ParameterDescriptionTypeStatus
User's nameuser 1

User name of person who forgot to sign their comment.

Userrequired
Datedate 2

Timestamp of when the comment was posted, in YYYY-MM-DD format.

Auto value
{{subst:#time:Y-m-d}}
Unknownsuggested
Yearyear

no description

Numberoptional
Monthmonth

no description

Numberoptional
Dayday

no description

Numberoptional
Commentcomment

no description

Unknownoptional

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:
    1. An overall Pages Object containing one or more Page Objects
    2. An object with a numeric key: a Page Object
  • Additions/changes to the actual TemplateData Object
    1. Add the title key with the name of the page from which the data was requested (e.g. "Template:Cite web").
    2. Add the paramOrder key and array using the order in which the parameters occur in the params key within the TemplateData Object on the template page.
    3. 添加sets
    4. Change all TemplateText occurrences that are just strings to objects with a single key for the current wiki's Wikipedia language code
    5. 删除所有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.
    6. Add default values for all keys in each Parameter Object which have not been explicitly defined or inherited.

Visualised differences in a TemplateData JSON object delivered by the API

Wrapper objects added to JSON delivered by API
These are not defined in the JSON object enclosed by the <templatedata> tags on the template page.
JSON Root
类型 描述
pages Page Object The Page object (contains one key which is the page number). If multiple pages were requested at one time there will be multiple Page Objects.

Page Object
类型 描述
(页码) TemplateData Object The TemplateData Object containing the information payload. The key is a static page number for the template page from which the JSON data was requested.


TemplateData Object (added by API)
The following key is added to the root of the JSON object which is enclosed in <templatedata> tags on the template page.
类型 描述
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.


Parameter Objects from API
类型 描述
label InterfaceText translated to InterfaceText object, if not already in that format.
required 布尔值 如果未定义,则添加默认值false
description InterfaceTextnull 如果定义为字符串,则转换为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.


InterfaceText (Object or null):
An object containing strings keyed by language code.
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:

配置

变量 默认 描述
$wgTemplateDataUseGUI true 实验性对话框界面,用于编辑templatedata JSON

参见