Modulo:Progetti interessati

Versione del 2 dic 2015 alle 18:51 di Rotpunkt (discussione | contributi) (utilizzo livello da altro modulo)
Info Istruzioni per l'uso
Questo è un modulo scritto in Lua. Le istruzioni che seguono sono contenute nella sottopagina Modulo:Progetti interessati/man (modifica · cronologia)
Sandbox: Modulo:Progetti interessati/sandbox (modifica · cronologia) · Sottopagine: lista · Test: Modulo:Progetti interessati/test (modifica · cronologia · esegui)

Modulo che implementa il template {{Progetti interessati}}.

Ha due sottopagine di configurazione:


--[[
* Modulo che implementa il template Monitoraggio.
]]--

require('Modulo:No globals')

local getArgs = require('Modulo:Arguments').getArgs

-- Configurazione
local cfg = mw.loadData('Modulo:Monitoraggio/Configurazione')

-- Variabili globali
local projParams = { 'progetto', 'progetto2', 'progetto3', 'progetto4' }
local gradeParams = { 'accuratezza', 'scrittura', 'fonti', 'immagini' }

-------------------------------------------------------------------------------
--                             Funzioni di utilità
-------------------------------------------------------------------------------

-- Wrapper di mw.title.exists, verifica sia che name sia valido, sia che esista
local function titleExists(name)
	local title = mw.title.new(name)
	return title and title.exists
end

local function project(param, args)
	return cfg.alias[args[param]] or mw.language.getContentLanguage():ucfirst(args[param])
end

local function grade(param, args)
	return args[param] or 'nc'
end

local function lowestGrade(args)
	local t = { args.accuratezza, args.scrittura, args.fonti, args.immagini }
	table.sort(t)
	return t[4]
end

local function parseArgs(args)
	local validGrades = { a = true, b = true, c = true, d = true, e = true }
	local cat = ''
	
	for _, param in ipairs(gradeParams) do
		if args[param] and not validGrades[args[param]] then
			if args[param] ~= 'nc' then
				cat = '[[Categoria:Pagine con template Monitoraggio con valutazione invalida]]'
			end
			args[param] = nil
		end
	end
	args.data = args.data and args.data:lower() or nil

	return args, cat
end

-------------------------------------------------------------------------------
--                             Monitoraggio
-------------------------------------------------------------------------------

local function getLivello(args)
	local ret

	-- è presente accuratezza
	if args.accuratezza then
		if not args.scrittura then
			ret = '0.4'
		elseif not args.fonti then
			ret = '0.3'
		elseif not args.immagini then
			ret = '0.2'
		else
			-- sono presenti tutti e quattro i parametri di valutazione
			if args.accuratezza == 'e' then
				ret = 'BOZZA'
			elseif args.scrittura == 'e' then
				ret = 'W'
			elseif args.fonti == 'e' then
				ret = 'F'
			elseif args.immagini == 'e' then
				ret = 'IMMAGINI'
			else
				local values = { a = 4, b = 3, c = 2, d = 1 }
				ret = tostring(values[lowestGrade(args)])
			end
		end
	-- manca accuratezza
	else
		if args.scrittura or args.fonti or args.immagini then
			ret = '0.5'
		else
			ret = nil
		end
	end

	return ret
end

