Jump to content

Module:Freeze

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by PleaseStand (talk | contribs) at 23:13, 6 February 2024 (fall back to current title if none provided to samePageLink). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = {}

function p.main(frame)
	return p._main(frame, frame:getParent().args)
end

function p._main(frame, args)
	local curTitle = mw.title.getCurrentTitle()
	local page = mw.text.trim(args[1] or '')
	local text = args[2] or ''

	if page == '' or string.sub(page, 1, 1) == '#' then
		page = curTitle.subjectPageTitle.prefixedText .. page
	end

	if text == '' then
		text = page .. ' (as of now)'
	end

	local title = mw.title.new(page)
	if mw.title.equals(title, curTitle) then
		local pageId = frame:callParserFunction('PAGEID', title.prefixedText)
		local revTimestamp = frame:callParserFunction('REVISIONTIMESTAMP', title.prefixedText)
		if mw.isSubsting() then
			return string.format('{{freeze/same page link|main|1=%s|2=%s|3=%s|4=%s}}',
				title.fullText, pageId, revTimestamp, text)
		else
			return p._samePageLink{title.fullText, pageId, revTimestamp, text}
		end
	else
		local revId = frame:callParserFunction('REVISIONID', title.prefixedText)
		if revId == '' then
			return string.format('<strong class="error">Unable to get latest revision ID for "%s".</strong>', title.prefixedText)
		end
		if mw.isSubsting() then
			return string.format('{{oldid|1=%s|2=%s|3=%s}}<!-- Template:Freeze -->', title.fullText, revId, text)
		else
			return frame:expandTemplate{title='oldid', args={title.fullText, revId, text}}
		end
	end
end

function p.samePageLink(frame)
	return p._samePageLink(frame:getParent().args)
end

function p._samePageLink(args)
	local page = args[1] or ''
	local pageId = args[2] or ''
	local revTimestamp = args[3] or ''
	local text = args[4] or ''

	local title = page ~= '' and mw.title.new(page) or mw.title.getCurrentTitle()
	title.fragment = ''
	
	local queryParts = {'action=history'}
	if pageId ~= '' then
		table.insert(queryParts, 'curid=' .. pageId)
	end
	if revTimestamp ~= '' then
		table.insert(queryParts, 'offset=' .. revTimestamp)
	end
	table.insert(queryParts, 'limit=2')

	local url = title:fullUrl(table.concat(queryParts, '&'))
	return string.format('<span class="plainlinks">[%s %s]</span>', url, text)
end

return p