Naar inhoud springen

Module:IMDb

Uit Wikipedia, de vrije encyclopedie
Dit is een oude versie van deze pagina, bewerkt door Bdijkstra (overleg | bijdragen) op 3 aug 2022 om 11:00.
Deze versie kan sterk verschillen van de huidige versie van deze pagina.
Moduledocumentatie​[bekijk] [bewerk] [ververs] [geschiedenis]

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, label)
	if not label then
		label = "IMDb-profiel"
	end
	path = mw.ustring.gsub(path, "$1", id)
	return "[https://www.imdb.com/" .. path .. " " .. label .. "]"
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 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 externalLink(frame, prefix)
	local path = paths[prefix]
	local suffix
	if frame.args['id'] and #frame.args['id'] > 0 then
		suffix = frame.args['id']
		if not string.find(suffix, '^%d+$') then
			return einval(frame, "niet een nummer: " .. suffix)
		end
	end

	local labelFromArgs, label
	labelFromArgs = frame.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 suffix then
		idFromArgs = prefix .. suffix
		r = frame:expandTemplate{ title='en', args={} } .. 
			linkId(path, idFromArgs, label)
	end
	p345 = fetchWd(prefix)
	if not suffix then
		if p345 and #p345 > 0 then
			r = frame:expandTemplate{ title='en', args={} } .. 
				'[[Bestand:Comicsfilm.png|15px|class=noviewer|Pictogram film]] ' ..
				linkId(path, p345[1], label) ..
				' in de [[Internet Movie Database]]'
		else
			r = einval(frame, "geen id gevonden")
			local parent = frame:getParent()
			if parent then
				r = r .. " voor [[:" .. frame: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] 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