Module:Contentious topics/talk notice: Difference between revisions

Content deleted Content added
accept more date formats
sync with sandbox which contains better section support, better support for date formatting, checking for unknown parameters, code refactoring, and other improved functionality
Line 1:
require('strict')
local p = {}
local TableTools = require('Module:TableTools')
local yesno = require('Module:Yesno')
local setToList = require('Module:Set to list')
local checkForUnknownParameters = require('Module:Check for unknown parameters')._check
local restrictionsDatabase = mw.loadJsonData("Template:Contentious topics/Additional restrictions.json")
local restrictionsDefinition = mw.loadJsonData("Template:Contentious topics/Restrictions definition.json")
Line 8 ⟶ 10:
local categoryDatabase = mw.loadJsonData("Template:Contentious topics/Category database.json")
 
local function collectTopics(args, sectionParameter)
local seen = {}
local topicscompleteTopics = {}
local partialTopics = {}
 
local i = 2 -- initialize index
local keepGoing = true -- whether to keep checking for more CTOPs
local function add(value)
if value then
value = mw.text.trim(value)
if value ~= '' and not seen[value] then
local applicableSection = seenargs[value .. '-section'] =or truesectionParameter
if applicableSection table.insert(topics, value)then
partialTopics[value] = applicableSection
else
table.insert(completeTopics, value)
end
seen[value] = true
keepGoing = true
end
end
Line 23 ⟶ 34:
 
-- Primary topic params
add(args.topic[1])
add(args.t)
add(args[1].t1)
add(args.topic)
 
add(args.topic1)
-- Additional topics via numbered forms
forwhile i = 2, 10keepGoing do
keepGoing = false -- this is set back to true if any of the below are found
add(args[i])
add(args[i])
add(args['t' .. i])
add(args['topic' .. i])
i = i + 1 -- increment the index
end
 
return topicscompleteTopics, partialTopics
end
 
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame)
local sectionParameter = yesno(args.section, args.section) or yesno(args.relatedcontent, args.relatedcontent) -- whether we are sections all the way down
local topics = collectTopics(args)
local completeTopics, partialTopics = collectTopics(args, sectionParameter) -- completeTopics is a sequence. partialTopics is a table with topics as keys and scope as values
local restrictions = {} -- A list of which restrictions are enabled for easier iteration
local restrictionFlags = {} -- Track which restrictions are enabled, as a set
local currentTitleObject = mw.title.getCurrentTitle()
local subjectTitleObject = currentTitleObject.subjectPageTitle
local section = sectionParameter or #partialTopics > 0 -- whether any topics apply to parts of the article
local underRestrictions -- a boolean for whether there are any active restrictions
local articleOrPage -- the string "article" (if a mainspace pagearticle) or "page" (if not)
local protectionLevel -- the edit protection level
local numberOfCompleteTopics = #completeTopics -- the number of complete topics
local numberOfPartialTopics = TableTools.size(partialTopics) -- the number of partial topics (not a sequence, so we have to use TableTools.size)
local numberOfTopics = numberOfCompleteTopics + numberOfPartialTopics -- total number of topics
local messageBody -- the text within the message box
local messageBox -- the message box itself
local unknownParameterCheck -- the result of [[Module:Check for unknown parameters]]
local unknownParameterTable -- the table to pass to the [[Module:Check for unknown parameters]] call
local categories = '' -- initialize categories
local manualRestrictions = false -- have we set any restrictions via |parameters?
local section = yesno(args.section) or yesno(args.relatedcontent) -- whether we are a section
--[[
Line 84 ⟶ 105:
 
-- Topic-based restrictions
for _, topic in ipairs (topicscompleteTopics) do
local topicWide = restrictionsDatabase["topic-wide"][topic]
if topicWide then
for _, restriction in ipairs(topicWide) do
Line 91 ⟶ 112:
end
end
local additional = restrictionsDatabase["additional-available"][topic]
 
if additional then
for _, restriction in ipairs(additional) do
maybeAddRestriction(restriction)
end
end
end
for topic, scope in pairs(partialTopics) do
local additional = restrictionsDatabase["additional-available"][topic]
if additional then
Line 98 ⟶ 127:
end
end
local always = restrictionsDatabase["topic-wide"][topic]
if always then
for _, restriction in ipairs(always) do
maybeAddRestriction(restriction)
end
end
end
Line 142 ⟶ 177:
-- Check whether any of the added restrictions are enabled
underRestrictions = #restrictions > 0 or args.other or args.other1
-- Determines whether we should use the string "article" or "page"
local articleOrPage = currentTitleObject:inNamespaces(1) and "article" or "page"
 
local function addToMessage(s)
messageBody = messageBody .. s
end
local function getTopicBlurb(code)
return frame:expandTemplate{
title = "Contentious topics/list",
args = { scope = code }
}
end
-- Makes a bullet point for a given contentious topic
-- the scope is either a string representing the exact scope of the topic
local function makeTopicBulletPoint(code)
-- and is nil if it applies to the entire page or unspecified parts of the page
topicBlurb = frame:expandTemplate{ title = "Contentious topics/list", args = { scope=code } }
local function makeTopicBulletPoint(code, scope)
local topicBlurb = getTopicBlurb(code)
if topicBlurb == '' then
addCategory(categoryDatabase['bad-topic'])
returnelseif ''scope then
-- scope is not nil, so we write that into the bullet point
else
addToMessage('* <b>' .. topicBlurb return.. '*</b>, specifically the parts about <b>' .. topicBlurbscope .. '</b>\n')
else
-- scope is nil, so we have nothing to add
addToMessage('* <b>' .. topicBlurb .. '</b>\n')
end
end
-- Makes a restriction bullet point
local function makeRestrictionBulletPoint(code)
Line 165 ⟶ 216:
--[[
Error categories
Place into an error category if not given a contentious topic code
--]]
if #topics == 0 then
-- No contentious topic codes
if numberOfTopics == 0 then
addCategory(categoryDatabase['no-topic'])
end
Line 175 ⟶ 228:
--]]
messageBody = '<b>The [[Wikipedia:Contentious topics|contentious topics]] procedure applies to this ' .. articleOrPage .. '.</b>'
.. articleOrPage .. '.</b>'
-- if there's only one topic, we make a short blurb
.. (section and (' Parts of this ' .. articleOrPage .. ' relate ') or (' This ' .. articleOrPage .. ' relates '))
if numberOfTopics == 1 then
 
