Module:WebsiteCitation
From BIONICLEsector01
Documentation for this module may be created at Module:WebsiteCitation/doc
local p = {}
-- The text to place at the end of a WebsiteCitation to indicate
-- that a link in the citation is archived
local ARCHIVE_TEXT = "archived on"
-- The text to place at the end of a WebsiteCitation to indicate
-- that a link in the citation is broken
local BROKEN_TEXT = "broken link"
-- Return a name to uniquely identify the citation.
-- Assumes that mainUrl is not nil.
local function getRefName(mainUrl)
-- This function used to return
-- mw.uri.encode(mainUrl),
-- but at some point (perhaps with the upgrade to MW 1.42.1?)
-- the "reference tooltips" in MediaWiki:Gadget-ReferenceTooltips.js
-- could no longer handle ref names containing percent encoded text.
-- Extension:Cite lets us set the ref name to the original url with no encoding,
-- and the reference tooltips can handle that naming scheme,
-- so encoding actually isn't necessary!
return mainUrl
end
-- Return the main citation link.
-- Assumes that mainUrl and mainLinkText are not nil.
local function getMainLink(mainUrl, mainLinkText)
return "[" .. mainUrl .. " " .. mainLinkText .. "]"
end
-- Return a notice at the end of the citation indicating that the citation
-- features a link to an archived resource.
-- Assumes that at least one of originalUrl or archiveUrl is not nil,
-- and that a non-nil archiveUrl is a valid URL that mw.uri.new can parse.
local function getArchiveNotice(originalUrl, archiveUrl)
if archiveUrl == nil then
return ""
end
local archiveMessage = ARCHIVE_TEXT .. " " .. mw.uri.new(archiveUrl)["host"]
if originalUrl ~= nil then
-- since originalUrl is not nil, archiveUrl was not used as the main URL
-- and so should be linked here
archiveMessage = "[" .. archiveUrl .. " " .. archiveMessage .. "]"
end
return " " .. mw.getCurrentFrame():expandTemplate{
title = "C",
args = {archiveMessage}
}
end
-- Return a notice at the end of the citation indicating that the citation
-- features a broken link.
local function getBrokenNotice()
return " " .. mw.getCurrentFrame():expandTemplate{
title = "C",
args = {BROKEN_TEXT}
}
end
-- Return a notice to be placed at the end of the citation that indicates what
-- type of links are included in the citation.
-- Assumes that at least one of originalUrl, archiveUrl, and brokenUrl is not
-- nil; note that brokenUrl is not passed into the function.
-- Assumes that a non-nil archiveUrl is a valid URL that mw.uri.new can parse.
local function getNotice(originalUrl, archiveUrl)
if originalUrl ~= nil or archiveUrl ~= nil then
return getArchiveNotice(originalUrl, archiveUrl)
end
-- if originalUrl and archiveUrl are both nil, then the link must be broken
return getBrokenNotice()
end
-- Build and return the desired citation.
-- Asserts that websiteTitle and mainLinkText are not nil,
-- that at least one of originalUrl, archiveUrl, and brokenUrl is not nil.
function p.getWebsiteCitation(websiteTitle, mainLinkText, originalUrl, archiveUrl, brokenUrl)
assert(websiteTitle ~= nil)
assert(mainLinkText ~= nil)
assert(originalUrl ~= nil or archiveUrl ~= nil or brokenUrl ~= nil)
local mainUrl = originalUrl or archiveUrl or brokenUrl
return mw.getCurrentFrame():extensionTag{
name = "ref",
content = getMainLink(mainUrl, mainLinkText) .. ". " ..
websiteTitle .. "." ..
getNotice(originalUrl, archiveUrl),
args = { name = getRefName(mainUrl) }
}
end
-- Pull out the relevant args for use.
function p.websiteCitation(frame)
local websiteTitle = frame.args["websitetitle"]
local pageTitle = frame.args["pagetitle"]
local originalUrl = frame.args["originalurl"]
if originalUrl == "" then
originalUrl = nil
end
local archiveUrl = frame.args["archiveurl"]
if archiveUrl == "" then
archiveUrl = nil
end
local brokenUrl = frame.args["brokenurl"]
if brokenUrl == "" then
brokenUrl = nil
end
assert(websiteTitle ~= nil,
[[WebsiteCitation received no website title;
all WebsiteCitations must have a website title specified
through the `websitetitle` parameter]])
assert(pageTitle ~= nil,
[[WebsiteCitation received no page title;
all WebsiteCitations must have a page title specified
through the `pagetitle` parameter]])
assert(originalUrl ~= nil or archiveUrl ~= nil or brokenUrl ~= nil,
[[WebsiteCitation received no page URL;
all WebsiteCitations must have a page URL specified through
at least one of the following parameters:
`originalurl`, `archiveurl`, or `brokenurl`]])
local mainLinkText = "\"" .. pageTitle .. "\""
return p.getWebsiteCitation(websiteTitle, mainLinkText, originalUrl, archiveUrl, brokenUrl)
end
return p