Module:Wayback

local p = {}

--[[--< inline_error >---

Render red error message inline (as opposed to the system error in large font) Add article to tracking category.

]]

function p.inline_error(arg, msg) return ' Error in wayback template: Check  value. ' .. msg .. ' ' 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=

]]

function p.trimArg(arg) if arg == "" or arg == nil then return nil else return mw.text.trim(arg) end end function p.trimArg2(arg) if arg == nil then return nil else return mw.text.trim(arg) end end

--[[-- ---

Given df argument, return "yes" or "no" confirming what it is Default is "no". If df is nil or "", return "no".

]]

function isdf(df)

if df == "yes" or df == "Yes" or df == "YES" or df == "y" or df == "Y" or df == "dmy" or df == "DMY" then return "yes" elseif df == "iso" or df == "ISO" then return "iso" end return "no"

end

--[[-- ---

Given a year, month and day, convert into a full date while respecting df (dmy or mdy)

]]

function p.makedate(year, month, day, df)

local nmonth, zmonth, zday

if not year or year == "" or not month or month == "" or not day or day == "" then return nil end

zmonth = month month = month:match("0*(%d+)")                                     -- strip leading 0 if tonumber(month) < 1 or tonumber(month) > 12 then return year end nmonth = os.date("%B", os.time{year=2000, month=month, day=1} )    -- Month in name form if not nmonth then return year end

zday = day day = day:match("0*(%d+)") if tonumber(day) < 1 or tonumber(day) > 31 then return nmonth .. " " .. year end

if df == "yes" then return day .. " " .. nmonth .. " " .. year elseif df == "iso" then return year .. "-" .. zmonth .. "-" .. zday else return nmonth .. " " .. day .. ", " .. year end

end

--[[-- ---

Main function for Template:wayback

]]

function p.wayback(frame)

local pframe = frame:getParent local args = pframe.args

local tname = "Wayback"                                -- name of calling template. Change if template rename.

local url = nil                                        -- source url (not archive.org url) local title = nil                                      -- title argument local df = nil                                         -- df argument local comma = nil                                      -- "," for mdy or "" for dmy local snapdate = nil                                   -- eg. "20160520000000" local snapdatelong = nil                               -- 14-digit 0-padded version of snapdate if it is truncated local fulldate = nil                                   -- eg. "May 20, 2016" local currdate = nil                                   -- today's date local urlhead = "https://web.archive.org/web/" local tail = nil local nolink = nil -- URL argument (positional #1)

url = p.trimArg(args[1]) or p.trimArg(args.url) or p.trimArg(args.site)               -- "site" for if not url then return p.inline_error("url", "Empty.") end local safe = url local l, count = string.gsub(safe, "archive.org/?w?e?b?/[0-9]+/http", "")             -- Count number of "archive.org" if count > 0 then return p.inline_error("url", "Should be the original URL not an archive.org URL.") end local l, count = string.gsub(safe, "archive.org/?w?e?b?/http", "")                    -- Count number of "archive.org" if count > 0 then return p.inline_error("url", "Should be the original URL not an archive.org URL.") end -- Title argument (positional #2)

title = p.trimArg(args[2]) or p.trimArg(args.title)

-- Date argument (positional #3)

snapdate = p.trimArg(args[3]) or p.trimArg(args.date) if not snapdate or snapdate == "*" then snapdate = "*" else local safe = snapdate local starcount = 0 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, starcount = string.gsub(safe, "[*]$", "")                -- Remove trailing "*" and re-add below after processing if not tonumber(snapdate) then return p.inline_error("date", "Should be 14-digit snapshot ID in form YYYYMMDDhhmmss (error code 1)") end local dlen = string.len(snapdate) if dlen < 4 then return p.inline_error("date", "Should be 14-digit snapshot ID in form YYYYMMDDhhmmss (error code 2)") 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 p.inline_error("date", "Should be 14-digit snapshot ID in form YYYYMMDDhhmmss (error code 3)") end if tonumber(month) > 12 or tonumber(day) > 31 or tonumber(month) < 1 then return p.inline_error("date", "Date has invalid day or month. (error code 4)") end currdate = os.date("%Y") if tonumber(year) > tonumber(currdate) or tonumber(year) < 1900 then return p.inline_error("date", "Date has invalid year. (error code 5)") end

if starcount == 1 then snapdate = snapdate .. "*"   end

df = isdf( p.trimArg(args.df) ) if df == "yes" then comma = "" else comma = "," end

fulldate = p.makedate(year, month, day, df) if not fulldate then return p.inline_error("date", "(error code 6)") end

end -- Nolink argument

nolink = p.trimArg2(args.nolink) if not nolink then tail = " at the Wayback Machine" else tail = " at the Wayback Machine" end

-- Render

if not title and not fulldate then -- No title. No date return "[" .. urlhead .. snapdate .. "/" .. url .. " Archive copy]" .. tail elseif not title and fulldate then -- No title. Date. return "[" .. urlhead .. snapdate .. "/" .. url .. " Archived] " .. fulldate .. comma .. tail .. "." elseif title and not fulldate then  -- Title. No date. return "[" .. urlhead .. snapdate .. "/" .. url .. " " .. title .. "]" .. tail elseif title and fulldate then         -- Title. Date. return "[" .. urlhead .. snapdate .. "/" .. url .. " " .. title .. "]" .. tail .. "&#32;(archived " .. fulldate .. ")" end

error("Error in Template:"..tname..": Unknown problem. Please report on template talk page (code 7)")

end

return p