Module:Professional wrestling profiles

local getArgs = require('Module:Arguments').getArgs

local profiles = {
	cagematch = {
		name = "Cagematch.net",
		url = "https://www.cagematch.net/",
		person = { url = "?id=2&nr=%s", wikidata = "P2728" },
		team = { url = "?id=28&nr=%s", wikidata = "P2939" },
		stable = { url = "?id=29&nr=%s", wikidata = "P3042" }
	},
	wrestlingdata = {
		name = "Wrestlingdata.com",
		url = "https://www.wrestlingdata.com/index.php",
		person = { url = "?befehl=bios&wrestler=%s", wikidata = "P2764" },
		team = { url = "?befehl=stables&stable=%s", wikidata = nil },
		stable = { url = "?befehl=stables&stable=%s", wikidata = nil }
	},
	iwd = {
		name = "Internet Wrestling Database",
		url = "http://www.profightdb.com/",
		person = { url = "wrestlers/%s.html", wikidata = "P2829" },
		team = { url = nil, wikidata = nil },
		stable = { url = nil, wikidata = nil }
	}
}

local types = {
	["Q5"] = "person",
	["Q1066670"] = "team",
	["Q25178247"] = "stable"
}

local get_wikidata_value = function(entity, property)
	if entity and property then
		local value = entity:getBestStatements(property)[1]
		if value then
			return value["mainsnak"]["datavalue"]["value"]
		end
	end
	return nil
end

local get_type = function(value, entity)
	if value and (value == "person" or value == "team" or value == "stable") then
		return value
	end
	value = get_wikidata_value(entity, "P31")
	if value and types[value.id] then
		return types[value.id]
	end
	return "person"
end

local get_name = function(frame, name)
	return name or frame:expandTemplate{title = 'PAGENAMEBASE'}
end

local get_external_link = function(frame, text, category, entity, property)
	local args = getArgs(frame)
	local id = args[1] or args["id"]
	if not id and entity then
		id = get_wikidata_value(entity, property)
		if id then
			text = text .. frame:expandTemplate{title = 'EditAtWikidata', args = { pid = property }}
		end
	end
	if not id then
		return mw.title.getCurrentTitle().namespace == 0 and category or nil
	end
	return string.format(text, id, get_name(frame, args[2] or args["name"]))
end

local p = {}

function p.cagematch(frame)
	local args = getArgs(frame)
	local entity = mw.wikibase.getEntity()
	local _type = get_type(args["type"], entity)
	return get_external_link(frame,
		"[" .. profiles["cagematch"]["url"] .. profiles["cagematch"][_type]["url"] .. " %s]'s profile at " .. profiles["cagematch"]["name"],
		nil,
		entity,
		profiles["cagematch"][_type]["wikidata"]
	)
end

function p.wrestlingtitlespersonalities(frame)
	return get_external_link(frame,
		"[http://www.wrestling-titles.com/personalities/%s/ %s]'s profile at Wrestling-Titles.com",
		"[[Category:Wrestling Titles template with no id set]]",
		nil,
		nil
	)
end

function p.rohroster(frame)
	return get_external_link(frame,
		"[https://www.ringofhonor.com/active-roster/%s %s]'s [[Ring of Honor]] profile",
		nil,
		nil,
		nil
	)
end

function p.njpw(frame)
	local args = getArgs(frame)
	return get_external_link(frame,
		"[" .. (args["newlink"] and "http://www.njpw1972.com/profile/%s" or "http://www.njpw.co.jp/english/data/detail_profile.php?f=%s") .. " %s]'s [[New Japan Pro-Wrestling]] profile",
		nil,
		nil,
		nil
	)
end

function p.gfw(frame)
	return get_external_link(frame,
		"[http://globalforcewrestling.com/roster/%s/ %s]'s [[Global Force Wrestling]] profile",
		nil,
		nil,
		nil
	)
end

function p.dragongateusa(frame)
	return get_external_link(frame,
		"[http://dgusa.tv/bio/%s %s]'s [[Dragon Gate USA]] profile",
		nil,
		nil,
		nil
	)
end

function p.chikara(frame)
	return get_external_link(frame,
		"[http://chikarapro.com/chikara-roster/%s %s]'s [[Chikara (professional wrestling)|Chikara]] profile",
		nil,
		nil,
		nil
	)
end

function p.profiles(frame)
	local args = getArgs(frame)
	local entity = mw.wikibase.getEntity()
	local _type = get_type(args["type"], entity)
	local text = ""

	for site, data in pairs(profiles) do
		local value = args[site] or get_wikidata_value(entity, data[_type]["wikidata"])
		if value and data[_type]["url"] then
			text = string.format("[" .. data["url"] .. data[_type]["url"] .. " " .. data["name"] .. "]", value)
			.. (args[site] and "" or frame:expandTemplate{title = 'EditAtWikidata', args = { pid = data[_type]["wikidata"] }})
			.. ", "
			.. text
		end
	end

	if text == "" then
		return mw.title.getCurrentTitle().namespace == 0 and "[[Category:Professional wrestling profiles template without any identifiers]]" or nil
	end

	return get_name(frame, args["name"]) .. "'s profile at " .. string.sub(text, 1, -3)
end

return p