Mòdul:Wikidata/en/ús

De Viquipèdia
Salta a la navegació Salta a la cerca
Wikidata Stamp Rec Light.svg

This is the documentation of Module:Wikidata in English.

This module fetches formatted data from Wikidata. It works in local language and it has options for internationalization.

Functions[modifica]

Basic function:

  • claim: returns the value (or values) of a statement or a qualifier formatted with parameters or using a default format depending on the data type.

Other functions:

  • getLabel: returns the label of an item, with some optional parameters.
  • getParentValues: returns labels and parent values of a property, recursively.
  • linkWithParentLabel: creates a link with the label of a parent property. For example to link autor taxonòmic (P405) with the label abreviatura d'autor en zoologia (P835) from the parent item.
  • getTAValue: gets the TA98 (Terminologia Anatomica first edition 1998) values for property Terminologia Anatomica 98 (P1323). It takes no parameters and constructs its output as a list, with each value linked to an external resource.
  • yearsOld: returns the age of a person: difference between data de naixement (P569) and data de defunció (P570), when the two properties exist; or difference between data de naixement (P569) and current date, if there is no P570. Returns the completed years (nn) or range of years (nn/mm) depending on the precision of the dates.

Function claim[modifica]

Returns the value (or values) of a statement or a qualifier formatted with parameters or using a default format depending on the data type. By default, it refers to the Wikidata item (Qid) associated to the current page.

Full syntax:

