This page is a work in progress page, not an article or policy, and may be incomplete and/or unreliable.
Please offer suggestions on the talk page.

Lua programing language is used as a back end of many templates on Commons, including many templates that interact with Wikidata and now, it also can be used to access structured data on Commons. Documentation of Lua libraries designed to interface with Wikidata can be found at mw:Extension:Wikibase_Client/Lua. Some of the functions described there work with structured data on Commons, but some do not work as described. The rest of this page will describe what is the same, what is different and what is untested in that library as applied to Commons.

Basics edit

 
Diagram of a Commons file with terms used to describe it

According to Wikidata Glossary the entity is the content of a Wikidata page, such as an item (in the Wikidata main namespace) or a property (in the property namespace). In structured data on Commons each file (page in file namespace) has associated entity. The unique entity identifier for entities on Commons are derived from unique page ID, which can be accessed by magic word {{PAGEID}} preceded by letter "M". Those M-IDs are equivalent to Q-IDs of Wikidata items or P-IDs of properties. For example this Mona Lisa file has entity ID (M-ID) "M15442524". The content of structured data entity associated with that file can be seen through URL like https://commons.wikimedia.org/w/api.php?action=wbgetentities&ids=M15442524 or https://commons.wikimedia.org/w/api.php?action=wbgetclaims&entity=M15442524, which is equivalent to URL showing content of Wikidata item. For example content of Mona Lisa (Q12418) can be by https://commons.wikimedia.org/w/api.php?action=wbgetentities&ids=Q12418.

Lua access edit

This section discusses how Lua modules can access data stored in Structured data

mw.wikibase.mediainfo edit

mw.wikibase.mediainfo.getEntityIdForTitle edit

mw.wikibase.mediainfo.getEntityIdForTitle( pageTitle )
Gets the mw.wikibase.mediainfo.entityId from the page name. An example call might look like this:

mw.wikibase.mediainfo.getEntityIdForTitle( 'File:Mona Lisa, by Leonardo da Vinci, from C2RMF retouched.jpg' ) -- Returns the M-ID for the file, like "M15442524".

mw.wikibase.mediainfo.getCaptionWithLang edit

mw.wikibase.mediainfo.getCaptionWithLang( id )
Get the caption from an entity, returns the label as string or nil if it couldn't be found and the language code as a second value. This doesn't apply any language fallbacks.

mw.wikibase.mediainfo.getCaptionWithLang( 'M15442524' ) -- Returns the caption in the language the user has set the interface too, returns a string, example "Leonardo da Vinci's Mona Lisa (between 1503 and 1506)" en.

mw.wikibase.mediainfo.getCaption edit

mw.wikibase.mediainfo.getCaption( id )
Get the caption from an entity with the interface language as preferred, returns the label as string or nil if it couldn't be found. This doesn't apply any language fallbacks.

mw.wikibase.mediainfo.getCaption( 'M15442524' ) -- Returns the caption in the language the user has set the interface too, returns a string, example "Leonardo da Vinci's Mona Lisa (between 1503 and 1506)".

mw.wikibase.mediainfo.getCaptionByLang edit

mw.wikibase.mediainfo.getCaptionByLang( id, languageCode )
Get the label from an entity for a specific language, returns the label as string or nil if it couldn't be found. This doesn't apply any language fallbacks.

An example call might look like this:

mw.wikibase.mediainfo.getCaptionByLang( 'M15442524', 'ru' ) -- Returns the Russian label of the item as a string, like "Мона Лиза".

mw.wikibase edit

mw.wikibase.getEntity edit

wikibase.getEntity( id )

Gets a mw.wikibase.entity table with data of the Wikibase item requested by id. An example call might look like this:

mw.wikibase.getEntity( 'M15442524' ) -- Returns a mw.wikibase.entity table for the item with the id M15442524

wikibase.getEntity() without an ID should get a mw.wikibase.entity table with data of the item connected to the current page, so on Commons when used on page in File namespace should return entity for that file. However as of fall 2019, that functionality does not work. See phabricator:T237107.

mw.wikibase.getLabelByLang edit

wikibase.getLabelByLang( id, languageCode )
Get the label from an entity for a specific language, returns the label as string or nil if it couldn't be found. This doesn't apply any language fallbacks.

An example call might look like this:

mw.wikibase.getLabelByLang( 'M15442524', 'ru' ) -- Returns the Russian label of the item as a string, like "Мона Лиза".

That function is used by Module:Wikidata label so that {{#invoke:Wikidata label|getLabel |item=M15442524|lang=ru}} returns "Мона Лиза".

mw.wikibase.entity:getBestStatements edit

entity:getBestStatements( propertyIdOrLabel )
Suppose to get the best statements with the given property id or label. Does not work as of fall 2019. See phabricator:T237107.


mw.wikibase.entity:getSitelink edit

entity:getSitelink()
entity:getSitelink( globalSiteId )
This property is removed from the MediaInfo entities. See phabricator:T240563.

Lua Modules accessing SDC edit

  • if description field is missing, and SDC stores captions (labels) than a SDC caption will be shown in place of description.
  • if source field is missing, and SDC has source of file (P7482) property, than source field is set based on P7482.
  • if date field is missing, and SDC has inception (P571) property, than source field is set based on P571.
  • if author field is missing, and SDC has creator (P170) property, than author field is set based on P170.
  • Presents with {{FileSD}} an image in a thumb format with the use of Captions and Structured Data.
  • {{Structured Data}} - fetches all information about the file from the structured data fields + Wikidata
  • Module:SDC tracking - adds maintenance categories based on absence or presence of different SDC properties
  • Module:Statement – can be used by other templates or modules

See Also edit