Module:IMDb
Uiterlijk
Voor infoboxen van artiesten en films en ook voor Sjabloon:Link IMDb naam, Sjabloon:Link IMDb titel en Sjabloon:Link IMDb bedrijf.
Gebruikt volgcategorie: Categorie:Wikipedia:IMDb-code niet op Wikidata
local p = {}
local paths = {
tt="title/$1/",
nm="name/$1/",
co="company/$1/",
ev="event/$1",
ni="news/$1/",
};
-- creates an external link from a single IMDb ID
local function linkId(path, id)
path = mw.ustring.gsub(path, "$1", id)
return "[https://www.imdb.com/" .. path .. " IMDb-profiel]"
end
-- creates a sequence of external links from one or more IMDb IDs
local function linkIds(path, ids)
local r
for i=1, #ids do
link = linkId(path, ids[i])
if r then
r = r .. ", " .. link
else
r = link
end
end
return r
end
-- fetches an array of IMDb IDs from Wikidata
-- returns nil if no entity found
-- if a prefix is provided, only matching IDs are returned
local function fetchWd(prefix)
local entity = mw.wikibase.getEntity()
if not entity then
return nil
end
local r = {}
local i = 1
for _, s in pairs(entity:getBestStatements('P345')) do
if s.mainsnak.snaktype == "value" then
local datavalue = s.mainsnak.datavalue
if datavalue then
local imdbId = datavalue.value
if not prefix or prefix == mw.ustring.sub(imdbId, 1, 2) then
if r then
r[i] = imdbId
i = i + 1
end
end
end
end
end
return r
end
local function tracking(sortKey)
if mw.title.getCurrentTitle().namespace ~= 0 then
return ''
end
local r = "[[Categorie:Wikipedia:IMDb-code niet op Wikidata"
if sortKey then
r = r .. "|" .. sortKey
end
return r .. "]]"
end
local function einval(frame, msg)
return frame:expandTemplate{ title='Error', args={"Fout: " .. msg} }
end
-- arg 1: required ID prefix, tt/nm/co/ev/ni
-- arg 2: optional ID suffix, numeric
function p.infoboxLink(frame)
local prefix, path
if frame.args[1] and #frame.args[1] > 0 then
prefix = frame.args[1]
if #prefix == 2 then
path = paths[prefix]
end
if not path then
return einval(frame, "ongeldige prefix: " .. prefix)
end
else
return einval(frame, "prefix ontbreekt")
end
local suffix
if frame.args[2] and #frame.args[2] > 0 then
suffix = frame.args[2]
if not string.find(suffix, '^%d+$') then
return einval(frame, "niet een nummer: " .. suffix)
end
end
local r, idFromArgs, p345
if suffix then
idFromArgs = prefix .. suffix
r = frame:expandTemplate{ title='en', args={} } ..
linkId(path, idFromArgs)
end
p345 = fetchWd(prefix)
if not suffix then
if p345 and #p345 > 0 then
r = frame:expandTemplate{ title='en', args={} } ..
linkIds(path, p345)
else
r = ''
end
end
if idFromArgs and p345 then
local sortKey
if #p345 == 0 then
sortKey = idFromArgs
elseif #p345 == 1 and idFromArgs ~= p345[1] then
sortKey = '≠'
else
sortKey = '#'
end
r = r .. tracking(sortKey)
end
return r
end
-- deprecated
function p.link_infobox(frame)
local entity = mw.wikibase.getEntity()
if not entity then
return ''
end
local qid = entity.id
local prefix = frame.args[1] -- optional
local spec;
if prefix and #prefix == 2 then
local path = paths[prefix]
if not path then
return ''
end
spec = "https://www.imdb.com/" .. path
else
spec = "https://wikidata-externalid-url.toolforge.org/?p=345&url_prefix=https://www.imdb.com/&id=$1"
end
local r = nil
for _, s in pairs(mw.wikibase.getBestStatements(qid, 'P345')) do
if s.mainsnak.snaktype == "value" then
local datavalue = s.mainsnak.datavalue
if datavalue then
local imdbId = datavalue.value
if not prefix or prefix == mw.ustring.sub(imdbId, 1, 2) then
local url = mw.ustring.gsub(spec, "$1", imdbId)
local link = "[" .. url .. " IMDb-profiel]"
if r then
r = r .. ", " .. link
else
r = link
end
end
end
end
end
-- if r then
-- r = "{{en}} " .. r
-- end
return r
end
local function externalLink(frame, prefix)
local entity = mw.wikibase.getEntity()
if not entity then
return '<span style="color:red">Fout: pagina niet verbonden met een Wikidata-item</span>'
end
local qid = entity.id
local label = frame.args[1] -- optional
if not label or label == "" then
label = mw.wikibase.getLabel(qid)
end
local spec = "https://www.imdb.com/" .. paths[prefix]
local sa = mw.wikibase.getBestStatements(qid, 'P345')
if not sa or #sa == 0 then
return '<span style="color:red">Fout: geen IMDb-id gevonden op Wikidata-item</span>'
end
local r
for _, s in pairs(sa) do
if s.mainsnak.snaktype == "value" then
local datavalue = s.mainsnak.datavalue
if datavalue then
local imdbId = datavalue.value
if prefix == mw.ustring.sub(imdbId, 1, 2) then
local url = mw.ustring.gsub(spec, "$1", imdbId)
local link = "[" .. url .. " " .. label .. "]"
if prefix == 'tt' then
link = "''" .. link .. "''"
end
r = link
break
end
end
end
end
if not r then
return '<span style="color:red">Fout: geen geschikt IMDb-id gevonden op Wikidata-item</span>'
end
-- r = "{{en}} [[Bestand:Comicsfilm.png|15px|class=noviewer|Pictogram film]] " .. r
return r
end
function p.link_naam(frame)
return externalLink(frame, 'nm')
end
function p.link_titel(frame)
return externalLink(frame, 'tt')
end
function p.link_bedrijf(frame)
return externalLink(frame, 'co')
end
return p