Module:WikidataIB/doc: Difference between revisions

Content deleted Content added
now that values are returned in normal case, we can use ucfirst to capitalise just the first letter
 
(49 intermediate revisions by 18 users not shown)
Line 1:
{{Module rating |protected}}
{{#ifeq:{{SUBPAGENAME}}|sandbox||{{High-use}}}}
{{high risk|800,000|all-pages=yes}}
{{ombox|text=For a more user-friendly wrapper of this module see {{t|wdib}}.}}
This module is designed specifically to implement a mechanism which moves control of whether Wikidata values are used in an infobox from the template coder at the infobox design level to the editor at the article level. It is only intended to be used inside an infobox.
 
This module is intended to be used inside {{t|infobox}} (letters Wikidata'''IB''' stand for ''Info Box'', hence the name) or other templates and designed specifically to allow editors of an article (as opposed to editors of the infobox the article uses) to control whether Wikidata values are displayed.
One of the three sandboxes should be used for testing anything other than trivial amendments.
 
One of the two sandboxes should be used for testing anything other than trivial amendments.
Test cases for the main module and two of the sandboxes are available.
 
Test examples for the main module and the two sandboxes are available.
 
{| class="wikitable plainrowheaders
Line 25 ⟶ 27:
| [[Module:WikidataIB/sandbox1]] ({{Compare pages|Module:WikidataIB|Module:WikidataIB/sandbox1|diff}})
| [[Module talk:WikidataIB/sandbox1/testing]] ({{Compare pages|Module talk:WikidataIB/testing|Module talk:WikidataIB/sandbox1/testing|diff}})
|-
! scope="row" | sandbox2
| [[Module:WikidataIB/sandbox2]] ({{Compare pages|Module:WikidataIB|Module:WikidataIB/sandbox2|diff}})
|
|}
 
Line 41 ⟶ 39:
# <code>getAwardCat</code> if the item has values of P166 (award received), then it examines each of those awards for P2517 (category for recipients of this award) and it returns the corresponding category, with the item's P734 (family name) as sort key, or no sort key if there is no family name
# <code>getIntersectCat</code> for each value of the prop1 it fetches the value's main category and then each value of prop2, then it returns all of the categories representing the intersection of those properties
# <code>getSumOfParts</code> scans the property 'has part' (P527) for values matching a list,. If the matched values have a qualifier 'quantity' (P1114), those quantities are summed and returned (but zero returns nil)
# <code>getCoords</code> - gets coordinates and passes them through Template:{{t|Coord}}
# <code>getPreferredValue</code> - {{color|red|'''Deprecated'''}} but retained for backward compatibility. Use the <code>|getValue|rank=best</code> parameters instead
The obsolete call getSourcedValue has now been removed as it is redundant to getValue which can do the same job using the {{para|onlysourced|true}} parameter (which is set by default). The deprecated call getPreferredValue is still retained, but should be replaced by <code>getValue|rank=best</code>.
The obsolete call getSourcedValue has now been removed as it is redundant to getValue which can do the same job using the {{para|onlysourced|true}} parameter (which is set by default).
 
===Utilities functions===
There are also these utility calls:
# <code>getLink</code> if there is a sitelink to an article on the local Wiki, it returns a link to the article with the Wikidata label as the displayed text. If there is no sitelink, it returns the label as plain text. If there is no label in the local language, it returns the entity-ID
# <code>getAT</code> (Article Title) If there is a sitelink to an article on the local Wiki, it returns the sitelink as plain text, otherwise nothing
Line 52 ⟶ 50:
# <code>getAllLabels</code> fetches the set of labels and formats it for display as wikitext
# <code>labelorid</code> returns the label with all wikitext removed, or the entity-ID if no label
# <code>getDescription</code> returns the article description for the Wikidata entity if the local parameter is "Wikidata".
# <code>getAllDescriptions</code> fetches the set of descriptions and formats it for display as wikitext
# <code>getAliases</code> returns the aliases for the entity in the current or given language
Line 58 ⟶ 56:
# <code>pageId</code> returns the connected Wikidata page id (entity-ID, Q-number) of the current page
# <code>formatDate</code> takes a datetime of the usual format from mw.wikibase.entity:formatPropertyValues and formats it according to the df (date format) and bc parameters
# <code>formatNumber</code> formats a number according to the the supplied language code
# <code>checkBlacklist</code> returns true if the field is ''not'' blacklisted (i.e. allowed)
# <code>emptyor</code> returns nil if the parameter is just punctuation, whitespace or html tags, otherwise returns the argument unchanged
Line 67 ⟶ 65:
# <code>followQid</code> given a list of properties, looks for each property in turn and returns the entity-ID of the first value that matches (optionally, returns all entity-IDs that match)
# <code>getGlobe</code> returns the entity-ID of the globe used in P625 (coordinate ___location), or nil if there isn't one
# <code>getCommonsLink</code> returns one of the following in order of preference: the Commons sitelink of the linked Wikidata item; the Commons sitelink of the topic's main category of the linked Wikidata item;
# <code>siteID</code> returns the root of the globalSiteID, e.g. "en" for "enwiki", "enwikisource", "en-gb", etc.
# <code>projID</code> same as siteID
Line 78 ⟶ 76:
# <code>showNoLinks</code> displays the article titles that should not be linked
 
===Examples of calls:===
<nowiki>{{#invoke:WikidataIB |getValue |<PropertyID> |name=<fieldname> |suppressfields=<list of fields which will never display> |fetchwikidata=<list of fields to fetch values from Wikidata> |onlysourced=<yes/no> |noicon=<yes/no> |df=<dmy/mdy/y> |bc=<BC/BCE> |qual=<ALL/DATES/P999> |list=<ubl/hlist/prose> |linked=<yes/no> |<local parameter>}}</nowiki>
 
Line 88 ⟶ 86:
 
<nowiki>{{#invoke:WikidataIB |getValueByLang |<PropertyID> |lang=<language code to match> |name=<fieldname> |suppressfields=<list of fields which will never display> |fetchwikidata=<list of fields to fetch values from Wikidata> |onlysourced=<yes/no>}}</nowiki>
 
 
== Function getValue ==
=== Parameters to getValue ===
{| class ="wikitable sortable"
|-
! scope="col" | Name
Line 113 ⟶ 110:
|
| The Q-number (entity-ID) of the entity that the property belongs to. If not supplied or empty, defaults to the associated Wikidata entry of the current page – uses [[:mw:Extension:Wikibase Client/Lua #mw.wikibase.getEntityIdForCurrentPage|mw.wikibase.getEntityIdForCurrentPage()]].
| Item id for current&nbsp;page
|-
| eid
|
| An alternative to qid. Except for <code>eid</code>, all parameters to getValue treat nil and the empty string the same. So, setting {{para|x}} gives the same result as omitting the parameter. However, to provide some compatibility with other modules, using {{para|eid}} always returns an empty string, while omitting <code>eid</code> allows <code>qid</code> to work as normal.
| Item id for current&nbsp;page
|-
Line 147 ⟶ 149:
| onlysourced
| osd
| A boolean which will filter out Wikidata values that are unsourced or only sourced to Wikipedia. [[Wikipedia:Wikidata/2018_Infobox_RfC#Discussion|This 2018 RFC]] requires that Wikidata values displayed in article infoboxes must be sourced. Values <code>no</code>, <code>false</code> and <code>0</code> are all false; anything else is true.
| true
|-
Line 183 ⟶ 185:
| sn
| A boolean that enables the use of shortname (P1813) instead of label for a linked item. Values <code>no</code>, <code>false</code> and <code>0</code> are all false; anything else is true.
| false
|-
| uselabel
| uselbl
| A boolean that forces the display of the label instead of the disambiguated sitelink for a linked item. Labels are much more prone to vandalism that sitelinks. Values <code>no</code>, <code>false</code> and <code>0</code> are all false; anything else is true.
| false
|-
Line 191 ⟶ 198:
|-
| unitabbr
| uabbr
| A boolean that enables unit abbreviations for common units. Values <code>no</code>, <code>false</code> and <code>0</code> are all false; anything else is true.
| false
Line 272 ⟶ 279:
| noicon
|
| A boolean which will suppress the trailing "edit at Wikidata" pen-icon. Useful for when the returned value is to be further processed. Values <code>no</code>, <code>false</code> and <code>0</code> are all false (i.e. shows the icon); anything else is true (i.e. suppresses the icon).
| false
|-
| list
|
| The name of a template that the list of multiple values is then passed through. Examples include "hlistcslist", and"hlist", "ubl"., Setting"blist", it to"olist", "prosep-1". producesA somethingspecial likevalue, <code>prose</code>, produces "1, 2, 3, and 4".
| none
|-
Line 304 ⟶ 311:
| Allows an unlinked value to be returned in the chosen language. Takes a standard ISO language code recognised by MediaWiki. If not supplied or blank, the local language (or set language for multi-lingual wikis) is used as normal.
| local language
|-
| parameterset
| ps
| Convenience parameter to allow commonly used sets of parameters to be specified with a single parameter: ps=1 gets a simple linked value wherever possible; ps=2 represents a plain text value. See [[#Parameter sets|Parameter sets]].
|
|-
| linkredir
|
| Boolean to switch on or off the check for a redirect with the same name as the label when there is no sitelink on Wikidata for the value. Values <code>no</code>, <code>false</code> and <code>0</code> are all false; anything else is true.
| false
|}
 
Line 319 ⟶ 336:
 
=== Sourcing ===
The getValue function will accept a boolean parameter <code>onlysourced</code> which will suppress return of Wikidata values that are unsourced or only sourced to Wikipediaa Wikimedia project. The absence of the parameter, an empty parameter ({{para|onlysourced}}) and the empty string (<code>""</code>) all default to true (i.e. only referenced values are returned). The values <code>no</code>, <code>false</code> and <code>0</code> are treated as false (i.e. all values are returned); any other value is true (although {{para|onlysourced|yes/no}} is recommended for readability).
 
=== Link to Wikidata ===
The getValue function will accept a boolean parameter <code>noicon</code> which will suppress the trailing "edit at Wikidata" icon and link for cases when the returned value is to be further processed by the infobox (e.g. a url). The absence of the parameter or an empty parameter ({{para|noicon}}) default to false (i.e. the icon is added). The empty string (<code>""</code>) and the values <code>no</code>, <code>false</code> and <code>0</code> are treated as false; any other value is true (although {{para|noicon|true}} is recommended for readability).
 
Following a discussion at [[Module talk:WikidataIB #Visibility of pen icon]], the pen icon is hidden from users who are not autoconfirmed. This means that most readers don't see the pen icon, and represents a balance between aesthetics and vandalism at Wikidata on the one hand, and the desire to encourage editing Wikidata on the other.
 
=== Dates ===
Line 355 ⟶ 374:
* {{para|sorted|<yes{{!}}no>}} is a boolean passed to enable sorting of the values returned. No parameter, or an empty string, or "false", or "no", or "0" disables sorting. It's only a very dumb alphabetical sort and sorts linked values as "[[ ..."
* {{para|sep|<separator characters>}} allows the separator between multiple returned values to be defined. The default is <code>", "</code> (comma plus normal space). If the separator has leading or trailing spaces, enclose it in double quotes (e.g. {{para|sep|" - "}}). Any double quotes are stripped from the separator. The pipe character (<code>|</code>) must be escaped as <code><nowiki>{{!}}</nowiki></code>. For reasons of accessibility (see [[MOS:PLIST]]), do not use {{para|sep|&lt;br>}} for vertical unbulleted lists; use {{para|list|ubl}} instead.
* {{para|list|<prose{{!}}cslist{{!}}hlist{{!}}ubl{{!}}blist{{!}}olist>}} allows multiple returned values to be displayed as a sentence with last two values separated by "and" ({{para|list|prose}}), a horizontal comma-separated list ({{para|list|hlistcslist}}, not to be used in prose), ora horizontal list ({{para|list|hlist}}), a vertical unbulleted list ({{para|list|ubl}}), a vertical bulleted list ({{para|list|blist}}), or a vertical ordered list ({{para|list|olist}}). These override the separator and do not display the 'pen icon' linked to "Edit at Wikidata".
* {{para|list|p-1}} displays the last value. Combine with {{para|maxvals|''n''}} to display the ''n''th value.
 
=== Limiting the returned values ===
Line 375 ⟶ 395:
* <code>spf → suppressfields</code>
* <code>wdl → wdlinks</code>
 
=== Parameter sets ===
Generally, <code>getValue</code> has a set of defaults for its parameters that represent consensus decisions by editors. For example, {{para|onlysourced}} defaults to <code>true</code> so only Wikidata values that are sourced to something better than "Wikipedia" will be returned, and {{para|fetchwikidata}} defaults to <code>none</code> so nothing is returned until it is enabled by setting some field names or "ALL". This represents the fail-safe condition and allows infoboxes to be made Wikidata-capable without changing any article until enabled ''for that article''.
 
To simplify the use of <code>getValue</code> in other circumstances, common combinations of parameters can be specified with {{para|parameterset}} or its alias {{para|ps}} for convenience. Two combinations are implemented at present and these are:
;ps=1 : a common set of overrides to get a simple value, linked where possible:
<pre>
|rank ="best"
|fetchwikidata="ALL"
|onlysourced ="no"
|noicon ="true"
</pre>
 
;ps=2 : a sort of raw value in plain text:
<pre>
|rank = "best"
|fetchwikidata = "ALL"
|onlysourced = "no"
|noicon = "true"
|linked = "no"
|plaindate = "true"
</pre>
Other sets could be created if there is a demand.
 
=== Wrapper template ===
Line 388 ⟶ 431:
* The coordinates from Wikidata are parsed and passed to [[Template:Coord]] which returns the display as if it were called manually.
* The blacklist of fields that are never to be displayed, and the whitelist are implemented in the same way as for getValue using {{para|suppressfields}} and {{para|fetchwikidata}}
* The <code>format</code> parameter sets the display format to decimal or dms. Any value beginning with "dec" sets decimal; anything else sets dms.
* The <code>display</code> parameter sets the display position to "inline", "title" or "inline, title". Default is nothing (so uses default for {{tl|Coord}}, currently "inline").
 
=== Function getQualifierValue ===
Line 402 ⟶ 447:
==== Example of getQualifierValue ====
In {{Q|Q1513315}} there is a property {{Q|P793}}, which has a value {{Q|Q385378}}. That has two qualifiers, {{Q|P580}} and {{Q|P582}}. To get the start date:
* <code><nowiki>{{#invoke:WikidataIB |getQualifierValue |qid=Q1513315 |P793 |pval=Q385378 |qual=P580 |name=xyz |fetchwikidata=ALL }}</nowiki></code>
In [[South Pole Telescope]] it returns:
* {{#invoke:WikidataIB |getQualifierValue |P793 |pval=Q385378 |qual=P580 |name=xyz |fetchwikidata=ALL |qid=Q1513315}}
Line 417 ⟶ 462:
 
==== Example of getValueByQual ====
In {{Q|Q10990}} there is a property {{Q|P443}} that has multiple values, each of which has a qualifier {{Q|P407}}. We can return the property value whose qualifier has the value {{Q|Q797913955}}
* <code><nowiki>{{#invoke:WikidataIB |getValueByQual |qid=Q10990 |P443 |qualID=P407 |qvalue=Q7979Q13955 |fwd=ALL |osd=no |noicon=true}}</nowiki></code> → {{#invoke:WikidataIB/sandbox |getValueByQual |qid=Q10990 |P443 |qualID=P407 |qvalue=Q7979Q13955 |fwd=ALL |osd=no |noicon=true}}
 
=== Function getValueByLang ===
The getValueByLang function returns the value of a property which has a qualifier {{Q|P407}} whose value has the given language code. The parameters are:
* The property ID passed in the unnamed parameter (or {{para|1}})
* The MediaWiki language code{{Q|P424}} to match the language whose code is given by {{para|lang|xx[-yy]}}. If no code is supplied, it uses the default language.
* The same parameters to implement whitelisting and blacklisting of the property as in getValue
* Optional boolean to specify whether only sourced values of the property are returned (defaults to "no") in {{para|onlysourced}}
* Optional item ID for arbitrary access in {{para|qid}}
* The same parameters to format output as in getValue
 
==== Example of getValueByLang ====
In {{Q|Q7565108}} there is a property {{Q|P856}} that has multiple values, each of which has a qualifier {{Q|P407}}. We can return the property value whose {{Q|P407}} qualifier value (a WD item) itself has the {{Q|P424}} property that is "ja", i.e, {{Q|Q5287}}
* <code><nowiki>{{#invoke:WikidataIB |getValueByLang |qid=Q7565108 |P856 |lang=ja |fwd=ALL |osd=no |noicon=true}}</nowiki></code> → {{#invoke:WikidataIB |getValueByLang |qid=Q7565108 |P856 |lang=ja |fwd=ALL |osd=no |noicon=true}}
If {{para|lang}} is unspecified, we can obtain the same value with the default language (here that is the {{Q|Q1860}} and its {{Q|P424}} is "en")
* <code><nowiki>{{#invoke:WikidataIB |getValueByLang |qid=Q7565108 |P856 |fwd=ALL |osd=no |noicon=true}}</nowiki></code> → {{#invoke:WikidataIB |getValueByLang |qid=Q7565108 |P856 |fwd=ALL |osd=no |noicon=true}}
 
== Utility functions ==
Line 442 ⟶ 493:
 
=== Function getLabel ===
getLabel has the qid of a Wikidata entity passed as the first unnamed parameter or as {{para|qid=}}
 
It returns the Wikidata label forin the local language asfor an item by the plaingiven textqid.
If there is no label in the local language, it displaysreturns the qid instead.
Note that this is the label given to the Wikidata entry in the same language as the current Wiki, if the label exists.
: Wikidata: {{q|Q29016906}} and {{q|Q3621491}}
* <code><nowiki>{{#invoke:WikidataIB |getLabel |Q29016906}}</nowiki></code> → {{#invoke:WikidataIB |getLabel |Q29016906}}
* <code><nowiki>{{#invoke:WikidataIB |getLabel |Q3621491}}</nowiki></code> → {{#invoke:WikidataIB |getLabel |Q3621491}}
* <code><nowiki>{{#invoke:WikidataIB |getLabel |Q19805408}}</nowiki></code> → {{#invoke:WikidataIB |getLabel |Q19805408}}
 
=== Function label ===
label has the qid of a Wikidata entity passed as the first unnamed parameter or as {{para|qid}}
 
It returns the Wikidata label in the local language for an item by the given qid or linked to the current page.
If there is no label in the local language, it returns an empty string.
Note that this is the label given to the Wikidata entry in the same language as the current Wiki, if the label exists.
: Wikidata: {{q|Q29016906}} and {{q|Q3621491}}
* <code><nowiki>{{#invoke:WikidataIB |label |Q29016906}}</nowiki></code> → {{#invoke:WikidataIB |label |Q29016906}}
* <code><nowiki>{{#invoke:WikidataIB |label |Q3621491}}</nowiki></code> → {{#invoke:WikidataIB |label |Q3621491}}
* <code><nowiki>{{#invoke:WikidataIB |label |Q19805408}}</nowiki></code> → {{#invoke:WikidataIB |label |Q19805408}}
 
=== Function getAT ===
Line 534 ⟶ 597:
}}{{nowiki template demo|prefix=#|format=inline|output=nowiki+|code=<nowiki>{{#invoke:WikidataIB |url2 |url= http://www.example.com/path/ }}</nowiki>
}}{{nowiki template demo|prefix=#|format=inline|output=nowiki+|code=<nowiki>{{#invoke:WikidataIB |url2 |url= {{wdib |P856 |qid=Q23317 |fwd=ALL |osd=no}} }}</nowiki>
}}{{nowiki template demo|prefix=#:|format=inline|output=nowiki+|code=<nowiki>{{wdib |P856 |qid=Q23317 |fwd=ALL |osd=no}}</nowiki>}}
}}{{nowiki template demo|prefix=#|format=inline|output=nowiki+|code=<nowiki>{{#invoke:WikidataIB |url2 |url= {{url|http://www.example.com/}} }}</nowiki>
}}
 
Comparison with output of {{tl|URL}}:
Line 541 ⟶ 606:
}}{{nowiki template demo|prefix=#|format=inline|output=nowiki+|code=<nowiki>{{URL | {{wikidata|property|Q23317|P856}} }}</nowiki>
}}{{nowiki template demo|prefix=#:|format=inline|output=nowiki+|code=<nowiki>{{wikidata|property|Q23317|P856}}</nowiki>}}
 
==== See also ====
* {{ml|URL|url}} & {{ml|URL|url2}}
 
== Coding into an infobox ==
Line 599 ⟶ 667:
; [[Template:If then wikilink]] : tests whether the first unnamed parameter is not an empty string and if it isn't, it returns the parameter formatted as piped wiki-link using an optional namespace prefix.
; [[Template:Formatter link]] : takes an external identifier code as {{para|code}} and uses a formatter url as {{para|url}} to construct a link to the external resource, which uses the code as display.
; [[Template:Emptyor]] : tests a piece of text to ascertain whether it's effectively empty or contains some text. If the unnamed parameter consists only of html tags, punctuation (e.g. Wiki-wiki markup) and whitespace, then Emptyor returns nothing; otherwise it returns the parameter unchanged. Wrapper for p.emptyor function.
 
== Example of use: Infobox book ==
Line 692 ⟶ 760:
</pre>
The genre field will not be fetched from Wikidata. Only the author, publication date, number of pages, Dewey index, and Library of Congress catalogue number are imported. A local value for genre will display.
 
== Exporting the module ==
WikidataIB has been developed to run on any language wiki (or Wikimedia project) with a minimum of modification. An optional sub-module [[Module:WikidataIB/i18n]] may be created to replace the values for error messages, ordinal suffixes etc. as shown in the local i18n definitions in the module. Although the same result can be arrived at by directly editing those values in WikidataIB, using the sub-module will allow an updated version of WikidataIB to directly replace the older module without having to re-edit the i18n definitions.
 
=== Linked items ===
If the target Wiki has a convention that certain items are not normally linked (see [[:en:Wikipedia:Manual of Style/Linking #What generally should not be linked]] for an example), then a sub-module [[Module:WikidataIB/nolinks]] can be created to list items that should not be linked, using the English Wikipedia sub-module as an exemplar.
 
=== Italic titles ===
If the target Wiki has a convention that certain works should be italicised or quoted (see [[:en:Wikipedia:Manual of Style/Text formatting #Names and titles]] for an example), then a sub-module [[Module:WikidataIB/titleformats]] can be created to list items that should be italicised or quoted, using the English Wikipedia sub-module as an exemplar.
 
=== Complex date ===
Most of the output from the module will use the local language (or any user-specified one on multi-lingual wikis) where the sitelink or label exists on Wikidata. However, dates are more complicated and not handled perfectly in all languages. To solve this, WikidataIB uses [[Module:Complex date]] – developed on Commons by {{u|Jarekt}} – which allows expansion to support a wider variety of languages. As a result, it is necessary to install Complex date and its dependencies alongside WikidataIB when installing on another Wikimedia project.
 
==== Complex date dependencies ====
[[Module:Complex_date]] relies on the following modules:
* [[Module:ISOdate]]
** [[Module:DateI18n]] (no dependencies)
* [[Module:i18n/complex date]]
** [[Module:Ordinal]] (lazy loading)
*** [[Module:I18n/ordinal]] (no dependencies)
*** [[Module:Yesno]] (from en.wikipedia) (no dependencies)
*** [[Module:Formatnum]] (no dependencies)
** [[Module:Roman]] (lazy loading with no dependencies)
Unless otherwise noted, the authoritative version of each module can be found on [https://commons.wikimedia.org/ Commons] although the versions on English Wikipedia are usually kept in sync.
 
==See also==
* [[Module:Wikidata]], handling Wikidata more basically
* [[Template:WikidataOI]], a template that uses [[Module:Wd]] but also has and "opt-in" options {{para|fetch}} and {{para|ifeq}} similar to this module's {{para|fetchwikidata}} or {{para|fwd}}
 
==Tracking categories==