Jump to content

Module:Deprecated

From Wikipedia, the free encyclopedia

--- Marks items as deprecated, and provides a warning when they are called.
-- @release alpha
-- @author [[User:Awesome_Aasim|Awesome Aasim]]
-- @function deprecated
-- @param {table} p package frame
-- @param deprecatedTable 
-- @return package

return function(p, deprecatedTable, replacement)
	local pckg = {}
	--- Warn
	-- @param {string} text warning text
	function warn(text)
		local tb = debug.traceback()
		mw.log(text .. '\n' .. tb)
		mw.addWarning(text .. tb:gsub("\n", "<br/>"):gsub("\t", "&emsp;"))
	end
	if deprecatedTable == nil or deprecatedTable == true then
		deprecatedTable = {}
		for k,_ in pairs(p) do
			deprecatedTable[k] = {
				deprecated = true,
				replacement = replacement or ""
			}
		end
	end
	setmetatable(pckg, {
		__index = function(t, index)
			if deprecatedTable[index] and deprecatedTable[index]["deprecated"] then
				warn(
					mw.ustring.format(
						"Deprecated member <code>%s</code> called. ", index
					) .. (
						deprecatedTable[index]["replacement"]
						and mw.ustring.format("Please %s instead.", deprecatedTable[index]["replacement"])
						or ''
					)
				)
			end
			return p[index]
		end
	})
	return pckg
end