Module:Community Wishlist Survey/Proposal header

Permanently protected module
From Meta, a Wikimedia project coordination wiki
Module documentation
local p = {}

local currentTitle = mw.title.getCurrentTitle()
local titleParts = mw.text.split( currentTitle.fullText, '/', true )
local category = titleParts[ 2 ]
local surveyRoot = currentTitle.rootPageTitle.fullText
if string.sub( currentTitle.fullText, 0, 25 ) ~= 'Community Wishlist Survey' then
	surveyRoot = titleParts[ 1 ] .. '/' .. titleParts[ 2 ]
	category = titleParts[ 3 ]
end
local canonicalProposalPage
local proposalSubpage
local viewingCategoryPage
local phase

-- Get the localized title of the proposal
function p._getProposalTitle()
	return '{{TNT|' .. canonicalProposalPage.fullText .. '/Proposal|namespace=' ..
		canonicalProposalPage.namespace .. '|titleonly=yes|uselang={{int:lang}}}}'
end

function p.isViewingCategoryPage()
	return category ~= nil and currentTitle.fullText == surveyRoot .. '/' .. category
end

-- Get any phase-specific messaging. Shown next to the buttons
function p._getPhaseMessages()
	if viewingCategoryPage then
		return ''
	end
	if phase == 'closed' or phase == 'complete' then
		return '{{TNT|Community Wishlist Survey/Proposal header/Messages|uselang={{int:lang}}}}\n'
	end
	return ''
end

-- Level 2 heading
function p._getHeading( proposalTitle )
	local out = '\n=='
	if proposalSubpage.exists then
		out = out .. p._getProposalTitle( proposalTitle )
	else
		out = out .. proposalTitle
	end
	return out .. '==\n'
end

-- Support/Edit/Random proposal buttons
function p._getButtons()
	if viewingCategoryPage then
		mw.logObject(canonicalProposalPage.fullText)
		return '<div style="{{dir|{{int:lang}}|direction:rtl|}}" class="community-wishlist-proposal-header">' ..
			'{{Community Wishlist Survey/Proposal header/Support button|category=' .. category .. '|proposal=' .. canonicalProposalPage.fullText .. '}}' ..
			'{{Community Wishlist Survey/Proposal header/Edit button|proposal=' .. canonicalProposalPage.fullText .. '}}</div>'
	else
		return '<div style="{{dir|{{int:lang}}|direction:rtl|}}" class="community-wishlist-proposal-header">' ..
			'<span style="{{dir|{{int:lang}}|float:left|float:right}}">{{Community Wishlist Survey/Proposal header/Random button}}</span>' ..
			'{{Community Wishlist Survey/Proposal header/Support button|category=' .. category .. '|proposal=' .. canonicalProposalPage.fullText .. '}}' ..
			'{{Community Wishlist Survey/Proposal header/Back button|1=' .. surveyRoot .. '/' .. category .. '|2=' .. category .. '}}' ..
			p._getPhaseMessages() ..
			'</div>'
	end
end

-- Larger suggestions disclaimer, if applicable
function p._largerSuggestionsDisclaimer()
	if category ~= 'Larger suggestions' then
		return ''
	end
	if viewingCategoryPage then
		return '{{TNT|Community Wishlist Survey/Larger suggestions/Disclaimer|type=inline|uselang={{int:lang}}}}\n'
	end
	return '{{TNT|Community Wishlist Survey/Larger suggestions/Disclaimer|type=small|uselang={{int:lang}}}}\n'
end

function p._main( proposalTitle, userLang )
	canonicalProposalPage = mw.title.new( surveyRoot .. '/' .. category .. '/' .. proposalTitle )
	proposalSubpage = mw.title.new( canonicalProposalPage.text .. '/Proposal', canonicalProposalPage.namespace )
	viewingCategoryPage = p.isViewingCategoryPage()
	phase = mw.title.new( surveyRoot .. '/Phase' ):getContent()
	out = ''
	if viewingCategoryPage then
		out = p._getHeading( proposalTitle )
			.. p._largerSuggestionsDisclaimer()
			.. p._getButtons()
	else
		out = p._largerSuggestionsDisclaimer()
			.. p._getButtons()
			.. p._getHeading( proposalTitle )
	
		-- MediaWiki categorization
		if category ~= 'Archive' and canonicalProposalPage.namespace == 0 then
			out = out .. '[[Category:' .. surveyRoot .. '/Proposals|' .. category .. '/' .. proposalTitle .. ']]'
		end
	end

	-- Disable editing post-survey
	if phase == 'closed' or phase == 'complete' then
		out = out .. '\n__NOEDITSECTION__'
	end

	if proposalSubpage.exists then
		out = out .. '<div style="{{dir|{{int:lang}}|direction:rtl|}}" class="community-wishlist-proposal-header">'

		-- Translate link
		if userLang ~= 'en' and userLang ~= nil then
			out = out .. '<span class="translatelink plainlinks">{{int:parentheses|' ..
				'[https://meta.wikimedia.org/w/index.php?title=Special:Translate&group=page-' ..
				'{{urlencode:' .. canonicalProposalPage.fullText .. '/Proposal}}' ..
				'&action=page&language={{int:lang}} {{int:tpt-tab-translate}}]}}</span>'
		end

		-- Transclude proposal subpage, and add closing div
		out = out .. '{{dynamite|title=:' .. proposalSubpage.fullText .. '}}'
			.. '</div>'
	end

	return mw.getCurrentFrame():preprocess( out )
end

function p.main( frame )
	return p._main( frame.args[ 1 ], frame.args[ 2 ] )
end

return p