Modulo:Webarchive: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
Moroboshi (discussione | contributi)
Rollback (credevo che stavo lavorando in sandbox)
Moroboshi (discussione | contributi)
aggiorno dopo refactoring in sandbox
Riga 5:
 
]]
 
require('Module:No globals')
local getArgs = require('Module:Arguments').getArgs
 
local p = {}
local track = {} -- Associative array to hold tracking categories
local maxurls = 10 -- Max number of URLs allowed.
 
local servizi = {
Riga 76 ⟶ 81:
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 >-----------------------
Riga 139 ⟶ 123:
]]
 
local function tableLength(Tt)
local count = 0
for _ in pairs(Tt) do count = count + 1 end
return count
end
Riga 204 ⟶ 188:
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
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 end
return year
end
 
local zday = day
day = zday:match("0*(%d+)")
Riga 243 ⟶ 221:
-- 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"
Riga 267 ⟶ 245:
end
 
local fulldate = makeDate(dt.year, dt.month, dt.day, df)
if not fulldate then
return inlineRed("[Date error] (4)", "error")
Riga 288 ⟶ 266:
local snapdate, snapdatelong, currdate, fulldate
 
 
local safe = path
local snapdate = string.gsub(safepath, "^/w?e?b?/?", "") -- Remove leading "/web/" or "/"
local safe = snapdate
local Npath_elements = mw.text.split(safe, "/")
snapdate = Npath_elements[1]
if snapdate == "*" then -- eg. /web/*/http..
return "index"
Riga 329 ⟶ 307:
end
 
local fulldate = makeDate(year, month, day, df)
if not fulldate then
return inlineRed("[Date error] (7)", "error")
Riga 344 ⟶ 322:
]]
 
local function serviceName(hosturl_main, nolink)
 
local tracking = "Categoria:Template Webarchive - altri archivi"
Riga 354 ⟶ 332:
end
 
ulx.url1url_main.service = "altri"
ulx.url1.tail = " a " .. ulx.url1.host .. " " .. inlineRed("Errore: URL di servizio di archiviazione sconosciuto")
 
for _,servizio in ipairs(servizi) do
if mw.ustring.find(url_main.host, servizio.signature, 1, plain) then
ulx.url1url_main.service = servizio['service'] or ulx.url1url_main.service
if servizio['tailbracket'] then
ulx.url1url_main.tail = mw.ustring.format(servizio.tailbracket, bracketopen, bracketclose)
else
ulx.url1url_main.tail = servizio['tail'] or ulx.url1.tail
end
tracking = servizio['tracking'] or tracking
break
end
end
if url_main.tail == nil then
url_main.tail = " a " .. url_main.host .. " " .. inlineRed("Errore: URL di servizio di archiviazione sconosciuto")
end
track[tracking] = 1
Riga 374 ⟶ 354:
--[[--------------------------< parseExtraArgs >-----------------------
 
Parse numbered arguments starting atfrom 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.url2[1].url = <argument value for url4>
urlx.url3[2].url = <argument value for url7>
Returns the parsed table
Returns the number of URL arguments found numbered 2 or greater (in this case returns "2")
 
]]
 
local function parseExtraArgs(args, maxurls)
 
local i, jindx, argurl, argurl2, argdate, argtitle
local ulx = {}
 
jindx = 21
for i = 2, maxurls do
argurl = "url" .. i
if trimArg(args[argurl]) then
argurl2ulx[indx] = "url" .. j{}
ulx[argurl2indx]["url"] = {}args[argurl]
ulx[argurl2indx]["urldate"] = args[argurl"date" .. i] or args["data" .. i]
if not ulx[argurl2indx]["date"] = args["date" .. j] or args["data" .. j] then
if not ulx[argurl2indx]["date"] then= inlineRed("[Data mancante]", "warning")
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
ulx[indx]["title"] = args["title" .. i] or args["titolo" .. i]
j = j + 1
indx = indx + 1
end
end
return ulx
end
 
if j == 2 then
return 0
else
return j - 2
end
 
end
 
--[[--------------------------< comma >-----------------------
Riga 425 ⟶ 394:
 
local function comma(date)
local Nn = mw.text.split(date, " ")
local Oo = mw.text.split(Nn[1], "-") -- for ISO
if Oo[1] == "index" then return "" end
if not tonumber(Oo[1]) then
return ","
else
Riga 463 ⟶ 432:
]]
 
local function createRendering(url_main, ulx)
 
local sand, displayheader, displayfield
Riga 470 ⟶ 439:
local period2 = "."
 
--local url_main = ulx[1]
local indexstr = "archiviato"
if ulx.url1url_main.date == "index" then
indexstr = "archivio"
end
-- For {{wayback}}, {{webcite}}
 
if ulx.url1url_main.format == "none" then
if not ulx.url1url_main.title and not ulx.url1url_main.date then -- No title. No date
sand = "[" mw.ustring. ulx.url1.url .. format("[%s Archiviato]%s", url_main..url, ulx.url1url_main.tail)
elseif not ulx.url1url_main.title and ulx.url1url_main.date then -- No title. Date.
if ulx.url1url_main.service == "wayback" then
period1 = "."
period2 = ""
end
sand = "[" mw.ustring. ulx.url1.url .. format("[%s Archiviato] il %s%s%s%s", url_main..url, ulx.url1url_main.date .., comma(ulx.url1url_main.date), .. ulx.url1url_main.tail .., period1)
elseif ulx.url1url_main.title and not ulx.url1url_main.date then -- Title. No date.
sand = "[" mw.ustring. ulx.url1.url .. format("[%s %s]%s", url_main..url, ulx.url1url_main.title, .. "]" .. ulx.url1url_main.tail)
elseif ulx.url1url_main.title and ulx.url1url_main.date then -- Title. Date.
sand = mw.ustring.format("[%s %s]%s&#32;(%s %s)", url_main.url, url_main.title, url_main.tail, indexstr, url_main.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: "
local archives_output = {}
for i=2,tot do
localfor indx,urlx =in "url"ipairs(ulx) .. ido
archives_output[#archives_output+1] = mw.ustring.format("[%s %s]", urlx["url"], urlx["title"] or urlx["date"] )
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
sand = sand .. table.concat(archives_output, ", ") .. "."
else
return sand
end
return sand
Riga 516 ⟶ 474:
-- For {{cite archives}}
else
if ulx.url1url_main.format == "addlarchives" then -- Multiple archive services
displayheader = "Archivi aggiuntivi: "
else -- Multiple pages from the same archive
displayheader = mw.ustring.format("Pagine di archivio aggiuntivie&nbsp;aggiuntive su %s: ", .. ulx.url1url_main.date .. ": ")
end
local tot = 1 + ulx.url1.extraurls
local sand = displayheader
local archives_output = {}
for i=1,tot do
localfor indx, =urlx "url"in ..ipairs(ulx) ido
displayfield = ulx[indx]urlx["title"]
if ulx.url1url_main.format == "addlarchives" then
if not displayfield then displayfield = urlx["date"] end
displayfield = ulx[indx]["date"]
end
else
if not displayfield then displayfield = "Pagina " .. indx+1 end
displayfield = "Pagina " .. i
end
end
sand = sand .. "[" .. ulx[indx]["url"] .. " " .. displayfield .. "]"
if i == tot then
sand = sand .. "."
else
sand = sand .. ", "
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
end
 
--[[--------------------------< createRendering >-----------------------
 
Funzione di interfaccia principale per implementazione di
template:Webarchive
 
]]
 
function p.webarchive(frame)
-- carico in args i parametri, se sono nulli sono ignorati, eccetto che per il prametro nolink
args = frame.args
local args = getArgs(frame, {
if (args[1]==nil) and (args["url"]==nil) then -- if no argument provided than check parent template/module args
valueFunc = function (key, value)
args = frame:getParent().args
if key == 'nolink' then
end
return true
elseif value then
value = mw.text.trim(value)
if value ~= '' then return value end
end
return nil
end
})
 
 
local tname = "Webarchive" -- name of calling template. Change if template rename.
local verifydates = "yes" -- See documentation. Set "no" to disable.
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)
if not url1 then
return inlineError("url", "Emptyvuoto.") .. 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()
Riga 572 ⟶ 534:
end
 
ulx.url1local url_main = {}
ulx.url1url_main.url = url1
local uri1 = mw.uri.new(ulx.url1.url)
ulx.url1url_main.host = uri1.host
serviceName(url_main, args.nolink)
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.url1url_main.service == "wayback" then
date = "index"
elseif date and ulx.url1url_main.service == "wayback" and verifydates == "yes" then
local ldf = dateFormat(date)
if ldf then
Riga 595 ⟶ 552:
end
end
elseif date and ulx.url1url_main.service == "webcite" and verifydates == "yes" then
local ldf = dateFormat(date)
if ldf then
Riga 604 ⟶ 561:
end
end
elseif not date and ulx.url1url_main.service == "wayback" then
date = decodeWaybackDate( uri1.path, "iso" )
if not date then
date = inlineRed("[Date error] (1)", "error")
end
elseif not date and ulx.url1url_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
ulx.url1url_main.date = date
 
-- Format argument
local format = trimArg(args.format) or trimArg(args.formato) or "none"
if not format ~= "none" then
format = "none"
else
if format == "addlpages" then
if not ulx.url1url_main.date then
format = "none"
end
elseif format =~= "addlarchives" then
format = "addlarchives"
else
format = "none"
end
end
ulx.url1url_main.format = format
 
-- Title argument
local url_main.title = trimArg(args.title) or trimArg(args.title1) or trimArg(args.titolo) or trimArg(args.titolo1)
local ulx = parseExtraArgs(args, maxurls)
ulx.url1.title = title
--if true then return mw.text.jsonEncode(ulx, mw.text.JSON_PRETTY) end
 
local rend = createRendering(url_main, ulx)
if not rend then
rend = mw.ustring.format('<span style="font-size:100%" class="error citation-comment">Errori in [[:Template:' .. tname .. '%s]]: Problema sconosciuto. Si prega di segnalarlo nella [[Discussioni_template:' .. tname .. '%s|pagina di discussione]] del template.</span>', tname, tname)
track["Category:Webarchive template errors"] = 1
end