if #topics > 1section then
for topic, part in pairs(partialTopics) do
messageBody = messageBody .. 'to the following contentious topics:\n'
-- there's only one item, so this one runs once
for _, topic in ipairs(topics) do
addToMessage( ' Parts of this ' .. articleOrPage
messageBody = messageBody .. makeTopicBulletPoint(topic)
.. (yesno(part, false) and '' or (' about <b>' .. part .. '</b>'))
end
.. ' relate to <b>'
else
.. getTopicBlurb(topic)
local topicBlurb = frame:expandTemplate{
.. '</b>, a contentious topic.')
title = "Contentious topics/list",
end
args = { scope=topics[1] } }
else
if topicBlurb ~= '' then
messageBody addToMessage(' =This messageBody' .. articleOrPage .. ' relates to <b>'
.. getTopicBlurb(completeTopics[1])
.. topicBlurb
.. '</b>, a contentious topic.</p>'
)
else
end
addCategory(categoryDatabase['bad-topic'])
else
end
if numberOfCompleteTopics ~= 0 then
end
addToMessage('<p>The entire ' .. articleOrPage .. ' relates to ')
 
if numberOfCompleteTopics > 1 then
addToMessage('the following contentious topics:</p>\n')
for _, topic in ipairs(completeTopics) do
makeTopicBulletPoint(topic, nil)
end
else
addToMessage('<b>' .. getTopicBlurb(completeTopics[1]) .. '</b>, a contentious topic.</p>')
end
end
if numberOfPartialTopics ~= 0 then
addToMessage('<p>')
if numberOfCompleteTopics ~= 0 then
addToMessage('Additionally, parts ')
else
addToMessage('Parts ')
end
addToMessage('of this ' .. articleOrPage .. ' relate to ')
if numberOfPartialTopics > 1 then
addToMessage('the following contentious topics:\n')
for topic, scope in pairs(partialTopics) do
if yesno(scope, false) then
-- the scope parameter is something like 'yes', which we can treat as nil
makeTopicBulletPoint(topic, nil)
else
makeTopicBulletPoint(topic, scope)
end
end
else
-- There's only one topic and scope, so this loop only runs once
for topic, scope in pairs(partialTopics) do
addToMessage('<b>' .. getTopicBlurb(topic) .. '</b>')
if yesno(scope, nil) == nil then
-- the scope is not a boolean value, so we have a free-text explanation of the applicable parts
addToMessage(', in particular the parts about <b>' .. scope .. '</b>.')
else
addToMessage('.')
end
end
end
end
end
if underRestrictions then
messageBody = '<p style="margin-top:0"><strong style="text-transform: uppercase;">Warning: active arbitration remedies</strong></p>'
Line 202 ⟶ 297:
.. '<p style="text-decoration:underline; text-align:center; font-size:120%;">The following restrictions apply to everyone editing this ' .. articleOrPage .. ':</p>\n'
for _, restriction in ipairs(restrictions) do
messageBody = messageBody .. addToMessage(makeRestrictionBulletPoint(restriction))
addCategory(categoryDatabase[restriction])
end
if args.other or args.other1 then
messageBody-- =we messageBodyhave ..some '*form <b>'of .. args.other ..restrictions, so add the '</b>\n'category
addCategory(categoryDatabase["other"])
-- then define a helper function to add a restriction
local function addOther(s)
addToMessage('* <b>' .. s .. '</b>\n')
end
-- then add the generic 'other' parameter
if args.other then
addOther(args.other)
end
-- and now we loop to infinity and beyond
local i = 1
while true do
if args['other' .. i] then
addOther(args['other' .. i])
i = i + 1
else
break
end
end
end
end
messageBody = messageBody .. addToMessage(' Editors are advised to familiarise themselves with the [[Wikipedia:Contentious topics|contentious topics procedures]] before editing this page.')
if not yesno(args.brief) then
messageBody = messageBody .. addToMessage(' Editors who repeatedly or seriously fail to adhere to the [[WP:Five pillars|purpose of Wikipedia]], '
.. 'any expected [[WP:Etiquette|standards of behaviour]], '
.. 'or any [[WP:List of policies|normal editorial process]] may be blocked or restricted by an administrator.')
end
if section then
messageBody = messageBody .. addToMessage('<p>If it is unclear which parts of the page are related to this contentious topic, '
.. 'the content in question should be marked within the wiki text by an invisible comment. '
.. 'If no comment is present, please ask an administrator for assistance. If in doubt it is better to assume that the content is covered.</p>')
end
 
