Module:Mass notification: Difference between revisions
Appearance
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 |
local data |
||
do |
do |
||
local success |
local success |
||
success, |
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( |
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 |
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 |
|||
⚫ | |||
if data.link then |
|||
groupLink = string.format('[[%s|%s]]', data.link, groupLink) |
|||
else |
|||
groupLink = groupName |
|||
end |
|||
⚫ | |||
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( |
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 implements the {{mass notification}} template. It is used to send notifications to a WikiProject or other group of users.
Usage from wikitext
Usually you should use {{mass notification}} rather than calling this module directly from #invoke. However, if you want you can use the syntax {{#invoke:Mass notification|main|group name}}
. See the template documentation for details about group names.
Usage from Lua modules
To use this module from other Lua modules, first load the module.
local mMassNotification = require('Module:Mass notification')
You can then generate the notification links by using the _main function.
mMassNotification._main(groupName)
groupName is the group name, as a string, as explained in the template documentation.
-- 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