Module:IMDb
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
-- suffix and label are optional
local function linkId(prefix, id, suffix, label)
if not label then
label = "IMDb-profiel"
end
path = mw.ustring.gsub(paths[prefix], "$1", id)
if suffix then
path = path .. suffix
end
return "[https://www.imdb.com/" .. path .. " " .. label .. "]"
end
-- creates a sequence of external links from one or more IMDb IDs
local function linkIds(prefix, ids)
local r
for i=1, #ids do
link = linkId(prefix, 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, qid)
local entity = mw.wikibase.getEntity(qid)
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
-- returns a statement or nil
local function fetchWdGroup()
local entity = mw.wikibase.getEntity()
if not entity then
return nil
end
local r = nil
for _, s in pairs(entity:getBestStatements('P179')) do -- "part of the series"
if s.mainsnak.snaktype == "value" then
r = s
end
end
if not r then
for _, s in pairs(entity:getBestStatements('P361')) do -- "part of"
if s.mainsnak.snaktype == "value" then
r = s
end
end
end
return r
end
local function getP1545(s)
local r
local a = s.qualifiers
if a then
a = a["P1545"]
if a then
for _, q in ipairs(a) do
if q.snaktype == "value" then
r = q.datavalue.value
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 number, numeric
function p.infoboxLink(frame)
local prefix
if frame.args[1] and #frame.args[1] > 0 then
prefix = frame.args[1]
if not #prefix == 2 then
return einval(frame, "ongeldige prefix: " .. prefix)
end
else
return einval(frame, "prefix ontbreekt")
end
local idnum
if frame.args[2] and #frame.args[2] > 0 then
idnum = frame.args[2]
if not string.find(idnum, '^%d+$') then
return einval(frame, "niet een nummer: " .. idnum)
end
end
local r, idFromArgs, p345
if idnum then
idFromArgs = prefix .. idnum
r = frame:expandTemplate{ title='en', args={} } ..
linkId(prefix, idFromArgs)
end
p345 = fetchWd(prefix)
local season
if not idnum then
if p345 and #p345 > 0 then
r = frame:expandTemplate{ title='en', args={} } ..
linkIds(prefix, p345)
else
local p179 = fetchWdGroup()
if p179 then
local qid = p179.mainsnak.datavalue.value.id
season = getP1545(p179)
if season then
season = "episodes/?season=" .. season
end
p345 = fetchWd(prefix, qid)
if p345 and #p345 > 0 then
r = frame:expandTemplate{ title='en', args={} } ..
linkId(prefix, p345[1], season)
end
end
end
if not r then
r = ''
end
end
if p345 and idFromArgs and not season then
local sortKey
if #p345 == 0 then
sortKey = idFromArgs
elseif #p345 > 1 then
sortKey = '#'
elseif #p345 == 1 and idFromArgs ~= p345[1] then
sortKey = '≠'
end
if sortKey then
r = r .. tracking(sortKey)
end
end
return r
end
local function illustratedLink(frame, prefix, idnum, suffix, label)
if prefix == 'tt' and not suffix then
label = "''" .. label .. "''"
end
return frame:expandTemplate{ title='en', args={} } ..
' [[Bestand:Comicsfilm.png|15px|class=noviewer|Pictogram film]] ' ..
linkId(prefix, idnum, suffix, label) ..
' in de [[Internet Movie Database]]'
end
local function externalLink(frame, prefix)
local idnum
local suffix -- e.g. "bio"
local args = frame:getParent().args
if args['id'] and #args['id'] > 0 then
idnum = args['id']
if not string.find(idnum, '^%d+$') then
return einval(frame, "niet een nummer: " .. idnum)
end
end
if args[1] and #args[1] > 0 then
suffix = args[1]
end
local labelFromArgs, label
labelFromArgs = args['label'] -- optional
if labelFromArgs == '' then
labelFromArgs = nil
end
label = labelFromArgs or
mw.wikibase.getLabel() or
frame:expandTemplate{ title='PAGENAMEBASE', args={} }
local r, idFromArgs, p345
if idnum then
idFromArgs = prefix .. idnum
r = illustratedLink(frame, prefix, idFromArgs, suffix, label)
end
p345 = fetchWd(prefix)
if not idnum then
if p345 and #p345 > 0 then
r = illustratedLink(frame, prefix, p345[1], suffix, label)
else
local p179 = fetchWdGroup()
if p179 then
local qid = p179.mainsnak.datavalue.value.id
local season = getP1545(p179)
if season then
if not suffix then
suffix = ''
end
suffix = "episodes/?season=" .. season .. suffix
end
p345 = fetchWd(prefix, qid)
if p345 and #p345 > 0 then
r = illustratedLink(frame, prefix, p345[1], suffix, label)
end
end
end
end
if not r then
r = einval(frame, "geen id gevonden")
local parent = frame:getParent()
if parent then
r = r .. " voor [[:" .. parent:getTitle() .. "]]"
end
end
if p345 and not labelFromArgs then
local sortKey
if #p345 > 1 then
sortKey = '#'
elseif idFromArgs then
if #p345 == 0 then
sortKey = idFromArgs
elseif #p345 == 1 and idFromArgs ~= p345[1] and
(mw.ustring.sub(idFromArgs, 1, 2) ==
mw.ustring.sub(p345[1], 1, 2)) then
sortKey = '≠'
end
end
if sortKey then
r = r .. tracking(sortKey)
end
end
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