Documentation for this module may be created at Module:Refs/doc

p = {}

local getArgs = require('Dev:Arguments').getArgs
local userError = require('Dev:User error')
local foot = require "Module:Foot"


local errorcatname = "Articles with incomplete citations"

function p.refwebsitebroken(frame)
    return p.refwebsite(frame, { broken = true })
end

function p.refwebsite(frame, flags)
    
    local args = getArgs(frame)
    if not flags then flags = {} end

    local pageurl = args["page url"]
    local siteurl = args["site url"]

    local archiveurl = args["archive url"]

    local pagename = args["page name"] or "?"
    local sitename = args["site name"] or "?"

    local accesstime = args["access time"]
    
    local screenshot = args["screenshot"]

    pagename = mw.text.nowiki(pagename)
    sitename = mw.text.nowiki(sitename)

    if not accesstime and pageurl then
        --accesstime may be in a web archive link
        stastr, endstr, accesstime = pageurl:find("archive.org/web/(%d%d%d%d%d%d%d%d%d%d)/")
        if accesstime then pageurl = pageurl:sub(endstr+1) end
    end

    local pagelink
    local sitelink
    
    if pageurl then
        if flags.broken then
            pagelink = pagename .. " ''(dead)''"
        else
            pagelink = "[" .. pageurl .. " " .. pagename .. "]"
        end
    else
        pagelink = pagename .. userError("Missing page URL", errorcatname)
    end

    if siteurl then
        sitelink = "[" .. siteurl .. " " .. sitename .. "]"
    elseif flags.broken then
        sitelink = sitename .. " ''(dead)''"
    else
        sitelink = sitename .. userError("Missing site URL", errorcatname)
    end
    
    local lang = mw.language.getContentLanguage()
    
    local accessdate = lang:formatDate("F d, Y", accesstime)
    local accesstimestamp = lang:formatDate("YmdHis", accesstime)
    
    local accesslink = ""
    local accesstext = "Accessed: " .. (accesstime and accessdate or "Unknown")
        
    local retrievedfromtext = "Retrieved from " .. (pageurl or "Unknown")

    if screenshot then
        if screenshot:sub(1, 4) == "http" then
            if screenshot:find("finalfantasy/images") then
                
                accesslink = tostring(foot.render({
                    text = "[" .. screenshot .. " " .. accesstext .. "]",
                    tip = retrievedfromtext
                }))
                if not accesstime then 
                    accesslink = accesslink .. userError("Missing date screenshot taken in <code>access time</code> field", errorcatname)
                end
            else
                accesslink = userError("Screenshot must be hosted on site", errorcatname)
            end
        else
            local image = require "Module:Image"
            local sshotdata = image.new(screenshot)
            sshotdata:setSize(200, 150):setCaption(retrievedfromtext)

            local accessfoottext = "[[:" .. sshotdata:getPagename() .. "|" .. accesstext .. "]]"
            if not accesstime then
                accessfoottext = accessfoottext .. userError("Missing date screenshot taken in <code>access time</code> field", errorcatname)
            end
            
            accesslink = tostring(foot.render({
                text = accessfoottext,
                tip = tostring(sshotdata)
            }))
        end
    elseif pageurl then
        if not archiveurl then
            flags.autoarchiveurl = true
            archiveurl = "https://web.archive.org/web/" .. accesstimestamp .. "/" .. pageurl
        end
        
        if flags.broken then
            accesstext = tostring(foot.render({
                text = accesstext,
                tip = retrievedfromtext
            }))
        end
        
        if accesstime then
            accesslink = "[" .. archiveurl .. " " .. accesstext .. "]"
        elseif not flags.autoarchiveurl then
            accesslink = "[" .. archiveurl .. " " .. accesstext .. "]" .. userError("Missing <code>access time</code> field", errorcatname)
        elseif flags.broken then
            accesslink = accesstext .. userError("Missing <code>access time</code> field")
        else
            accesslink = "[https://web.archive.org/save/" .. pageurl .. " " .. accesstext .. "]" .. userError("See [[Project:Verifiability#Website|this]] for how to archive", errorcatname)
        end
    end
    
    return pagelink .. " <small>(" .. accesslink .. ")</small> at " .. sitelink
end

return p
Community content is available under CC-BY-SA unless otherwise noted.