Module:Sandbox/RexxS/ImageLegend
--[[
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
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.