Jump to content

Module:Sports reference

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Zyxw (talk | contribs) at 07:06, 21 May 2018 (update code and add tracking categories, tested first with /sandbox using test cases and previews in actual articles). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local function linktext(s1,s2)
	return "[https://www.sports-reference.com/olympics/athletes/" .. s1 .. ".html " .. s2 .. "] at [[Sports Reference]]"
end

local p = {}

function p.link(frame)

	-- Optional first parameter contains ID portion of Sports-Reference URL
	-- Trim any leading or trailing spaces. If it contains ".html", remove it.

	id = string.gsub((mw.text.trim(frame.args[1]) or ""), ".html", "")

	-- Optional second parameter contains name for link. Trim leading or trailing spaces. 
	-- If name is not provided, use article name without disambiguation.

	name = mw.text.trim(frame.args[2])
	if (name == nil) or (name == "") then
		name = string.gsub(mw.title.getCurrentTitle().text, "%s+%b()$", "", 1)
	end

	-- If mw.wikibase not available:
	-- if ID not provided, return error text
	-- if ID is provided, return link

	if not mw.wikibase then
		if (id == nil) or (id == "") then
			return "<span class='error'>Sports-Reference template missing ID.</span>"
		else
			return linktext(id,name)
		end
	end

	-- For articles without Wikidata property:
	-- if ID not provided, return error text and tracking category
	-- if ID is provided, return link and tracking category

	local entity = mw.wikibase.getEntityObject() or {}
	local claims = entity.claims or {}
	local hasProp = claims["P1447"]
	if not hasProp then
		if (id == nil) or (id == "") then
			return "<span class='error'>Sports-Reference template missing ID and not present in Wikidata.</span> [[Template:Sports-reference#Add ID in Wikidata|How do I fix this?]][[Category:Sports-Reference template missing ID and not in Wikidata]]"
		else
			return linktext(id,name) .. "[[Category:Sports-Reference template with ID not in Wikidata]]"
		end
	end

	-- For articles with Wikidata property:
	-- if ID not provided, return link (using Wikidata) and tracking category
	-- if ID is provided, return link (using ID) and one of two tracking categories

	local propValue = hasProp[1].mainsnak.datavalue.value
	if (id == nil) or (id == "") then
		return linktext(propValue,name) .. "[[Category:Sports-Reference template using Wikidata]]"
	end
	if id == propValue then
		return linktext(id,name) .. "[[Category:Sports-Reference template with ID same as Wikidata]]"
	else
		return linktext(id,name) .. "[[Category:Sports-Reference template with ID different from Wikidata]]"
	end

end

return p