Moduuli:ArchiveAccess

Jedipediasta, vapaasta Tähtien sota-tietosanakirjasta tänään, 24. marraskuuta 2024
Siirry navigaatioonSiirry hakuun

Tämän moduulin ohjeistuksen voi tehdä sivulle Moduuli:ArchiveAccess/ohje

-- 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)
	return ''
	--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 '[[Luokka:' .. 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 '[[Luokka:' .. templateName .. ' usages with custom archivedate]]'
	--end

	--return '[[Luokka:' .. 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 '[[Luokka:' .. templateSpecificCategory .. ']]'
	--end

	return '[[Luokka:' .. citationType .. ', joissa on puuttuvia arkistolinkkejä]]'
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 .. '|kuvakaappaus]]'
	end

	local archiveUrl = args.archiveurl
	local noArchive = args.no_archive
	local archiveLinkText = args.text or 'linkki varmuuskopioon'
	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 = '[[Luokka:Archiveurl usages with Wayback URLs]]'
		end
		return obsoleteDisclaimer .. '[' .. archiveUrl .. ' ' .. archiveLinkText .. ']' .. trackingCategories
	end

	-- Determine the Wayback URL to use based on the archive date and archive value 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 rather than the archive_value parameter.
	if target and not noArchive then
		if use_lua_subpage then
			local templateData = require('Moduuli:ArchiveAccess/' .. templateName)
			knownArchiveDate = templateData.knownArchiveDates[target]
		else
			knownArchiveDate = args.archive_value
		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

	-- 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 'Sivut'
	--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 .. ']' .. trackingCategory
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

	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

--local getArgs = require('Moduuli:Arguments').getArgs
function p.main(frame)
	--local args = getArgs(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.render(args)
end

return p