Jump to content

Module:Mass notification: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
change the output format to work inline, and to hide group member links
allow specifying a link to the group's page from the group submodule
Line 39: Line 39:


-- Load the group submodule and check for errors.
-- Load the group submodule and check for errors.
local usernames
local data
do
do
local success
local success
success, usernames = pcall(mw.loadData, groupSubmodule)
success, data = pcall(mw.loadData, groupSubmodule)
if not success then
if not success then
return makeWikitextError(message(LOAD_ERROR, groupSubmodule, groupName))
return makeWikitextError(message(LOAD_ERROR, groupSubmodule, groupName))
elseif type(usernames) ~= 'table' or not usernames[1] then -- # doesn't work with mw.loadData
elseif type(data) ~= 'table' or not data[1] then -- # doesn't work with mw.loadData
return makeWikitextError(message(NO_USER_ERROR, groupName))
return makeWikitextError(message(NO_USER_ERROR, groupName))
elseif usernames[MAX_USERS + 1] then -- # doesn't work with mw.loadData
elseif data[MAX_USERS + 1] then -- # doesn't work with mw.loadData
return makeWikitextError(message(MAX_USER_ERROR, groupName))
return makeWikitextError(message(MAX_USER_ERROR, groupName))
end
end
Line 59: Line 59:
{action = 'edit'}
{action = 'edit'}
))
))
local groupLink
introBlurb = message(INTRO_BLURB, groupName, optOutUrl)
if data.link then
groupLink = string.format('[[%s|%s]]', data.link, groupLink)
else
groupLink = groupName
end
introBlurb = message(INTRO_BLURB, groupLink, optOutUrl)
end
end


Line 67: Line 73:
local userNamespace = mw.site.namespaces[2].name
local userNamespace = mw.site.namespaces[2].name
local links = {}
local links = {}
for i, username in ipairs(usernames) do
for i, username in ipairs(data) do
username = tostring(username)
username = tostring(username)
links[i] = string.format(
links[i] = string.format(

Revision as of 23:26, 1 July 2014

-- 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 all members of $1'
	.. ' <small>([[Template:Mass notification|more info]]'
	.. " '''·''' "
	.. '<span class="plainlinks">[$2 opt out]</span>)</small>: '

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 data
	do
		local success
		success, data = pcall(mw.loadData, groupSubmodule)
		if not success then
			return makeWikitextError(message(LOAD_ERROR, groupSubmodule, groupName))
		elseif type(data) ~= 'table' or not data[1] then -- # doesn't work with mw.loadData
			return makeWikitextError(message(NO_USER_ERROR, groupName))
		elseif data[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'}
		))
		local groupLink
		if data.link then
			groupLink = string.format('[[%s|%s]]', data.link, groupLink)
		else
			groupLink = groupName
		end
		introBlurb = message(INTRO_BLURB, groupLink, optOutUrl)
	end

	-- Make the user links.
	local userLinks
	do
		local userNamespace = mw.site.namespaces[2].name
		local links = {}
		for i, username in ipairs(data) do
			username = tostring(username)
			links[i] = string.format(
				'[[%s:%s]]',
				userNamespace,
				username
			)
		end
		userLinks = string.format(
			'<span style="display: none;">(%s)</span>',
			table.concat(links, ', ')
		)
	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