Extension:TemplateData

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual
Crystal Clear action run.png
TemplateData

Release status: beta

Implementation Tag, API
Description Allows to store, retrieve and visualise information about templates
Author(s) Timo Tijhof, Moriel Schottlender, James Forrester
Latest version 0.1.0 (2013-05-16)
MediaWiki 1.22.0 or higher
(Old versions worked on 1.20.)
PHP 5.3 or higher
Database changes No
License GPL v2
Download
Parameters

$wgTemplateDataUseGUI

Hooks used
ParserFirstCallInit

PageContentSave
UnitTestsList
ResourceLoaderTestModules
EditPage::showEditForm:initial

Translate the TemplateData extension if it is available at translatewiki.net

Check usage and version matrix; code metrics
Bugs: list open list all report

The TemplateData extension introduces a <templatedata> tag and an API which together allow editors to specify how templates should be invoked. This information is available as a nicely-formatted table for end-users, and as a JSON API, which enables other systems (e.g. VisualEditor) to build interfaces for working with templates and their parameters.

Setup instructions[edit | edit source]

  • Download and extract the file(s) in a directory called TemplateData in your extensions/ folder. If you're a developer and this extension is in a Git repository, then instead you should clone the repository using:
git clone https://gerrit.wikimedia.org/r/p/mediawiki/extensions/TemplateData.git
  • Add the following code at the bottom of your LocalSettings.php:
require_once "$IP/extensions/TemplateData/TemplateData.php";
// Set this to true to enable the TemplateData GUI editor
$wgTemplateDataUseGUI = false;
  • Done! Navigate to "Special:Version" on your wiki to verify that the extension is successfully installed.

Defining a TemplateData block[edit | edit source]

The TemplateData extension works through having a <templatedata> tag defined in the wikitext of a template page (optionally it can be transcluded into the template page as well). 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 is parsed as JSON using the format described below; note that all descriptions must be in plain text (no wikitext is accepted inside the JSON).

When a <templatedata> block is included on a page, the TemplateData extension checks the JSON object prior to the page being saved. The checks performed are

  1. the JSON object must be properly formatted JSON;
  2. each item within the JSON structure must be of the type (object, array, and primitives) specified below; and
  3. For those items which have a specified list of possible values (e.g. params.yourParameterName.type), the value in the JSON object must match one of those values.

If any of these checks fails, the page will not be permitted to be saved and an error message with be displayed above the edit box.

Visualised TemplateData JSON object on a template page[edit | edit source]

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. Please refer to spec.templatedata.txt for the latest version.

TemplateData Object
JSON Root enclosed by <templatedata> tags on the template page.
Key Object or
primitive type
Description
description InterfaceText or null A brief description of the template. Can be displayed as caption when editing a single template and perhaps in search results when users pick one of many. The default is null.
params Object containing Parameter Objects An object containing ["parameter name"]→{parameter object} pairs of parameter specifications (The keys are the names of the template parameters).
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. The default is []. (the sets functionality is still in development)
params: Parameter Name Key → Parameter Objects
Key Object or
primitive type
Default Description
label InterfaceText null A (very) brief name for the parameter. Try to keep under 20-ish characters.
description InterfaceText null A brief description of the parameter, for users to know which to pick from an option list.
required boolean false Whether the parameter is required for the template to work (true if this parameter must be specified).
suggested boolean false Whether the parameter is suggested for the template to be useful (true if this parameter should be specified).
deprecated boolean or string false Whether the parameter is deprecated; this can be a string of instructions for the user detailing what instead the user can do, or be simply true.
aliases Array of 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 string "" The default value used by the template if no value is assigned to the parameter, or a description thereof.
type string "unknown" The type of the parameter, for (soft) type hinting. One of:
String Value Type of Parameter
"unknown" Assumed type if not set
"number" Any numerical value (without decimal points or thousand separators
"string" Any textual value
"line" Short text field - use for names, labels, and other short-form fields
"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-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 string none
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.
sets: Array of Set Objects

Key

Object or primitive type

Default

Description

Array of Arrays [] The type of the parameter, for (soft) type hinting. One of:
Set Object

Key

Object or primitive type

Description

label InterfaceText A (very) brief name for the parameter set. Try to keep under 20-ish characters.
params Array of strings 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.
InterfaceText (string or object):
A free-form string (no wikitext) in the content-language of the wiki, or an object containing those strings keyed by language code. The default for all InterfaceText fields is null
Key Object or
primitive type
Description
String
(non-localized)
string Non-localized string in the content-language of the local wiki.
Object
(containing localized strings by keys of Wikipedia language codes)
(Wiki language code) string Localized string for use on the Wikipedia corresponding to the Wikipedia language code.
Examples of object mappings:
en string Welcome to Wikipedia
de string Willkommen bei Wikipedia
fr string Bienvenue sur Wikipédia
nl string Welkom op Wikipedia
it string Benvenuti su Wikipedia
es string Bienvenidos a Wikipedia
ru string Добро пожаловать в Википедию

Example of TemplateData structure WikiText on template page[edit | edit source]

<templatedata>
{
	"description": "Label unsigned comments in a conversation.",
	"params": {
		"user": {
			"label": "Username",
			"type": "wiki-user-name",
			"required": true,
			"description": "User name of person who forgot to sign their comment.",
			"aliases": ["1"]
		},
		"date": {
			"label": "Date",
			"description": {
				"en": "Timestamp of when the comment was posted, in YYYY-MM-DD format."
			},
			"aliases": ["2"],
			"suggested": true
		},
		"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"]
		}
	]
}
</templatedata>

