Jump to content

Module:Contentious topics talk banner

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by HouseBlaster (talk | contribs) at 00:39, 28 May 2025 (save a WIP). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {}
local TableTools = require('Module:TableTools')
local yesno = require('Module:Yesno')
-- Maps normalized topic names to sub-template names (special cases)
local topicMap = {
    aa2 = 'Contentious topics/Armenia-Azerbaijan talk notice',
    ['a-a'] = 'Contentious topics/Armenia-Azerbaijan talk notice',
    ['a-i'] = 'Contentious topics/Arab-Israeli talk notice',
    gmo = 'Contentious topics/gmo talk notice',
    tt = 'Contentious topics/The Troubles talk notice'
}

local function collectTopics(args)
    local seen = {}
    local topics = {}

    local function add(value)
        if value then
            value = mw.text.trim(value)
            if value ~= '' and not seen[value] then
                seen[value] = true
                table.insert(topics, value)
            end
        end
    end

    -- Primary topic params
    add(args.topic)
    add(args.t)
    add(args[1])

    -- Additional topics via numbered forms
    -- Asking people to choose the top ten CTOP designations is reasonable
    for i = 2, 10 do
        add(args[i])
        add(args['t' .. i])
        add(args['topic' .. i])
    end

    return topics
end

local function makeBulletPoint(code, frame)
	-- given a CTOP code, outputs a bullet point explaining the scope of the contentious topic designation
	return '* <b>' .. frame:expandTemplate{ title = "Contentious topics/list", args = { scope=code } } .. '</b>\n'
end


function p.main(frame)
    local args = require('Module:Arguments').getArgs(frame)
    local topics = collectTopics(args)
	local multipleTopics = TableTools.length(topics) == 1
	local restrictionsDatabase = mw.loadJsonData("Template:Contentious topics/Additional restrictions.json")
	local restrictions = {}
	local currentTitleObject = mw.title.getCurrentTitle()
	local subjectTitleObject = currentTitleObject.subjectPageTitle
	local messageBody
	
	--== Generate the active restrictions==--
	-- helper function which add restrictions if active
	local function addRestriction(restriction)
		if yesno(args[restriction]) then
			table.insert(restrictions, restriction)
		end
	end
	
	-- add the always available restrictions, if applicable
	addRestriction("0RR")
	addRestriction("1RR")
	addRestriction("BRD")
	addRestriction("consensus-required")
	
	-- then check each topic...
    for _, topic in ipairs(topics) do
    	local currentTopicWideRestrictions = restrictionsDatabase["topic-wide"][topic]
    	--... add anything applicable to the entire topic...
        if currentTopicWideRestrictions then
        	TableTools.merge(currentTopicWideRestrictions, restrictions)
        end
        --...and add anything available to that topic in particular
        local availableTopicRestrictions = restrictionsDatabase["additional-available"][topic]
        if availableTopicRestrictions then
    		for __, availableRestriction in ipairs(availableTopicRestrictions) do
        		addRestriction(availableRestriction)
        	end
	    end
    end
    
    -- Finally, we automatically detect the protection level of the subject page
    local protectionLevel = subjectTitleObject["protectionLevels"]["edit"]
    if protectionLevel then
    	table.insert(restrictions, protectionLevel)
    end
	--== End generation of active restrictions ==--
	
	-- is this page under any restrictions?
	local underRestrictions = #restrictions == 0
	
	-- gets either "article" or "page", as the case may be
	local articleOrPage = frame:exapndTemplate{ template = 'Talkspace detect' , args = { main="article", default="page", other="page" } }

	if underRestrictions then
		-- we have some active restrictions
		messageBody = '<strong style="text-transform: uppercase;">Warning: active arbitration remedies</strong><p>The [[Wikipedia:Contentious topics|contentious topics]] procedure applies to this '
		.. articleOrPage .. '.' .. (yesno(args.section) and (' Parts of this ' .. articleOrPage .. ' relate ') or (' This ' .. articleOrPage .. ' relates '))
		if multipleTopics then
			messageBody = messageBody  .. 'to the following contentious topics:</p>\n'
			for _, topic in ipairs(topics) do
				messageBody = messageBody .. makeBulletPoint(topic, frame)
			end
		else
			messageBody = messageBody .. 'to <b>' ..
			frame:expandTemplate{ title = "Contentious topics/list", args = { scope=topics[1] } } ..
			'</b>, a contentious topic.'
		end
			
	else
		
	end
	
	local messageBox = require('Module:Message box').main( tmbox , 
		{
			["type"] = underRestrictions and "delete" or "content",
			["small"] = yesno(args.small),
			["image"] = "[[File:Commons-emblem-" 
				.. underRestrictions and "hand" or "issue" 
				.. ".svg|" .. 
				yesno(args.small) and "30" or "40"
				.. "px]]",
			["text"] = messageBody
		}
	)
	
	return messageBox
	
end
--[[
    -- Render special topics individually
    for _, topic in ipairs(specialTopics) do
        table.insert(output, frame:expandTemplate{
            title = 'Template wrapper',
            args = {['_template'] = topicMap[topic]}
        })
    end
    
    -- Render all other topics combined using the base template
    if #otherTopics > 0 then
        local baseArgs = {
            _template = 'Contentious topics/page restriction talk notice base',
            topic = table.concat(otherTopics, ','),
            protection = args.protection,
            ['0RR'] = args['0RR'],
            ['1RR'] = args['1RR'],
            ['consensus-required'] = args['consensus-required'],
            BRD = args.BRD,
            other = args.other,
            ['placed-date'] = args['placed-date'],
            nocat = args.nocat,
            small = args.small,
        }
        table.insert(output, frame:expandTemplate{
            title = 'Template wrapper',
            args = baseArgs
        })
    end

    return table.concat(output, '\n')
end
--]]

return p