{{#invoke:Wikidata|claim|item= |lang= |property= |qualifier= |value= |list= |tablesort= |formatting= |separator= |conjunction= |editicon= |showerrors= |default= }}

Additional syntax for table format:

{{#invoke:Wikidata|claim|item= |lang= |property= |qualifier= |qualifier2= |...|qualifierx= |formatting=table |tablesort= |sorting= |rowformat= |rowsubformat1= |...|rowsubformatx= |colformat0= |...|colformatx= |case0= |...|casex= |separator= |conjunction= |editicon= |showerrors= |default= |references= }}

General parameters[modifica]

  • item= (optional) Specifies an item (Qid) other than the associated to current page. Use it with moderation as arbitrary access to Wikidata is expensive. It can be defined in the parent frame of the template.
  • lang= (optional) Indicates the code of a given language. It can be defined in the parent frame of the template. By default, it uses the local language of the wiki. If there is no label in this language then it tries in the fall back languages ​​defined in MediaWiki. On the debug console type =mw.dumpObject(mw.language.getFallbacksFor(mw.getContentLanguage():getCode())) to see the fallback languages of your wiki. If the label has not been found in the preferred language, it adds a small pencil as an icon to translate it on Wikidata. This icon can be removed with the editicon parameter.
  • property= (required) Property id of the statement (Pid). It is possible to define alternatve properties with any separator, for example "P17 or P131", and it gets the first one found. It also accepts a minuscule p, but it is not recommended. It also accepts the property label, for example "property=country" is equivalent to "property=P17" for estat (P17).
  • qualifier= (optional) Qualifier (Pid) of a property value.
  • value= (optional) Value preferred to Wikidata one. It can be an optional parameter in a template with format {{{parameter|}}}, so if it exists then it takes the value of the parameter, if it is void then it gets the value of Wikidata.
  • references= (optional) With any value it will show the references of the value.
  • list= (optional):
    • list=true (default) shows a list of all the values (see separator and conjuction below).
    • list=false or list=no shows only one value, the best ranked (preferred / normal / obsolete) or the oldest of those with the highest rank.
    • list=firstrank shows a list of the best ranked values. It is equivalent to list=true if all values have the same rank. It is equivalent to list=false if only one value is best ranked.
    • list=lang for monolingual values, shows values corresponding to the language of the lang parameter. See parameters lang and formatting for monolingualtext.
    • tablesort=0 (optional) sorts the list in ascending order. By default, sorting is by rank (preferred, normal, obsolete), and by order in Wikidata.
  • separator= (optional) Separator to use in lists or tables. For lists the default is MediaWiki:Comma-separator (in English a comma and a blank space ', '). For tables the default is a line break <br />. In some specials cases, a <br /> may not work if it is included with strip markers. The alternative is to use the mark of a line feed control character: separator=LF.
  • conjunction= (optional) Conjunction to use as a separator between the last two elements of a list. By default it is equal to 'separator' if it is defined, otherwise it is MediaWiki:And plus MediaWiki:Word-separator (in English ' and '). For tables the default is a line break <br />.
  • itemgender= (optional) Item where to check sexe o gènere (P21) for using the feminine form if necessary. By default it is item. It is used in combination with case=gender.
  • editicon= (optional) By default 'true', optionnally 'false' or 'no'. It allows to remove the pencil icon Translate used to edit Wikidata when the label has not been found in the requested language.
  • showerrors= (optional) With any value, typically 'yes', it shows error messages, if any. By default it shows the 'default' parameter.
  • default= (optional) Text to show for any error if 'showerrors' parameter is not defined.

Parameters for formatting[modifica]

formatting= (optional). Options for formatting depending on the data type:

Data type entity:

  • formatting=raw Wikidata id (Qid or Pid) of the value.
  • formatting=label Label of the value in local language, its fall backs or as raw.
  • formatting=sitelink Title of the local site page, not linked. If there is no local page then it returns the format raw preffixed with interwiki link "wikidata:".
  • formatting=internallink Internal link whenever possibe, either to local page or to the label (possibly a red link). As a last option it links to Wikidata.
  • formatting=pattern Format using a pattern with $1 as a parameter to replace. It can include templates or parser functions with a non expanded format: {{((}}template{{!}}$1{{))}} or {{((}}template{{!}}parameter name{{=}}$1{{))}}, or alike.
The default format is a piped link with link target as sitelink and link label as label.
  • formatting=ucfirst Variant of the default piped link with uppercase in first letter of label. In a list, it is used only for the first value.
  • formatting=ucinternallink Both "ucfirst" and "internallink".

Data type string:

  • formatting=weblink Format of external link [http://example.com example.com].
  • formatting=pattern Format using a pattern with $1 as a parameter to replace, for example: formatting=[http://whc.unesco.org/en/list/$1 $1]. For identifiers statements with an autogenerated link, the pattern can be found in the talk page of the property. It can include templates or parser functions with a non expanded format (see above for entity).

Data type quantity:

By default it show the quantity.
  • formatting=unit Shows the quantity and the unit. It can show plural units if it is defined at Module:Wikidata/Units.
  • formatting=unitcode Shows the quantity and the code or abbreviation of the unit defined in símbol de la unitat (P5061). It can be changed locally at Module:Wikidata/Units. If not found it shows the unit name.
  • convert=Qid (optional) Convert the quantity to the unit requested by its Qid. Conversions are made according to factors indicated in Qid for properties conversió a unitats del SI (P2370) or conversió a unitats estàndard (P2442), except for temperatures defined with conversion formulas between ºC, ºF and ºK. For example, a value of "74 inch" with convert=Q174728 (centímetre (Q174728)) gives "188 centimeters" according to the conversion "2.54 centimeter" indicated in polzada (Q218593). It is rounded keeping the default precision of the original value according to the number of significant figures. It can be combined with formatting=unitcode. It can also be used with formatting=table, see below.

Data type monolingual (monolingualtext):

  • formatting=language Returns the language code of a monolingual value, for example used in nom oficial (P1448).
  • formatting=text Returns the text wrapped in a span with lang attribute, if it's different from the local language: <span lang="xx">foreign text</span>.
  • formatting=pattern Format using a pattern with $language and $text as parameters to be replaced, for exemple "formatting=($language) $text".
  • list=lang Shows the values corresponding to the defined language or the default language. See lang parameter above.

Data type coordinates (globecoordinate):

  • formatting=latitude Returns the latitude in decimal format.
  • formatting=longitude Returns the longitude in decimal format.
  • formatting=globe (default) Returns the globe parameter recognized by Template:Coord. It is requested for coordinates of globes other than Earth.
  • formatting=$lat...$lon...$globe Format using a pattern with $lat, $lon and optionally $globe as parameters to be replaced. It can include templates with non expanded format, i.ex. {{((}}coord{{!}}$lat{{!}}$lon{{))}}.
  • formatting=dimension Returns the dimension in meters equivalent to dim parameter of GeoHack.

Data type datetime (labeled time):

By default it uses the format defined Module:Wikidata/i18n, in datetime table, depending on the precision. Optionally, for precision of day:
  • formatting= #time syntax Accepts any valid format of #time parser function. For example: formatting=d-m-Y (10-12-2019), formatting=[[j xg]] (10 de desembre), formatting=Y (2019).

Data type musical notation:

Returns the content wrapped with tag <score> that generates an image of the score. See mw:Extension:Score and examples in motiu musical (P6686). Format options:
  • formatting=sound (optional) Adds the attribute sound="1" to the tag embeding an audio player after the score image.

Parameters of table with propiety and qualifiers[modifica]

property qualifier1 qualifier2 etc.
values 1st statement $0 $1 $2, etc.
2nd, etc. $0 $1 $2, etc.

formatting=table A statement with qualifiers is considered as a table of values shown at right. You can define the format of the rows and each column in the table. Separators for rows by default are line breaks (see separator and conjunction above). If you really want the output in a wikitable you can use separator=</tr><tr> and the corresponding opening and closing tags before and after the invoke. Specific parameters for this format:

  • qualifier1 ... qualifierx= Qualifiers with consecutive numbers without any limitation. General parameter qualifier is an alias for qualifier1.
    • qualifierx = Pid1 OR Pid2 Alternatives values for a qualifier, without limitation of successive ORs. It takes the first one found. A tyical example is (P585) OR data d'inici (P580).
    • qualifierx = Pid1/Pid2 From the entity value of property Pid1 it gets the parent value Pid2. This allows to obtain additional data in the table even though defined in a parent element. For example, to get railway lines with its icon: qualifier=P81/P154. It can be combined with the OR option taking priority the OR.
    • qualifierx = /Pid Equivalent to previous one but without Pid1, it gets another propiety of same entity. This allows to obtain values defined either in a qualifier or in a property.
  • rowformat= Format of the rows of values for property and qualifiers. The property uses the placeholder $0 and the qualifiers $1 to $x. By default it is rowformat=$0 ($1, ... $x). You can include list tags * or # and also templates or parser functions not expanded: {{((}}template{{!}}unnamed parameter{{!}}name{{=}}named parameter{{))}}
  • rowsubformat1 ... rowsubformatx= Previous format to apply to $1-$x defined in rowformat, only if there is any value. For example "rowformat=$0 $1" and "rowsubformat1=($1)" gives "$0 ($1)" or "$0" if $1 is void, avoiding empty parenthesis. A rowsubformat may have several variables $1-$x. Note that substitutions are made in order: 1st rowsubformat0, 2nd $0 in rowformat, 3rd rowsubformat1, 4th $1 in rowformat, etc.
  • colformat0 ... colformatx= Format to apply to each column, using 0 for the property and 1-x for every qualifier. It accepts the same syntax as formatting for each data type. See above #Parameters for formatting.
  • case0 ... casex= Grammatical case to apply to each column. Parameter case, without numbering, applies to all values. See above #General parameters.
  • convert0 ... convertx= Conversion of units to apply to each column. See above #Parameters for formatting, data type quantity.
  • whitelist0 ... whitelistx= List of items to show in a column, with a slash (/) as separator.
  • blacklist0 ... blacklistx= List of items to do not show in a column, with a slash (/) as separator. If a whitelist is used in a column and a blacklist in another column then results may be inexpected (vegeu un see a comment, in Catalan).
  • tablesort= with values 0 to x, it sorts the table per property (0) or per qualifier (1 to x). Sorting is in ascending order alphabetically, numerically or by date. It accepts multiple sorting keys with a slash / as separator, i.e. tablesort=0/2/1. By default it sorts the property by rank (preferred, normal, obsolete), and by order in Wikidata.
  • sorting=-1 sorts in descending order. With any other value, or by default, it sorts in ascending order. If tablesort is used with multiple keys, the sorting order is applied to all the keys.
  • list=false shows only the first row of the table, according to the order indicated or by default.

Function getLabel[modifica]

Returns the label of an item.

  • 1= (first positional parameter, required) Wikitada id of the item (Qid or Pid).
  • lang= (optional) Language code, as in function claim. See above #General parameters.
  • itemgender= (optional) Qid to determine the gender form to use for the label, as in function claim. See above #General parameters.
  • linked= (optional) With any value, except 'no', it returns the label linked either to sitelink page or to Wikidata.
  • label= (optional) Shows this label instead of one fetched from Wikidata. It makes sense if used with linked=yes.
  • editicon= (optional) allows to remove the pencil edit icon, as in function claim. See above #General parameters.

Function getParentValues[modifica]

Returns recursively values of a property labeled with a parent value.

  • item= (optional) Specifies an item (Qid) other than the associated to current page. Use it with moderation as arbitrary access to Wikidata is expensive.
  • property= (optional) Property id of the statement (Pid). By default it is localitzat a l'entitat territorial administrativa (P131). Supports a list of alternative values ​​separated by a blank space or a slash.
  • label= (optional) Parent property id to use as label. By defaut it is instància de (P31).
  • valuetext= (optional) Property to use as link label with 'property' value as link target. By default it is formatted as an internal link (see above formatting=internallink in function claim).
  • upto= (optional) Last iteration to fetch, if previously there no 'property' found. Possible values:
    • upto=label: last 'label' to fetch, for example "upto=country".
    • upto=quantity: maximum number of iterations. By default it is 10 as a protection, large enough for commons cases.
  • uptolinkid= (optional) Alternative to upto=label using Qid instead. It supports multiple values ​​separated by a blank space or a slash.
  • labelshow= (optional) White list of labels to show with a slash as a separator.
  • showlabelid= (optional) Alternative to labelshow using Qid instead.
  • include_self=true (optional) Includes current page on the output list.
  • sorting=-1 (optional) Sorts the list of paired values in descending order.
  • last_only=true (optional) Only shows the last value fetched. For example with uptolinkid=Qid and last_only=true it will show the value correspondig to label Qid.
  • rowformat= (optional) Output format for each pair of values, with placeholder $0 for the label and $1 for the value. By default it is "rowformat=$0 = $1", for example "country = [[France]]"
  • separator= (optional) Separator for each pair of label-value, by default <br />.
  • cascade=true (optional) Output with indentation. It may be useful if rowformat only uses $1.

Note: labels can be fixed for needs of an infobox in Module:Wikidata/labels. For example, in Catalan "Municipi del Brasil" is modified to "Municipi" removing the determiner introduced by a preposition.

Function linkWithParentLabel[modifica]

Creates a link with the label of a parent property.

It accepts most parameters of function claim, except "formatting" that uses the default "internallink". Additionally:

  • parent= the parent property id to use as a link label.

Function yearsOld[modifica]

Returns the age of a person calculating the completed years between birth date and death date or current date. If both dates do not have presicion of day then it show a range of years, maximum a decade.

The syntax is: {{#invoke:Wikidata|yearsOld|formatting= |item= }}. Parameters item and formatting are optionals. By default it show the quantity.

  • formatting=unit (optional) Gets the unit from table i18n["years-old"] at Module:Wikidata/i18n for singular or plural, in addition to paucal used in Slavic languages as Russian. See parser function PLURAL at mw:Help:Magic words#Localization.
  • formatting=pattern (optional) Format using a pattern with $1 as a placeholder for the quantity. A common use case is " ($1 years old)". It does not evaluate the expression in singular, plural or paucal amb fetches the unit from anys (Q24564698). As an alternative you can define a global pattern in table i18n["years-old"] that allows the parameter $1.

Function lang[modifica]

Returns the language code as managed by function claim. See #General parameters above.

Syntax: {{#invoke:Wikidata|lang|{{{lang|}}}}}. Parameter lang is optional.

Wikidata redirects[modifica]

A Wikidata element can be a redirect to another element, often as a result of a merge. The values ​​of a property can be provisionally a redirect, usually fixed by bot after a week. The treatment of redirects is as follows:

  • Function claim
    • With formatting=raw it returns the target of the redirect.
    • With parameters whitelist or blacklist, if the list of values ​​includes any redirect, it considers both the redirected Qid and the target Qid.
  • Function getLabel
    • It follows the redirected Qid to fetch the label from the target Qid.

All redirected Qids should be fixed in templates. There is a tracking in place to find them at Special:WhatLinksHere/Template:Track/wikidata/redirect.

In addition, for technical needs of redirection treatment, if an element has no label in the requested language or in any of the default alternative languages, add the Special: WhatLinksHere / Template: Track / wikidata / label crawl . In these cases it is necessary to add a tag to Wikidata to avoid features with high resource consumption.

Resolving redirects is pending of phab:T157868. Current technical solution may be expensive if there is no label in local language nor in fall back languages. It is recommanded to check the provisional tracking Special:WhatLinksHere/Template:Track/wikidata/label and add missing labels.

Using from another module[modifica]

All functions (claim, getLabel, getParentValues, linkWithParentLabel, yearsOld, lang) can be used from other modules via require with the same parameters provided in a table, for example:

require("Module:Wikidata").claim{item="Q...", property="P...", ...}
require("Module:Wikidata").getLabel({"Q...", ['lang']="en"})

Dependencies[modifica]

See also[modifica]