Module:WikidataIB/doc

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 should only be used inside an infobox.

Usage
The module provides these calls specifically for use in infoboxes at present: The call getSourcedValue is kept for backwards compatibility as it is now redundant to getValue which can do the same job using the true parameter.
 * 1)  - deprecated; use getValue instead.
 * 1)  - deprecated; use getValue instead.
 * 1)  - deprecated; use getValue instead.

There are also these utility calls:

Generalised calls:

Function getValue

 * getValue can also take a named parameter qid which is the Wikidata ID for the article. This will not normally be used but is available for testing, although it makes the call expensive.
 * The property to be returned is passed in the first unnamed property.
 * The name of the field that this function is called from is passed in the named parameter name, which is first checked against a blacklist of fields that are never to be displayed, (i.e. the call must return nil in all circumstances). If the field is not on the blacklist, it is then checked against a whitelist. If the name of the field matches, the call will return any locally supplied value if it is supplied as the second unnamed parameter, or the Wikidata value otherwise.
 * The name is compulsory when the blacklist or whitelist is used, so the module returns nil if it is not supplied.
 * The blacklist is passed in the named parameter suppressfields
 * The whitelist is passed in the named parameter fetchwikidata

The getValue function will accept a boolean parameter  which will suppress return of Wikidata values that are unsourced or only sourced to Wikipedia. The absence of the parameter, an empty parameter (onlysourced) and the empty string all default to true (i.e. only referenced values are returned). The values,   and   are treated as false (i.e. all values are returned); any other value is true (although yes is recommended for readability).

The getValue function will accept a boolean parameter  which will suppress the trailing "edit at Wikidata" icon for 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 (noicon) default to false (i.e. the icon is added). The empty string and the values ,   and   are treated as false; any other value is true (although true is recommended for readability).

In order to handle the requirement for dates in mdy, dmy or just year formats, getValue accepts a named parameter df that may take the values "dmy", "mdy", or "y" - default is "dmy".

As an article may require either of suffixes BC and BCE, getValue accepts a named parameter bc that may take the values "BC", or "BCE" - default is "BCE". Some test cases are shown at Module talk:WikidataIB.

Specific value-type handlers
The module has specific handlers for the following data types: Items that represent other types of data are returned using the mwdiawiki library call formatPropertyValues.
 * 1) Items that correspond to an article in some Wikipedia, called "wikibase-items". These will be linked to the corresponding (and disambiguated) article on English Wikipedia where possible.
 * 2) Items that represent dates.
 * 3) Items that represent Commons media, urls, external ids, or other sorts of plain text.

The third class of data types may be used with the parameters: If you don't supply linkprefix, then just prefix and postfix are used. For example, when getting the in : Use double-quotes to enclose the parameter value if it has leading or trailing spaces (otherwise they are stripped out). If you supply linkprefix, then all four parameters are used and a link is made for each value like this: That allows multiple links to be made to different sections of a list article, such as List of observatory codes. For example, when getting the in  we can make the links:
 * prefix, postfix, linkprefix, linkpostfix

Formatting multiple returned values

 * <yes 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 "[[ ..."
 * sep allows the separator between multiple returned values to be defined. The default is  (comma plus normal space). If the separator has leading or trailing spaces, enclose it in double quotes (e.g. " - "). Any double quotes are stripped from the separator. The pipe character  must be escaped as.
 * <hlist allows multiple returned values to be displayed as a horizontal list (hlist), or a vertical unbulleted list (ubl). These override the separator and do not display the 'pen icon' linked to "Edit at Wikidata"

Function getPreferredValue
The getPreferredValue function works exactly like getValue, taking the same parameters, but if any values for a property have the preferred rank set, it will only return those values.

Comparison example
Fetching the name(s) of the from :

Function getSourcedValue
This is now deprecated and retained only for backwards-compatibility. The getValue function now performs filtering of sources without references by default, but also allows setting no to see the unsourced values as well.
 * getSourcedValue works exactly as getValue does, but only returns values that have a reference to something other than a Wikipedia. That's not a guarantee of a reliable source, but it helps separate the wheat from the chaff.

Example of getSourcedValue
From :

