Module documentation follows |
---|
Note: the module above may sometimes be partially or fully invisible. |
Visit Module:WebCite/doc to edit this documentation. |
Usage instructions[]
{{#invoke:WebCite|main|url=|text=|author=|date=|format=|work=|publisher=|pages=|language=|quote=|archivedate=|archiveurl=|archivefile=|nobackup=|nolive=}}
This module is used in the following template(s):
- {{WebCite}}
-- This module implements [[Template:WebCite]].
local p = {}
-- Lazily load a mw.language object.
local lang
-- NS_MAIN holds the namespace number for the Main namespace.
local NS_MAIN = 0
-- NS_FILE holds the namespace number for the File namespace.
local NS_FILE = 6
--NS_INDEX holds the namespace number for the Index namespace.
local NS_INDEX = 120
-- NS_USER holds the namespace number for the User namespace. TESTING ONLY
--local NS_USER = 2
-- NS_MODULE holds the namespace number for the Module namespace. TESTING ONLY
--local NS_MODULE = 828
-- get the current page
local currentPageTitle = mw.title.getCurrentTitle()
-- get the current page's namespace
local namespace = currentPageTitle.namespace
-- Avoid rendering tracking categories outside of main or file namespaces
local isAllowedNamespace = (namespace == NS_MAIN or namespace == NS_FILE or namespace == NS_INDEX) and true
-- Formats all error messages and adds appropriate tracking category
local function makeWikitextError(msg, missing)
-- styling for the error message
local ret = '<span style="color: red;">[[Template:WebCite]] error: '..msg..'.</span>'
-- add appropriate tracking category
if isAllowedNamespace then
if missing then
ret = ret..'[[Category:Template usages with missing parameters]]'
else
ret = ret..'[[Category:Template usages with invalid parameter values]]'
end
end
return ret
end
-- Formats all dates including wookieepedia linking
local function makeDateLink(date)
lang = lang or mw.language.getContentLanguage()
local success, dateLink
if date and #date == 7 then
-- YYYY-MM
success, dateLink = pcall(
lang.formatDate, lang,
'[[F]], [[Y]]',
date .. '-01'
)
elseif date and #date == 4 then
-- YYYY
success, dateLink = pcall(
lang.formatDate, lang,
'[[Y]]',
date .. '-01-01'
)
else
-- YYYY-MM-DD
success, dateLink = pcall(
lang.formatDate, lang,
'[[F j]], [[Y]]',
date
)
end
return dateLink
end
function p._main(args)
-- Capture variables
local url = args.url
local text = args.text
if not text then
text = args.title
end
-- Validate input
if not url then
-- missing url parameter
return makeWikitextError("missing '''url''' parameter must be specified", true)
end
if not text then
-- missing title parameter
return makeWikitextError("missing '''text''' parameter must be specified", true)
end
-- Capture remaing variables
local author = args.author
local citeDate = args.date
local citeFormat = args.format
local work = args.work
local publisher = args.publisher
local pages = args.pages
local language = args.language
local quote = args.quote
local archiveDate = args.archivedate
local archiveUrl = args.archiveurl
local archiveFile = args.archivefile
local noBackup = args.nobackup
local noLive = args.nolive
local ret = {}
-- URL
-- check for any badly formatted urls
if not mw.ustring.find(url, 'http://') and not mw.ustring.find(url, 'https://') then
ret[#ret + 1] = makeWikitextError("badly formatted '''url''' parameter", false)
if isAllowedNamespace then
ret[#ret + 1] = '[[Category:Templates with invalid URL parameters]]'
return table.concat(ret)
end
end
-- build the left-most url/archive links
if url and text then
if archiveUrl then
-- use archiveUrl
ret[#ret + 1] = '['..archiveUrl..' '..text..']'
elseif archiveDate then
-- use archiveDate to build a Wayback url
ret[#ret + 1] = '[https://web.archive.org/web/'..archiveDate..'/'..url..' '..text..']'
elseif url then
-- if no archive links, then just use the url
ret[#ret + 1] = '['..url..' '..text..']'
end
end
-- Language
if language then
ret[#ret + 1] = ' ('..language..')'
end
-- Format or Pages
if citeFormat or pages then
-- opening (
ret[#ret + 1] = ' ('
-- Format
if citeFormat then
ret[#ret + 1] = citeFormat
end
-- add a space if both
if citeFormat and pages then
ret[#ret + 1] = ' '
end
-- Pages
if pages then
ret[#ret + 1] = pages
end
-- closing )
ret[#ret + 1] = ')'
end
-- Author
if author then
ret[#ret + 1] = ' by '..author
end
-- Publisher
if publisher then
ret[#ret + 1] = ', published by '..publisher
end
-- Work
if work then
ret[#ret + 1] = " on "..work
else
-- if work parameter hasn't been provided, we can create a link to the website's homepage automatically
-- grab the website's homepage - including http(s)://
local website = url:match('^(%w+://[^/]+)')
-- grab the website's domain name which we'll use for the website title
local domain = url:match('^%w+://([^/]+)')
-- create a link to the website's homepage
ret[#ret + 1] = " on ["..website..' '..domain.."]"
end
-- Date
if citeDate then
-- add a formatted data
ret[#ret + 1] = ' ('..makeDateLink(citeDate)..')'
-- check if date contains letters
-- we only want dates formatted as YYYY-MM-DD e.g. 2020-11-23)
if mw.ustring.find(citeDate, 'a') or mw.ustring.find(citeDate, 'e') or mw.ustring.find(citeDate, 'u') then
-- any dates containing the above letters, e.g. Jan 12, 2019
-- add the appropriate tracking category
if isAllowedNamespace then
ret[#ret + 1] = '[[Category:WebCite date argument usages with incorrect date formats]]'
end
end
end
-- Quote
if quote then
ret[#ret + 1] = ': "'..quote..'"'
end
-- Start of the right-most url/archive links
ret[#ret + 1] = ' <small>('
if noBackup then
-- noBackup i.e. when an archive link cannot be produced because the original content is no longer available
ret[#ret + 1] = 'content obsolete and backup link not available'
else
if noLive then
-- original content is no longer available (but archive link may exist)
ret[#ret + 1] = 'content now obsolete; '
end
if archiveFile then
ret[#ret + 1] = '[[:' .. archiveFile .. '|screenshot]]'
elseif archiveUrl or archiveDate then
-- add url link to output
ret[#ret + 1] = 'archived from ['..url..' the original]'
elseif mw.ustring.find(url, '//archive.org/') or mw.ustring.find(url, '.archive.org/') then
ret[#ret + 1] = 'archived on Archive.org'
else
-- format unverifie backup link message
local verifyBackupLink = '[[Template:WebCite|<span style="color: red;">\'\'\'not verified!\'\'\'</span>]]'
-- create a temporary Wayback link from the url
ret[#ret + 1] = '[https://web.archive.org/web/'..url..' backup link] '..verifyBackupLink
-- add a tracking category for the missing archival links
if isAllowedNamespace then
ret[#ret + 1] = '[[Category:Pages with missing permanent archival links]]'
end
end
-- Full Wayback URL given
if archiveUrl and mw.ustring.find(archiveUrl, 'web.archive.org/web') then
-- so return it with appropriate tracking categories
if isAllowedNamespace then
ret[#ret + 1] = '[[Category:Archiveurl usages with Wayback URLs]]'
end
-- grab the date value from the Wayback url
local waybackDate = archiveUrl:match('https://web.archive.org/web/(%d+)/http')
-- add Wayback date to output
ret[#ret + 1] = ' on '..makeDateLink(waybackDate)
elseif archiveDate then
-- add archiveDate to output
ret[#ret + 1] = ' on '..makeDateLink(archiveDate)
end
end
-- End of the right-most url/archive links
ret[#ret + 1] = ')</small>'
-- final output
return table.concat(ret)
end
-- this has slightly slower performance than the next function
--local getArgs = require('Module:Arguments').getArgs
--function p.main(frame)
-- local args = getArgs(frame)
-- return p._main(args)
--end
function p.main(frame)
local args = {}
for k, v in pairs(frame:getParent().args) do
v = v:match('^%s*(.-)%s*$') -- trim whitespace
if v ~= '' then
args[k] = v
end
end
return p._main(args)
end
return p