| 
 --[[ ---------------------------------- 
  Modulo Lua che implementa il template {{Webarchive}} e permette di 
  Lua module implementing the {{webarchive}} template. 
  decifrare la data negli URL di alcuni archivi anche ad altri moduli. 
	]] 
  
require('strict') 
  A merger of the functionality of three templates: {{wayback}}, {{webcite}} and {{cite archives}} 
  
local getArgs = require('Modulo:Arguments').getArgs 
	]] 
local cfg = mw.loadData('Modulo:Webarchive/Configurazione') 
  
require('Module:No globals') 
local getArgs = require('Module:Arguments').getArgs 
  
local p = {} 
local track = {}   -- Associative array toassociativo per accumulare le holdcategorie trackingdi categoriestracciamento 
local maxurls = 10 -- Maxmassimo numbernumero ofdi URLsURL allowed.permessi 
local encoded_date 
  
local servizi = { 
	{ signature = "archive.org", service = "wayback",  tailbracket = " in %sInternet Archive%s",  tracking = "Categoria:Template Webarchive - collegamenti all'Internet Archive" }, 
	{ signature = "webcitation.org", service = "webcite",  tailbracket = " in %sWebCite%s",  tracking = "Categoria:Template Webarchive - collegamenti a WebCite" }, 
	{ signature = "archive.is", service = "archiveis",  tailbracket = " in %sArchive.is%s",  tracking = "Categoria:Template Webarchive - collegamenti a archive.is" }, 
	{ signature = "archive.fo", service = "archiveis",  tailbracket = " in %sArchive.is%s",  tracking = "Categoria:Template Webarchive - collegamenti a archive.is" }, 
	{ signature = "archive.today", service = "archiveis",  tailbracket = " in %sArchive.is%s",  tracking = "Categoria:Template Webarchive - collegamenti a archive.is" }, 
	{ signature = "archive.il", service = "archiveis",  tailbracket = " in %sArchive.is%s",  tracking = "Categoria:Template Webarchive - collegamenti a archive.is" }, 
	{ signature = "archive.ec", service = "archiveis",  tailbracket = " in %sArchive.is%s",  tracking = "Categoria:Template Webarchive - collegamenti a archive.is" }, 
	{ signature = "archive[-]it.org", service = "archiveit",  tailbracket = " in %sArchive-It%s" }, 
	{ signature = "arquivo.pt", tail = " nel Portuguese Web Archive" }, 
	{ signature = "loc.gov", tailbracket = " nella %sLibrary of Congress%s" }, 
	{ signature = "webharvest.gov", tailbracket = " nel %sNational Archives and Records Administration%s" }, 
	{ signature = "bibalex.org", tail = " nella [[Bibliotheca Alexandrina#Struttura e collezioni|Bibliotheca Alexandrina]]" }, 
	{ signature = "collectionscanada", tail = " nel Canadian Government Web Archive" }, 
	{ signature = "haw.nsk", tail = " nel Croatian Web Archive (HAW)" }, 
	{ signature = "veebiarhiiv.digar.ee", tail = " nell'Estonian Web Archive" }, 
	{ signature = "vefsafn.is", tailbracket = " nella %sNational and University Library of Iceland%s]]" }, 
	{ signature = "proni.gov", tailbracket = " nel %sPublic Record Office of Northern Ireland%s" }, 
	{ signature = "uni[-]lj.si", tail = " nello Slovenian Web Archive" }, 
	{ signature = "stanford.edu", tail = " nello [[Stanford University Libraries|Stanford Web Archive]]" }, 
	{ signature = "nationalarchives.gov.uk", tailbracket = " in %sUK Government Web Archive%s" }, 
	{ signature = "parliament.uk", tailbracket = " in %sUK Parliament's Web Archive%s" }, 
	{ signature = "webarchive.org.uk", tailbracket = " in %sUK Web Archive%s" }, 
	{ signature = "nlb.gov.sg", tail = " in Web Archive Singapore" }, 
	{ signature = "pandora.nla.gov.au", tailbracket = " in %sPandora Archive%s" }, 
	{ signature = "perma.cc", tailbracket = " in %sPerma.cc%s" }, 
	{ signature = "perma-archives.cc", tailbracket = " in %sPerma.cc%s" }, 
	{ signature = "screenshots.com", tail = " in Screenshots" }, 
	{ signature = "wikiwix.com", tail = " in Wikiwix" }, 
	{ signature = "freezepage.com", tail = " in Freezepage" }, 
	{ signature = "webcache.googleusercontent.com", tail = " in Google Cache" } 
} 
  
