Jump to content

Module:Mass notification

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 13:09, 1 July 2014 (better separator for the intro blurb links). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

-- This module sends out notifications to multiple users.

local mList = require('Module:List')

local MAX_USERS = math.huge -- Not sure if there is a limit on notifications, so building it in now.
local GROUP_PAGE_PATH = 'Module:Mass notification/groups/'
local NO_NAME_ERROR = 'no group name was specified'
local LOAD_ERROR = 'the group "[[$1|$2]]" was not found'
local MAX_USER_ERROR = 'attempted to send notifications to more than $1 users'
local NO_USER_ERROR = 'could not find any usernames in $1'
local INTRO_BLURB = 'Notifying $1 <small>([[Template:Mass notification|more info]]'
	.. " '''·''' "
	.. '<span class="plainlinks">[$2 opt out]</span>)</small>:\n'
local LINK_LIST_STYLE = 'font-size: smaller'

local p = {}

local function message(msg, ...)
	return mw.message.newRawMessage(msg):params{...}:plain()
end

local function makeWikitextError(msg)
	return string.format(
		'<strong class="error">Error: %s.</strong>',
		msg
	)
end

local function makeUserLinks(t)
end

function p._main(groupName)
	-- Validate input.
	if type(groupName) ~= 'string' then
		return makeWikitextError(NO_NAME_ERROR)
	end

	local groupSubmodule = GROUP_PAGE_PATH .. groupName

	-- Load the group submodule and check for errors.
	local usernames
	do
		local success
		success, usernames = pcall(mw.loadData, groupSubmodule)
		if not success then
			return makeWikitextError(message(LOAD_ERROR, groupSubmodule, groupName))
		elseif type(usernames) ~= 'table' or not usernames[1] then -- # doesn't work with mw.loadData
			return makeWikitextError(message(NO_USER_ERROR, groupName))
		elseif usernames[MAX_USERS + 1] then -- # doesn't work with mw.loadData
			return makeWikitextError(message(MAX_USER_ERROR, groupName))
		end
	end

	-- Make the intro blurb.
	local introBlurb
	do
		local optOutUrl = tostring(mw.uri.fullUrl(
			groupSubmodule,
			{action = 'edit'}
		))
		introBlurb = message(INTRO_BLURB, groupName, optOutUrl)
	end

	-- Make the user links.
	local userLinks
	do
		local userNamespace = mw.site.namespaces[2].name
		local listArgs = {}
		for i, username in ipairs(usernames) do
			username = tostring(username)
			listArgs[i] = string.format(
				'[[%s:%s|%s]]',
				userNamespace,
				username,
				username
			)
		end
		listArgs.style = LINK_LIST_STYLE
		userLinks = mList.makeList('horizontal', listArgs)
	end

	return introBlurb .. userLinks
end

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		wrappers = 'Template:Mass notification'
	})
	local groupName = args[1]
	return p._main(groupName)
end

return p