Modulo:Bozza

Da Tematiche di genere.
Vai alla navigazione Vai alla ricerca

La documentazione per questo modulo può essere creata in Modulo:Bozza/man

require('Modulo:No globals')

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

local function checkForCategories(str)
	local catpattern = "[Cc][Aa][Tt][Ee][Gg][Oo][Rr][YyIi][Aa]?"

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

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

local function createRow(text1, text2)
	return mw.html.create('div')
		:addClass('bozza-history-row')
		:tag('div')
			:wikitext(text1)
			:done()
		:tag('div')
			:wikitext(text2)
			:done()
		:done()
end

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

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

	table.sort(a, comp)

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

	return iter
end

local function removeFalsePositives(str)
	return str
		:gsub('<!%-%-.-%-%->', '')
		:gsub('<nowiki>.-</nowiki>', '')
		:gsub('%f[%{]%{%f[^%{]', '\r')
		:gsub('%{%{ *[Cc]ategorie *bozza *%|.-%}%}', '')
end

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 pattern = tmp
		stored_submissions, tmp = getSubmissions(stored_submissions, tmp)
		new_text = replace(new_text, pattern, 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{}%}', '{{')
			submission = submission:gsub('\r', '{')

			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 ts then
				stored_submissions[key][ts] = stored_submissions[key][ts] or {}
				table.insert(stored_submissions[key][ts], submission)
			end
		end
	end

	return stored_submissions, new_text
end

function p.main(frame)
	local current_title = mw.title.getCurrentTitle()
	local ns = current_title.namespace
	local text = current_title:getContent()

	if ns == 0 then
		return '[[Categoria:Rimuovere template Bozza]]'
	elseif not text then
		return
	else
		text = removeFalsePositives(text)
	end

	local args = getArgs(frame, { parentOnly = true })
	local lang = mw.language.getContentLanguage()
	local submissions = getSubmissions({ current = {}, old = {} }, text)
	local edited_by_reviewer = false

	if ns == 118 then
		if args.sposta then
			args[1] = 'S'
		elseif next(submissions.current) then
			args[1] = 'R'
		elseif args.proroga then
			args[1] = 'A'
		elseif next(submissions.old) then
			args[1] = 'N'
		else
			args[1] = nil
		end
	else
		args[1] = lang:ucfirst(args[1] or args.stato or '')
	end

	if next(submissions.old) then
		local i = 0
		local revision_user = frame:preprocess('{{REVISIONUSER}}')
		local header = createRow('Data richiesta', 'Esito revisione')
			:addClass('nomobile bozza-history-header')
		local history_table = mw.html.create('div')
			:addClass('bozza-history')
		local collapsed_submissions = mw.html.create('div')
			:addClass('nomobile mw-collapsible mw-collapsed')
			:attr('data-expandtext', 'mostra richieste precedenti')
			:attr('data-collapsetext', 'nascondi richieste precedenti')
		local hide_last_submission = args[1] == 'S' or args[1] == 'R'

		if hide_last_submission then
			collapsed_submissions:node(header)
		else
			history_table:node(header)
		end

		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
					local reviewer
					if not edited_by_reviewer then
						reviewer = submission:match('%| *utente *= *(.-) *[%|%}]')
						edited_by_reviewer = revision_user == reviewer
					end
					local root =  history_table
					i = i + 1
					submission = submission:gsub('%}%}$', '|mostra esito=x}}')
					if i == (hide_last_submission and 1 or 2) then
						history_table:node(collapsed_submissions)
					end
					if i > (hide_last_submission and 0 or 1) then
						root = collapsed_submissions
					end
					if i == 1 and not hide_last_submission then
						args['ts ultima revisione'] = ts
						submission = submission:gsub('%}%}$', '|mostra pulsante=x}}')
					end
					root:node(createRow(jFY, frame:preprocess(submission)))
				end
			end
		end

		args['numero richieste respinte'] = i
		args['cronologia revisioni'] = tostring(history_table)
	end

	if edited_by_reviewer or args[1] == 'R' or args[1] == 'S' then
		args['mostra pulsante'] = 'no'
	else
		args['mostra pulsante'] = 'sì'
	end

	return frame:extensionTag{
			name = 'templatestyles',
			args = { src = 'Modulo:Bozza/styles.css' }
		} .. frame:expandTemplate{
			title = 'Template:Bozza/avviso',
			args = args
		} .. (ns == 118 and checkForCategories(text) or '')
end

return p