Jump to content

Module:FindYDCportal

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by BrownHairedGirl (talk | contribs) at 20:40, 22 November 2018 (getting there. Incomplete, but save). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {}

-- check for the existence of a portal with the given name
-- if it exists, returns the name
-- otherwise returns nil
function checkPortalExists(portalName)
	local portalPage = mw.title.new( portalName, "Portal" )
	if (portalPage.exists) then
		return portalName
	end
	return nil
end


-- check for the existence of a portal with the name of that year
-- if it exists, returns the year
-- otherwise calls decadeCheck, and returns that result
-- otherwise returns nil
function checkYear(year)
	if checkPortalExists(year) then
		return year
	end
	-- myDecade = the year, with the last digit stripped
	--            e.g. "1694" → "1690"
	--            Note that a deacde is written as usul=ally written "YYY0s"
	local myDecade = mw.ustring.gsub(year, "%d$", "0s")
	return checkDecade(myDecade)
end

-- check for the existence of a portal with the name of that decades
-- if it exists, returns the year
-- otherwise calls decadeCheck, and returns that result
-- otherwise returns nil
function checkYear(year)
	if checkPortalExists(year) then
		return year
	end
	-- myDecade = the year, with the last digit replaced with "0"
	--            e.g. "1694" → "1690"
	local myDecade = mw.ustring.gsub(year, "%d$", "0s")
	return checkDecade(myDecade)
end

function p.main(frame)
	--Expects one paramter
	-- {{{1}}}= a 3- or 4-digit year or deacde
	--    e.g. 1916
	--         1504
	--         1630s
	--         920s
	local arg1 = frame.args[1]
	if arg1 == nil
	then
		return ""
	end
	if (mw.ustring.match(arg1, "^%d%d%d%d?$")) then
		-- it's a 3- or 4-digit-year
		return checkYear(arg1)
	elseif (mw.ustring.match(arg1, "^%d%d%d?0s$")) then
		-- it's a 3- or 4-digit decade
		-- so strip the trailing "0s"
		local decadeArg = mw.ustring.gsub(arg1, "^(%?d%d%d)0s$", "%1")
		return checkDecade(decadeArg)
	end
	-- If we get here, then arg1 was neither a year nor a decade
	-- This is going to be a helper template, and diagnostics woud be intrusive
	-- So just return an empty string
	return ""
end

return p