Modulo:Tabella beni comune

Da Wikipedia, l'enciclopedia libera.
Vai alla navigazione Vai alla ricerca
require('strict')

local getArgs = require('Module:Arguments').getArgs
local errorCategory = '[[Categoria:Errori di compilazione del template Tabella beni comune]]'
local p = {}
local beni  = mw.loadData( 'Modulo:Tabella beni comune/comuni');

local function errhandler(msg)
	local cat = mw.title.getCurrentTitle().namespace == 0 and errorCategory or ''
	return string.format('<span class="error">%s</span>%s', msg, cat)
end


function p._tabella(frame, args)

local function trova(testo, blocco, inizio)
    local pos = mw.ustring.find(testo, blocco, inizio)
    if (pos == nil) then
        return mw.ustring.len(testo) + mw.ustring.len(blocco);
    else
        return pos + mw.ustring.len(blocco);
	end
end

	local comune = args[1]
	local benicul = beni[comune]
	if not benicul then
		error('Nessun bene trovato per il comune ' .. comune)
	end
	local tabella1 = {}
	table.insert(tabella1,'{| class="wikitable" style="width:100%"\n!Nome !!Sezione monumenti !!Panorama !!Immagini !!Avvisi')
	local cNome = mw.wikibase.getLabel(comune)
	local cLink = mw.wikibase.getSitelink(comune)
	local cP18 = mw.wikibase.getAllStatements(comune, 'P18')
	local cPagina = mw.title.new(cLink)
	local cTesto = ''
	if not cPagina.exists then
		error('Pagina del comune non esistente')
	end
	cTesto = string.lower(cPagina:getContent())
	local dimTesto = mw.ustring.len(cTesto)
	-- trova immagine panorama nel template --
    local avvioPos = trova(cTesto, '{{divisione amministrativa', 0);
    local DAtemplate = 0
    if (dimTesto <= avvioPos) then
        DAtemplate = 0
	else
		local conta = 1
		local cercaPos = avvioPos
		local apriPos = trova(cTesto, '{{', cercaPos)
		local chiudiPos = trova(cTesto, '}}', cercaPos)
		while (conta > 0) do
			if (apriPos < chiudiPos) then
				conta = conta + 1
				cercaPos = apriPos
				apriPos = trova(cTesto, '{{', cercaPos)
			else
				conta = conta - 1
				cercaPos = chiudiPos
				chiudiPos = trova(cTesto, '}}', cercaPos)
			end
		end
		local testotemplate = mw.ustring.sub(cTesto, avvioPos, cercaPos-2)
		local trovaparam = mw.ustring.match(testotemplate, '|%s*panorama%s*=%s*([^|\n]+)')
		if (trovaparam) then
			trovaparam = string.gsub(trovaparam, "^%s*(.-)%s*$", "%1")
			if string.len(trovaparam)>0 then
				DAtemplate = trovaparam
			end
		end
    end
    local cPan = ''
	if (cP18 and #cP18>0) then cPan = ' + P18 su Wikidata' end
	if (DAtemplate ~= 0) then cPan = cPan .. ' + parametro in template ' end
	if string.len(cPan) == 0 then cPan = 'assente' end
	-- trova monumenti --
	local monVarianti = { 'altri monumenti', 'architettur', 'arte ', 'attrattive turistiche', 'castelli', 'castello', 'chies', 'cultura - luoghi d',
		'da vedere', 'da visitare', 'edifici', 'frazioni, chiese e palazzi', 'gli edifici', 'la chiesa ', 'monumenti', 'museo', 'musei',
		'parrocchi', 'patrimonio artistico', 'storia e luoghi d', 'storia ed architettura', 'strutture e luoghi d', 'testimonianze'}
	local cMonumenti = ''
	local monSezioni2 = mw.ustring.gmatch(cTesto, '\n==\s*([^=]+)\s*\==')
	local monSezioni3 = mw.ustring.gmatch(cTesto, '\n===\s*([^=]+)\s*\===')
	local monSezione = ''
	for w in monSezioni2 do
		for a,b in pairs(monVarianti) do
			monSezione = mw.ustring.find(w,b,0)
			if monSezione then cMonumenti = cMonumenti .. ' - ' .. w end
		end
	end
	for w in monSezioni3 do
		for a,b in pairs(monVarianti) do
			monSezione = mw.ustring.find(w,b,0)
			if monSezione then cMonumenti = cMonumenti .. ' - ' .. w end
		end
	end
	-- trova template di avviso --
	local nomiAvvisi = {'C', 'Correggere', 'Curiosità', 'CV', 'D', 'Dubbio qualità', 'E', 'F', 'Finzione', 'L', 'NN', 'O', 'Organizzare', 'P',
		'Recentismo', 'S', 'Senza fonte', 'Stile manualistico', 'T', 'Tmp', 'Trasferimento', 'V', 'W'}
	local cAvvisi = {}
	for k, v in pairs(nomiAvvisi) do
		if (mw.ustring.match(cTesto, '\{\{'..string.lower(v)..'[\n\|]')) then
			table.insert(cAvvisi, '{{template|'..v..'}} ')
		end
	end
	-- trova immagini --
	local cImmagini = 0
	local tImmagini = mw.ustring.gmatch(cTesto, 'file\:([^|\n]+)\.jpg')
	for w in tImmagini do
		cImmagini = cImmagini +1
	end
	
	table.insert(tabella1,'\n|-\n')
	table.insert(tabella1,'\n| \'\'\'[['..cLink..'|'..cNome..']]\'\'\'')
	table.insert(tabella1,'\n| '..cMonumenti)
	table.insert(tabella1,'\n| '..cPan)
	table.insert(tabella1,'\n| '..cImmagini)
	table.insert(tabella1,'\n| '..table.concat(cAvvisi))
	table.insert(tabella1,'\n|}\n')
	
	local i = 1
	local imappa = 1
	local wmappac = {}
	table.insert(wmappac,'\n{{Mappa OSM|dati = [ { "type": "ExternalData", "service": "geoshape", "ids": "' .. comune ..'", "properties": { "stroke": "#0060c0", "fill-opacity": 0 } },')
	local wid, entita, wlabel, wlink, wdesc, wimma, wtipo, wluogo, wluoghi, wcoord, wlat, wlon, wmappa, wcommons
	local ww, t, wwid, wwlabel, wwlink
	local tabella2 = {}
	local mostramappa = tonumber(args['mappa']) or 0
	table.insert(tabella2, '{| class="wikitable sortable" width=100%\n|-\n!style="width:45px|Num !!style="width:100px"|Immagine !!style="width:25%"|Elemento !!Tipologia !!style="width:50px"|Mappa !!style="width:50px"|Commons')
	while (benicul[i] and i<251) do
		wid = benicul[i]
		entita = mw.wikibase.getEntity(wid)
		if not entita then error('Entità ' .. wid .. ' non presente') end
		wlabel = entita:getLabel('it')
		if not wlabel then wlabel = '' end
		wlabel = string.gsub(wlabel,'"','')
		wlink = entita:getSitelink('itwiki')
		if wlink then
			if wlabel == '' then
				wlink = '[[' .. wlink .. ']]'
			else
				wlink = '[[' .. wlink .. '|' .. wlabel .. ']]'
			end
		else
			wlink = wlabel
		end
		wdesc = entita:getDescription('it')
		if wdesc then
			wdesc = '<br/><span style="font-size:90%">\'\'' .. wdesc .. '\'\'</span>'
		else
			wdesc = ''
		end
		wtipo = entita:formatPropertyValues('P31').value
		wluogo = ''
		wimma = ''
		if entita.claims then
			t = entita.claims['P18']
			if t then
				wimma = '[[File:' .. t[1].mainsnak.datavalue.value .. '|80x80px]]'
			else
				wimma = '[[File:Monumento - Immagine mancante.svg|80px|alt=Immagine mancante|link=|Immagine mancante]]'
			end

			ww = 1
			t = entita.claims['P131']
			while t[ww] do
				wwid = t[ww].mainsnak.datavalue.value.id
				wwlabel = mw.wikibase.getLabel(wwid)
				wwlink = mw.wikibase.getSitelink(wwid,'itwiki')
				if wwlink then
					if wwlabel then
						wluogo = wluogo .. '• [[' .. wwlink .. '|' .. wwlabel .. ']] '
					else
						wluogo = wluogo .. '• [[' .. wwlink .. ']] '
					end
				else
					if wwlabel then
						wluogo = wluogo .. '• ' .. wwlink
					else
						wluogo = wluogo .. '• ...'
					end
				end
				ww = ww+1
			end
		end

		wcommons = entita:formatPropertyValues('P373').value
		if wcommons ~= '' then
			wcommons = '[[File:Commons-logo.svg|24px|alt=Categoria su Commons|link=c:Category:' .. wcommons .. '|Categoria su Commons]]'
		end
		wcoord = entita:formatPropertyValues('P625').value
		if wcoord ~= "" then
			wlat = entita.claims.P625[1].mainsnak.datavalue.value.latitude
			wlon = entita.claims.P625[1].mainsnak.datavalue.value.longitude
			wmappa = '{{#tag:maplink|[ { "type": "ExternalData", "service": "geoshape", "ids": "' .. comune ..'", "properties": { "stroke": "#0060c0", "fill-opacity": 0 } },'..
				'{ "type": "Feature", "properties": {"title": "'.. wlabel ..'", "marker-color": "0060c0"},'..
				'"geometry": { "type": "Point", "coordinates": [ '.. wlon .. ',' .. wlat ..' ] } } ] |text=Mappa}}'
			table.insert(wmappac,'{ "type": "Feature", "properties": {"title": "['.. i .. '] ' .. wlabel ..'", "marker-color": "0060c0"},'..
				'"geometry": { "type": "Point", "coordinates": [ '.. wlon .. ',' .. wlat ..' ] } },')
			imappa = imappa + 1
		else
			wmappa = ''
		end
		table.insert(tabella2, '\n|-')
		table.insert(tabella2, '\n|align=center| ' .. i)
		table.insert(tabella2, '\n|align=center| ' .. wimma)
		table.insert(tabella2, '\n|[[File:Wikidata-logo.svg|24px|alt=Vedi su Wikidata|link=d:'.. wid .. '|Vedi su Wikidata]]&nbsp;')
		table.insert(tabella2, wlink)
		table.insert(tabella2, wdesc)
		table.insert(tabella2, '\n|style="font-size:90%"|' .. wtipo)
		--table.insert(tabella2, '\n| ' .. wluogo)
		table.insert(tabella2, '\n|align=center| ' .. wmappa)
		table.insert(tabella2, '\n|align=center| ' .. wcommons)
		i = i+1
	end
	table.insert(tabella2,'\n|}\n')
	table.insert(wmappac,' ] |cornice=0|allinea=center|larghezza=600}}\n')
	if (mostramappa == 1 and imappa>1) then
		table.insert(tabella1, table.concat(wmappac))
	end
	table.insert(tabella1, table.concat(tabella2))

	return frame:preprocess(table.concat(tabella1))
end

function p.tabella(frame)
	return select(2, xpcall(function()
		return p._tabella(frame,getArgs(frame, { parentOnly = true }))
	end, errhandler))end
return p