ሞድዩል:ክርክራት

ካብ ዊኪፐድያ፣ ናጻ ኢንሳይክሎፐድያ

Documentation for this module may be created at ሞድዩል:ክርክራት/doc

local z = {}

function z.getArguments(frame)
	if frame.args[1] then 
		return frame.args
	end

	return frame:getParent().args
end

function z.getArgumentsWithValue(frame)
	if frame == mw.getCurrentFrame() then
		arguments = frame:getParent().args
	else
		arguments = frame.args or frame
	end

	return require('Module:Tables').copyElementsWithValue(arguments)
end

--[[
	@name	getArgumentsTable
	@global	args
	@param	frame
	@return	table
	@descr	Gets an argument table taking the parameters received from
			both the template and a module invocation.
			In the case of duplicates, the value of the invocation takes precedence.
			For example:
				with the template:	{{Template |field=value |key=key }}
				and the invocation:	{{#invoke:Module |key=value }}
				gets:				{ ['field'] = 'value', ['key'] = 'value' }
--]]
function z.getArgumentsTable(frame)
	-- global args
	args = {}
	local function paramMerge(orig, copy)
		local data = {}
		for key, val in pairs(orig) do
			data[key] = val
		end
		for key, val in pairs(copy) do
			data[key] = val
		end
		return data
	end
	if frame then
		-- parentArgs = frame:getParent().args or {}
		if type(frame.getParent) == 'function' then
			local parent = frame:getParent()
			if parent then
				args = paramMerge(args, parent.args)
			end
		end
		-- invokeArgs = frame.args or frame or {}
		if type(frame.args) == 'table' then
			args = paramMerge(args, frame.args)
		elseif type(frame) == 'table' then
			args = paramMerge(args, frame)
		end
	end
	return args
end

--[[
	@name	getArgumentsValue
	@global	args
	@param	list
	@return	string or nil
	@descr	Gets the first valid argument from a parameter table.
			This parameter table is a list containing the names
			of the arguments or other tables with the functions to get them.
	Parameters:
		with arguments: 		{ ['field'] = 'value', ['key'] = 'value' }
		and using the call: 	getArgumentsValue{'data', 'field', 'key'}
		the value is obtained:	'value'
		since 'data' is not an argument and 'field' is the first found
	Functions:
		it can also be called with a function of the form
			getArgumentsValue{'data', { getData, '1', '2' }}
		so that if the argument 'data' does not exist,
			the function getData('1', '2') is called
--]]
function z.getArgumentsValue(list)
	-- global args
	local lang = mw.language.getContentLanguage()
	local err, key
	if type(list) == 'number' then
		key = args[list]
	elseif type(list) == 'string' then
		key = args[list] or args[lang:ucfirst(list)]
	elseif type(list) == 'table' then
		for num, val in ipairs(list) do
			if type(val) == 'string' or type(val) == 'number' then
				key = z.getArgumentsValue(val)
			elseif type(val) == 'function' then
				err, key = pcall(val)
				if err ~= true then
					key = nil
				end
			elseif type(val) == 'table' then
				if val[1] and type(val[1]) == 'function' then
					err, key = pcall(val[1], unpack(val, 2))
					if err ~= true then
						key = nil
					end
				end
			end
			if key ~= nil and key ~= '' then
				return key -- break
			end
		end
	end
	return key
end

return z