Extension:TemplateData/zh

随附于MediaWiki 1.35及更高版本。 其余配置说明仍必须遵循。
Category:Extensions bundled with MediaWiki 1.35/zh Category:GPL licensed extensions/zh
TemplateData
Category:Stable extensions/zh
Category:Tag extensions/zh, API Category:API extensions/zh
描述
作者 Timo Tijhof, Moriel Schottlender, James Forrester, Trevor Parscal, Bartosz Dziewoński, Marielle Volz, ...
最新版本 0.2.0 (持續更新)
快照跟随MediaWiki发布。
PHP 5.4+
GNU通用公眾授權條款2.0或更新版本
下載 Category:Extensions in Wikimedia version control/zh
  • $wgTemplateDataMaxFavorites
  • $wgTemplateDataEnableFeaturedTemplates
  • $wgTemplateDataEnableCategoryBrowser
  • $wgTemplateDataEnableDiscovery
  • $wgTemplateDataEditorNamespaces
  • $wgTemplateDataUseGUI
<templatedata>
前往translatewiki.net翻譯TemplateData
問題 开启的任务 ·
Category:All extensions/zh
Category:Pages using deprecated NoteTA template#%20

TemplateData引入 <templatedata> 標籤和 API,它们共同允许编辑者指定应如何调用模板并使用对应的参数。 这些信息以格式良好的表格形式提供给终端用户,并以 JSON API 的形式提供给其他系统(例如 VisualEditor),使其能够建立接口,以处理模板。 有关深入的帮助,请参阅[[Special:MyLanguage/Help:TemplateData|Category:Pages using LC zh with unknown or deprecated arguments#zh]]

安裝

  • 下载,并将解压后的TemplateData移动到extensions/目录中。
    开发者和代码贡献人员应从Git安装扩展,输入:
    cd extensions/
    git clone https://gerrit.wikimedia.org/r/mediawiki/extensions/TemplateData
    
  • 将下列放置在您的LocalSettings.php 的底部:
    wfLoadExtension( 'TemplateData' );
    
  • Yes 完成 – 在您的wiki上Special:Version,以验证已成功安装

TemplateData扩展会在模板页面的wikitext中定义<templatedata>标签(当然也可以是从其他页面嵌入包含的),从而实现效果。 这种设计是为了兼容很多wiki都有的常见布局,即嵌入包含一个单独的文档页面(但不是必需的)。 <templatedata>的内容必须是有效的JSON,其格式如下所示,注意所有的描述必须是纯文本(模板数据中不接受wikitext)。

页面包含<templatedata>块时,TemplateData会在页面保存时,做出以下检查:

  1. 内容必须是有效的JSON;
  2. JSON结构中的每一项必须是下文指定的预设类型(例如对象、数组或原始类型);
  3. 对于那些具有指定的可能的值的列表(例如参数中的 type)的项,JSON 对象中的值必须与这些值中的某一种相匹配。

如果这些检查中的任何一项失败,服务器将不允许用户保存,并且将在编辑页面的上方显示错误消息。

对于autovalue项,T4700使得subst:无法在任何添加到标签(包括<ref><gallery>标签)的模板中生效。 TemplateData在这种情况下不会阻止保存,但最终的wikitext在使用模板时不能被正确解析。

格式

以下内容是JSON数据的视觉化版本,正如包含在<templatedata></templatedata>标签的模板页面定义的。 格式规定可参考TemplateData。 有关最新版本,请参考Specification.md

TemplateData对象是一个“JSON根元素”,包含在模板页面的<templatedata>元素中。

类型 描述
description InterfaceTextnull 模板的简要说明。 必须为纯文本格式。 如果填写了,当编辑单个模板时以及在用户选择其中的一个时搜索结果中会显示为说明文本。 默认设置为null

→ For more details see: description

params 包含参数(Param)对象的对象 一个对象,它将模板的每个参数名称映射到描述该参数属性的相应的Param对象上。

→ For more details see: params

paramOrder 包含参数名称的字符串的数组 应显示的参数的逻辑顺序。 该数组只包含每个参数的键值一次。 每个字符串都必须是 params 对象中的有效键值。

→ For more details see: paramOrder

sets 包含Set对象的数组 包含集合规范的数组。 集合是应该一起使用的一组参数。 默认值为[]。 请注意,sets功能仍在开发中。
format 字符串,inlineblock 模板的wikitext应该如何布局。 默认设置为inline。 有关其他自定义格式,请参阅 #Custom_formats
maps 包含Maps对象的对象 这个对象将模板数据的第三方读取机制(例如CitoidSpecial:MyLanguage/Wikidata等)的简化的名称,映射一个对象,从而将读取机制参数的名称映射到模板参数的一个或者多个对应的名称中。

类型 描述
labelInterfaceText null 参数的(非常)简短的名称。 尽量保持20个字符以下。

→ For more details see: label

description InterfaceText null 参数的简短描述,以使用户知道从选项列表中选择哪个。

→ For more details see: description

required false 模板是否需要该参数才能工作(如果必须指定此参数,则值为 true)。

→ For more details see: required

suggested false 是否建议该参数使模板有用(如果应指定此参数,则值为 true)。

→ For more details see: suggested

deprecated 布尔值或字符串 false 该参数是否被弃用。 该值可以是一串指令,指导用户可以做什么,或者只是 true

→ For more details see: deprecated

aliases包含字符串的数组 []别名列表。 别名是参数的替代名称,可以用来代替(而不是附加于)主名称。 别名不记录在单独的参数对象中。 如果它们需要更多信息,它们应该在自己的属性中被标记为“已弃用”。

→ For more details see: aliases

defaultInterfaceText null没有为参数分配值时模板使用的默认值,或其描述。

→ For more details see: default

autovalue null在wikitext中动态生成的默认值,如今天的日期或编辑用户的名字;这通常涉及wikitext的替换,如{{subst:CURRENTYEAR}}

→ For more details see: autovalue

example InterfaceText null 一份参数的示例文本,以帮助用户填写正确的值。

→ For more details see: example

type "unknown"参数的类型,用于(软)类型提示。其中之一:
参数类型
"unknown"没有设置时的假定类型

→ For more details see: value ‘unknown’

"number"任何数值(不含小数点或千位分隔符)

→ For more details see: value ‘number’

"string"任何文本值。可包含换行符。

→ For more details see: value ‘string’

"line"短文本字段——用于名称、标签和其他短格式字段。 这应该是不期望包含换行符的文本。

→ For more details see: value ‘line’

"boolean"一个布尔值(1代表true,0代表false,''代表未知),见三值逻辑

→ For more details see: value ‘boolean’

"date"一个ISO 8601格式的日期,如2014-05-09或2014-05-09T16:01:12Z。

→ For more details see: value ‘date’

"url"一个URL,包括协议,如"http://www.example.org", "https://example.org",或"//example.org"

→ For more details see: value ‘url’

"wiki-page-name"当前wiki的一个有效的MediaWiki页面名称。 不一定要存在,但如果不存在,应该是一个有效的页面名称,可以创建。

→ For more details see: value ‘wiki-page-name’

"wiki-file-name" 当前wiki的一个有效的MediaWiki文件名。 不一定要存在,但如果不存在,应该是一个有效的文件名,可以上传。 不应包括命名空间(如"Foo.svg"而非"File:Foo.svg"或"Bild:Foo.svg")。

→ For more details see: value ‘wiki-file-name’

"wiki-template-name" 当前wiki的一个有效的MediaWiki模板名称。

→ For more details see: value ‘wiki-template-name’

"wiki-user-name" 当前wiki的一个有效的MediaWiki用户名。 不一定要存在,但如果不存在,应该是一个有效的用户名,可以创建。 不应包括命名空间(如"Foo"而非"User:Foo"或"Usario:Foo")。

→ For more details see: value ‘wiki-user-name’

"content"wikitext格式的页面内容,如文本样式、链接、图像等。

→ For more details see: value ‘content’

"unbalanced-wikitext"不应作为独立内容处理的原始wikitext,因为它是不平衡的——例如,模板将不完整的wikitext串联成一个更大的整体,如{{echo|before=<u>|after=</u>}}

→ For more details see: value ‘unbalanced-wikitext’

→ For more details see: type

inherits
被替换
另一个参数的名称键(必须是params对象中的一个有效键)。 当前的Param对象将继承指定的Param对象的属性,当前对象的属性将覆盖继承的属性。

→ For more details see: inherits

suggestedvalues包含字符串的数组 [] 一个可选的参数属性。 创建一个参数值的列表,以帮助用户选择所需的值。 为了使建议值在可视化编辑器中显示为一个组合框,参数的类型必须设置为以下之一:content、line、string、number、unknown或unbalanced wikitext。

→ For more details see: suggestedvalues

对象或原始类型

描述

label InterfaceText 参数集的(非常)简短的名称。 尽量保持20个字符以下。
params 一个或多个要包含在集合中的参数名称(每个参数必须是params对象中的一个有效键)。 一个参数可以在多个集合中。 不是每个参数都必须在一个集合中。

Maps is a single data structure that goes multiple levels deep. It became common to call the second level a “map object”.

類型 描述
读取机制的名称 {} 读取机制的名称映射到对象,从而将读取机制的名称映射到一个或者多个对应的模板参数的名称

模板参数的名称可以指定为一个字符串(一个名称)、字符串的数组(多个名称),或者字符串的数组的数组(名称的多个集合);每个字符串都必要是params对象中的可用的键,或者在同一个模板的TemplateData的其他地方。

一个特定的模板参数可以在多个映射中,可以用于指定映射的多个值,但是并非每个模板参数都必须要在一个映射中。

英文维基百科的Template:Cite_news#TemplateData中的一个简化示例展示了map对象的结构。

{
	"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"
			]
		]
	}
}