local month_localized = { 'gennaio', 'febbraio', 'marzo', 'aprile', 'maggio', 'giugno', 
						   'luglio', 'agosto', 'settembre', 'ottobre', 'novembre', 'dicembre'} 
  
--[[--------------------------< inlineError >----------------------- 
	Errore critico. Formatta l'output completamente in rosso. Aggiunge una categoria di tracciamento. 
  
	Critical error. Render output completely in red. Add to tracking category. 
  
 ]] 
  
local function inlineError(arg, msg) 
	track['Categoria:Errori di compilazione del template Webarchive'] = 1 
  
	return '<span style="font-size:100%" class="error citation-comment">Errore di compilazione del template Webarchive: controllare il valore di <code style="color:inherit; border:inherit; padding:inherit;">|' .. arg .. '=</code> (' .. msg .. ').</span>' 
	track["Categoria:Errori di compilazione del template Webarchive"] = 1 
	return '<span style="font-size:100%" class="error citation-comment">Errore nella compilazione del template webarchive: controllare <code style="color:inherit; border:inherit; padding:inherit;">|' .. arg .. '=</code> value. ' .. msg .. '</span>' 
  
end 
  
--[[--------------------------< inlineRed >----------------------- 
	Formatta un frammento di testo in rosso, quale ad esempio un avvertimento da integrare nell'output finale. 
  
	Aggiunge una categoria di tracciamento. 
	Render a text fragment in red, such as a warning as part of the final output. 
	Add tracking category. 
  
 ]] 
  
local function inlineRed(msg, trackmsg) 
	if trackmsg == 'warning' then 
  
		track['Categoria:Errori di compilazione del template Webarchive - Avvisi'] = 1 
	if trackmsg == "warning" then 
	elseif trackmsg == 'error' then 
		track["Categoria:Errori di compilazione del template Webarchive - Avvisi"] = 1 
		track['Categoria:Errori di compilazione del template Webarchive'] = 1 
	elseif trackmsg == "error" then 
		track["Categoria:Errori di compilazione del template Webarchive"] = 1 
	end 
  
	return '<span style="font-size:100%" class="error citation-comment">' .. msg .. '</span>' 
  
end 
  
  
--[[--------------------------< base62 >----------------------- 
	Converte base-62 in base-10 
  
	Crediti: https://de.wikipedia.org/wiki/Modul:Expr 
	Convert base-62 to base-10 
	Credit: https://de.wikipedia.org/wiki/Modul:Expr 
  
	]] 
  
local function base62( value ) 
  
	local r = 1 
	if value:match('^%w+$') then 
  
	if value:match( "^%w+$" ) then 
		local n = #value 
		local k = 1 
		for i = n, 1, -1 do 
			c = value:byte( i, i ) 
			if c >= 48  and  c <= 57 then 
				c = c - 48 
			elseif c >= 65  and  c <= 90 then 
				c = c - 55 
			elseif c >= 97  and  c <= 122 then 
				c = c - 61 
			else    -- How comes? 
				r = 1 
				break    -- for i 
			end 
			r = r + c * k 
			k = k * 62 
		end -- for i 
	end 
	return r 
  
--[[--------------------------< tableLength >----------------------- 
	Restituisce il numero di elementi in una tabella 
  
	Given a 1-D table, return number of elements 
  
	]] 
  
local function tableLength(t) 
	local count = 0 
end 
  
