Modulo:Webarchive: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
Moroboshi (discussione | contributi)
semplifico ifelse di ricerca e traduco termini
Moroboshi (discussione | contributi)
pulizia di mesi e spazi bianchi; localizzo i mesi se le date sono generate dal template; categorie di tracking solo in ns:0
Riga 1:
--[[ ----------------------------------
Lua module implementing the {{webarchive}} template.
 
A merger of the functionality of three templates: {{wayback}}, {{webcite}} and {{cite archives}}
 
]]
 
Riga 9:
 
local servizi = {
{ signature = "archive.org", service = "wayback", tailbracket = " all'%sInternet Archive%s", tracking = "Categoria:Template Webarchive - collegamenti all'Internet Archive" },
{ signature = "webcitation.org", service = "webcite", tailbracket = " a %sWebCite%s", tracking = "Categoria:Template Webarchive - collegamenti a WebCite" },
{ signature = "archive.is", service = "archiveis", tailbracket = " a %sArchive.is%s", tracking = "Categoria:Template Webarchive - collegamenti a Archiveis" },
{ signature = "archive.fo", service = "archiveis", tailbracket = " a %sArchive.is%s", tracking = "Categoria:Template Webarchive - collegamenti a archiveis" },
{ signature = "archive.today", service = "archiveis", tailbracket = " a %sArchive.is%s", tracking = "Categoria:Template Webarchive - collegamenti a archiveis" },
{ signature = "archive.il", service = "archiveis", tailbracket = " a %sArchive.is%s", tracking = "Categoria:Template Webarchive - collegamenti a archiveis" },
{ signature = "archive.ec", service = "archiveis", tailbracket = " a %sArchive.is%s", tracking = "Categoria:Template Webarchive - collegamenti a archiveis" },
{ signature = "archive[-]it.org", service = "archiveit", tailbracket = " a %sArchive-It%s" },
{ signature = "arquivo.pt", tail = " al Portuguese Web Archive" },
{ signature = "loc.gov", tailbracket = " al %sLibrary of Congress%s" },
{ signature = "webharvest.gov", tailbracket = " al %sNational Archives and Records Administration%s" },
{ signature = "bibalex.org", tail = " alla [[Bibliotheca Alexandrina#Struttura e collezioni|Bibliotheca Alexandrina]]" },
{ signature = "collectionscanada", tail = " al Canadian Government Web Archive" },
{ signature = "haw.nsk", tail = " al Croatian Web Archive (HAW)" },
{ signature = "veebiarhiiv.digar.ee", tail = " all'Estonian Web Archive" },
{ signature = "vefsafn.is", tailtailbracket = " al [[National%sNational and University Library of Iceland%s]]" },
{ signature = "proni.gov", tailbracket = " al %sPublic Record Office of Northern Ireland%s" },
{ signature = "uni[-]lj.si", tail = " al Slovenian Web Archive" },
{ signature = "stanford.edu", tail = " al [[Stanford University Libraries|Stanford Web Archive]]" },
{ signature = "nationalarchives.gov.uk", tailbracket = " all'%sUK Government Web Archive%s" },
{ signature = "parliament.uk", tailbracket = " all'%sUK Parliament's Web Archive%s" },
{ signature = "webarchive.org.uk", tailbracket = " all'%sUK Web Archive%s" },
{ signature = "nlb.gov.sg", tail = " al Web Archive Singapore" },
{ signature = "pandora.nla.gov.au", tailbracket = " al %sPandora Archive%s" },
{ signature = "perma.cc", tailbracket = " a %sPerma.cc%s" },
{ signature = "perma-archives.cc", tailbracket = " a %sPerma.cc%s" },
{ signature = "screenshots.com", tail = " a Screenshots" },
{ signature = "wikiwix.com", tail = " a Wikiwix" },
{ signature = "freezepage.com", tail = " a Freezepage" },
{ signature = "webcache.googleusercontent.com", tail = " a Google Cache" }
}
 
local month_localized = { 'gennaio', 'febbraio', 'marzo', 'aprile', 'maggio', 'giugno',
'luglio', 'agosto', 'settembre', 'ottobre', 'novembre', 'dicembre'}
 
