Modulo:Webarchive/sandbox: differenze tra le versioni
Contenuto cancellato Contenuto aggiunto
-codice obsoleto o inutile per l'italiano |
use require('strict') instead of require('Module:No globals') |
||
(35 versioni intermedie di un altro utente non mostrate) | |||
Riga 1:
--[[ ----------------------------------
Modulo Lua che implementa il template {{Webarchive}} e
decifrare la data negli URL di alcuni archivi anche ad altri moduli.
]]
require('
local getArgs = require('
local cfg = mw.loadData('Modulo:Webarchive/Configurazione/sandbox')
local p = {}
local track = {} -- array associativo per accumulare le categorie di tracciamento
local maxurls = 10 -- massimo numero di URL permessi
local encoded_date
--[[--------------------------< inlineError >-----------------------
Line 51 ⟶ 17:
]]
local function inlineError(arg, msg)
track[
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>'
end
Line 60 ⟶ 26:
]]
local function inlineRed(msg, trackmsg)
if trackmsg ==
track[
elseif trackmsg ==
track[
end
return '<span style="font-size:100%" class="error citation-comment">' .. msg .. '</span>'
Line 75 ⟶ 41:
local function base62( value )
local r = 1
if value:match(
local n = #value
local k = 1
Line 82 ⟶ 48:
for i = n, 1, -1 do
c = value:byte( i, i )
if c >= 48
c = c - 48
elseif c >= 65
c = c - 55
elseif c >= 97
c = c - 61
else
Line 109 ⟶ 75:
--[[--------------------------< formatDate >-----------------------
Verifica il formato di una data (dmy
la riformatta in dmy, altrimenti restituisce il valore così com'è
]]
Line 119 ⟶ 85:
try_year = tonumber(split[1])
if try_year and try_year > 1900 and try_year < 2200 then -- iso
y, m, d = split[1], cfg.month_localized[tonumber(split[2])], split[3]
end
else
Line 131 ⟶ 97:
end
end
if d == 1 then d = '1º' end return m and mw.ustring.format('%s %s %s', d, m, y) or date
end
Line 140 ⟶ 107:
]]
local function formatUrlDate(y, m, d)
local current_year = tonumber(os.date(
y, m, d = tonumber(y), tonumber(m), tonumber(d)
if not y or y == '' or not m or m == '' or not d or d == '' or d > 31 or
Line 146 ⟶ 113:
return nil
end
m = cfg.month_localized[m]
if d == 1 then d = '1º' end
return mw.ustring.format('%s %s %s', d, m, y)
Line 152 ⟶ 119:
--[[--------------------------< decodeWebciteDate >-----------------------
Ricava la data da un URI-path a Webcite (es. /67xHmVFWP)
]]
local function
local path_elements = mw.text.split(path,
--
-- http://www.webcitation.org/query?id=1138911916587475
Line 166 ⟶ 132:
-- http://www.webcitation.org/getfile.php?fileid=1c46e791d68e89e12d0c2532cc3cf629b8bc8c8e
if
return
mw.ustring.find(path_elements[2], 'cache') or
mw.ustring.find(path_elements[2], 'getfile') or
tonumber(path_elements[2]) then
return
end
local snapdate = os.date(
local dt = mw.text.split(snapdate,
local fulldate = formatUrlDate(dt[1], dt[2], dt[3])
return fulldate
Line 182 ⟶ 151:
Gestisce anche le non cifre come "re_", "-" e "*".
]]
local function
local snapdate = string.gsub(path,
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,
snapdate = string.gsub(snapdate,
snapdate = string.gsub(snapdate,
local fulldate
local year = string.sub(snapdate, 1, 4)
local month = string.sub(snapdate, 5, 6)
Line 200 ⟶ 169:
return fulldate
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 >-----------------------
Line 205 ⟶ 194:
estratto da mw.uri.new() (es. web.archive.org)
]]
local function serviceName(
local tracking = 'Categoria:Template Webarchive - collegamenti ad altri archivi'
local bracketopen, bracketclose = nolink and '' or '[[',
encoded_date = nil -- reset
for _,servizio in ipairs(cfg.servizi) do
if
if not notail and servizio
elseif not notail then
end
tracking = servizio
encoded_date = servizio.service and true
break
end
end
if
tracking = 'Categoria:Template Webarchive - collegamenti ad archivi sconosciuti'
url_data.tail = ' su ' .. url_data.host .. ' ' .. inlineRed('URL di servizio di archiviazione sconosciuto')
end
track[tracking] = 1
end
Line 275 ⟶ 235:
--[[--------------------------< createRendering >-----------------------
Restituisce la resa dei dati in url_data[][]
]]
local function createRendering(
local sand
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 '
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)
elseif not url_data[1].title and url_data[1].date then
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 #
sand = sand ..
local archives_output = {}
for
archives_output[#archives_output+1] = mw.ustring.format(
end
sand = sand .. table.concat(archives_output,
end
return sand
Line 313 ⟶ 264:
--[[--------------------------------------------------------------------
Entry point per chiamata diretta da un modulo.
Riceve l'URL di un archivio e ne restituisce la data se riesce a decodificarla.
]]
function p.decodeArchiveDate(url)
local uri = mw.uri.new(url)
local host, path = uri.host, uri.path
if not url or not host or path == '' then return end
host = string.gsub(host, 'www%.', '')
for _, servizio in ipairs(cfg.servizi) do
if host == servizio.signature then
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
end
--[[--------------------------------------------------------------------
Funzione di interfaccia principale per implementazione del
Template:Webarchive
]]
function p.webarchive(frame)
Line 320 ⟶ 294:
local args = getArgs(frame, {
valueFunc = function(key, value)
if
if key == 'nolink' then
return true
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]
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
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
return inlineError('url' .. n, 'URL non valido') .. createTracking()
end
serviceName(url_data[i], args.nolink, i > 1 and true)
-- gestione delle date
local date = i == 1 and (args.date or args.date1 or args.data or args.data1) or
args['date' .. i] or args['data' .. i]
if date then
date = formatDate(date)
local udate = url_data[i].service == 'wayback' and decodeWaybackDate(url_data[i].path) or
url_data[i].service == 'webcite' and decodeWebciteDate(url_data[i].path) or
url_data[i].service == 'archiveis' and decodeArchiveisDate(url_data[i].path)
if udate and udate ~= date then
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 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
if not date then
date = encoded_date == false and inlineRed('Data mancante', 'warning') or
inlineRed('Data nell\'URL indecifrabile', 'error')
end
url_data[i].date = date
args['title' .. i] or args['titolo' .. i]
i = i + 1
end
local rend = createRendering(url_data)
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:Webarchive]]: problema sconosciuto. Si prega di segnalarlo nella [[ end
|