local function getCategories(livello, args)
	local ret = {}
	local cat

	local suffix = livello and cfg.livello[livello].cat or 'non compilate'

	-- per la "Situazione generale" di [[Progetto:Qualità/Monitoraggio voci/Tabella]]
	cat = string.format('[[Categoria:Voci monitorate - %s]]', suffix)
	table.insert(ret, cat)

	for _, param in ipairs(projParams) do
		if args[param] then
			cat = string.format('[[Categoria:Voci monitorate Progetto %s]]', project(param, args))
			table.insert(ret, cat)
		end
	end

	for _, gradeParam in ipairs(gradeParams) do
		-- per la "Situazione monitoraggio per parametro" di [[Progetto:Qualità/Monitoraggio voci/Tabella]]
		cat = string.format('[[Categoria:Voci monitorate - %s %s]]',
							gradeParam, grade(gradeParam, args):upper())
		table.insert(ret, cat)

		-- categorie per il [[template:Tabella monitoraggio]]
		for _, projParam in ipairs(projParams) do
			if args[projParam] then
				cat = string.format('Categoria:Progetto:%s/Tabella monitoraggio automatico - %s %s',
									project(projParam, args), gradeParam, grade(gradeParam, args))
				if titleExists(cat) then
					table.insert(ret, '[[' .. cat .. ']]')
				end
			end
		end
	end

	-- categorie per il [[template:Tabella monitoraggio]]
	for _, param in ipairs(projParams) do
		if args[param] then
			cat = string.format('Categoria:Voci monitorate Progetto %s - %s',
								project(param, args), suffix)
			if titleExists(cat) then
				table.insert(ret, '[[' .. cat .. ']]')
			end
		end
	end

	-- per la "Situazione monitoraggio per mese" di [[Progetto:Qualità/Monitoraggio voci/Tabella]]
	if args.data then
		cat = string.format('Categoria:Voci monitorate - %s', args.data)
		if titleExists(cat) then
			table.insert(ret, '[[' .. cat .. ']]')
		else
			table.insert(ret, '[[Categoria:Errori di compilazione del template Monitoraggio]]')
		end
	else
		table.insert(ret, '[[Categoria:Voci monitorate - non datate]]')
	end

	return ret
end

-------------------------------------------------------------------------------
--                             Creazione HTML
-------------------------------------------------------------------------------

local function getIconProject(name)
	local icon = mw.getCurrentFrame():expandTemplate {
		title = 'Icona argomento',
		args = { name }
	}
	icon = icon == '' and 'Crystal Clear app ksirtet.png' or icon

	return string.format('[[File:%s|35x50px]]', icon)
end

local function getWlinkProjects(args)
	local ret = ''
	for _, param in ipairs(projParams) do
		if args[param] then
			ret = ret .. string.format("%s[[Progetto:%s/Monitoraggio voci|'''%s%s''']]",
									   #ret == 0 and '' or ' - ',
									   project(param, args),
									   #ret == 0 and 'Monitoraggio ' or '',
									   project(param, args))
		end
	end

	return ret
end

local function getNodeProjects(args)
	local tableStyle = {
		['text-align'] = 'center',
		['background'] = 'none',
		width = '100%'
	}
	local tableNode = mw.html.create('table')
	local trNode = tableNode:css(tableStyle):tag('tr')
	for _, param in ipairs(projParams) do
		trNode
			:tag('td')
				:attr('width', '5%')
				:wikitext(args[param] and getIconProject(args[param]) or '')
	end
	trNode
		:tag('td')
			:wikitext(getWlinkProjects(args))
			:done()
		:tag('td')
			:attr('width', '10%')
			:wikitext('')
			:done()

	return tableNode
end

local function getWlinkLivello(livello)
	local spanStyle = {
		['font-weight'] = 'bold',
		['font-size'] = '145%',
		['border'] = '1px solid lightsteelblue',
		['background'] = (livello and cfg.livello[livello].color) and cfg.livello[livello].color or 'white',
		['color']  = 'blue'
	}
	local spanNode = mw.html.create('span')
	local text
	if livello and cfg.livello[livello].label then
		text = string.format('[[:Categoria:Voci monitorate - %s|%s]]',
							 cfg.livello[livello].cat, cfg.livello[livello].label)
	elseif not livello then
		text = '[[:Categoria:Voci monitorate - non compilate|NC]]'
	end
	if text then
		spanNode:css(spanStyle)
		text = string.format("'''<tt>&nbsp;%s&nbsp;</tt>'''", text)
	else
		text = '[[File:Symbol stub class.svg|25px|center]]'
	end
	spanNode:wikitext(text)

	return spanNode
end

local function getTextData(args)
	local ret

	if args.data then
		local cat = string.format('Categoria:Voci monitorate - %s', args.data)
		if titleExists(cat) then
			ret = 'Voce monitorata nel mese di ' .. args.data
		end
	end

	return ret or "Voce monitorata nel mese di <span style=\"color:red;\"><small>'''''inserisci mese e anno'''''</small></span>"
end

