Module:Forumheader

Revision as of 19:21, 6 January 2025 by Artoire (talk | contribs) (Don't add categories to preload page Template:Forumheader/Forum Grove)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

--[=[
Implements {{Forumheader}}, displaying a header and adding relevant categories.

Categories added by this module:
> [[Category:Forum Grove]]
> [[Category:Active threads]]
> [[Category:Forums excluded from the main page]]
> [[Category:Forum archives/$type]]
> [[Category:Forum archives with invalid type parameter]]
> [[Category:Forum archives with missing type parameter]]
> [[Category:Forum archives by date closed]]
> [[Category:Forum archives by month/$year]]
> [[Category:Forum archives with missing date parameter]]
> [[Category:Forum archives with missing name parameter]]
> [[Category:Forum archives with missing subject parameter]]
> [[Category:Forum archives with invalid closure parameter]]
> [[Category:Forum archives with missing closure parameter]]
]=]

require('strict')
local lang = mw.getContentLanguage()
local yesno = require('Module:Yesno')

local p = {}

-- Types of threads -> name of category
local types = {
	community = 'Community',
	content = 'Content',
	discussion = 'Discussion',
	miscellaneous = 'Miscellaneous',
	policy = 'Policy',
	technical = 'Technical',
	['user-related'] = 'User-related',
	meta = 'Meta',
}

-- Valid arguments for the "closure" parameter
local closures = {
	'allow', 'disallow', 'no consensus', 'withdrawn', 'no proposal',
	'no formal closure', 'partial allow and partial disallow',
}


-- Category handler
local function categories(_args, title)
	local cats = {}
	local args = {}

	-- make blank params behave like missing params
	for k, v in pairs(_args) do
		if v ~= '' then
			args[k] = v
		end
	end

	-- forum grove
	table.insert(cats, '[[Category:Forum Grove|' .. title .. ']]')

	-- option to hide the forum on the main page
	if yesno(args.mainpage) == false and not yesno(args.archive) then
		table.insert(cats, '[[Category:Forums excluded from the main page]]')
	end

	-- active/closed threads
	if yesno(args.archive) then
		-- handle type
		if args.type then
			local catType = types[args.type]

			if catType then
				table.insert(cats, '[[Category:Forum archives/' .. catType .. '|'.. title .. ']]')
			else
				table.insert(cats, '[[Category:Forum archives with invalid type parameter]]')
			end
		else
			table.insert(cats, '[[Category:Forum archives with missing type parameter]]')
		end

		-- date closed
		if args.date then
			local date_str = lang:formatDate('!c', args.date)
			table.insert(cats, '[[Category:Forum archives by date closed|*' .. dateStr .. ']]')

			local year = lang:formatDate('!Y', args.date)
			local month = tonumber(lang:formatDate('!n', args.date))

			table.insert(cats, ('[[Category:Forum archives by month/%s|%x%s]]'):format(year, month, dateStr))
		else
			table.insert(cats, '[[Category:Forum archives with missing date parameter]]')
		end

		-- other args
		if not args.user then
			-- @todo Should we do anything here?
		end

		if not args.name then
			table.insert(cats, '[[Category:Forum archives with missing name parameter]]')
		end

		if not args.subject then
			table.insert(cats, '[[Category:Forum archives with missing subject parameter]]')
		end

		if args.closure then
			local closure

			for _, v in pairs(closures) do
				if v == args.closure then
					closure = v
					break
				end
			end

			if not closure then
				table.insert(cats, '[[Category:Forum archives with invalid closure parameter]]')
			end
		else
			table.insert(cats, '[[Category:Forum archives with missing closure parameter]]')
		end
	else
		table.insert(cats, '[[Category:Active threads|'.. title .. ']]')
	end

	return table.concat(cats, '')
end

function p.main(frame)
	local args = frame:getParent().args
	local title = mw.title.getCurrentTitle().text

	local result = mw.html.create('div')
		:addClass('forumheader')
		:tag('strong'):wikitext('Forums:'):done()
		:wikitext(' [[Forum:Forum Grove|Forum Grove]] > ')
		:tag('strong'):wikitext(title):done()
	:done()

	-- Add [[Template:Archive]] if this is an archive
	-- @todo convert {{Archive}} into a module
	if yesno(args.archive) then
		result:node(frame:expandTemplate{ title = 'Archive', args = { date = args.date or '', user = args.user or '' } })
	end

	-- Don't add categories to preload page [[Template:Forumheader/Forum Grove]]
	if title ~= 'Forumheader/Forum Grove' then
		result:wikitext(categories(args, title))
	end

	return result
end

return p