Module:ArchiveAccess

From SW420
Revision as of 10:53, 21 August 2022 by imported>Plume Tray
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
Module documentation follows
Note: the module above may sometimes be partially or fully invisible.
Visit Module:ArchiveAccess/doc to edit this documentation.

Usage instructions

{{#invoke:ArchiveAccess|main|nobackup=|nobackup_text=|template_name=|use_lua_subpage=|target=|archivedate=|archiveurl=|archivefile=|full_url=|no_archive=|par=|space=|text=|nolive=|nolive_text=|citation_type=|font_size=}}

This module is used in the following template(s):


-- ArchiveAccess implements rendering for web page archive links.
local p = {}

-- 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

local NS_MODULE = 828

-- isCurrentPageMainSpaceOrFile determines whether the page being parsed is a mainspace or file page.
local function isCurrentPageMainSpaceOrFile()
	local title = mw.title.getCurrentTitle()

	return title.namespace == NS_MAIN or title.namespace == NS_FILE or title.namespace == NS_MODULE
end

-- getArchiveTrackingCategories returns tracking categories based on whether the explicit archive date used for this invocation
-- matches the implicit archive date sourced from the /Archive subpage of the template on behalf of which this module is invoked.
-- This is generally only useful if said /Archive subpage exists.
local function getArchiveTrackingCategories(explicitArchiveDate, knownArchiveDate, templateName)
	if not isCurrentPageMainSpaceOrFile() then
		return ''
	end

	-- User-provided explicit archive date parameter matches the implicit archive date from template data.
	if knownArchiveDate == explicitArchiveDate then
		return '[[Category:' .. templateName .. ' usages with the same archivedate value]]'
	end

	-- User-provided explicit archive date parameter does not match the implicit archive date from template data.
	if knownArchiveDate then
		return '[[Category:' .. templateName .. ' usages with custom archivedate]]'
	end

	return '[[Category:' .. templateName .. ' usages with archived URLs not in Archive]]'
end

-- getMissingPermalinkTrackingCategory returns the tracking category to be used for the current owning template
-- if this invocation contained an archival link without a permalink timestamp.
-- It prefers to use a template-specific tracking category if one exists and a generic one otherwise.
local function getMissingPermalinkTrackingCategory(citationType, templateName)
	if not isCurrentPageMainSpaceOrFile() then
		return ''
	end

	local templateSpecificCategory = citationType .. ' with missing permanent archival links/' .. templateName

	-- Use the template-specific tracking category if possible
	if mw.title.makeTitle('Category', templateSpecificCategory).exists then
		return '[[Category:' .. templateSpecificCategory .. ']]'
	end

	return '[[Category:' .. citationType .. ' with missing permanent archival links]]'
end

-- getBackupLink generates a Wayback machine archive URL based on invocation parameters
-- with appropriate tracking categories appended.
local function getBackupLink(args)
	-- No backup link is available, so return a customizable disclaimer
	if args.nobackup then
		return (args.nobackup_text or 'content obsolete and backup link not available')
	end

	-- If the original link is now inaccessible, render a customizable disclaimer
	local obsoleteDisclaimer = args.nolive and (args.nolive_text or 'content now obsolete; ') or ''

	local archiveFile = args.archivefile
	if archiveFile then
		return obsoleteDisclaimer .. '[[:' .. archiveFile .. '|screenshot]]'
	end

	local archiveUrl = args.archiveurl
	local noArchive = args.no_archive
	local archiveLinkText = args.text or 'backup link'
	local use_lua_subpage = args.use_lua_subpage

	-- Full Wayback URL given, so return it with appropriate tracking categories
	if archiveUrl then
		local trackingCategories = ''
		if mw.ustring.find(archiveUrl, 'web.archive.org/web') and isCurrentPageMainSpaceOrFile() then
			trackingCategories = '[[Category:Archiveurl usages with Wayback URLs]]'
		end
		return obsoleteDisclaimer .. '[' .. archiveUrl .. ' ' .. archiveLinkText .. ']' .. trackingCategories
	end

	-- Determine the Wayback URL to use based on the archive date and full_url params, if given.
	local templateName = args.template_name or ''
	local fullUrl = args.full_url or ''
	local explicitArchiveDate = args.archivedate
	local target = args.target
	local knownArchiveDate

	-- If the 'target' parameter was forwarded to this invocation, attempt fetch known archive dates from a Lua module subpage
	if target and not noArchive then
		if use_lua_subpage then
			local templateData = require('Module:ArchiveAccess/' .. templateName)
			target = string.lower(target)
			knownArchiveDate = templateData.knownArchiveDates[target]
		end
	end

	-- If an explicit archive date was given, use it to point to the Wayback snapshot at that given time
	-- and append appropriate tracking categories.
	if explicitArchiveDate then
		local trackingCategories = not noArchive and getArchiveTrackingCategories(explicitArchiveDate, knownArchiveDate, templateName) or ''
		return obsoleteDisclaimer .. '[https://web.archive.org/web/' .. explicitArchiveDate .. '/' .. fullUrl .. ' ' .. archiveLinkText .. ']' .. trackingCategories
	end

	-- No explicit archive date was given, so use the archive date from the archive subpage of the owning template if possible
	if not noArchive and knownArchiveDate then
		return obsoleteDisclaimer .. '[https://web.archive.org/web/' .. knownArchiveDate .. '/' .. fullUrl .. ' ' .. archiveLinkText .. ']'
	end

	-- If optional param is given, then ignore tracking cateogry
	if args.optional then
		return ''
	else
		-- Neither an explicit nor implicit archive date is available, so point to the latest Wayback snapshot with a disclaimer
		local citationType = args.citation_type or ''
		local notVerifiedDisclaimer = ' [[Template:' .. templateName .. '|<span style="color: red">\'\'\'not verified!\'\'\'</span>]]'
		local trackingCategory = getMissingPermalinkTrackingCategory(citationType, templateName)
		return obsoleteDisclaimer .. '[https://web.archive.org/web/' .. fullUrl .. ' ' .. archiveLinkText .. ']' .. notVerifiedDisclaimer .. trackingCategory
	end
end

-- render outputs a full Wayback link wrapped in a container.
function p.render(args)
	local isSmallFont = args.font_size == 'small'
	local wrapInParentheses = args.par
	local useSpaceSeparator = args.space
	
	if not args.nobackup and not args.archivefile and not args.archiveurl and not args.archivedate and args.optional then
		return ''
	else
		return mw.ustring.format(
			'%s%s%s%s%s',
			isSmallFont and '<small>' or '',
			wrapInParentheses and '&#32;(' or (useSpaceSeparator and '&#32;' or ''),
			getBackupLink(args),
			wrapInParentheses and ')' or '',
			isSmallFont and '</small>' or ''
		)
	end
end

local getArgs = require('Module:Arguments').getArgs
function p.main(frame)
	local args = getArgs(frame)
	return p.render(args)
end

return p