自由格式的纯文本字符串(即没有wikitext或HTML),语言为wiki的内容语言,或包含这些字符串的对象,以语言代码为键。

所有InterfaceText字段的默认值是null

如果是字符串,则必须包含以本地wiki内容语言编写的非本地化的字符串。

如果它是一个对象,该对象必须有这个映射:

類型描述
維基百科語言代碼对应界面语言用户的本地化字符串。

对象映射的例子:

"en""Welcome to Wikipedia"
"de""Willkommen bei Wikipedia"
"fr""Bienvenue sur Wikipédia"
"nl""Welkom op Wikipedia"
"it""Benvenuti su Wikipedia"
"es""Bienvenidos a Wikipedia"
"ru""Добро пожаловать в Википедию"

在模板页面的wikitext中指定的TemplateData结构示例。

<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>

以下是上述例子在模板页面上的显示方式:

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

Special:ApiHelp/templatedata生成的帮助。 一个调用示例(为便于人类阅读而格式化):

这将返回Template:Cite web的模板数据。 这个模板的<templatedata>...</templatedata>块在嵌入其中的模板文档页en:Template:Cite web/doc上。

从API接收的JSON的添加和更改

响应API HTTP获取请求所提供的JSON结构与<templatedata>...</templatedata>块中定义的结构有很大不同。 作为对API请求的回应,TemplateData扩展对JSON对象做了如下修改:

  • 添加两个包装对象
    1. 一个总体的Pages对象,包含一个或者多个Page对象
    2. 带有数字键的对象:Page对象
  • 对实际的TemplateData对象的添加/更改
    1. 加入title键,其值为请求数据的页面名称(如"Template:Cite web")。
    2. 添加sets
    3. 将TemplateText所有以字符串形式出现的地方改为对象,其唯一键为当前wiki的维基百科语言代码
    4. 删除所有inherits键。
      • 添加来自定义了继承关系的参数的所有属性,这些属性没有被继承者的Param对象中明确定义的键所取代。
    5. 为每个Param对象中没有明确定义或继承的所有键添加默认值。

