Module:Sandbox/RexxS/ImageLegend
Appearance
From Wikipedia, the free encyclopedia
< Module:Sandbox | RexxS
You might want to create a documentation page for this Scribunto module.
Editors can experiment in this module's sandbox (create | mirror) and testcases (create) pages.
Add categories to the /doc subpage. Subpages of this module.
Editors can experiment in this module's sandbox (create | mirror) and testcases (create) pages.
Add categories to the /doc subpage. Subpages of this module.
--[[ This is used to return an image legend from Wikidata image is property P18 image legend is property P2096 Call as {{#invoke:Sandbox/RexxS/ImageLegend |getImageLegend | PARAMETER | lang=<ISO-639code> |id=<QID>}} 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 expensive call) If lang is omitted, it uses the local wiki language, otherwise it uses the provided ISO-639 language code ISO-639: https://docs.oracle.com/cd/E13214_01/wli/docs92/xref/xqisocodes.html#wp1252447 Ranks are: 'preferred' > 'normal' This returns the label from the first image with 'preferred' rank Or the label from the first image with 'normal' rank if preferred returns nothing Ranks: https://www.mediawiki.org/wiki/Extension:Wikibase_Client/Lua In South Pole Telescope, the structure of P18 is: ["P18"] = table#26 { table#27 { ["id"] = "Q1513315$bc122c22-4da5-8301-9d2f-8a5eb415e9e8", ["mainsnak"] = table#28 { ["datatype"] = "commonsMedia", ["datavalue"] = table#29 { ["type"] = "string", ["value"] = "South pole telescope nov2009.jpg", }, ["property"] = "P18", ["snaktype"] = "value", }, ["qualifiers"] = table#30 { ["P2096"] = table#31 { table#32 { ["datatype"] = "monolingualtext", ["datavalue"] = table#33 { ["type"] = "monolingualtext", ["value"] = table#34 { ["language"] = "en", ["text"] = "The South Pole Telescope in November 2009", }, }, ["hash"] = "6d87da1d793aa25eed516953ef8ad15e9b030d12", ["property"] = "P2096", ["snaktype"] = "value", }, table#35 { ["datatype"] = "monolingualtext", ["datavalue"] = table#36 { ["type"] = "monolingualtext", ["value"] = table#37 { ["language"] = "lt", ["text"] = "Pietų ašigalio teleskopas 2009 m. lapkritį", }, }, ["hash"] = "7a04953630950ebf0cce672c30d1b150e74f7003", ["property"] = "P2096", ["snaktype"] = "value", }, }, }, ["qualifiers-order"] = table#38 { "P2096", }, ["rank"] = "normal", ["type"] = "statement", }, }, ]] local p = {} p.getImageLegend = function(frame) -- look for named parameter id; if it's blank make it nil local id = frame.args.id if id and (#id == 0) then id = nil end -- look for named parameter lang -- it should contain a two-character ISO-639 language code -- if it's blank fetch the language of the local wiki local lang = frame.args.lang if (not lang) or (#lang < 2) then lang = mw.language.getContentLanguage().code end -- first unnamed parameter is the local parameter, if supplied local input_parm = mw.text.trim(frame.args[1] or "") if input_parm == "FETCH_WIKIDATA" then local ent = mw.wikibase.getEntityObject(id) local imgs if ent and ent.claims then imgs = ent.claims.P18 end local imglbl if imgs then -- look for an image with 'preferred' rank for k1, v1 in pairs(imgs) do if v1.rank == "preferred" and v1.qualifiers.P2096 then local imglbls = v1.qualifiers.P2096 for k2, v2 in pairs(imglbls) do if v2.datavalue.value.language == lang then imglbl = v2.datavalue.value.text break end end end end -- if we don't find one, look for an image with 'normal' rank if (not imglbl) then for k1, v1 in pairs(imgs) do if v1.rank == "normal" and v1.qualifiers.P2096 then local imglbls = v1.qualifiers.P2096 for k2, v2 in pairs(imglbls) do if v2.datavalue.value.language == lang then imglbl = v2.datavalue.value.text break end end end end end end return imglbl else return input_parm end end return p