Module:workgroup ping

Hello, you have come here looking for the meaning of the word Module:workgroup ping. In DICTIOUS you will not only get to know all the dictionary meanings for the word Module:workgroup ping, but we will also tell you about its etymology, its characteristics and you will know how to say Module:workgroup ping in singular and plural. Everything you need to know about the word Module:workgroup ping you have here. The definition of the word Module:workgroup ping will help you to be more precise and correct when speaking or writing your texts. Knowing the definition ofModule:workgroup ping, as well as those of other words, enriches your vocabulary and provides you with more and better linguistic resources.

This module is in beta stage.
Its interface has been stabilised, but the module may still contain errors. Do not deploy widely until the module has been tested.

This module implements {{wgping}}, which enables one to quickly and conveniently notify users interested in a particular topic of a discussion. See documentation for {{wgping}} for details.

For workgroup membership data, see Module:workgroup ping/data.


local concat = table.concat
local get_template_invocation_name = require("Module:template parser").getTemplateInvocationName
local insert = table.insert
local is_preview = require("Module:pages").is_preview

local export = {}

local m_data = mw.loadData('Module:workgroup ping/data')

function export.ping(frame)
	local parent_frame = frame:getParent()
	local args = parent_frame.args
	local users, errors = {}, {}
	local had_ind = false
	
	local function gather_users(myself)
		local had_user = { }
		if myself and not args.fix then
			had_user = true
		end

		local empty = true		
		for _, wgid in ipairs(args) do
			local _, user = wgid:match("^User:(.*)")
			if user then
				insert(users, user)
				had_user = true
				had_ind = true
			else
				local data = m_data
				if data then
					if data then
						for i, user in ipairs(data) do
							if not had_user then
								insert(users, user)
							end
						end
					else
						insert(errors, ('%s=empty'):format(wgid))
					end
				else
					insert(errors, ('%s=404'):format(wgid))
				end
			end
			empty = false
		end
		
		if empty then
			insert(errors, "nogroups")
		end
	end
	
	local title = mw.title.getCurrentTitle()
	local invocation = get_template_invocation_name(mw.title.new(parent_frame:getTitle()))
	if mw.isSubsting() then
		local output = { '{{', invocation }
		
		local my_username = frame:callParserFunction("REVISIONUSER", title.fullText)
		gather_users(my_username)
		
		for _, wgid in ipairs(args) do
			insert(output, '|' .. wgid)
		end

		for i, user in ipairs(users) do
			insert(output, ('|u%u=%s'):format(i, user))
		end
		
		-- ]
		if #users > 50 then
			insert(errors, "limit")
		end

		for i, err in ipairs(errors) do
			insert(output, ('|e%u=%s'):format(i, err))
		end
		
		insert(output, '}}')
		return concat(output)
	end

	if parent_frame:getTitle() == title.fullText then
		return '<small>(Notifying ]): </small>'	
	end

	local output = { '<small>(Notifying ' }
	local i

	-- the |u#= and |e#= parameters are for internal use only.
	i = 1
	while args do
		insert(errors, args)
		i = i + 1	
	end

	i = 1
	while args do
		insert(users, args)
		i = i + 1	
	end

	if (#users + #errors) == 0 then
		if is_preview() then
			local unfmted = {}
			for _, item in ipairs(args) do
				insert(unfmted, '|' .. item)
				-- XXX: more detailed?
			end

			return '<strong class="error">Error: <code>{{' .. invocation .. '}}</code> must be ]! ' ..
				'Use <code>{{subst:' .. invocation .. concat(unfmted) .. '}}</code> instead</strong>'
		else
			insert(output, "]")
			insert(errors, "subst")
			gather_users()
		end
	end

	for i, user in ipairs(users) do
		insert(output, ('%s]'):format(
			(i == 1) and "" or ", ",
			user, user
		))
	end

	local function explain_error(e)
		if e == "subst" then
			return "template was not substituted; please substitute passing |fix=1"
		elseif e == "nogroups" then
			return "no workgroups specified"
		else
			local g, e = e:match("(.-)=(.*)")
			if e == "404" then
				return "group '" .. g .. "' does not exist"
			elseif e == "empty" then
				return "group '" .. g .. "' is empty"
			elseif e == "limit" then
				return "more than 50 users to notify: notification will not work"
			end
		end
		return "unknown error '" .. e .. "'"
	end

	if #errors > 0 then
		if is_preview() then
			insert(output, '; <strong class="error">errors: ')
			for i, e in ipairs(errors) do
				insert(output, ((i == 1) and "" or ", ") .. explain_error(e))
			end
			insert(output, '</strong>')
		else
			insert(output, '; <span style="border-bottom: 1px dotted red; color: red; font-weight: bold;" title="')
			for i, e in ipairs(errors) do
				insert(output, ((i == 1) and "" or ", ") .. explain_error(e))
			end
			insert(output, '">errors</span>')
		end
	end

	insert(output, '): </small>')
	return concat(output)
end

function export.show_list(frame)
	local categories_list = {}
	local categories_map = {}
	local wg_codes, wg_aliases = {}, {}
	
	for key, data in pairs(m_data) do
		if type(data) == 'string' then
			if not wg_aliases then
				wg_aliases = {}	
			end
			insert(wg_aliases, ("<code>%s</code>"):format(key))
		else
			local categ = data.category or ""
			if not categories_map then
				local newcat = { desc = categ, groups = {} }
				insert(categories_list, newcat)
				categories_map = newcat
			end
			categ = categories_map
			wg_codes = key
			
			insert(categ.groups, data)
		end
	end

	table.sort(categories_list, function (apple, orange)
		return apple.desc < orange.desc
	end)
	
	local output = {
		'{| class="wikitable" style="width: 90%;" \n|-\n! style="width: 8em;" | Shortcut(s) \n! Group name \n! Members'
	}
	for _, categ in pairs(categories_list) do
		table.sort(categ.groups, function (apple, orange)
			return apple.desc < orange.desc
		end)

		if categ.desc ~= "" then
			insert(output, '|-\n! colspan="3" style="font-size: smaller; text-align: left;" | ' .. categ.desc)
		end
		for _, wgdata in pairs(categ.groups) do
			local members = {}
			local had_users, had_tfs = {}, {}
			
			if type(wgdata) ~= 'table' then
				return	
			end
			
			for _, user in ipairs(wgdata) do
				if not had_users then
					insert(members, ("]"):format(user, user))
					had_users = true
				end
			end

			local wg_code = ("<code>%s</code>"):format(wg_codes)
			if wg_aliases] then
				wg_code = ('%s<br/><small>(%s)</small>'):format(wg_code, concat(wg_aliases], ", "))
			end
			insert(output, ('|- id="%s" style="vertical-align: top;" \n|%s\n|%s\n|%s <small>(%u %s)</small>'):format(
				wg_codes, wg_code, wgdata.desc, concat(members, ", "), #members, ((#members == 1) and 'user' or 'users')
			))
		end
	end
	insert(output, "|}")
	return concat(output, "\n")
end

return export