Modifica di Modulo:Wikidata/doc

Attenzione: non hai effettuato l'accesso. Se effettuerai delle modifiche il tuo indirizzo IP sarà visibile pubblicamente. Se accedi o crei un'utenza, le tue modifiche saranno attribuite al tuo nome utente, insieme ad altri benefici.

Questa modifica può essere annullata. Controlla le differenze mostrate sotto fra le due versioni per essere certo che il contenuto corrisponda a quanto desiderato, e quindi pubblicare le modifiche per completare la procedura di annullamento.

Versione attuale Il tuo testo
Riga 1: Riga 1:
{{Ombox|image=[[Image:Stop hand nuvola.svg|40px]]|type=delete|text=[[Module:Wikidata]] is [[deprecation|deprecated]]. Please use [[Module:WikidataIB]] or [[Module:Wd]] instead.}}
Module permitting basic data retrieval from items. It is mostly useful for other modules or for {{tl|Data}}
{{module rating|protected}}
{{High-use}}


== Methods ==
<small><i>A copy is available on [https://test.wikidata.org/wiki/Module:Wikidata test Wikidata]</i></small>
The module "{{BASEPAGENAME}}" contains the following methods, that allow the calling script to retrieve the value for any property from Wikidata by supplying the property ID as the first parameter:
== Functions callable from Lua ==
* <code>getValue</code>: Returns wiki-linked values, if applicable. All other values will be output similar to <code><nowiki>{{#property:}}</nowiki></code>, except that if values with preferred rank exist, then only they are returned. Unlike <code>formatStatements</code> from [[:w:ru:Модуль:Wikidata]], <code>getValue</code> does not yet pick up any references (see [[:w:en:Module talk:Wikidata/Archive 1#Why the references from Wikidata get dropped?]]).
* <code>getRawValue</code>: Returns non-linked property values and numbers with the thousand separator. It also returns the plain numeric value of a property which is a quantity. All other values will be output similar to <code><nowiki>{{#property:}}</nowiki></code>, including normal values, even if preferred values exist.
* <code>getDateValue</code>: Special method to return formatted dates. The default date format is [day month year]; an optional third parameter can be added for other date formats: for [month day, year], add "mdy"; for [month year], add "my"; for year only, add "y". A further optional parameter, which must be fourth, such as "BCE", may be added to override the default "BC" that is suffixed for dates BC.
* <code>getImages</code>: Returns all of the images of a particular property, e.g. image (P18), Gene Atlas Image (P692), etc. Parameters are <code>| propertyID | value / FETCH_WIKIDATA / nil | separator (default=space) | size (default=220px)</code>. The returned text will be in the format <nowiki>[[File:Filename | size]]</nowiki> for each image with a selectable size and separator (which may be html). If a property is supplied that is not of type "commonsMedia", it will return empty text.
* <code>getUnits</code> This takes a propertyID which is a quantity as the first parameter, and returns the name of the units that the property is using on the current page. It returns empty if the property is not a quantity type or if the value is not set. The second parameter may be used to override fetching from Wikidata, if it is anything other than "FETCH_WIKIDATA".
* <code>getUnitID</code> This takes a propertyID which is a quantity as the first parameter, and returns the QID of the units that the property is using on the current page. It returns empty if the property is not a quantity type or if the value is not set. The second parameter may be used to override fetching from Wikidata, if it is anything other than "FETCH_WIKIDATA".


Wikidata qualifier values (if present) can be retrieved by the following methods:
'''p.getClaims returns claims in a particular item that match a particular query
* <code>getQualifierValue</code>: returns only wiki-linked values, if applicable.
* item (required): its Qid
* <code>getRawQualifierValue</code>: returns non-linked values
* property (required) property that the claims should have
* <code>getQualifierDateValue</code>: returns formatted dates
* qualifier = qualifiers that the statement should have
* withrank = rank of the statement ; 'preferred', 'normal', 'deprecated' or 'valid' (ie. normal and preferred). By default = preferred.
* sourceproperty = this property should be used in the source
* withsource = the source that should be provided in the statement (if sourceproperty is not provided, the property used is {{p|248}})
* excludespecial = set to true if you do not want to get "novalue" and "somevalue".  
* numval = if you want to set a maximum number of values to be returned
* sorttype = set to "chronological" to get the statements in chronological order using the {{p|580}}, {{p|582}} and {{p|585}} qualifiers. Set to "inverted" for chronological order. From a Lua module, you can also define your own sorting criteria.
* showsource = set to "true" if you want the source of the statement to be displayed.


If the property is not defined in Wikidata for the article that invokes this code, then an empty string ("") is returned.
'''p.formatStatements(args)''': returns a string containing the statements given in the table args. Same keys as getClaims, plus formatting arguments:
* lang (required) for the desired language
* displayformat = the format in which the args should be returned. For example, for a string-type property displayformat = "weblink" returns a formatted weblink.
* conj = the conjunction separating the statements. For example, conj = '<nowiki><br /></nowiki>' will make a new line between each statement.
* showqualifiers = the qualifiers that should be shown along with the mainsnak value


Other methods:
'''p.getLabel''' get the label of an entity
* <code>pageId</code>: returns the Wikidata id (Q…) of the current page or nothing if the page is not connected to Wikidata. To get the Wikidata id of another page, use [[Module:ResolveEntityId|<nowiki>{{#invoke:ResolveEntityId|</nowiki>''page''<nowiki>}}</nowiki>]].
* entity = entity ID with its Q or P
* <code>getTAValue</code>: gets the TA98 (Terminologia Anatomica first edition 1998) values for property P1323. It takes no parameters and constructs its output as a list, with each value linked to an external resource. This is an exemplar for writing calls that need to return external links.
* lang
* <code>ViewSomething</code>: gets anything in the structured data, including labels, descriptions, references and interwiki links. See [https://en.wikipedia.org/w/index.php?title=Module_talk%3AWikidata&diff=652812891&oldid=650633541#Is_it_possible_to_get_value.28s.29_in_structured_tree_by_this_Module.3F]
* <code>getSiteLink</code>: gets name of a page in given in argument wiki (like "enwikiquote" for English Wikiquote, "arwiki" for the Arabic Wikipedia).
* <code>Dump</code>: <code><nowiki>{{#invoke:Wikidata|Dump|claims}}</nowiki></code> spies the structured data. It uses the same arguments as ViewSomething. Try this with preview only to see results. That helps you a lot in developing Lua scripts that access the data. If used without arguments, it dumps everything including labels, descriptions, references and interwiki links. There is the wrapper template, see {{tl|Dump}}.
* <code>getImageLegend</code>: returns an image legend (image is property P18; image legend is property P2096).
*: Call as <code><nowiki>{{#invoke:Wikidata |getImageLegend | <PARAMETER> | lang=<ISO-639code> |id=<QID>}}</nowiki></code>
*: Returns PARAMETER, unless it is equal to "FETCH_WIKIDATA", from Item QID (expensive call). If QID is omitted or blank, the current article is used (not an expensive call). If lang is omitted, it uses the local wiki language, otherwise it uses the provided ISO-639 language code.
*: The label is returned from the first image with 'preferred' rank; or from the first image with 'normal' rank if no image has preferred rank.
* <code>getValueShortName</code>: returns the same data as getValue, but utilizes the property [[d:Property:P1813|short name]] as the label, if available. This allows for piped links to use a shorter label where preferred. If short name is not set on the item, the normal label is used.


=== Arbitrary Access ===
== Functions callable from wikitext ==
As of 16 September 2015, it is now possible to fetch data from other articles by using their QID.
* '''p.formatStatementsE''' same as p.formatStatements, except that "lang" is not required. It is most conveniently used from {{tl|Data}} that takes exactly the same arguments.
The following call:
* '''p.getLabel''' get the label of an entity
* <code><nowiki>{{#invoke:Wikidata|getValueFromID|<QID>|<Property>|FETCH_WIKIDATA}}</nowiki></code>
will do the same as getValue, but takes an extra parameter, which is the QID of the Wikidata item that you want to get the property value from.
For example:
* <code><nowiki>{{#invoke:Wikidata|getValueFromID|Q151973|P26|FETCH_WIKIDATA}}</nowiki></code>
will fetch a list of the linked values for 'spouse' (P26) from 'Richard Burton' (Q151973) from anywhere in the English Wikipedia.


This means that testing environments may be set up in user space, but remember that these calls are classed as ''expensive'', so please use them as sparingly as possible.
=== Examples ===


== Parameters ==
{| class="wikitable"
* For the generalized case (getValue), two unnamed parameters are supplied. The first is the ID of the property that is to be retrieved (e.g. P19 for birthplace or P26 for spouse). The second may be null, "FETCH_WIKIDATA", or any other string, which becomes the returned value.
|-
* For the generalized unlinked case (getRawValue), two unnamed parameters are supplied. The first is the ID of the property that is to be retrieved (e.g. P21 for gender). The second may be null, "FETCH_WIKIDATA", or any other string, which becomes the returned value.
! Code !! Render !! Comment
* For the generalized date case (getDateValue), three unnamed parameters are supplied. The first is the ID of the property that is to be retrieved (e.g. P569 for date of birth). The second may be null, "FETCH_WIKIDATA", or any other string, which becomes the returned value. The third is the format that the date should be returned in, either dmy, mdy, my, or y.
|-
Please note that lower-case parameters are no longer supported by the wikibase call: <s>p123</s>, so please check that upper-case, like P123, is used if problems should arise.
| <nowiki>{{#invoke:Wikidata|getLabel|entity=Q42}}</nowiki> || {{#invoke:Wikidata|getLabel|entity=Q42}} || {{Q|42}}
|-
| <nowiki>{{#invoke:Wikidata|getLabel|entity=Q42|link=wikipedia}}</nowiki> || {{#invoke:Wikidata|getLabel|entity=Q42|link=wikipedia}} ||
|-
| <nowiki>{{#invoke:Wikidata|getLabel|entity=Q42|link=-}}</nowiki> || {{#invoke:Wikidata|getLabel|entity=Q42|link=-}} ||
|-
| <nowiki>{{#invoke:Wikidata|getLabel|entity=Q42|link=wikipedia|lang=ja}}</nowiki> || {{#invoke:Wikidata|getLabel|entity=Q42|link=wikipedia|lang=ja}} ||
|-
| <nowiki>{{#invoke:Wikidata|getLabel|entity=Q42|link=wikidata}}</nowiki> || {{#invoke:Wikidata|getLabel|entity=Q42|link=wikidata}} ||
|-
| <nowiki>{{#invoke:Wikidata|getLabel|entity=Q42|link=wikidata|lang=ja}}</nowiki> || {{#invoke:Wikidata|getLabel|entity=Q42|link=wikidata|lang=ja}} ||
|-
| <nowiki>{{#invoke:Wikidata|formatStatementsE|item=Q42|property=p31}}</nowiki> || {{#invoke:Wikidata|formatStatementsE|item=Q42|property=p31}} || {{Q|42}} {{P|31}}
|-
| <nowiki>{{#invoke:Wikidata|formatStatementsE|item=Q42|property=p31|lang=ja}}</nowiki> || {{#invoke:Wikidata|formatStatementsE|item=Q42|property=p31|lang=ja}} ||
|-
| <nowiki>{{#invoke:Wikidata|formatStatementsE|item=Q42|property=p569}}</nowiki> || {{#invoke:Wikidata|formatStatementsE|item=Q42|property=p569}} || {{Q|42}} {{P|569}}
|-
| <nowiki>{{#invoke:Wikidata|formatStatementsE|item=Q42|property=p569|lang=ja}}</nowiki> || {{#invoke:Wikidata|formatStatementsE|item=Q42|property=p569|lang=ja}} ||
|-
| <nowiki>{{#invoke:Wikidata|formatStatementsE|item=Q42|property=p569|displayformat=raw}}</nowiki> || {{#invoke:Wikidata|formatStatementsE|item=Q42|property=p569|displayformat=raw}} ||
|-
| <nowiki>{{#invoke:Wikidata|formatStatementsE|item=Q12418|property=p186}}</nowiki> || {{#invoke:Wikidata|formatStatementsE|item=Q12418|property=p186}} || {{Q|12418}} {{P|186}}
|-
| <nowiki>{{#invoke:Wikidata|formatStatementsE|item=Q12418|property=p186|displayformat=raw}}</nowiki> || {{#invoke:Wikidata|formatStatementsE|item=Q12418|property=p186|displayformat=raw}} ||
|-
| <nowiki>{{#invoke:Wikidata|formatStatementsE|item=Q12418|property=p186|lang=ja}}</nowiki> || {{#invoke:Wikidata|formatStatementsE|item=Q12418|property=p186|lang=ja}} ||
|-
| <nowiki>{{#invoke:Wikidata|formatStatementsE|item=Q12418|property=p186|rank=valid}}</nowiki> || {{#invoke:Wikidata|formatStatementsE|item=Q12418|property=p186|rank=valid}} || rank = "valid" accepts both "preferred" and "normal" values
|-
| <nowiki>{{#invoke:Wikidata|formatStatementsE|item=Q12418|property=p186|numval=1}}</nowiki> || {{#invoke:Wikidata|formatStatementsE|item=Q12418|property=p186|numval=1}} || returns the number of values numval (priority to those with "rank= preferred", if there are not enough of them, also accepts "rank = normal")
|-
| <nowiki>{{#invoke:Wikidata|formatStatementsE|item=Q12418|property=p186|qualifier=p518}}</nowiki> || {{#invoke:Wikidata|formatStatementsE|item=Q12418|property=p186|qualifier=p518}} || {{Q|12418}} {{P|186}} {{P|518}} should only display values that have a p518 qualifier
|-
| <nowiki>{{#invoke:Wikidata|formatStatementsE|item=Q12418|property=p186|showqualifiers=p518}}</nowiki> || {{#invoke:Wikidata|formatStatementsE|item=Q12418|property=p186|showqualifiers=p518}} ||  shows the value of the p518 qualifier (if any) in addition to the main value
|-
| <nowiki>{{#invoke:Wikidata|formatStatementsE|item=Q83259|property=p669|showqualifiers=p670|delimiter=&amp;#32;}}</nowiki> || {{#invoke:Wikidata|formatStatementsE|item=Q83259|property=p669|showqualifiers=p670|delimiter=&#32;}} || {{Q|83259}} {{P|669}} {{P|670}} shows the value of the P670 qualifier (if any) in addition to the main value separated by a space
|-
| <nowiki>{{#invoke:Wikidata|formatStatementsE|item=Q12418|property=P276|sourceproperty=P854}}</nowiki> || {{#invoke:Wikidata|formatStatementsE|item=Q12418|property=P276|sourceproperty=P854}} || {{Q|12418}} {{P|276}} {{P|854}}
|-
| <nowiki>{{#invoke:Wikidata|formatStatementsE|item=Q11879536|property=P460|withsource=Q1645493}}</nowiki> || {{#invoke:Wikidata|formatStatementsE|item=Q11879536|property=P460|withsource=Q1645493}} || {{Q|11879536}} {{P|460}} {{Q|1645493}}
|-
| <nowiki>{{#invoke:Wikidata|formatStatementsE|item=Q11879536|property=P460|withsource=Q1645493|showsource=true}}</nowiki> || {{#invoke:Wikidata|formatStatementsE|item=Q11879536|property=P460|withsource=Q1645493|showsource=true}} ||
|-
| <nowiki>{{#invoke:Wikidata|formatStatementsE|item=Q153|property=P231|showsource=true}}</nowiki> || {{#invoke:Wikidata|formatStatementsE|item=Q153|property=P231|showsource=true}} || {{Q|153}} {{P|231}}
|-
| <nowiki>{{#invoke:Wikidata|formatStatementsE|item=Q205309|property=P793|sorttype=inverted}}</nowiki> || {{#invoke:Wikidata|formatStatementsE|item=Q205309|property=P793|sorttype=inverted}} || {{Q|205309}} {{P|793}}
|}


== Usage ==
== See also ==
=== Example: spouse (P26) ===
* [[Module:Wd]]
* <code><nowiki>{{#invoke:Wikidata|getValue|P26|}}</nowiki></code> = returns nothing, so suppresses the display of spouse in an infobox
* <code><nowiki>{{#invoke:Wikidata|getValue|P26|FETCH_WIKIDATA}}</nowiki></code> = returns the linked value(s) of property P26 (spouse) stored in wikidata for the corresponding article (e.g. for article [[Bill Clinton]], it returns [[Hillary Clinton]])
* <code><nowiki>{{#invoke:Wikidata|getValue|P26|[[Hillary Rodham Clinton]]}}</nowiki></code> = returns [[Hillary Rodham Clinton]], allowing an infobox to use a local value rather than the value stored in Wikidata.
 
=== Example in Infobox template ===
Inside an infobox definition, it may be called like this:
* <code><nowiki>| data55 = {{#invoke:Wikidata|getValue|P26|{{{spouse|FETCH_WIKIDATA}}} }}</nowiki></code>
which causes the infobox to:
# not display spouse if the infobox parameter <code>|spouse</code> is set to be blank (as <code>"| spouse ="</code>}
# display the linked value(s) from Wikidata if the infobox parameter <code>|spouse</code> is not supplied
# display the local infobox parameter <code>|spouse</code> if it is supplied (e.g. <code>"|spouse = Hillary Rodham Clinton"</code>)
 
Optionally, it could be called as:
* <code><nowiki>| data55 = {{#invoke:Wikidata|getValue|P26|{{{spouse|}}} }}</nowiki></code>
which causes the infobox to:
# not display spouse if the infobox parameter <code>|spouse</code> is set to be blank (as <code>"| spouse ="</code>}
# not display spouse if the infobox parameter <code>|spouse</code> is not supplied
# display the local infobox parameter <code>|spouse</code> if it is supplied (e.g. <code>"|spouse = Hillary Rodham Clinton"</code>)
# display the linked value(s) from Wikidata if the infobox parameter is locally set to FETCH_WIKIDATA
 
See [[Module:WikidataIB]] for a modification that allows fields, on a per article basis, to be blacklisted so that they never display. It also allows the editor to specify, on a per article basis, which fields may be automatically fetched from Wikidata when local parameter is supplied; the default is none, allowing an infobox to be modified to accept Wikidata without any change in the articles using the infobox until the functionality is enabled in the article.
 
=== Example:birth place ===
This works in just the same way as the calls above:
* <code><nowiki>{{#invoke:Wikidata|getValue|P19|}}</nowiki></code> = returns nothing, so suppresses the display of birth place in an infobox
* <code><nowiki>{{#invoke:Wikidata|getValue|P19|FETCH_WIKIDATA}}</nowiki></code> = returns the linked value(s) of property P19 (place of birth) stored in wikidata for the corresponding article (e.g. for article [[Bill Clinton]], it returns [[Hope, Arkansas]])
* <code><nowiki>{{#invoke:Wikidata|getValue|P19|[[Hope, Arkansas|Hope]]}}</nowiki></code> = returns [[Hope, Arkansas|Hope]], allowing an infobox to use a local value rather than the value stored in Wikidata.
 
=== Example:gender ===
We don't want the returned value linked, so use:
* - <code><nowiki>{{#invoke:Wikidata|getRawValue|P21|FETCH_WIKIDATA}}</nowiki></code>
 
=== Example:date of birth ===
If we want the date of birth in dmy format, we use:
* - <code><nowiki>{{#invoke:Wikidata|getDateValue|P569|FETCH_WIKIDATA|dmy}}</nowiki></code>
 
If we want the date of birth in mdy format, we use:
* - <code><nowiki>{{#invoke:Wikidata|getDateValue|P569|FETCH_WIKIDATA|mdy}}</nowiki></code>
 
If we want a year of birth, we use:
* - <code><nowiki>{{#invoke:Wikidata|getDateValue|P569|FETCH_WIKIDATA|y}}</nowiki></code>
 
If we want a year of birth that may be BC, but should read "BCE", we use:
* - <code><nowiki>{{#invoke:Wikidata|getDateValue|P569|FETCH_WIKIDATA|y|BCE}}</nowiki></code>
 
=== Example: Linking to Wikidata item ===
Use the following code to just retrieve the Q-ID:
* - <code><nowiki>{{#invoke:Wikidata|pageId}}</nowiki></code>
 
Linking to Wikidata used the usual Wiki markup:
* - <code><nowiki>[[d:{{#invoke:Wikidata|pageId}}|Name of Link]]</nowiki></code>
 
=== Example: Linking to another wiki page ===
Use code like this to link to another wiki. English Wikivoyage in the example:
* - <code><nowiki>[[voy:{{#invoke:Wikidata|getSiteLink|enwikivoyage}}|Name of Link]]</nowiki></code>
 
 
== Testing ==
=== Testing spouse ===
Copy and paste the following into any article and preview it (please don't save!):
<syntaxhighlight lang="moin">
* - {{#invoke:Wikidata|getValue|P26|}}
* - {{#invoke:Wikidata|getValue|P26|FETCH_WIKIDATA}}
* - {{#invoke:Wikidata|getValue|P26|[[Hillary Rodham Clinton]]}}
</syntaxhighlight>
 
In [[Bill Clinton]] you should get:
* -
* - [[Hillary Clinton]]
* - [[Hillary Rodham Clinton]]
 
 
In [[Barack Obama]] you should get:
* -
* - [[Michelle Obama]]
* - [[Hillary Rodham Clinton]]
 
 
In [[Richard Burton]] you should get:
* -
* - [[Elizabeth Taylor]], [[Sally Burton]], [[Sybil Christopher]], [[Suzy Miller]], [[Elizabeth Taylor]]
* - [[Hillary Rodham Clinton]]
 
 
In [[Franz Kafka]] you should get:
* -
* -
* - [[Hillary Rodham Clinton]]
 
=== Testing birthplace ===
Copy and paste the following into any article and preview it (please don't save!):
<syntaxhighlight lang="moin">
* - {{#invoke:Wikidata|getValue|P19|}}
* - {{#invoke:Wikidata|getValue|P19|FETCH_WIKIDATA}}
* - {{#invoke:Wikidata|getValue|P19|[[Newport]]}}
</syntaxhighlight>
Try [[William Ellery]] and check that the Wikidata call correctly disambiguates.
 
=== Testing getValue, getRawValue and getDateValue ===
Copy and paste the following into any article and preview it (please don't save!):
<pre>
* - {{#invoke:Wikidata|getValue|P19|FETCH_WIKIDATA}}
* - {{#invoke:Wikidata|getValue|P26|FETCH_WIKIDATA}}
* - {{#invoke:Wikidata|getValue|P27|FETCH_WIKIDATA}}
* - {{#invoke:Wikidata|getValue|P140|FETCH_WIKIDATA}}
* - {{#invoke:Wikidata|getRawValue|P21|FETCH_WIKIDATA}}
* - {{#invoke:Wikidata|getDateValue|P569|FETCH_WIKIDATA|dmy}}
* - {{#invoke:Wikidata|getDateValue|P569|FETCH_WIKIDATA|mdy}}
* - {{#invoke:Wikidata|getDateValue|P569|FETCH_WIKIDATA|y}}
</pre>
This should return the Wikidata values for birthplace, spouse, citizenship, religion, gender, date of birth (twice) and year of birth, if they exist. The dob is first in dmy format and then in mdy.
 
=== Testing dates BC/BCE ===
Copy and paste the following into a short section of article such as [[Horace #See also]] and preview it (please don't save!):
<pre>
* - {{#invoke:Wikidata|getDateValue|P569|FETCH_WIKIDATA|y}}
* - {{#invoke:Wikidata|getDateValue|P569|FETCH_WIKIDATA|y|BCE}}
</pre>
This should return 65 BC and 65 BCE, respectively.
 
==See also==
* [[:Module:WikidataIB]], for handling Wikidata in infoboxes
 
<includeonly>{{#ifeq:{{SUBPAGENAME}}|sandbox||
[[Category:Wikidata modules]]
[[Category:Modules using data from Wikidata]]
}}</includeonly>
Per favore tieni presente che tutti i contributi a Tematiche di genere si considerano pubblicati nei termini d'uso della licenza Creative Commons Attribuzione-Condividi allo stesso modo (vedi Tematiche di genere:Copyright per maggiori dettagli). Se non desideri che i tuoi testi possano essere modificati e ridistribuiti da chiunque senza alcuna limitazione, non inviarli qui.
Inviando il testo dichiari inoltre, sotto tua responsabilità, che è stato scritto da te personalmente oppure è stato copiato da una fonte di pubblico dominio o similarmente libera. Non inviare materiale protetto da copyright senza autorizzazione!
Annulla Guida (si apre in una nuova finestra)