--[[--------------------------< inlineError >-----------------------
Line 50 ⟶ 53:
 
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: controlaltecontrollare <code style="color:inherit; border:inherit; padding:inherit;">&#124;' .. arg .. '=</code> value. ' .. msg .. '</span>'
 
end
Line 64 ⟶ 67:
 
if trackmsg == "warning" then
track["Categoria:Errori di compilazione del template Webarchive - Avvisi"] = 1
elseif trackmsg == "error" then
track["Categoria:Errori di compilazione del template Webarchive"] = 1
end
 
Line 75 ⟶ 78:
--[[--------------------------< 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=
 
Line 98 ⟶ 101:
 
Convert base-62 to base-10
Credit: https://de.wikipedia.org/wiki/Modul:Expr
 
]]
Line 104 ⟶ 107:
local function base62( value )
 
local r = 1
 
if value:match( "^%w+$" ) then
local n = #value
local k = 1
local c
r = 0
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
end
 
--[[--------------------------< tableLength >-----------------------
Line 162 ⟶ 165:
return nil
end
end
 
dt.split = mw.text.split(date, " ")
Line 172 ⟶ 175:
else
return "mdy"
end
else
if tonumber(dt.split[1]) then
Line 199 ⟶ 202:
end
 
local zmonth = month -- month with leading 0
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
local nmonth = month_localized[tonumber(month)]
if not nmonth then
return year
Line 212 ⟶ 216:
day = zday:match("0*(%d+)")
if tonumber(day) < 1 or tonumber(day) > 31 then
return mw.ustring.format("%s %s", nmonth, year)
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
if day == "1" then day = "1º" end
 
return mw.ustring.format("%s %s %s", day, nmonth, year)
end
 
Line 258 ⟶ 243:
-- 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
Line 294 ⟶ 279:
 
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 "*"
 
Line 308 ⟶ 293:
local N = mw.text.split(safe, "/")
snapdate = N[1]
if snapdate == "*" then -- eg. /web/*/http..
return "index"
end
safe = snapdate
snapdate = string.gsub(safe, "[a-z][a-z]_[0-9]?$", "") -- Remove any trailing "re_" from date
safe = snapdate
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
Line 383 ⟶ 368:
break
end
end
track[tracking] = 1
end
Line 391 ⟶ 376:
Parse numbered arguments starting at 2, such as url2..url10, date2..date10, title2..title10
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:
Line 437 ⟶ 422:
--[[--------------------------< comma >-----------------------
 
Given a date string, return "," if it's MDY
 
]]
Line 459 ⟶ 444:
 
local function createTracking()
 
-- Return tracking category only in namespace 0
local current_namespace = mw.title.getCurrentTitle().namespace
if current_namespace ~= 0 then return '' end
 
local sand = ""
if tableLength(track) > 0 then
for key,_ in pairs(track) do
sand = sand .. "[[" .. key .. "]]"
Line 478 ⟶ 467:
local function createRendering()
 
local sand, displayheader, displayfield
 
local period1 = "" -- For backwards compat with {{wayback}}
local period2 = "."
local indexstr = "archived"
if ulx.url1.date == "index" then
indexstr = "archive"
end
-- For {{wayback}}, {{webcite}}
 
local indexstr = "archiviato"
if ulx.url1.format == "none" then
if ulx.url1.date == "index" then
if not ulx.url1.title and not ulx.url1.date then -- No title. No date
indexstr = "archivio"
sand = "[" .. ulx.url1.url .. " archiviato]" .. ulx.url1.tail
end
elseif not ulx.url1.title and ulx.url1.date then -- No title. Date.
-- For {{wayback}}, {{webcite}}
if ulx.url1.service == "wayback" then
period1 = "."
period2 = ""
end
sand = "[" .. ulx.url1.url .. " archiviato] " .. 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 .. "&#32;(" .. 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 .. " Archivi aggiuntivi: "
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}}
 
if ulx.url1.format == "none" then
else
if not ulx.url1.formattitle ==and "addlarchives"not ulx.url1.date then -- MultipleNo archivetitle. servicesNo date
sand = "[" .. ulx.url1.url .. " archiviato]" .. ulx.url1.tail
displayheader = "Archivi aggiuntivi: "
elseif not ulx.url1.title and ulx.url1.date then -- No title. Date.
else -- Multiple pages from the same archive
if ulx.url1.service == "wayback" then
displayheader = "Pagine di archivio aggiuntivie&nbsp;su " .. ulx.url1.date .. ": "
period1 = "."
period2 = ""
end
sand = "[" .. ulx.url1.url .. " archiviato] il " .. ulx.url1.date .. comma(ulx.url1.date) .. ulx.url1.tail .. period1
local tot = 1 + ulx.url1.extraurls
elseif ulx.url1.title and not ulx.url1.date then -- Title. No date.
local sand = displayheader
sand = "[" .. ulx.url1.url .. " " .. ulx.url1.title .. "]" .. ulx.url1.tail
for i=1,tot do
elseif ulx.url1.title and ulx.url1.date then -- Title. Date.
sand = "[" .. ulx.url1.url .. " " .. ulx.url1.title .. "]" .. ulx.url1.tail .. "&#32;(" .. 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 .. " Archivi aggiuntivi: "
for i=2,tot do
local indx = "url" .. i
displayfield =if ulx[indx]["title"] then
displayfield = "title"
if ulx.url1.format == "addlarchives" then
if not displayfield then
displayfield = ulx[indx]["date"]
end
else
if not displayfield then= "date"
displayfield = "Pagina " .. i
end
end
sand = sand .. "[" .. ulx[indx]["url"] .. " " .. ulx[indx][displayfield] .. "]"
if i == tot then
sand = sand .. "."
Line 555 ⟶ 511:
end
end
else
return sand
end
return sand
 
-- For {{cite archives}}
else
if ulx.url1.format == "addlarchives" then -- Multiple archive services
displayheader = "Archivi aggiuntivi: "
else -- Multiple pages from the same archive
displayheader = "Pagine di archivio aggiuntivie&nbsp;su " .. 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
if not displayfield then
displayfield = "Pagina " .. i
end
end
sand = sand .. "[" .. ulx[indx]["url"] .. " " .. displayfield .. "]"
if i == tot then
sand = sand .. "."
else
sand = sand .. ", "
end
end
return sand
end
end
 
function p.webarchive(frame)
args = frame.args
if (args[1]==nil) and (args["url"]==nil) then -- if no argument provided than check parent template/module args
args = frame:getParent().args
end
 
local tname = "Webarchive" -- name of calling template. Change if template rename.
ulx = {} -- Associative array to hold template data
track = {} -- Associative array to hold tracking categories
maxurls = 10 -- Max number of URLs allowed.
local verifydates = "yes" -- See documentation. Set "no" to disable.
 
-- URL argument (first)
local url1 = trimArg(args.url) or trimArg(args.url1)
 
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
Line 592 ⟶ 580:
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
elseif date and ulx.url1.service == "webcite" and verifydates == "yes" then
local ldf = dateFormat(date)
if ldf then
Line 617 ⟶ 603:
if udate == "query" then -- skip
elseif udate ~= date then
date = udate .. inlineRed("<sup>[Date mismatch]</sup>", "warning")
end
end
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
Line 629 ⟶ 615:
if date == "query" then
date = inlineRed("[Data mancante]", "warning")
elseif not date then
date = inlineRed("[Date error] (1)", "error")
end
Line 637 ⟶ 623:
ulx.url1.date = date
 
-- Format argument
 
local format = trimArg(args.format) or trimArg(args.formato)
if not format then
Line 655 ⟶ 640:
ulx.url1.format = format
 
-- Title argument
 
local title = trimArg(args.title) or trimArg(args.title1) or trimArg(args.titolo) or trimArg(args.titolo1)
ulx.url1.title = title
 
local rend = createRendering()
if not rend then
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>'
track["Category:Webarchive template errors"] = 1
end