Jump to content

Module:Discussion ping

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Awesome Aasim (talk | contribs) at 12:59, 20 September 2024. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

--- This module extracts all the users in either a marked LST or in wikitext and
-- generates links reflecting a courtesy ping.
-- 
-- @module Courtesy ping
-- @require Module:Arguments
-- @release alpha

local p = {}
local getArgs = require("Module:Arguments").getArgs

--- Makes a user link
-- @param {table} userTitle the title for the user object
-- @return {string} the formatted user link in the form of a ping
function makeUserLink( userTitle )
	return mw.ustring.format( '@[[%s|%s]]', userTitle.prefixedText, userTitle.text )
end

--- Entry point for module
-- @param {table} frame processing frame
-- @return {string} wikitext
function p.main( frame )
	local args = getArgs( frame )
	return p._main( args, frame )
end

function p._main( args, frame )
	frame = frame or mw.getCurrentFrame()
	if not mw.isSubsting() and not args['__demo'] then
		error( "[[Module:Courtesy ping]] must be substituted" )
	end
	local links = {}
	local wikitext = args['wikitext'] or args[1] or nil
	local lstCallText = args['title'] or nil
	if lstCallText then
		wikitext = frame:callParserFunction('#lsth', { lstCallText })
	elseif not wikitext then
		error( "Wikitext or a page title with section must be specified!" )
	end

	local wikitextArray = mw.text.split( wikitext, '' )
	local inWikiLink = false
	local inPipedLink = false
	local trimmed = false
	local toProcess = ''
	for k,v in ipairs( wikitextArray ) do
		if k ~= 1 then
			if inWikiLink then
				toProcess = toProcess .. v
				if wikitextArray[ k ] == '|' then
					toProcess = mw.ustring.sub( toProcess, 0,
						mw.ustring.len( toProcess ) - 1 )
					trimmed = true
					inPipedLink = true
					inWikiLink = false
				end
			end
			if not inWikiLink and not inPipedLink then
				trimmed = false
				if wikitextArray[ k ] == '[' and wikitextArray[ k - 1 ] == '['
					then
					toProcess = ''
					inWikiLink = true
				end
			else
				if wikitextArray[ k ] == ']' and wikitextArray[ k - 1 ] == ']'
					then
					if not trimmed then
						toProcess = mw.ustring.sub( toProcess, 0,
							mw.ustring.len( toProcess ) - 2 )
						trimmed = true
					end
					table.insert( links, toProcess )
					toProcess = ''
					inWikiLink = false
					inPipedLink = false
				end
			end
		end
	end
	
	mw.logObject( links )
	
	local out = ''
	
	for k,v in pairs(links) do
		local title = mw.title.new( v )
		mw.logObject( title )
		if title.namespace == 2 and not title.isSubpage then
			out = out .. ' ' .. makeUserLink( title )
		end
	end
	
	return '<!-- Begin [[Module:Courtesy ping]] --> ' .. mw.text.trim( out )
		.. ' <!-- End [[Module:Courtesy ping]] -->'	
end

return p