if underRestrictions then
if args['placed-date'] then
messageBody = messageBody .. addToMessage('<p>Restrictions placed: ' .. require('Module:Format time')._main{args['placed-date']} .. '</p>')
elseif manualRestrictions then
addCategory(categoryDatabase['no-date'])
end
end
--[[
Now build some further information collapse boxes
We get the content of each collapse box from Wikipedia:Contentious topics/[Topic]/Further information
--]]
-- Now build the messageBox
Line 258 ⟶ 367:
-- Hard code for [[WP:BER]]
if TableTools.inArray(topicscompleteTopics, "a-i") then
messageBox = messageBox .. "<p class='PIA-flag' style='display:none; visibility:hidden;'>This page is subject to the extended confirmed restriction related to the Arab-Israeli conflict.</p>"
addCategory("Wikipedia pages subject to the extended confirmed restriction related to the Arab-Israeli conflict")
Line 279 ⟶ 388:
end
-- checking for unknown parameters with extensible modules is annoying; the module is primarily intended for hardcoded stuff
return messageBox .. categories
-- luckily, it makes use of a table for its main settings, and we can do some clever stuff with that
-- helper to mark a parameter as known
local function addKnownParameter(s)
table.insert(unknownParameterTable, s)
end
-- initialize the table with the keys of restrictionsDefinition JSON
unknownParameterTable = TableTools.keysToList(restrictionsDefinition, false, true)
-- then some hardcoded parameters
addKnownParameter('aeprotection')
addKnownParameter('brief')
addKnownParameter('nocat')
addKnownParameter('placed-date')
addKnownParameter('protection')
addKnownParameter('relatedcontent')
addKnownParameter('section')
addKnownParameter('small')
-- then add all of the partialTopics section parameters
for code, _ in pairs(partialTopics) do
addKnownParameter(code .. '-section')
end
-- then all the various topic parameters. Table is no longer a sequence; perform any computation requring a sequence above
unknownParameterTable['regexp1'] = 'topic[%d]+'
unknownParameterTable['regexp2'] = 't[%d]+'
unknownParameterTable['regexp3'] = '[%d]+'
unknownParameterTable['regexp4'] = 'other[%d]*'
 
-- set the tracking category
unknownParameterTable['unknown'] = '[[Category:' .. categoryDatabase['unknown'] .. '|_VALUE_' .. currentTitleObject.text .. ']]'
-- an finally, call the unknownParameterCheck
unknownParameterCheck = checkForUnknownParameters(unknownParameterTable, args)
return messageBox .. categories .. unknownParameterCheck
end