Module:IMDb

Uit Buitenwatersloot Delft
Versie door Sybolt (overleg | bijdragen) op 18 nov 2023 om 20:41 (1 versie geïmporteerd)
(wijz) ← Oudere versie | Huidige versie (wijz) | Nieuwere versie → (wijz)

Documentatie voor deze module kan aangemaakt worden op de volgende pagina: Module:IMDb/doc

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)
	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 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 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(prefix, idFromArgs)
	end
	p345 = fetchWd(prefix)
	if not suffix then
		if p345 and #p345 > 0 then
			r = frame:expandTemplate{ title='en', args={} } .. 
				linkIds(prefix, p345)
		else
			r = ''
		end
	end
	if p345 and idFromArgs 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(qid) 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
			r = einval(frame, "geen id gevonden")
			local parent = frame:getParent()
			if parent then
				r = r .. " voor [[:" .. parent:getTitle() .. "]]"
			end
		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