Burton currently has four values for on Wikidata:
 * (sourced to Dutch Wikipedia);
 * (sourced to https://www.nytimes.com/1983/05/09/theater/theater-private-lives-burton-and-miss-taylor.html);
 * (sourced to English Wikipedia);
 * (unsourced).

Using getValue with no in Richard Burton: Using getSourcedValue in Richard Burton: Same as getValue's default setting:

Function getCoords

 * getCoords can also take a named parameter qid which is the Wikidata ID for the article. This will not normally be used but is available for testing, although it makes the call expensive.
 * The coordinates from Wikidata are parsed and passed to Template:Coord which returns the display as if it were called manually.
 * The name of the field that this function is called from is passed in the named parameter name, which is first checked against a blacklist of fields that are never to be displayed, (i.e. the call must return nil in all circumstances). If the field is not on the blacklist, it is then checked against a whitelist. If the name of the field matches, the call will return any locally supplied value if it is supplied as the unnamed parameter, or the Wikidata value otherwise.
 * The name is compulsory when the blacklist or whitelist is used, so the module returns nil if it is not supplied.
 * The blacklist is passed in the named parameter suppressfields
 * The whitelist is passed in the named parameter fetchwikidata

Function getQualifierValue
The getQualifierValue is for use when we want to fetch the value of a qualifier. We need to know the property and the value of the property that it qualifies. The parameters are:
 * The property ID passed in the unnamed parameter (or 1)
 * The target value for that property in pval
 * The qualifier ID for that target value in qual
 * The name of the field where its called from to implement whitelisting and blacklisting of the property in
 * The list of fields to be fetched ("whitelist") in fetchwikidata - accepts ALL to fetch all fields
 * Optional list of fields not to be displayed ("blacklist") in suppressfields
 * Optional boolean to specify whether only sourced values of the property are returned (defaults to "no") in onlysourced
 * Optional item ID for arbitrary access (expensive call!) in qid

Example of getQualifierValue
In there is a property, which has a value. That has two qualifiers, and. To get the start date: In South Pole Telescope it returns:

Function getLink
getLink returns the label for a Qid wiki-linked to the local article (if the article exists). If label doesn't exist, it returns the Qid wiki-linked to the local article (if the article exists).

Function getLabel
getLabel returns the label for a Qid. If label doesn't exist, it returns the Qid. Note that this is the label given to the Wikidata entry in the same language as the current Wikipedia, if the label exists.

Function getAT
getAT returns the article title for a Qid. If the article title doesn't exist, it returns nothing. Note that this is the title of the article in the current Wikipedia, if the interlanguage link exists in the Wikidata entry.

Function formatDate
formatDate accepts a datetime of the usual format from mw.wikibase.entity:formatPropertyValues, like "1 August 30 BCE" as parameter 1 and formats it according to the df (date format) and bc parameters.
 * df = "dmy" / "mdy" / "y" - default is "dmy"
 * bc = "BC" / "BCE" - default is "BCE"
 * df = "dmy" / "mdy" / "y" - default is "dmy"
 * bc = "BC" / "BCE" - default is "BCE"

Coding into an infobox
Typically, the getValue call will be invoked in an infobox definition, using appropriate template parameters. One simple implementation is given as an example in Template:Infobox book/Wikidata/Sandbox. As an illustration, the 'author' field in the infobox is coded like this:  The property to be fetched is the first unnamed parameter. In this case it is.
 * label2 = Author
 * data2 =

The name of the field is passed in name and that name is checked against the blacklist and the whitelist. To always suppress the author field in a particular article, an editor will set author in the infobox. The author field will then never be displayed.

If the field is not blacklisted, then the infobox can be set to display a locally supplied value for author simply by setting George Orwell, for example, in the infobox. It also accepts authors. If the name of the field is on the whitelist, e.g. author; genre; pub_date; pages; dewey; congress, and the local value is not supplied, then the infobox will display the value retrieved from Wikidata. Any separators can be used, except | and {}.

As a shorthand, ALL will fetch all of the fields that are not blacklisted, as long as no local value is already provided in the article for a given field.

Since Wikidata labels are normally lower case, the sentence case function from Module:String2 can be used to capitalise the first letter of the returned text, e.g.
 * in produces:

Example of calls in an infobox
Basic use of getValue:

Full collection of parameters: Any of the parameters can, of course, be be fixed for a given field in an infobox, rather than taking the parameter supplied to the infobox, which will affect all fields. For example, one field may set hlist where a series of short words is expected; whereas another field could use ubl where an unbulleted vertical list of several words on each line is required.

Coodinates
The getCoords call will display the output of Template:Coord when supplied with the coordinates returned from Wikidata.There's an example in Template:Sandbox/Infobox biosphere reserve where it's coded like this: 
 * label20 = Coordinates
 * data20 =

Upgrading existing infoboxes
Since the parameter fetchwikidata is needed for any Wikidata functionality, an existing infobox may be replaced by an infobox incorporating these calls without any change whatsoever to any article. Each article using the new infobox can later be enabled by supplying ALL, or a list of required fields for that article. At that point, the onus is on the editor enabling the functionality to check that no unwanted fields are now being displayed. If so, they can be added to a blacklist for the article by setting suppressfields to the list of unwanted fields.

Verifiability
Where it will always be essential for a particular field to only contain values that are referenced, use, making sure that onlysourced is not set to 'false', '0' or 'no'. By default it will exclude values that are unsourced or only sourced to a Wikipedia, thus making the job of checking easier at the article level. If unsourced data is acceptable (!), set no. As it is beyond my wit to produce an automated mechanism that knows whether an existing source is reliable or not in a given context, that job must still be performed at the article level by an editor familiar with the subject. It should always be done when first enabling Wikidata for that article.

Examples
This section is taken from Template:Infobox book/Wikidata/Sandbox/doc.

No Wikidata


Works as a non-aware infobox: only locally supplied parameters are displayed. 

The blacklist and whitelist can be omitted if unused

All Wikidata


Fetches all of its values from Wikidata. 

As shorthand, the fetchwikidata can be set to ALL to fetch all available fields. Any of them can be suppressed by suppressfields, or overridden by supplying a local value.

Never display genre


The genre field will always be suppressed, even if a local value is supplied. 

Local override


The genre field is set to display "Political satire", no matter what is stored in Wikidata. 

The genre field is set to display "Novel", no matter what is stored in Wikidata.

Don't fetch genre


The genre field will not be fetched from Wikidata.

Coordinates


Displays coordinates when used in an article where Wikidata has coordinates.