Jump to content

Module:Sandbox/Zyxw/test

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Zyxw (talk | contribs) at 04:30, 21 May 2018 (test). 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 text. 
	-- Trim any leading or trailing spaces. If it is not provided, use article name.

	name = mw.text.trim(frame.args[2])
	if (name == nil) or (name == "") then
		name = mw.title.getCurrentTitle().text
	end

	-- If Wikidata not available, return link

	if not mw.wikibase then
		return linktext(id,name) .. " 1"
	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()
	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?]]) 2[[Category:Sports-Reference template missing ID and not in Wikidata]]"
		else
			return linktext(id,name) .. " 3[[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) .. " 4[[Category:Sports-Reference template using Wikidata]]"
	end
	if id == propValue then
		return linktext(id,name) .. " 5[[Category:Sports-Reference template with ID same as Wikidata]]"
	end
	return linktext(id,name) .. " 6[[Category:Sports-Reference template with ID different from Wikidata]]"

end

return p