Modul:IMDb

Daripada Wikipedia, ensiklopedia bebas.

Pendokumenan untuk modul ini boleh diciptakan di Modul:IMDb/doc

local p = {}

local paths = {
	tt="title/$1/",
	nm="name/$1/",
	co="company/$1/",
	ev="event/$1",
	ni="news/$1/",
};

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 .. " Profil di IMDb]"
					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]]&nbsp;" .. 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