API返回的TemplateData JSON对象中的可视化差异

添加到API交付的JSON中的封装对象
这些内容在模板页面上由<templatedata>标签包裹的JSON对象中没有被定义。
JSON根元素
類型描述
pages Page对象 Page对象(包含一个键,这个键为页面序号)。 如果一次请求了多个页面,那么会有多个Page对象。

Page对象
類型描述
(頁碼) TemplateData对象 TemplateData对象,包含信息的有效载荷。 键是JSON数据请求的模板页面的静态页面序号。
TemplateData对象(由API添加)
下面的键被添加到JSON对象的根元素中,该对象在模板页面上被包裹在<templatedata>标签中。
類型描述
title 模板的页面名称(如"Template:Cite web")。
这个键不包含在模板页面的JSON结构中。 它是由MediaWiki软件在通过API交付的过程中添加到结构中的。
sets 包含Set对象的数组 一个包含集合规范的数组。 一个集合是一组应该一起使用的参数。
如果这在模板页面上不存在,它将作为一个空数组添加到API提供的结构中。
来自API的Param对象
類型描述
labelInterfaceText转换成InterfaceText对象,如果不是已经是这种格式的话。
required如果未定义,则添加默认值false
descriptionInterfaceTextnull如果定义为字符串,则转换为InterfaceText对象。 如果没有定义,键会被添加,值会被设为null
deprecated布尔值或字符串如果没有定义,则添加默认值false
aliases如果没有定义,将分配一个默认的空数组(即[])。
default如果未定义,则分配默认值""。
type如果未定义,则分配默认值"unknown"。
inherits(已移除)这个键被删除,所有继承的键被添加。 为当前参数定义的键优先,任何未在当前参数或其继承的参数上定义的键都被赋予默认值。
InterfaceText(对象或null):
一个对象,其包含的字符串以语言代码为键。
对象或
原始类型
描述

