Edge Rewrite
Jump to content

Module:Sandbox/Mant08/IMDb name

From Wikipedia, the free encyclopedia

local p = {}
local getArgs = require('Module:Arguments').getArgs

function p.imdb_name(frame)
	local args = getArgs(frame)
	return p._imdb_name(args)
end

function p._imdb_name(args)
	local config = args
	
	local section = config["3"] or config[3] or config["section"] or nil
	local name = config["2"] or config [2] or config["name"] or mw.title.getCurrentTitle().text or "John Doe"
	local id = config["1"] or config[1] or config["id"] or nil

	local current_page_id = mw.wikibase.getEntityIdForCurrentPage()
	
	if (id == nil)
	then
		if (current_page_id ~= nil)
		then
			local p345_value_table = mw.wikibase.getBestStatements( current_page_id, 'P345' )
			if (p345_value_table ~= nil)
			then 
				if (p345_value_table[1] ~= nil)
			    then
				    id = p345_value_table[1].mainsnak.datavalue.value
			    end
			end
		end
	else
		if (tonumber(id) ~= nil)
		then
			id = "nm" .. id
		else
			id = id
		end
	    
	end
    
    local at_imdb_text = "at [[IMDb]]."
    
    if (id == nil)
    then
    	local encoded_name = mw.uri.encode(name)
    	return pronoun .. " " .. "[https://imdb.com/find/?q=" .. encoded_name .. "&s=nm" .. " " .. name .. "]" .. at_imdb_text
    else
    	local base_link_object = "[https://imdb.com/name/" .. id .. "/"
    	
    	if (section == nil)
    	then
    		return pronoun .. " " .. base_link_object .. " " .. name .. "]" .. at_imdb_text
    	elseif (section == "biography" or section == "bio")
    	then
    		return "The biography of " .. base_link_object .. "bio/ " .. name .. "]" .. at_imdb_text
    	elseif (section == "awards" or section == "award")
    	then
    		return "The awards of " .. base_link_object .. "awards/ " .. name .. "]" .. at_imdb_text
    	else
    		return '<span class="error">IMDb Name: Error: A non-valid "section" variable input was given.</span>'
    	end
    end
  
end

return p