--[[ ----------------------------------
Modulo Lua moduleche implementingimplementa theil template {{webarchiveWebarchive}} template.e permette di
decifrare la data negli URL di alcuni archivi anche ad altri moduli.
A merger of the functionality of three templates: {{wayback}}, {{webcite}} and {{cite archives}}
]]
require('strict')
local getArgs = require('Modulo:Arguments').getArgs
local cfg = mw.loadData('Modulo:Webarchive/Configurazione')
local p = {}
local track = {} -- array associativo per accumulare le categorie di tracciamento
local maxurls = 10 -- massimo numero di URL permessi
local encoded_date
--[[--------------------------< 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: controlalte <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
--[[--------------------------< trimArg >-----------------------
trimArg returns nil if arg is "" while trimArg2 returns 'true' if arg is ""
trimArg2 is for args that might accept an empty value, as an on/off switch like nolink=
]]
local function trimArg(arg)
if arg == "" or arg == nil then
return nil
else
return mw.text.trim(arg)
end
end
local function trimArg2(arg)
if arg == nil then
return nil
else
return mw.text.trim(arg)
end
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
local r = 1
local n = #value
local k = 1
if value:match( "^%w+$" ) then
local n = #valuec
local kr = 10
for i = n, 1, -1 do
local c
rc = 0value:byte( i, i )
forif ic >= n,48 1,and -1c do<= 57 then
c = value:byte( i,c i- )48
ifelseif c >= 48 65 and c <= 5790 then
c = c - 4855
elseif c >= 65 97 and c <= 90122 then
c = c - 5561
else
elseif c >= 97 and c <= 122 then
cr = c - 611
break
else -- How comes?
r = 1end
breakr = r + --c for* ik
endk = k * 62
r = r + c * k
k = k * 62
end -- for i
end
end
return r
return r
end
end
--[[--------------------------< tableLength >-----------------------
Restituisce il numero di elementi in una tabella
Given a 1-D table, return number of elements
]]
local function tableLength(t)
local function tableLength(T)
local count = 0
for _ in pairs(Tt) do count = count + 1 end
return count
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 = os.date("%B", os.time{year=2000, month=month, day=1} ) -- month in name form
if not nmonth then
return year
end
local zday = day
day = zday:match("0*(%d+)")
if tonumber(day) < 1 or tonumber(day) > 31 then
if df == "mdy" or df == "dmy" then
return nmonth .. " " .. year
elseif df == "iso" then
return year .. "-" .. zmonth
elseif df == "ymd" then
return year .. " " .. nmonth
else
return nmonth .. " " .. year
end
end
if df == "mdy" then
return nmonth .. " " .. day .. ", " .. year -- September 1, 2016
elseif df == "dmy" then
return day .. " " .. nmonth .. " " .. year -- 1 September 2016
elseif df == "iso" then
return year .. "-" .. zmonth .. "-" .. zday -- 2016-09-01
elseif df == "ymd" then
return year .. " " .. nmonth .. " " .. cday -- 2016 September 1
else
return nmonth .. " " .. day .. ", " .. year -- September 1, 2016
end
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", 1, plain) or
mw.ustring.find( dt.split[2], "cache", 1, plain) or
mw.ustring.find( dt.split[2], "getfile", 1, plain) 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
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/"
local function decodeWaybackDate(path, df)
snapdate = string.gsub(snapdate, '^/w?e?b?/?', '') -- rimuove la sequenza iniziale "/web/" o "/"
local path_elements = mw.text.split(snapdate, '/')
local snapdate, snapdatelong, currdate, fulldate
snapdate = path_elements[1]
if snapdate == '*' then return end
local safe = path
snapdate = string.gsub(safesnapdate, "^/w'[a-z][a-z]_[0-9]?e?b?/?"$', ""'') -- Remove leading "/web/" or "/"
safe snapdate = string.gsub(snapdate, '[-]', '')
snapdate = string.gsub(snapdate, '[*]$', '')
local N = mw.text.split(safe, "/")
local fulldate
snapdate = N[1]
if tonumber(snapdate) and string.len(snapdate) >= 8 then
if snapdate == "*" then -- eg. /web/*/http..
local year = string.sub(snapdate, 1, 4)
return "index"
local month = string.sub(snapdate, 5, 6)
end
safelocal day = string.sub(snapdate, 7, 8)
fulldate = formatUrlDate(year, month, day)
snapdate = string.gsub(safe, "[a-z][a-z]_[0-9]?$", "") -- Remove any trailing "re_" from date
end
safe = snapdate
return fulldate
snapdate = string.gsub(safe, "[-]", "") -- Remove dashes from date eg. 2015-01-01
safe = snapdate
snapdate = string.gsub(safe, "[*]$", "") -- Remove trailing "*"
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
fulldate = makeDate(year, month, day, df)
if not fulldate then
return inlineRed("[Date error] (7)", "error")
else
return fulldate
end
end
--[[--------------------------< decodeArchiveisDate >-----------------------
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 function decodeArchiveisDate(path)
local path_elements = mw.text.split(path, '/')
local snapdate = path_elements[2]
if not path_elements[2] or path_elements[2] == '' then return end
snapdate = string.gsub(snapdate, '[%.%-]', '')
if not tonumber(snapdate) then encoded_date = false return end -- formato link breve
local fulldate
if 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
--[[--------------------------< 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(host, 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
if string.gsub(url_data.host, 'www%.', '') == servizio.signature then
local bracketopen = "[["
url_data.service = servizio.service or 'altri'
local bracketclose = "]]"
if not notail and servizio.tailbracket then
if nolink then
url_data.tail = mw.ustring.format(servizio.tailbracket, bracketopen, bracketclose)
bracketopen = ""
elseif not notail then
bracketclose = ""
url_data.tail = servizio.tail
end
tracking = servizio.tracking or tracking
encoded_date = servizio.service and true
break
end
end
if url_data.service == nil then
tracking = 'Categoria:Template Webarchive - collegamenti ad archivi sconosciuti'
ulx.url1.service = "altri"
ulx.url1 url_data.tail = "' atsu "' .. ulx.url1url_data.host .. "' "' .. inlineRed("Errore: 'URL di servizio di archiviazione sconosciuto"')
if mw.ustring.find( host, "archive.org", 1, plain ) then
ulx.url1.service = "wayback"
ulx.url1.tail = " all'" .. bracketopen .. "Internet Archive" .. bracketclose
tracking = "Categoria:Template Webarchive collegamenti all'Internet Archive"
elseif mw.ustring.find( host, "webcitation.org", 1, plain ) then
ulx.url1.service = "webcite"
ulx.url1.tail = " a " .. bracketopen .. "WebCite" .. bracketclose
tracking = "Categoria:Template Webarchive collegamenti a WebCite"
elseif mw.ustring.find( host, "archive.is", 1, plain ) then
ulx.url1.service = "archiveis"
ulx.url1.tail = " a " .. bracketopen .. "Archive.is" .. bracketclose
tracking = "Categoria:Template Webarchive collegamenti a Archiveis"
elseif mw.ustring.find( host, "archive.fo", 1, plain ) then
ulx.url1.service = "archiveis"
ulx.url1.tail = " a " .. bracketopen .. "Archive.is" .. bracketclose
tracking = "Categoria:Template Webarchive collegamenti a archiveis"
elseif mw.ustring.find( host, "archive.today", 1, plain ) then
ulx.url1.service = "archiveis"
ulx.url1.tail = " a " .. bracketopen .. "Archive.is" .. bracketclose
tracking = "Categoria:Template Webarchive collegamenti a archiveis"
elseif mw.ustring.find( host, "archive.il", 1, plain ) then
ulx.url1.service = "archiveis"
ulx.url1.tail = " a " .. bracketopen .. "Archive.is" .. bracketclose
tracking = "Categoria:Template Webarchive collegamenti a archiveis"
elseif mw.ustring.find( host, "archive.ec", 1, plain ) then
ulx.url1.service = "archiveis"
ulx.url1.tail = " a " .. bracketopen .. "Archive.is" .. bracketclose
tracking = "Categoria:Template Webarchive collegamenti a archiveis"
elseif mw.ustring.find( host, "archive[-]it.org", 1, plain ) then
ulx.url1.service = "archiveit"
ulx.url1.tail = " a " .. bracketopen .. "Archive-It" .. bracketclose
elseif mw.ustring.find( host, "arquivo.pt", 1, plain) then
ulx.url1.tail = " a " .. "Portuguese Web Archive"
elseif mw.ustring.find( host, "loc.gov", 1, plain ) then
ulx.url1.tail = " a " .. bracketopen .. "Library of Congress" .. bracketclose
elseif mw.ustring.find( host, "webharvest.gov", 1, plain ) then
ulx.url1.tail = " a " .. bracketopen .. "National Archives and Records Administration" .. bracketclose
elseif mw.ustring.find( host, "bibalex.org", 1, plain ) then
ulx.url1.tail = " alla " .. "[[Bibliotheca Alexandrina#Struttura e collezioni|Bibliotheca Alexandrina]]"
elseif mw.ustring.find( host, "collectionscanada", 1, plain ) then
ulx.url1.tail = " al " .. "Canadian Government Web Archive"
elseif mw.ustring.find( host, "haw.nsk", 1, plain ) then
ulx.url1.tail = " al " .. "Croatian Web Archive (HAW)"
elseif mw.ustring.find( host, "veebiarhiiv.digar.ee", 1, plain ) then
ulx.url1.tail = " all'" .. "Estonian Web Archive"
elseif mw.ustring.find( host, "vefsafn.is", 1, plain ) then
ulx.url1.tail = " al " .. "[[National and University Library of Iceland]]"
elseif mw.ustring.find( host, "proni.gov", 1, plain ) then
ulx.url1.tail = " al " .. bracketopen .. "Public Record Office of Northern Ireland" .. bracketclose
elseif mw.ustring.find( host, "uni[-]lj.si", 1, plain ) then
ulx.url1.tail = " al " .. "Slovenian Web Archive"
elseif mw.ustring.find( host, "stanford.edu", 1, plain ) then
ulx.url1.tail = " al " .. "[[Stanford University Libraries|Stanford Web Archive]]"
elseif mw.ustring.find( host, "nationalarchives.gov.uk", 1, plain ) then
ulx.url1.tail = " al " .. bracketopen .. "UK Government Web Archive" .. bracketclose
elseif mw.ustring.find( host, "parliament.uk", 1, plain ) then
ulx.url1.tail = " al " .. bracketopen .. "UK Parliament's Web Archive" .. bracketclose
elseif mw.ustring.find( host, "webarchive.org.uk", 1, plain ) then
ulx.url1.tail = " al " .. bracketopen .. "UK Web Archive" .. bracketclose
elseif mw.ustring.find( host, "nlb.gov.sg", 1, plain ) then
ulx.url1.tail = " a " .. "Web Archive Singapore"
elseif mw.ustring.find( host, "pandora.nla.gov.au", 1, plain ) then
ulx.url1.tail = " a " .. bracketopen .. "Pandora Archive" .. bracketclose
elseif mw.ustring.find( host, "perma.cc", 1, plain ) then
ulx.url1.tail = " a " .. bracketopen .. "Perma.cc" .. bracketclose
elseif mw.ustring.find( host, "perma-archives.cc", 1, plain ) then
ulx.url1.tail = " a " .. bracketopen .. "Perma.cc" .. bracketclose
elseif mw.ustring.find( host, "screenshots.com", 1, plain ) then
ulx.url1.tail = " a Screenshots"
elseif mw.ustring.find( host, "wikiwix.com", 1, plain ) then
ulx.url1.tail = " a Wikiwix"
elseif mw.ustring.find( host, "freezepage.com", 1, plain ) then
ulx.url1.tail = " a Freezepage"
elseif mw.ustring.find( host, "webcache.googleusercontent.com", 1, plain ) then
ulx.url1.tail = " a Google Cache"
else
tracking = "Category:Webarchive template unknown archives"
end
track[tracking] = 1
end
--[[--------------------------< parseExtraArgscreateTracking >-----------------------
Restituisce le categorie di tracciamento inserite in track[]
]]
Parse numbered arguments starting at 2, such as url2..url10, date2..date10, title2..title10
local function createTracking()
For example: {{webarchive |url=.. |url4=.. |url7=..}}
-- procede solo nel namespace 0
Three url arguments not in numeric sequence (1..4..7).
local current_namespace = mw.title.getCurrentTitle().namespace
Function only processes arguments numbered 2 or greater (in this case 4 and 7)
if current_namespace ~= 0 then return '' end
It creates numeric sequenced table entries like:
local sand = ''
urlx.url2.url = <argument value for url4>
if tableLength(track) > 0 then
urlx.url3.url = <argument value for url7>
for key,_ in pairs(track) do
Returns the number of URL arguments found numbered 2 or greater (in this case returns "2")
sand = sand .. '[[' .. key .. ']]'
]]
local function parseExtraArgs()
local i, j, argurl, argurl2, argdate, argtitle
j = 2
for i = 2, maxurls do
argurl = "url" .. i
if trimArg(args[argurl]) then
argurl2 = "url" .. j
ulx[argurl2] = {}
ulx[argurl2]["url"] = args[argurl]
argdate = "date" .. j or "data" .. j
if trimArg(args[argdate]) then
ulx[argurl2]["date"] = args[argdate]
else
ulx[argurl2]["date"] = inlineRed("[Data mancante]", "warning")
end
argtitle = "title" .. j
if trimArg(args[argtitle]) then
ulx[argurl2]["title"] = args[argtitle]
else
ulx[argurl2]["title"] = nil
end
j = j + 1
end
end
return sand
if j == 2 then
return 0
else
return j - 2
end
end
--[[--------------------------< commacreateRendering >-----------------------
Restituisce la resa dei dati in url_data[][]
Given a date string, return "," if it's MDY
]]
local function createRendering(url_data)
local function comma(date)sand
local Nday = url_data[1].date and mw.textustring.splitmatch(url_data[1].date, " "'^%d+')
local article = (day == '8' or day == '11') and 'l\'' or 'il '
local O = mw.text.split(N[1], "-") -- for ISO
if not url_data[1].title and not url_data[1].date then
if O[1] == "index" then return "" end
sand = mw.ustring.format('[%s Archiviato]%s.', url_data[1].url, url_data[1].tail)
if not tonumber(O[1]) then
elseif not url_data[1].title and url_data[1].date then
return ","
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
sand = mw.ustring.format('[%s %s]%s.', url_data[1].url, url_data[1].title, url_data[1].tail)
elseif url_data[1].title and url_data[1].date 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)
else
return ""nil
end
if #url_data > 1 then -- per più URL di archivio
sand = sand .. ' Archivi aggiuntivi: '
local archives_output = {}
for i = 2, #url_data do
archives_output[#archives_output+1] = mw.ustring.format('[%s %s]%s%s', url_data[i].url, url_data[i].title or url_data[i].date, url_data[i].title and (' (' .. url_data[i].date .. ')') or '', url_data[i].tail or '')
end
sand = sand .. table.concat(archives_output, ', ') .. '.'
end
return sand
end
--[[--------------------------< createTracking >------------------------------------------
Entry point per chiamata diretta da un modulo.
Riceve l'URL di un archivio e ne restituisce la data se riesce a decodificarla.
Return data in track[] ie. tracking categories
]]
function p.decodeArchiveDate(url)
local functionuri createTracking= mw.uri.new(url)
local host, path = uri.host, uri.path
if not url or not host or path == '' then return end
local sand = ""
host = string.gsub(host, 'www%.', '')
if tableLength(track) > 0 then
for key,_, servizio in pairsipairs(trackcfg.servizi) do
if host == servizio.signature then
sand = sand .. "[[" .. key .. "]]"
if servizio.service == 'wayback' then
return decodeWaybackDate(path)
elseif servizio.service == 'webcite' then
return decodeWebciteDate(path)
elseif servizio.service == 'archiveis' then
return decodeArchiveisDate(path)
end
end
end
return sand
end
--[[--------------------------< createRendering >------------------------------------------
Funzione di interfaccia principale per implementazione del
Template:Webarchive
Return a rendering of the data in ulx[][]
]]
function p.webarchive(frame)
-- carica in args i parametri e se sono nulli li ignora, eccetto che per il parametro nolink
local function createRendering()
local args = getArgs(frame, {
valueFunc = function(key, value)
local sand, displayheader, displayfield
if value then
if key == 'nolink' then
local period1 = "" -- For backwards compat with {{wayback}}
return true
local period2 = "."
local indexstr = "archived"
if ulx.url1.date == "index" then
indexstr = "archive"
end
-- For {{wayback}}, {{webcite}}
if ulx.url1.format == "none" then
if not ulx.url1.title and not ulx.url1.date then -- No title. No date
sand = "[" .. ulx.url1.url .. " Archived]" .. ulx.url1.tail
elseif not ulx.url1.title and ulx.url1.date then -- No title. Date.
if ulx.url1.service == "wayback" then
period1 = "."
period2 = ""
end
sand = "[" .. ulx.url1.url .. " Archived] " .. ulx.url1.date .. comma(ulx.url1.date) .. ulx.url1.tail .. period1
elseif ulx.url1.title and not ulx.url1.date then -- Title. No date.
sand = "[" .. ulx.url1.url .. " " .. ulx.url1.title .. "]" .. ulx.url1.tail
elseif ulx.url1.title and ulx.url1.date then -- Title. Date.
sand = "[" .. ulx.url1.url .. " " .. ulx.url1.title .. "]" .. ulx.url1.tail .. " (" .. indexstr .. " " .. ulx.url1.date .. ")"
else
return nil
end
if ulx.url1.extraurls > 0 then -- For multiple archive URLs
local tot = ulx.url1.extraurls + 1
sand = sand .. period2 .. " Additional archives: "
for i=2,tot do
local indx = "url" .. i
if ulx[indx]["title"] then
displayfield = "title"
else
displayfield = "date"
end
sand = sand .. "[" .. ulx[indx]["url"] .. " " .. ulx[indx][displayfield] .. "]"
if i == tot then
sand = sand .. "."
else
sand = sand .. ", "
end
end
else
return sand
end
return sand
-- For {{cite archives}}
else
if ulx.url1.format == "addlarchives" then -- Multiple archive services
displayheader = "Additional archives: "
else -- Multiple pages from the same archive
displayheader = "Additional pages archived on " .. ulx.url1.date .. ": "
end
local tot = 1 + ulx.url1.extraurls
local sand = displayheader
for i=1,tot do
local indx = "url" .. i
displayfield = ulx[indx]["title"]
if ulx.url1.format == "addlarchives" then
if not displayfield then
displayfield = ulx[indx]["date"]
end
else
value = mw.text.trim(value)
if not displayfield then
displayfieldif value ~= "Page'' then "return ..value iend
end
end
sand = sand .. "[" .. ulx[indx]["url"] .. " " .. displayfield .. "]"
if i == tot then
sand = sand .. "."
else
sand = sand .. ", "
end
end
return sandnil
end
})
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
function p.webarchive(frame)
if i == 1 and not url then
args = frame.args
return inlineError('url', 'vuoto') .. createTracking()
if (args[1]==nil) and (args["url"]==nil) then -- if no argument provided than check parent template/module args
elseif not url or i > maxurls then
args = frame:getParent().args
break
end
elseif mw.ustring.find(url, 'https://web.http') then
track['Categoria:Errori di compilazione del template Webarchive'] = 1
local tname = "Webarchive" -- name of calling template. Change if template rename.
return inlineError('url' .. n, 'https://web.http') .. createTracking()
ulx = {} -- Associative array to hold template data
elseif url == 'https://web.archive.org/http:/' then
track = {} -- Associative array to hold tracking categories
track['Categoria:Errori di compilazione del template Webarchive'] = 1
maxurls = 10 -- Max number of URLs allowed.
return inlineError('url' .. n, 'URL non valido') .. createTracking()
local verifydates = "yes" -- See documentation. Set "no" to disable.
-- URL argument (first)
local url1 = trimArg(args.url) or trimArg(args.url1)
if not url1 then
return inlineError("url", "Empty.") .. createTracking()
end
if mw.ustring.find( url1, "https://web.http", 1, plain ) then -- track bug
track["Categoria:Errori di compilazione del template Webarchive"] = 1
return inlineError("url", "https://web.http") .. createTracking()
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
ulx.url1 = {}
ulx.url1.url = url1
local uri1 = mw.uri.new(ulx.url1.url)
ulx.url1.host = uri1.host
ulx.url1.extraurls = parseExtraArgs()
-- Nolink argument
local nolink = trimArg2(args.nolink)
serviceName(uri1.host, nolink)
-- Date argument
local date = trimArg(args.date) or trimArg(args.date1)
if date == "*" and ulx.url1.service == "wayback" then
date = "index"
elseif date and ulx.url1.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
end
url_data[i] = {}
elseif date and ulx.url1.service == "webcite" and verifydates == "yes" then
url_data[i].url = url
local ldf = dateFormat(date)
url_data[i].uri = mw.uri.new(url)
if ldf then
url_data[i].host, url_data[i].path = url_data[i].uri.host, url_data[i].uri.path
local udate = decodeWebciteDate( uri1.path, ldf )
if not url_data[i].host or url_data[i].path == '' then
if udate == "query" then -- skip
return inlineError('url' .. n, 'URL non valido') .. createTracking()
elseif udate ~= date then
date = udate .. inlineRed("<sup>[Date mismatch]</sup>", "warning")
end
end
serviceName(url_data[i], args.nolink, i > 1 and true)
elseif not date and ulx.url1.service == "wayback" then
date = decodeWaybackDate( uri1.path, "iso" )
if not date then
date = inlineRed("[Date error] (1)", "error")
end
elseif not date and ulx.url1.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
ulx.url1.date = date
-- gestione delle date
-- Format argument
local date = i == 1 and (args.date or args.date1 or args.data or args.data1) or
args['date' .. i] or args['data' .. i]
local format = trimArg(args.format) or trimArg(args.formato)
if not formatdate then
date = formatDate(date)
format = "none"
local udate = url_data[i].service == 'wayback' and decodeWaybackDate(url_data[i].path) or
else
url_data[i].service == 'webcite' and decodeWebciteDate(url_data[i].path) or
if format == "addlpages" then
url_data[i].service == 'archiveis' and decodeArchiveisDate(url_data[i].path)
if not ulx.url1.date then
if udate and udate ~= date then
format = "none"
date = date .. ' ' .. inlineRed('Data nell\'URL non combaciante: ' .. udate, 'warning')
elseif not udate and encoded_date == true then
date = date .. ' ' .. inlineRed('Data nell\'URL indecifrabile', 'error')
end
elseif formaturl_data[i].service == "addlarchives"'wayback' then
date = decodeWaybackDate(url_data[i].path)
format = "addlarchives"
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')
format = "none"
end
if not date then
end
date = encoded_date == false and inlineRed('Data mancante', 'warning') or
ulx.url1.format = format
inlineRed('Data nell\'URL indecifrabile', 'error')
end
url_data[i].date = date
-- gestione del titolo
-- Title argument
url_data[i].title = i == 1 and (args.title or args.title1 or args.titolo or args.titolo1) or
args['title' .. i] or args['titolo' .. i]
i = i + 1
local title = trimArg(args.title) or trimArg(args.title1) or trimArg(args.titolo) or trimArg(args.titolo1)
end
ulx.url1.title = title
local rend = createRendering(url_data)
local rend = createRendering()
if not rend then
track['Categoria:Errori di compilazione del template Webarchive'] = 1
rend = '<span style="font-size:100%" class="error citation-comment">Errori in [[:Template:' .. tname .. ']]: Problema sconosciuto. Si prega di segnalarlo nella [[Discussioni_template:' .. tname .. '|pagina di discussione]] del template.</span>'
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
|