(包含以维基百科语言代码为键的本地化字符串)
如果模板页面的TemplateData字段包含一个字符串,而不是带有本地化字符串的对象,那么MediaWiki将其转换为一个InterfaceText对象,其键分配到源Wikipedia维基百科语言代码的名称。 所有InterfaceText字段的默认值是null
(wiki语言代码)对应界面语言用户的本地化字符串。

API提供的JSON对象示例

对于(上面的)示例章节的TemplateData结构,其API请求的响应参见:

描述
$wgTemplateDataUseGUI true 实验性对话框界面,用于编辑templatedata JSON
$wgTemplateDataEditorNamespaces [ 10 ] The namespaces in which the template data editor appears when creating or editing a page. (T189989)
$wgTemplateDataMaxFavorites 50 The maximum number of favorite templates that can be stored. (T377460)


參見

Category:Extensions used on Wikimedia/zh#TemplateData/zh Category:Metadata/zh
Category:API extensions/zh Category:All extensions/zh Category:CommunityConfigurationProvider initList extensions/zh Category:EditPage::showEditForm:initial extensions/zh Category:Extensions available as Debian packages/zh Category:Extensions bundled with MediaWiki 1.35/zh Category:Extensions in Wikimedia version control/zh Category:Extensions included in BlueSpice/zh Category:Extensions included in Canasta/zh Category:Extensions included in Fandom/zh Category:Extensions included in Miraheze/zh Category:Extensions included in MyWikis/zh Category:Extensions included in ProWiki/zh Category:Extensions included in ShoutWiki/zh Category:Extensions included in WikiForge/zh Category:Extensions included in semantic::core/zh Category:Extensions included in wiki.gg/zh Category:Extensions used on Wikimedia/zh Category:GPL licensed extensions/zh Category:GetPreferences extensions/zh Category:Metadata/zh Category:MultiContentSave extensions/zh Category:OutputPageBeforeHTML extensions/zh Category:Pages using LC zh with unknown or deprecated arguments Category:Pages using deprecated NoteTA template Category:ParserFetchTemplateData extensions/zh Category:ParserFirstCallInit extensions/zh Category:ResourceLoaderRegisterModules extensions/zh Category:SaveUserOptions extensions/zh Category:SkinTemplateNavigation::Universal extensions/zh Category:Stable extensions/zh Category:Tag extensions/zh