Module:WikiProjectBanner/shared

MyWikiBiz, Author Your Legacy — Saturday December 28, 2024
Jump to navigationJump to search

Documentation for this module may be created at Module:WikiProjectBanner/shared/doc

-------------------------------------------------------------------------------
--              Shared functions for Module:WikiProjectBanner                --
-------------------------------------------------------------------------------

-- Lazily initialise modules that we don't always need.
local mPortal, yesno

local p = {}

function p.maybeLoadData(page)
	local success, data = pcall(mw.loadData, page)
	if success then
		return data
	end
end

function p.maybeRequire(page)
	local success, module = pcall(require, page)
	if success then
		return module	
	end
end

function p.makePortal(portal, options)
	options = options or {}
	mPortal = mPortal or require("Module:Portal")
	return mPortal._portal({ portal }, options)
end

function p.isActiveRow(args, bannerData, cfg, rowCfg)
	yesno = yesno or require('Module:Yesno')
	if not rowCfg then
		return false
	elseif bannerData.isDemo then
		return true
	elseif rowCfg.params then
		for i, param in ipairs(rowCfg.params) do
			local isSet = yesno(args[param], true)
			if isSet then
				return true
			elseif isSet == false then
				return false
			end
		end
		return false
	else
		local param = cfg.defaultParameters[key]
		return param and yesno(args[param]) or false
	end
end

function p.substituteParams(msg, ...)
	return mw.message.newRawMessage(msg):params{...}:plain()
end

function p.fetchArgument(args, keys)
	local keysType = type(keys)
	if keysType == 'string' then
		return args[keys]
	elseif keysType == 'table' then
		for _, key in ipairs(keys) do
			local val = args[key]
			if val ~= nil then
				return val
			end
		end
	end
end

function p.makeWikilink(link, display)
	if display then
		return string.format('[[%s|%s]]', link, display)
	else
		return string.format('[[%s]]', link)
	end
end

return p