Jump to content

Module:Rewrite

From the Vrienden Universe, a fictional wiki

Documentation for this module may be created at Module:Rewrite/doc

local p = {}

local function trim(value)
	if value == nil then
		return nil
	end
	value = mw.text.trim(value)
	if value == '' then
		return nil
	end
	return value
end

local function format_marked_date(language, value)
	local success, result = pcall(language.formatDate, language, 'j F Y', value)
	if success then
		return result
	end
	return value
end

local function is_older_than_one_week(language, value)
	local marked_success, marked = pcall(language.formatDate, language, 'U', value)
	if not marked_success then
		return false
	end

	local current = tonumber(language:formatDate('U'))
	marked = tonumber(marked)
	return current ~= nil and marked ~= nil and current - marked > 7 * 24 * 60 * 60
end

function p.main(frame)
	local args = frame:getParent().args
	local language = mw.getContentLanguage()
	local reason = trim(args.reason)

	if mw.isSubsting() then
		local invocation_args = {
			date = language:formatDate('Y-m-d', nil, true)
		}
		if reason then
			invocation_args.reason = reason
		end
		return require('Module:Template invocation').invocation('Rewrite', invocation_args)
	end

	local marked_date = trim(args.date)
	local overdue = marked_date and is_older_than_one_week(language, marked_date)
	local text

	if overdue then
		text = "'''This page has been marked for rewrite for more than one week.''' It was marked on "
			.. format_marked_date(language, marked_date)
			.. '. It may be out of date or no longer follow current wiki standards.'
	else
		text = "'''This page is marked for rewrite.''' It may be out of date or no longer follow current wiki standards."
		if marked_date then
			text = text .. ' It was marked on ' .. format_marked_date(language, marked_date) .. '.'
		else
			text = text .. ' The marking date was not recorded.'
		end
	end

	text = text .. ' See [[Vrienden Universe:Pages marked for rewrite|pages marked for rewrite]] for guidance.'
	if reason then
		text = text .. "<br>'''Reason:''' " .. reason
	end

	local box_class = overdue and 'vu-rewrite-box vu-rewrite-box--overdue' or 'vu-rewrite-box'
	local output = frame:extensionTag {
		name = 'templatestyles',
		args = { src = 'Rewrite/styles.css' }
	} .. '<div class="'
		.. box_class
		.. '" role="note">'
		.. text
		.. '</div>'

	if mw.title.getCurrentTitle().namespace == 0 then
		output = output .. '[[Category:Pages marked for rewrite]]'
	end
	return output
end

return p