local function getNodeLivello(livello, args)
	local url = mw.title.getCurrentTitle():fullUrl( { action = 'edit' } )
	local trNode = mw.html.create('tr')
	trNode
		:tag('td')
			:css({
				['background-color'] = 'none',
				width = '90px'
			})
			:node(getWlinkLivello(livello))
			:done()
		:tag('td')
			:css('font-size', '107%')
			:wikitext(livello and cfg.livello[livello].msg or
				"<div class=\"plainlinks\">La voce non è stata ancora monitorata, [" .. url .. " fallo ora]!</div>")
			:done()
		:tag('td')
			:css({
				color = 'grey',
				['text-align'] = 'right',
				['padding-right'] = '6px',
				['font-size'] = '95%'
			})
			:wikitext(getTextData(args))
			:done()

	return trNode
end

local function getNodeGrade(param, args)
	local tableStyle = {
		['margin-top'] = '2px',
		['border-bottom'] = param ~= 'immagini' and '1px solid #B5D9D3' or nil,
		['width'] = '100%'
	}
	local spanStyle = {
		['font-weight'] = 'bold',
		['font-size'] = '155%',
		['border'] = '1px solid lightsteelblue',
		background = cfg.colors[args[param]] or 'white'
	}
	local tableNode = mw.html.create('table')
	local descr = cfg[param][args[param] or 'nc'] .. ' ' .. cfg[param].help
	local grade = args[param] and args[param]:upper() or '<small>nc</small>'
	grade = "&thinsp;'''<tt>" .. grade .. "&thinsp;</tt>'''"
	tableNode
		:css(tableStyle)
		:tag('tr')
			:tag('td')
				:css('width', '40px')
					:tag('span')
						:css(spanStyle)	
						:wikitext(grade)
						:done()
				:done()
			:tag('td')
				:wikitext(descr)
				:done()

	return tableNode
end

local function getHTML(livello, args)
	local tableStyle = {
		border = '1px solid silver',
		['margin-bottom'] = '0.2em',
		['margin-left'] = 'auto',
		['margin-right'] = 'auto',
		['background-color'] = '#FFFFFF'
	}
	local tableNode = mw.html.create('table')
	tableNode
		:addClass('messagebox')
		:addClass('standard-talk')
		:css(tableStyle)
		:attr('align', 'center')
	-- progetti
	tableNode
		:tag('tr')
			:tag('td')
				:attr('colspan', '4')
				:css({
					background = '#B5D9D3',
					border = '0px solid #C0C090'
				})
				:node(getNodeProjects(args))
	-- livello	
	tableNode
		:node(getNodeLivello(livello, args))
	-- valutazioni
	tableNode
		:tag('tr')
			:tag('td')
				:attr('colspan', '3')
				:css({
					border = '1px solid #B5D9D3',
					['background-color'] = 'none',
					['font-size'] = '95%'
				})
				:node(getNodeGrade('accuratezza', args))
				:node(getNodeGrade('scrittura', args))
				:node(getNodeGrade('fonti', args))
				:node(getNodeGrade('immagini', args))
	-- note
	if args.note then
		tableNode
			:tag('tr')
				:tag('td')
					:attr('colspan', '3')
					:wikitext("* ''note:'' " .. args.note)
	end

	return tostring(tableNode)
end

-------------------------------------------------------------------------------
--                                    API
-------------------------------------------------------------------------------

local p = {}

-- Per l'utilizzo da un altro modulo
function p._livello(args)
	return getLivello(parseArgs(args))	
end

-- Entry-point per {{#invoke:monitoraggio|livello}}
function p.livello(frame)
	return p._livello(getArgs(frame))
end

-- Entry-point per {{#invoke:monitoraggio|categorie}}
function p.categorie(frame)
	local args = parseArgs(getArgs(frame))
	local livello = getLivello(args)	
	local categories = getCategories(livello, args)
	return args.debug and ( table.concat(categories, '<br />'):gsub('%[%[', '[[:') ) .. '<br />' or
		   table.concat(categories)
end

-- Entry-point per il template {{Monitoraggio}}
function p.main(frame)
	local args, errorCat = parseArgs(getArgs(frame, {parentOnly = true}))
	local livello = getLivello(args)
	local categories = mw.title.getCurrentTitle().namespace == 1 and
					   (table.concat(getCategories(livello, args)) .. errorCat) or ''
	return getHTML(livello, args) .. categories
end

return p