Example of how TemplateData is displayed on the template page[edit | edit source]

Label unsigned comments in a conversation.

Template parameters
Parameter Description Type Default Status
Username user1 User name of person who forgot to sign their comment. wiki-user-name empty required
Date date2 Timestamp of when the comment was posted, in YYYY-MM-DD format. unknown empty suggested
Year year no description number empty optional
Month month no description number empty optional
Day day no description number empty optional
Comment comment no description unknown empty optional

Using the TemplateData API[edit | edit source]

See the automatically formatted help at https://en.wikipedia.org/w/api.php?action=help&modules=templatedata . An example of a working call is https://en.wikipedia.org/w/api.php?action=templatedata&titles=Template:Cite%20web (for Template:Cite web). You can also see a formatted version to better understand the structure.

Additions and changes to JSON as received from the API[edit | edit source]

The JSON structure provided in response to an API HTTP get request is significantly different than that which is defined in the <templatedata></templatedata>. In response to the API request, the MediaWiki software makes the following changes to the JSON object:

  1. An overall Pages Object containing one or more Page Objects
  2. An object with a numeric key: a Page 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. Add the sets key
  4. Change all TemplateText occurrences that are just strings to objects with a single key for the current wiki's Wikipedia language code
  5. Remove all inherits keys.
  • 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.
  1. 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[edit | edit source]

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
Key Object or
primitive type
Description
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
Key Object or
primitive type
Description
(page number) 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 (Addition by API)
The following key is added to the root of the JSON object which is enclosed in <templatedata> tags on the template page.
Key Object or
primitive type
Default Description
title string 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
Key Object or
primitive type
Description
label InterfaceText translated to InterfaceText object, if not already in that format.
required boolean If not defined, default value of false added.
description InterfaceText or null If defined as a string, converted to a InterfaceText object. If not defined, key is added with a null value.
deprecated boolean or string If not defined, default value of false added.
aliases Array of strings If not defined, a default empty array is assigned (i.e. [] ).
default string If not defined, default of "" is assigned.
type string If not defined, default of "unknown" is assigned.
inherits removed 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.
Key Object or
primitive type
Description
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) string Localized string for use on the Wikipedia corresponding to the Wikipedia language code.


Example of JSON object provided by API[edit | edit source]

The response to an API request for the TemplateData structure in the Example of TemplateData structure WikiText on template page section (above) can be seen:

Live Example[edit | edit source]

Template:Cite web:
source template page
<templatedata></templatedata> is transcluded from the template documentation page
API response in HTML format: https://en.wikipedia.org/w/api.php?action=templatedata&titles=Template:Cite%20web&format=jsonfm
Actual API response: https://en.wikipedia.org/w/api.php?action=templatedata&titles=Template:cite%20web

Configuration[edit | edit source]

variable default comment
$wgTemplateDataUseGUI false Experimental dialog interface to edit templatedata JSON