Modul:MonitorTemplateArguments

Z Wikizdrojů, volně dostupné knihovny
Skočit na navigaci Skočit na vyhledávání

Dokumentaci tohoto modulu lze vytvořit na stránce Nápověda:Modul:MonitorTemplateArguments

-- @brief
--  Monitoring of template arguments.
-- 
-- @author
--  [[meta:User:Danny B.]]
local _module = {}
----------------------------------------


local kpairs = require( "Module:Iterators" ).kpairs
local mixedScalarTypes = require( "Module:SortingComparators" ).mixedScalarTypes


-- @brief
--  Returns the argument name as a trimmed string or number depending on what applies
-- 
-- @param
--  argumentString String to be examined
-- 
-- @return
--  String|Number Argument name 
function _module.getArgumentName( argumentString )

	local output
	
	output = mw.text.trim( argumentString )
	output = tonumber( output ) and tonumber( output ) or output
	
	return output
	
end


-- @brief
--  Creates the argument monitoring category name
-- 
-- @param
--  templateName Monitored template
--  argumentName Monitored argument
--  argumentValue Value or value type of monitored argument
-- 
-- @return
--  Wikitext Monitoring category
function _module.getArgumentMonitoringCategory( templateName, argumentName, argumentValue )
	
	local output
	
	output = mw.ustring.format( "[[Kategorie:Monitoring:%s/%s/%s]]", templateName, tostring( argumentName ), argumentValue )
	
	return output
	
end


-- @brief
--  Creates the argument presence monitoring category name
-- 
-- @param
--  templateName Monitored template
--  argumentName Monitored argument
--  argumentValue Value type of monitored argument
-- 
-- @return
--  Wikitext Monitoring category
function _module.getArgumentPresenceMonitoringCategory( templateName, argumentName, argumentValue )
	
	local output
	
	argumentValue = " " .. argumentValue
	
	output = _module.getArgumentMonitoringCategory( templateName, argumentName, argumentValue )
	
	return output
	
end


-- @brief
--  Creates the Wikidata status monitoring category name
-- 
-- @param
--  templateName Monitored template
--  argumentName Monitored argument
--  argumentValue Value type of Wikidata status
-- 
-- @return
--  Wikitext Monitoring category
function _module.getWikidataStatusMonitoringCategory( templateName, argumentName, argumentValue )
	
	local output
	
	argumentValue = mw.ustring.format( " Wikidata/%s", argumentValue )
	
	output = _module.getArgumentMonitoringCategory( templateName, argumentName, argumentValue )
	
	return output
	
end


-- @brief
--  Monitors the presence and emptiness of given parameter
-- 
-- @param
--  frame The current frame object
-- 
-- @return
--  PreprocessedWikitext Relevant monitoring category
function _module.printArgumentPresence( frame )
	
	local output = ""
	
	local templateName = mw.title.new( frame:getParent():getTitle() ).text
	local argName = _module.getArgumentName( frame.args["parametr"] )
	local passedArg = frame:getParent().args[argName]
	
	local result = "nepřítomný"
	
	if passedArg then
		result = mw.text.trim( passedArg ) == "" and "prázdný" or "vyplněný"
	end
	
	output = output .. _module.getArgumentPresenceMonitoringCategory( templateName, argName, result )
	
	output = frame:preprocess( output )
	
	return output
	
end


-- @brief
--  Monitors the presence and emptiness of all passed and enumerated available parameters
-- 
-- @param
--  frame The current frame object
-- 
-- @return
--  PreprocessedWikitext Relevant monitoring categories
function _module.printArgumentsPresence( frame )
	
	local output = ""
	
	local templateName = mw.title.new( frame:getParent():getTitle() ).text
	local availableArgs = frame.args
	local passedArgs = frame:getParent().args
	
	local argVals = {}
	
	for _, arg in ipairs( availableArgs ) do
		argVals[_module.getArgumentName( arg )] = "nepřítomný"
	end
	
	for arg, val in pairs( passedArgs ) do
		argVals[arg] = mw.text.trim( val ) == "" and "prázdný" or "vyplněný"
	end
	
	for arg, val in kpairs( argVals, mixedScalarTypes ) do
		output = output .. _module.getArgumentPresenceMonitoringCategory( templateName, arg, val )
	end
	
	output = frame:preprocess( output )
	
	return output
	
end


-- @brief
--  Monitors the status of possible Wikidata values in template parameters
-- 
-- @param
--  frame The current frame object
-- 
-- @return
--  PreprocessedWikitext Relevant monitoring categories
function _module.printWikidataStatus( frame )
	
	local output = ""
	
	local templateName = mw.title.new( frame:getParent():getTitle() ).text
	local wikidataArgs = frame.args
	local templateArgs = frame:getParent().args
	
	local argVals = {}
	
	for arg, val in pairs( wikidataArgs ) do
		
		local wikidataVal = wikidataArgs[arg] and mw.text.trim( wikidataArgs[arg] )
		local templateVal = templateArgs[arg] and mw.text.trim( templateArgs[arg] )
		wikidataVal = wikidataVal ~= "" and wikidataVal or nil
		templateVal = templateVal ~= "" and templateVal or nil
		
		if wikidataVal then
			argVals[arg] = "převzato"
			if templateVal then
				argVals[arg] = wikidataVal == templateVal and "shodné" or "rozdílné"
			end
		else
			argVals[arg] = nil
		end
		
	end
	
	for arg, val in kpairs( argVals, mixedScalarTypes ) do
		output = output .. _module.getWikidataStatusMonitoringCategory( templateName, arg, val )
	end
	
	output = frame:preprocess( output )
	
	return output
	
end


----------------------------------------
return _module