Module:Sandbox/Zyxw/test
Appearance
< Module:Sandbox | Zyxw
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 item, return link and tracking category
local entity = mw.wikibase.getEntityObject()
if not entity then
return linktext(id,name) .. " 2[[Category:Articles without Wikidata item]]"
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 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) .. " 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