Módulo:Wayback

De WikiINRICRISTOpedia
Ir para navegação Ir para pesquisar

<section begin=header />

<section end=header />

Descrição

Este Módulo implementa a predefinição {{Wayback}}. Por favor consulte a predefinição para mais instruções.

Uso

Outra documentação:


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 '<span style="font-size:100%" class="error citation-comment">Erro na predefinição wayback: Verifique <code style="color:inherit; border:inherit; padding:inherit;">&#124;' .. arg .. '=</code> value. ' .. msg .. '[[Categoria:!Páginas com erros na predefinição wayback]]</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=

 ]]

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

--[[--------------------------< isdf >-----------------------

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" or df == "s" or df == "Sim" or df == "SIM" then
    return "yes"
  elseif df == "iso" or df == "ISO" then
     return "iso"
  end
  return "no"

end

--[[--------------------------< makedate >-----------------------

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
  local month_names = {"janeiro", "fevereiro", "março", "abril", "maio", "junho", "julho", "agosto", "setembro", "outubro", "novembro", "dezembro"}

  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 = month_names[tonumber(month)]
--  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 .. " de " .. nmonth .. " de  " .. year
  elseif df == "iso" then
    return year .. "-" .. zmonth .. "-" .. zday
  else
    return day .. " de " .. nmonth .. " de  " .. year
  end
 

end


--[[--------------------------< wayback >-----------------------

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 {{waybackdate}}
  if not url then
    return p.inline_error("url", "Vazio.")
  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", "Deve ser o URL original não um URL archive.org.")
  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", "Devia ser snapshot ID de 14-dígitos na forma de YYYYMMDDhhmmss (código erro 1)")
    end
    local dlen = string.len(snapdate)
    if dlen < 4 then
      return p.inline_error("date", "Devia ser snapshot ID de 14-dígitos na forma de YYYYMMDDhhmmss (código erro 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", "Devia ser snapshot ID de 14-dígitos na forma de YYYYMMDDhhmmss (código erro  3)")
    end
    if tonumber(month) > 12 or tonumber(day) > 31 or tonumber(month) < 1 then
      return p.inline_error("date", "Valor da data contêm um dia inválido ou mês. (código erro 4)")
    end
    currdate = os.date("%Y")
    if tonumber(year) > tonumber(currdate) or tonumber(year) < 1900 then
      return p.inline_error("date", "Data com ano inválido. (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 = " no [[Wayback Machine]]"
  else
    tail = " no Wayback Machine"
  end

                                                          -- Render

  if not title and not fulldate then  -- No title. No date
    return "[" .. urlhead .. snapdate .. "/" .. url .. " Cópia arquivada]" .. tail
  elseif not title and fulldate then  -- No title. Date.
    return "[" .. urlhead .. snapdate .. "/" .. url .. " Arquivado em] " .. 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;(arquivado em " .. fulldate .. ")"
  end

  error("Error in [[:Predefinição:"..tname.."]]: Problema desconhecido. Por favor relate na página de discussão desta predefinição (código 7)")

end

return p