--[[--------------------------< formatDate >----------------------- 
  
	Verifica il formato di una data (dmy o iso) e se lo riconosce 
--[[--------------------------< dateFormat >----------------------- 
	la riformatta in dmy, altrimenti restituisce il valore così com'è 
  
	Given a date string, return its format: dmy, mdy, iso, ymd 
	If unable to determine return nil 
  
	]] 
local function formatDate(date) 
  
	local functiony, dateFormat(date)m, d 
	local try_year 
  
	local dtsplit = {}mw.text.split(date, '-') 
	dt.if tableLength(split) == 3 {}then 
		try_year = tonumber(split[1]) 
  
		if try_year and try_year > 1900 and try_year < 2200 then -- iso 
	dt.split = mw.text.split(date, "-") 
			y, m, d = split[1], cfg.month_localized[tonumber(split[2])], split[3] 
	if tableLength(dt.split) == 3 then 
		if tonumber(dt.split[1]) > 1900 and tonumber(dt.split[1]) < 2200 and tonumber(dt.split[2]) and tonumber(dt.split[3]) then 
			return "iso" 
		else 
			return nil 
		end 
	else 
	end 
		split = mw.text.split(date, ' ') 
  
		if tableLength(split) == 3 then 
	dt.split = mw.text.split(date, " ") 
			try_year = tonumber(split[3]) 
	if tableLength(dt.split) == 3 then 
			if try_year and try_year > 1900 and try_year < 2200 and 
		if tonumber(dt.split[3]) then 
			if tonumber		(dt.split[31]) >== 1900'1º' andor tonumber(dt.split[31])) <then 2200-- thendmy 
				ifd, m, y = tonumber(dt.split[1]), thensplit[2], split[3] 
					return "dmy" 
				else 
					return "mdy" 
				end 
			else 
				if tonumber(dt.split[1]) then 
					if tonumber(dt.split[1]) > 1900 and tonumber(dt.split[1]) < 2200 then 
						return "ymd" 
					end 
				end 
			end 
		end 
	end 
	d = tonumber(d) or d 
	return nil 
	if d == 1 then d = '1º' end 
  
	return m and mw.ustring.format('%s %s %s', d, m, y) or date 
end 
  
--[[--------------------------< makeDateformatUrlDate >----------------------- 
	Controlla la data prelevata automaticamente dall'url di un archivio. 
  
	Se è valida la formatta in dmy, altrimenti restituisce nil. 
	Given a zero-padded 4-digit year, 2-digit month and 2-digit day, return a full date in df format 
	df = mdy, dmy, iso, ymd 
  
 ]] 
local function formatUrlDate(y, m, d) 
  
	local current_year = tonumber(os.date('%Y')) 
local function makeDate(year, month, day, df) 
	y, m, d = tonumber(y), tonumber(m), tonumber(d) 
  
	if not yeary or yeary == ""'' or not monthm or monthm == ""'' or not dayd or dayd == ""'' thenor d > 31 or 
			m < 1 or m > 12 or y < 1900 or y > current_year then 
		return nil 
	end 
	m = cfg.month_localized[m] 
  
	if d == 1 then d = '1º' end 
	local zmonth = month -- month with leading 0 
	return mw.ustring.format('%s %s %s', d, m, y) 
	month = month:match("0*(%d+)") -- month without leading 0 
	if tonumber(month) < 1 or tonumber(month) > 12 then return year end 
	local nmonth = month_localized[tonumber(month)] 
	if not nmonth then return year end 
	local zday = day 
	day = zday:match("0*(%d+)") 
	if tonumber(day) < 1 or tonumber(day) > 31 then 
		return mw.ustring.format("%s %s", nmonth, year) 
	end 
	if day == "1" then day = "1º" end 
	return mw.ustring.format("%s %s %s", day, nmonth, year) 
end 
  
  
--[[--------------------------< decodeWebciteDate >----------------------- 
	Ricava la data da un URI-path a Webcite (es. /67xHmVFWP) 
  
	Given a URI-path to Webcite (eg. /67xHmVFWP) return the encoded date in df format 
  
	]] 
local function decodeWebciteDate(path, df) 
	local path_elements = mw.text.split(path, '/') 
  
	-- formati URL validi che non sono base62: 
		local dt = {} 
		dt.split = {} 
  
	-- http://www.webcitation.org/query?id=1138911916587475 
		dt.split = mw.text.split(path, "/") 
	-- http://www.webcitation.org/query?url=http..&date=2012-06-01+21:40:03 
  
	-- http://www.webcitation.org/1138911916587475 
		-- valid URL formats that are not base62 
	-- http://www.webcitation.org/cache/73e53dd1f16cf8c5da298418d2a6e452870cf50e 
  
			-- http://www.webcitation.org/querygetfile.php?idfileid=11389119165874751c46e791d68e89e12d0c2532cc3cf629b8bc8c8e 
		-- http://www.webcitation.org/query?url=http..&date=2012-06-01+21:40:03 
		-- http://www.webcitation.org/1138911916587475 
		-- http://www.webcitation.org/cache/73e53dd1f16cf8c5da298418d2a6e452870cf50e 
		-- http://www.webcitation.org/getfile.php?fileid=1c46e791d68e89e12d0c2532cc3cf629b8bc8c8e 
  
		if mw.ustring.find( dt.split[2], "query") or 
			 mw.ustring.find( dt.split[2], "cache") or 
			 mw.ustring.find( dt.split[2], "getfile") or 
			 tonumber(dt.split[2]) then 
			return "query" 
		end 
  
		dt.full = os.date("%Y %m %d", string.sub(string.format("%d", base62(dt.split[2])),1,10) ) 
		dt.split = mw.text.split(dt.full, " ") 
		dt.year = dt.split[1] 
		dt.month = dt.split[2] 
		dt.day = dt.split[3] 
  
		if not tonumber(dt.year) or not tonumber(dt.month) or not tonumber(dt.day) then 
			return inlineRed("[Date error] (1)", "error") 
		end 
  
		if tonumber(dt.month) > 12 or tonumber(dt.day) > 31 or tonumber(dt.month) < 1 then 
			return inlineRed("[Date error] (2)", "error") 
		end 
		if tonumber(dt.year) > tonumber(os.date("%Y")) or tonumber(dt.year) < 1900 then 
			return inlineRed("[Date error] (3)", "error") 
		end 
  
		local fulldate = makeDate(dt.year, dt.month, dt.day, df) 
		if not fulldate then 
			return inlineRed("[Date error] (4)", "error") 
		else 
			return fulldate 
		end 
  
	if not path_elements[2] or path_elements[2] == '' then 
		return 
	elseif mw.ustring.find(path_elements[2], 'query') or 
			mw.ustring.find(path_elements[2], 'cache') or 
			mw.ustring.find(path_elements[2], 'getfile') or 
			tonumber(path_elements[2]) then 
		encoded_date = false 
		return 
	end 
	local snapdate = os.date('%Y %m %d', string.sub(string.format('%d', base62(path_elements[2])),1,10)) 
	local dt = mw.text.split(snapdate, ' ') 
	local fulldate = formatUrlDate(dt[1], dt[2], dt[3]) 
	return fulldate 
end 
  
--[[--------------------------< snapDateToString >----------------------- 
	Ricava la data da un URI-path a Wayback (es. /web/20160901010101/http://example.com ). 
  
	Gestisce anche le non cifre come "re_", "-" e "*". 
Given a URI-path to Wayback (eg. /web/20160901010101/http://example.com ) 
	return the formatted date eg. "September 1, 2016" in df format 
	Handle non-digits in snapshot ID such as "re_" and "-" and "*" 
  
 ]] 
local function decodeWaybackDate(path) 
	local snapdate = string.gsub(path, '^/all/', '') -- rimuove la sequenza iniziale "/all/" 
	snapdate = string.gsub(snapdate, '^/w?e?b?/?', '') -- rimuove la sequenza iniziale "/web/" o "/" 
	local path_elements = mw.text.split(snapdate, '/') 
	snapdate = path_elements[1] 
	if snapdate == '*' then return end 
	snapdate = string.gsub(snapdate, '[a-z][a-z]_[0-9]?$', '') 
	snapdate = string.gsub(snapdate, '[-]', '') 
	snapdate = string.gsub(snapdate, '[*]$', '') 
	local fulldate 
	if tonumber(snapdate) and string.len(snapdate) >= 8 then 
		local year = string.sub(snapdate, 1, 4) 
		local month = string.sub(snapdate, 5, 6) 
		local day = string.sub(snapdate, 7, 8) 
		fulldate = formatUrlDate(year, month, day) 
	end 
	return fulldate 
end 
  
--[[--------------------------< decodeArchiveisDate >----------------------- 
local function decodeWaybackDate(path, df) 
	Ricava la data dall'URI-path di un link esteso ad Archive.is (es. /2016.08.28-144552/http://example.com). 
  
	Gestisce "." e "-" nella data, rendendo 2016.08.28-144552 uguale a 20160828144552. 
		local snapdate, snapdatelong, currdate, fulldate 
  ]] 
  
local function decodeArchiveisDate(path) 
  
    local path_elements = mw.text.split(path, '/') 
		local snapdate = string.gsub(path, "^/w?e?b?/?", "")                      -- Remove leading "/web/" or "/" 
		    local safesnapdate = snapdatepath_elements[2] 
		local    if not path_elements[2] or path_elements[2] == '' then mw.text.split(safe,return "/")end 
		    snapdate = path_elementsstring.gsub(snapdate, '[1%.%-]', '') 
		    if not tonumber(snapdate) then encoded_date == "*"false thenreturn end -- eg.formato link /web/*/http..breve 
	local fulldate 
			return "index" 
	if string.len(snapdate) >= 8 then 
		end 
		safelocal year = string.sub(snapdate, 1, 4) 
		local month = string.sub(snapdate, 5, 6) 
		snapdate = string.gsub(safe, "[a-z][a-z]_[0-9]?$", "")              -- Remove any trailing "re_" from date 
		safelocal day = string.sub(snapdate, 7, 8) 
		fulldate = formatUrlDate(year, month, day) 
		snapdate = string.gsub(safe, "[-]", "")                             -- Remove dashes from date eg. 2015-01-01 
	end 
		safe = snapdate 
	return fulldate 
		snapdate = string.gsub(safe, "[*]$", "")                            -- Remove trailing "*" 
 end 
  
		if not tonumber(snapdate) then 
			return inlineRed("[Date error] (2)", "error") 
		end 
		local dlen = string.len(snapdate) 
		if dlen < 4 then 
			return inlineRed("[Date error] (3)", "error") 
		end 
		if dlen < 14 then 
			snapdatelong = snapdate .. string.rep("0", 14 - dlen) 
		else 
			snapdatelong = snapdate 
		end 
		local year = string.sub(snapdatelong, 1, 4) 
		local month = string.sub(snapdatelong, 5, 6) 
		local day = string.sub(snapdatelong, 7, 8) 
		if not tonumber(year) or not tonumber(month) or not tonumber(day) then 
			return inlineRed("[Date error] (4)", "error") 
		end 
		if tonumber(month) > 12 or tonumber(day) > 31 or tonumber(month) < 1 then 
			return inlineRed("[Date error] (5)", "error") 
		end 
		currdate = os.date("%Y") 
		if tonumber(year) > tonumber(currdate) or tonumber(year) < 1900 then 
			return inlineRed("[Date error] (6)", "error") 
		end 
  
		local fulldate = makeDate(year, month, day, df) 
		if not fulldate then 
			return inlineRed("[Date error] (7)", "error") 
		else 
			return fulldate 
		end 
  
end 
  
--[[--------------------------< serviceName >----------------------- 
	Imposta la stringa di coda e l'ID del servizio in base al dominio 
  
	Givenestratto a ___domain extracted byda mw.uri.new() (eges. web.archive.org) set tail string and service ID 
  
	]] 
local function serviceName(url_data, nolink, notail) 
  
	local tracking = 'Categoria:Template Webarchive - collegamenti ad altri archivi' 
local function serviceName(url_main, nolink) 
	local bracketopen, bracketclose = nolink and '' or '[[', nolink and '' or ']]' 
  
	encoded_date = nil -- reset 
	local tracking = "Categoria:Template Webarchive - altri archivi" 
	for _,servizio in ipairs(cfg.servizi) do 
	local bracketopen = "[[" 
		if string.gsub(url_data.host, 'www%.', '') == servizio.signature then 
	local bracketclose = "]]" 
			url_data.service = servizio.service or 'altri' 
	if nolink then 
			if not notail and servizio.tailbracket then 
		bracketopen = "" 
				url_data.tail = mw.ustring.format(servizio.tailbracket, bracketopen, bracketclose) 
		bracketclose = "" 
			elseif not notail then 
	end 
				url_data.tail = servizio.tail 
  
	url_main.service = "altri" 
	 
	for _,servizio in ipairs(servizi) do 
		if mw.ustring.find(url_main.host, servizio.signature) then 
			url_main.service = servizio['service'] or url_main.service 
			if servizio['tailbracket'] then 
				url_main.tail = mw.ustring.format(servizio.tailbracket, bracketopen, bracketclose) 
			else 
				url_main.tail = servizio['tail'] 
			end 
			tracking = servizio['.tracking'] or tracking 
			encoded_date = servizio.service and true 
			break 
		end 
	end 
	if url_mainurl_data.tailservice == nil then 
		tracking = 'Categoria:Template Webarchive - collegamenti ad archivi sconosciuti' 
		url_main.tail = " a " .. url_main.host .. " " .. inlineRed("Errore: URL di servizio di archiviazione sconosciuto") 
		url_data.tail = ' su ' .. url_data.host .. ' ' .. inlineRed('URL di servizio di archiviazione sconosciuto') 
	end 
	track[tracking] = 1 
end 
  
--[[--------------------------< parseExtraArgs >----------------------- 
  
	Parse numbered arguments from 2 to maxurls, such as url2..url10, date2..date10, title2..title10 
	e memorized them in a table 
	For example: {{webarchive |url=.. |url4=.. |url7=..}} 
		Three url arguments not in numeric sequence (1..4..7). 
		Function only processes arguments numbered 2 or greater (in this case 4 and 7) 
		It creates numeric sequenced table entries like: 
	   		urlx[1].url = <argument value for url4> 
	 		urlx[2].url = <argument value for url7> 
	Returns the parsed table  
 ]] 
  
local function parseExtraArgs(args, maxurls) 
  
	local i, indx, argurl, argurl2, argdate, argtitle 
	local ulx = {} 
	indx = 1 
	for i = 2, maxurls do 
		argurl = "url" .. i 
		if args[argurl] then 
			ulx[indx] = {} 
			ulx[indx]["url"] = args[argurl] 
			ulx[indx]["date"] = args["date" .. i] or args["data" .. i]  
			if not ulx[indx]["date"] then 
				ulx[indx]["date"] = inlineRed("[Data mancante]", "warning") 
			end 
			ulx[indx]["title"] = args["title" .. i] or args["titolo" .. i] 
			indx = indx + 1 
		end 
	end 
	return ulx 
end 
  
  
--[[--------------------------< comma >----------------------- 
  
	Given a date string, return "," if it's MDY 
  
	]] 
  
local function comma(date) 
	local n = mw.text.split(date, " ") 
	local o = mw.text.split(n[1], "-") -- for ISO 
	if o[1] == "index" then return "" end 
	if not tonumber(o[1]) then 
		return "," 
	else 
		return "" 
	end 
end 
  
--[[--------------------------< createTracking >----------------------- 
	Restituisce le categorie di tracciamento inserite in track[] 
  
	Return data in track[] ie. tracking categories 
  
	]] 
  
local function createTracking() 
	-- procede solo nel namespace 0 
  
	-- Return tracking category only in namespace 0 
	local current_namespace = mw.title.getCurrentTitle().namespace 
	if current_namespace ~= 0 then return '' end 
	local sand = '' 
  
	local sand = "" 
	if tableLength(track) > 0 then 
		for key,_ in pairs(track) do 
			sand = sand .. "'[["' .. key .. "']]"' 
		end 
	end 
	return sand 
  
end 
  
--[[--------------------------< createRendering >----------------------- 
	Restituisce la resa dei dati in url_data[][] 
  
		 Return a rendering of the data in ulx[][] 
  
	]] 
local function createRendering(url_data) 
  
	local sand 
local function createRendering(url_main, ulx) 
	local day = url_data[1].date and mw.ustring.match(url_data[1].date, '^%d+') 
  
	local article = (day == '8' or day == '11') and 'l\'' or 'il ' 
	local sand, displayheader, displayfield 
	if not url_data[1].title and not url_data[1].date then 
  
		sand = mw.ustring.format('[%s Archiviato]%s.', url_data[1].url, url_data[1].tail) 
	local period1 = ""   -- For backwards compat with {{wayback}} 
	elseif not url_data[1].title and url_data[1].date then 
	local period2 = "." 
		sand = mw.ustring.format('[%s Archiviato] %s%s%s.', url_data[1].url, article, url_data[1].date, url_data[1].tail) 
  
	elseif url_data[1].title and not url_data[1].date then 
	--local url_main = ulx[1] 
		sand = mw.ustring.format('[%s %s]%s.', url_data[1].url, url_data[1].title, url_data[1].tail) 
	local indexstr = "archiviato" 
	elseif url_data[1].title and url_data[1].date then 
	if url_main.date == "index" then 
		sand = mw.ustring.format('[%s %s]%s (archiviato %s%s).', url_data[1].url, url_data[1].title, url_data[1].tail, article, url_data[1].date) 
		indexstr = "archivio" 
	else 
		return nil 
	end 
	if #url_data > 1 then -- per più URL di archivio 
	-- For {{wayback}}, {{webcite}} 
		sand = sand .. ' Archivi aggiuntivi: ' 
  
		local archives_output = {} 
	if url_main.format == "none" then 
		for i = 2, #url_data do 
		if not url_main.title and not url_main.date then -- No title. No date 
			sandarchives_output[#archives_output+1] = mw.ustring.format("'[%s Archiviato%s]%s"%s', url_mainurl_data[i].url, url_mainurl_data[i].title or url_data[i].date, url_data[i].title and (' (' .. url_data[i].date .. ')') or '', url_data[i].tail or '') 
		elseif not url_main.title and url_main.date then -- No title. Date. 
			if url_main.service == "wayback" then 
				period1 = "." 
				period2 = "" 
			end 
			sand = mw.ustring.format("[%s Archiviato] il %s%s%s%s", url_main.url, url_main.date, comma(url_main.date), url_main.tail, period1) 
		elseif url_main.title and not url_main.date then -- Title. No date. 
			sand = mw.ustring.format("[%s %s]%s", url_main.url, url_main.title, url_main.tail) 
		elseif url_main.title and url_main.date then -- Title. Date. 
			sand = mw.ustring.format("[%s %s]%s (%s %s)", url_main.url, url_main.title, url_main.tail, indexstr, url_main.date) 
		else 
			return nil 
		end 
		sand = sand .. table.concat(archives_output, ', ') .. '.' 
		if #ulx > 0 then -- For multiple archive URLs 
	end 
			sand = sand .. period2 .. " Archivi aggiuntivi: " 
	return sand 
			local archives_output = {} 
end 
			for indx,urlx in ipairs(ulx) do 
				archives_output[#archives_output+1] = mw.ustring.format("[%s %s]", urlx["url"], urlx["title"] or urlx["date"] ) 
			end 
			sand = sand .. table.concat(archives_output, ", ") .. "." 
		end 
		return sand 
  
--[[-------------------------------------------------------------------- 
	 -- For {{cite archives}} 
	Entry point per chiamata diretta da un modulo. 
	else 
	Riceve l'URL di un archivio e ne restituisce la data se riesce a decodificarla. 
		if url_main.format == "addlarchives" then -- Multiple archive services 
	]] 
			displayheader = "Archivi aggiuntivi: " 
function p.decodeArchiveDate(url) 
		else  -- Multiple pages from the same archive 
	local uri = mw.uri.new(url) 
			displayheader = mw.ustring.format("Pagine di archivio aggiuntive su %s: ", url_main.date) 
	local host, path = uri.host, uri.path 
		end 
	if not url or not host or path == '' then return end 
		local sand = displayheader 
	host = string.gsub(host, 'www%.', '') 
		local archives_output = {} 
			for indx_, urlxservizio in ipairs(ulxcfg.servizi) do 
		if host == servizio.signature then 
			displayfield = urlx["title"] 
			if url_mainservizio.formatservice == "addlarchives"'wayback' then 
				return decodeWaybackDate(path) 
				if not displayfield then displayfield = urlx["date"] end 
			elseif servizio.service == 'webcite' then 
			else 
				return decodeWebciteDate(path) 
				if not displayfield then displayfield = "Pagina " .. indx+1 end 
			elseif servizio.service == 'archiveis' then 
				return decodeArchiveisDate(path) 
			end 
			archive_ouptut[#archives_output+1] = mw.ustring.format("[%s %s]", urlx["url"], displayfield ) 
		end 
		sand = mw.ustring.format("%s%s.", sand, table.concat(archive_output, ", ")) 
		return sand 
	end 
	return 
end 
  
--[[--------------------------< createRendering >------------------------------------------ 
	Funzione di interfaccia principale per implementazione del 
  
	Template:Webarchive 
		 Funzione di interfaccia principale per implementazione di  
		 template:Webarchive 
  
	]] 
  
function p.webarchive(frame) 
	-- caricocarica in args i parametri, e se sono nulli sonoli ignoratiignora, eccetto che per il prametroparametro nolink 
	local args = getArgs(frame, { 
		valueFunc = function (key, value) 
			if key == 'nolink'value then 
				if key == 'nolink' then 
				return true 
					return true 
			elseif value then 
				else 					value = mw.text.trim(value) 
					if value ~= '' then return value end 
				end 
			end 
			return nil 
		end 
	}) 
	local url_data = {} 
	local i = 1 
	while true do 
		local n = i == 1 and args.url and '' or i 
		local url = i == 1 and (args.url or args.url1) or args['url' .. i] 
  
		-- verifica eventuali errori nel parametro url 
		if i == 1 and not url then 
			return inlineError('url', 'vuoto') .. createTracking() 
		elseif not url or i > maxurls then 
			break 
		elseif mw.ustring.find(url, 'https://web.http') then 
			track['Categoria:Errori di compilazione del template Webarchive'] = 1 
			return inlineError('url' .. n, 'https://web.http') .. createTracking() 
		elseif url == 'https://web.archive.org/http:/' then 
			track['Categoria:Errori di compilazione del template Webarchive'] = 1 
			return inlineError('url' .. n, 'URL non valido') .. createTracking() 
		end 
		url_data[i] = {} 
		url_data[i].url = url 
		url_data[i].uri = mw.uri.new(url) 
		url_data[i].host, url_data[i].path = url_data[i].uri.host, url_data[i].uri.path 
		if not url_data[i].host or url_data[i].path == '' then 
			return inlineError('url' .. n, 'URL non valido') .. createTracking() 
		end 
		serviceName(url_data[i], args.nolink, i > 1 and true) 
  
		-- gestione delle date 
	local tname = "Webarchive" -- name of calling template. Change if template rename. 
		local date = i == 1 and (args.date or args.date1 or args.data or args.data1) or 
	local verifydates = "yes"  -- See documentation. Set "no" to disable. 
				args['date' .. i] or args['data' .. i] 
  
		if date then 
	-- URL argument (first) 
			date = formatDate(date) 
	local url1 = args.url or args.url1 
			local udate = url_data[i].service == 'wayback' and decodeWaybackDate(url_data[i].path) or 
	if not url1 then 
					url_data[i].service == 'webcite' and decodeWebciteDate(url_data[i].path) or 
		return inlineError("url", "vuoto.") .. createTracking() 
					url_data[i].service == 'archiveis' and decodeArchiveisDate(url_data[i].path) 
	end 
			if udate and udate ~= date then 
	if mw.ustring.find( url1, "https://web.http") then -- track bug 
				date = date .. ' ' .. inlineRed('Data nell\'URL non combaciante: ' .. udate, 'warning') 
		track["Categoria:Errori di compilazione del template Webarchive"] = 1 
			elseif not udate and encoded_date == true then 
		return inlineError("url", "https://web.http") .. createTracking() 
				date = date .. ' ' .. inlineRed('Data nell\'URL indecifrabile', 'error') 
	end 
	if url1 == "https://web.archive.org/http:/" then -- track bug 
		track["Categoria:Errori di compilazione del template Webarchive"] = 1 
		return inlineError("url", "Invalid URL") .. createTracking() 
	end 
  
	local url_main = {} 
	url_main.url = url1 
	local uri1 = mw.uri.new(url1) 
	url_main.host = uri1.host 
	serviceName(url_main, args.nolink) 
  
	-- Date argument 
	local date = args.date or args.date1 or args.data or args.data1 
	if date == "*" and url_main.service == "wayback" then 
		date = "index" 
	elseif date and url_main.service == "wayback" and verifydates == "yes" then 
		local ldf = dateFormat(date) 
		if ldf then 
			local udate = decodeWaybackDate( uri1.path, ldf ) 
			if udate ~= date then 
				date = udate .. inlineRed("<sup>[Date mismatch]</sup>", "warning") 
			end 
		elseif url_data[i].service == 'wayback' then 
			date = decodeWaybackDate(url_data[i].path) 
		elseif url_data[i].service == 'webcite' then 
			date = decodeWebciteDate(url_data[i].path) 
		elseif url_data[i].service == 'archiveis' then 
			date = decodeArchiveisDate(url_data[i].path) 
		else 
			date = inlineRed('Data mancante', 'warning') 
		end 
	elseif date and url_main.service == "webcite" and verifydates == "yes" then 
		local ldf = dateFormat(date) 
		if ldf then 
			local udate = decodeWebciteDate( uri1.path, ldf ) 
			if udate == "query" then -- skip 
			elseif udate ~= date then 
				date = udate .. inlineRed("<sup>[Date mismatch]</sup>", "warning") 
			end 
		end 
	elseif not date and url_main.service == "wayback" then 
		date = decodeWaybackDate( uri1.path, "iso" ) 
		if not date then 
			date = encoded_date == false and inlineRed('Data mancante', 'warning') or 
			date = inlineRed("[Date error] (1)", "error") 
					inlineRed('Data nell\'URL indecifrabile', 'error') 
		end 
		url_data[i].date = date 
	elseif not date and url_main.service == "webcite" then 
		date = decodeWebciteDate( uri1.path, "iso" ) 
		if date == "query" then 
			date = inlineRed("[Data mancante]", "warning") 
		elseif not date then 
			date = inlineRed("[Date error] (1)]", "error") 
		end 
	elseif not date then 
		date = inlineRed("[Data mancante]", "warning") 
	end 
	url_main.date = date 
  
		-- gestione del titolo 
	-- Format argument 
	local		url_data[i].title format= i == 1 and (args.formattitle or args.formatotitle1 or "none"args.titolo or args.titolo1) or 
				args['title' .. i] or args['titolo' .. i] 
	if not format ~= "none" then 
  
		if format == "addlpages" then 
		i = i + 1 
			if not url_main.date then 
				format = "none" 
			end 
		elseif format ~= "addlarchives" then 
			format = "none" 
		end 
	end 
	local rend = createRendering(url_data) 
	url_main.format = format 
  
	-- Title argument 
	url_main.title = args.title or args.title1 or args.titolo or args.titolo1 
	local ulx = parseExtraArgs(args, maxurls) 
	--if true then return mw.text.jsonEncode(ulx, mw.text.JSON_PRETTY) end 
	local rend = createRendering(url_main, ulx) 
	if not rend then 
		track['Categoria:Errori di compilazione del template Webarchive'] = 1 
		rend = mw.ustring.format('<span style="font-size:100%" class="error citation-comment">Errori in [[:Template:%s]]: Problema sconosciuto. Si prega di segnalarlo nella [[Discussioni_template:%s|pagina di discussione]] del template.</span>', tname, tname) 
		rend = '<span style="font-size:100%" class="error citation-comment">Errori in [[:Template:Webarchive]]: problema sconosciuto. Si prega di segnalarlo nella [[Discussioni template:Webarchive|pagina di discussione]] del template.</span>' 
		track["Category:Webarchive template errors"] = 1 
	end 
  
	return rend .. createTracking() 
  
end 
  
 |