Modulo che implementa le funzionalità del template:Bozza.

Legge i vari template:Richiesta revisione bozza presenti nella pagina e include il template:Bozza/avviso coi parametri adeguati.


require('Modulo:No globals')

local p = {}
local getArgs = require('Module:Arguments').getArgs

local function replace(str, pattern, replace)
	local i, j = str:find(pattern, 1, true)
	str = str:sub(0, i - 1) .. replace .. str:sub(j + 1)
	return str
end

local function getSubmissions(stored_submissions, text)
	local new_text = text

	for tmp in text:gmatch('%{(%b{})%}') do
		local old_tmp = tmp
		stored_submissions, tmp = getSubmissions(stored_submissions, tmp)
		new_text = replace(new_text, old_tmp, tmp)

		if tmp:find('^%{ *[Rr]ichiesta *revisione *bozza *[%|%}]') then
			local submission = string.format('{%s}', tmp)
			new_text = replace(new_text, submission, '')
			tmp = tmp:gsub('%{%b{}%}', '{{')

			local ts = tmp:match('%| *ts *= *(%d%d%d%d%d%d%d%d%d%d%d%d%d%d) *[%|%}]')
			local key = tmp:find('%| *esito *= *[^%s%|%}]') and 'old' or 'current'

			if type(stored_submissions[key][ts]) == 'table' then
				table.insert(stored_submissions[key][ts], submission)
			else
				stored_submissions[key][ts] = { submission }
			end
		end
	end

	return stored_submissions, new_text
end

local function pairsByTimestamp(t)
	local a = {}
	local i = 0
	local function comp(ts1, ts2)
		return ts1 > ts2
	end

	for ts in pairs(t) do
		table.insert(a, ts)
	end

	table.sort(a, comp)

	return function ()
			i = i + 1
			if not a[i] then
				return
			else
				return a[i], t[a[i]]
			end
		end
end

function p.main(frame)
	local current_title = mw.title.getCurrentTitle()

	if current_title.namespace == 0 then
		return '[[Categoria:Rimuovere template Bozza]]'
	end

	local text = current_title:getContent()

	if not text then return end

	text
		:gsub('<!%-%-.-%-%->', '')
		:gsub('<nowiki>.-</nowiki>', '')
		:gsub('%f[%{]%{%f[^%{]', '\r')
		:gsub('%{%{ *[Cc]ategorie *bozza *%|.-%}%}', '')

	local args = getArgs(frame, { parentOnly = true })
	local lang = mw.language.getContentLanguage()
	local state = lang:ucfirst(args[1] or '')
	local submissions = getSubmissions({ current = {}, old = {} }, text)

	if state ~= 'S' then
		if next(submissions.current) then
			state = 'R'
		elseif next(submissions.old) and state ~= 'A' then
			state = 'N'
		end
	end

	local history_table

	if next(submissions.old) then
		local styles = frame:extensionTag{
			name = 'templatestyles',
			args = { src = 'Modulo:Bozza/styles.css' }
		}

		history_table = mw.html.create('div')
			:addClass('bozza-history')
			:tag('div')
				:wikitext('Data richiesta')
				:done()
			:tag('div')
				:wikitext('Esito revisione')
				:done()

		for ts, a in pairsByTimestamp(submissions.old) do
			local success, jFY = pcall(lang.formatDate, lang, 'j F Y', ts)

			if success then
				for _, submission in ipairs(a) do
					submission = submission:gsub('%}%}$', '|mostra esito=x}}')
					history_table
						:tag('div')
							:wikitext(jFY)
							:done()
						:tag('div')
							:wikitext(frame:preprocess(submission))
							:done()
				end
			end
		end

		history_table = styles .. tostring(history_table)
	end

	local ret = frame:expandTemplate{
			title = 'Template:Bozza/avviso',
			args = { state, ts = args.ts, ['cronologia revisioni'] = history_table }
		}

	if current_title.namespace == 118 then
		local catpattern = "[Cc][Aa][Tt][Ee][Gg][Oo][Rr][YyIi][Aa]?"

		for colonprefix in text:gmatch('%[%[ *(' .. catpattern .. ') *: *[^%s][^\n]-%]%]') do
			local ns = mw.site.namespaces[colonprefix]

			if ns and ns.canonicalName == 'Category' then
				ret = ret .. '[[Categoria:Bozze con categorie da disabilitare]]'
				break
			end
		end
	end

	return ret
end

return p