Přeskočit na obsah

Modul:Maintenance

Z Wikislovníku

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

-- @brief
--  Maintenance handling.
-- 
-- @author
--  [[meta:User:Danny B.]]
local Maintenance = {}
----------------------------------------


Maintenance.prefix = {
	message = "UPOZORNĚNÍ: ",
	category = "Údržba:"
}
Maintenance.default = {
	message = "Chybné vložení.",
	category = "Opravit chybné volání šablony %s",
	categoryPattern = "%s v šabloně %s"
}
Maintenance.commonTypes = {
	parameterMissing = {
		message = {
			prefix = "Chybí ",
			numbered = "%d. parametr (%s)",
			named = "parametr „%s“ (%s)",
			suffix = "."
		},
		category = {
			prefix = "Doplnit ",
			numbered = "%d. parametr",
			named = "parametr „%s“",
			suffix = ""
		}
	},
	parameterUnknown = {
		message = {
			prefix = "Neznámý ",
			numbered = "parametr „%s“",
			named = "parametr „%s“",
			suffix = "."
		},
		category = {
			prefix = "Zkontrolovat neznámý parametr",
			numbered = "",
			named = "",
			suffix = ""
		}
	},
	valueMissing = {
		message = {
			prefix = "Chybí hodnota ",
			numbered = "%d. parametru (%s)",
			named = "parametru „%s“ (%s)",
			suffix = "."
		},
		category = {
			prefix = "Doplnit hodnotu ",
			numbered = "%d. parametru",
			named = "parametru „%s“",
			suffix = ""
		}
	},
	valueOutOfRange = {
		message = {
			prefix = "Hodnota ",
			numbered = "%d. parametru (%s)",
			named = "parametru „%s“ (%s)",
			suffix = " je mimo akceptovaný rozsah."
		},
		category = {
			prefix = "Opravit hodnotu ",
			numbered = "%d. parametru",
			named = "parametru „%s“",
			suffix = ""
		}
	},
	valueUnknown = {
		message = {
			prefix = "Neznámá hodnota ",
			numbered = "%d. parametru (%s)",
			named = "parametru „%s“ (%s)",
			suffix = "."
		},
		category = {
			prefix = "Opravit hodnotu ",
			numbered = "%d. parametru",
			named = "parametru „%s“",
			suffix = ""
		}
	},
	valueWrongFormat = {
		message = {
			prefix = "Chybný formát hodnoty ",
			numbered = "%d. parametru (%s)",
			named = "parametru „%s“ (%s)",
			suffix = "."
		},
		category = {
			prefix = "Opravit hodnotu ",
			numbered = "%d. parametru",
			named = "parametru „%s“",
			suffix = ""
		}
	}
}


setmetatable( Maintenance, {
	__call = function ( class, ... )
		return class.new( ... )
	end
})


Maintenance.__index = Maintenance

Maintenance.class = "warning mwm-maintenance"


function Maintenance.new( template )
	
	local self = setmetatable( {}, Maintenance )
	
	self.template = template
	self.message = false
	self.category = false
	self.overrideDefaultCategoryPattern = false
	
	return self
	
end


function Maintenance:setMessage( message )
	
	self.message = message
	
	return self
	
end


function Maintenance:setCategory( category, override )
	
	if not ( type( override ) == "boolean" or type( override ) == "nil" ) then
		error( "setCategory: Wrong type of the \"override\" parameter - boolean or nil expected.", 2 )
	end
	
	self.category = category
	self.overrideDefaultCategoryPattern = override
	
	return self
	
end


function Maintenance:getMessage()
	
	local templateLink = ""
	
	if self.template then
		templateLink = mw.ustring.format(
			"<nowiki>{{</nowiki>[[%s:%s|%s]]<nowiki>}}</nowiki> — ",
			mw.site.namespaces.Template.name,
			self.template,
			self.template
		)
	end
	
	return tostring(
		mw.html.create( "strong" )
			:attr( "class", self.class )
			:wikitext(
				self.prefix.message,
				templateLink,
				( self.message or self.default.message )
			)
			:allDone()
	)
	
end


function Maintenance:getCategory()
	
	if self.category then
		self.category = self.overrideDefaultCategoryPattern and self.category or mw.ustring.format( self.default.categoryPattern, self.category, ( self.template or "" ) )
	else
		self.category = mw.ustring.format( self.default.category, ( self.template or "" ) )
	end
	
	return mw.ustring.format(
		"[[%s:%s%s]]",
		mw.site.namespaces.Category.name,
		self.prefix.category,
		mw.text.trim( self.category )
	)
	
end


function Maintenance:getText()
	
	return mw.ustring.format(
		"%s<includeonly>%s</includeonly>",
		self:getMessage(),
		self:getCategory()
	)
	
end


for functionName, functionData in pairs( Maintenance.commonTypes ) do
	
	Maintenance[functionName] = function( self, paramInfo )
		
		if paramInfo and paramInfo.name and paramInfo.desc then
			local paramType = ( tonumber( paramInfo.name ) and "numbered" or "named" )
			self.message = functionData.message.prefix .. functionData.message[paramType]:format( paramInfo.name, paramInfo.desc ) .. functionData.message.suffix
			self.category = functionData.category.prefix .. functionData.category[paramType]:format( paramInfo.name, paramInfo.desc ) .. functionData.category.suffix
			self.overrideDefaultCategoryPattern = false
		else
			error( functionName .. ": Missing paramInfo table or any of its parts.", 2 )
		end
		
		return self
		
	end
	
end


----------------------------------------
return Maintenance