You can edit almost every page by Creating an account and confirming your email.

Module:SportsReference: Difference between revisions

From EverybodyWiki Bios & Wiki
WikiMaster (talk | contribs)
m 1 revision imported
 
WikiMasterBot2 (talk | contribs)
m Add new revision
Line 1: Line 1:
-- This is the code to insert a template to indicate that the link is in English:
local function linktext(s1,s2,s3)
-- frame:expandTemplate{ title = "LANGUAGETEMPLATENAME", args = { "en" } }
if (s3 == nil) or (s3 == "") then
-- It is obviously not used in the English Wikipedia itself.
return "[https://www.sports-reference.com/olympics/athletes/" .. s1 .. ".html " .. s2 .. "] at [[Sports Reference#Olympics|Olympics at Sports-Reference.com]]"
local function linktext(s)
entity = mw.wikibase.getEntityObject()
if not entity then
label = mw.title.getCurrentTitle().text
else
else
label = mw.wikibase.label(entity.id) or mw.title.getCurrentTitle().text
return "[https://www.sports-reference.com/olympics/athletes/" .. s1 .. ".html " .. s2 .. "] at [[Sports Reference#Olympics|Olympics at Sports-Reference.com]] ([https://web.archive.org/web/" .. s3 .. "/https://www.sports-reference.com/olympics/athletes/" .. s1 .. ".html archive])"
end
end
if (s == nil) or (s == "") then
end
-- This text returns an error that says that the Sports Reference ID is neither
 
-- present on Wikidata nor in the article, and categorises the page as missing
local function category(s)
-- the Wikidata property.
if mw.title.getCurrentTitle().namespace ~= 0 then
return "<span class='error'>Sports Reference ID is neither present in Wikidata nor the article!</span> \
return ""
[[Template:Sports-reference#Add ID in Wikidata|How do I fix this?]]\
[[Category:Sports Reference ID not in Wikidata]]"
else
-- This is the text that is returned if there is a Sports Reference ID on Wikidata or in the article.
return "[https://www.sports-reference.com/olympics/athletes/" .. s .. ".html " .. label .. "] at [[Sports Reference]]"
end
end
return "[[Category:Sports-Reference template " .. s .. "]]"
end
end


Line 25: Line 17:


function p.link(frame)
function p.link(frame)
-- This is a check to see if the optional first parameter contains ".html". If it does, remove it.
 
id = string.gsub((frame.args[1] or ""), ".html", "")
-- Optional first parameter contains ID portion of Sports-Reference URL.
if not mw.wikibase then
-- Trim any leading or trailing spaces. If it contains ".html", remove it.
return linktext(id)
 
end
local id = string.gsub((mw.text.trim(frame.args[1]) or ""), ".html", "")
local entity = mw.wikibase.getEntityObject()
 
if not entity then
-- Optional second parameter contains name for link. Trim leading or trailing spaces.
-- Category for articles that don't have items in Wikidata.
-- If name is not provided, use article name without disambiguation.
return linktext(id) .. "[[Category:Articles without Wikidata item]]"
 
local name = mw.text.trim(frame.args[2])
if (name == nil) or (name == "") then
name = string.gsub(mw.title.getCurrentTitle().text, "%s+%b()$", "", 1)
end
end
-- Optional third parameter contains date/time portion of Archive.org URL.
local archive = mw.text.trim(frame.args[3])
-- 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 claims = entity.claims or {}
local hasProp = claims["P1447"]
local hasProp = claims["P1447"]
if not hasProp then
if not hasProp then
-- Category for articles that don't have the Sports Reference ID property on Wikidata.
if (id == nil) or (id == "") then
return linktext(id) .. "[[Category:Sports Reference ID not in Wikidata]]"
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("missing ID and not in Wikidata")
else
return linktext(id,name,archive) .. category("with ID not in Wikidata")
end
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
local propValue = hasProp[1].mainsnak.datavalue.value
return linktext(propValue)
if (id == nil) or (id == "") then
return linktext(propValue,name,archive) .. " [[File:Blue pencil.svg |frameless |text-top |10px |alt=Edit this at Wikidata |link=https://www.wikidata.org/wiki/" .. entity.id .. "#P1447|Edit this at Wikidata]]" .. category("using Wikidata")
end
if id == propValue then
return linktext(id,name,archive) .. category("with ID same as Wikidata")
else
return linktext(id,name,archive) .. category("with ID different from Wikidata")
end
 
end
end


return p
return p

Revision as of 14:35, 14 March 2019

Usage

This module returns a link to Sports-Reference.com. The URL is taken from Wikidata, unless there is no link on Wikidata. If that's the case, the module uses the link from the optional backup parameter; if there is no link on Wikidata and no backup parameter, it will return an error message explaining how to fix the issue.

The module only has one function, link, which returns the external link text ready for use. It should be used in the {{Sports-reference}} template thusly:

{{#invoke:SportsReference|link|optional backup parameter}}

The reason the module prefers links from Wikidata rather than the links given in the backup parameter is that it is much simpler to correct the links in Wikidata, and that will benefit all projects and not just this wiki; also, Wikidata supports various constraints so that the items and values can easily be checked for errors.



local function linktext(s1,s2,s3)
	if (s3 == nil) or (s3 == "") then
		return "[https://www.sports-reference.com/olympics/athletes/" .. s1 .. ".html " .. s2 .. "] at [[Sports Reference#Olympics|Olympics at Sports-Reference.com]]"
	else
		return "[https://www.sports-reference.com/olympics/athletes/" .. s1 .. ".html " .. s2 .. "] at [[Sports Reference#Olympics|Olympics at Sports-Reference.com]] ([https://web.archive.org/web/" .. s3 .. "/https://www.sports-reference.com/olympics/athletes/" .. s1 .. ".html archive])"
	end
end

local function category(s)
	if mw.title.getCurrentTitle().namespace ~= 0 then
		return ""
	end
	return "[[Category:Sports-Reference template " .. s .. "]]"
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.

	local 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.

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

	-- Optional third parameter contains date/time portion of Archive.org URL.

	local archive = mw.text.trim(frame.args[3])

	-- 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("missing ID and not in Wikidata")
		else
			return linktext(id,name,archive) .. category("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,archive) .. " [[File:Blue pencil.svg |frameless |text-top |10px |alt=Edit this at Wikidata |link=https://www.wikidata.org/wiki/" .. entity.id .. "#P1447|Edit this at Wikidata]]" .. category("using Wikidata")
	end
	if id == propValue then
		return linktext(id,name,archive) .. category("with ID same as Wikidata")
	else
		return linktext(id,name,archive) .. category("with ID different from Wikidata")
	end

end

return p

This module "SportsReference" is from Wikipedia if otherwise notified