پودمان:Ping2

از ویکی‌پدیا، دانشنامهٔ آزاد
توضیحات پودمان[ایجاد] [پاکسازی]
local p = {}

function p.main(frame)
	local output = ''
	local pingees_hidden = {}
	local pingees_visible = {}
	local pframe = frame:getParent()
	
	-- process all usernames
	for i, arg in pairs( pframe.args ) do
		if(arg ~= '') then -- (ignore empty usernames)
			if p.isPingeable(arg) == 1 then
				table.insert(pingees_visible, p.pingUser(arg))
			else -- user created [[Special:MyPage/noping]], to not appear visibly in @ping
				table.insert(pingees_hidden, p.pingUser(arg))
			end
		end
	end
	if (next(pingees_hidden)==nil and next(pingees_visible)==nil) then
		output = "'''<strong class='error'>Error: No username specified!</strong>'''"
	else
		-- fill in the hidden pingees span
		if(#pingees_hidden > 0) then
			mw.log('Invisible pingees are present')
			output = output .. '<span style="display:none;">'
			output = output .. table.concat(pingees_hidden, " ")
			output = output .. '</span>'
		end
		-- fill in the visible pingees, comma separated
		if(#pingees_visible > 0) then
			output = output .. '@'
			output = output .. table.concat(pingees_visible, ", ")
			output = output .. ':'
		end
	end
	return output
end

-- checks whether user created [[Special:MyPage/noping]], to not appear visibly in @ping
function p.isPingeable(username)
	if (string.match(username, "=")) then
		return nil
	end
	local success, pageObject = pcall(mw.title.new, 'User:' .. username .. '/noping')
	if not success or not pageObject then
		return nil
	else
		if pageObject.id == 0 then
			return 1
		else
			return 0
		end
	end
end

-- returns a ping
function p.pingUser(username)
	string.gsub(username, "&amp;", "&")
	local foo = '[[User:' .. username .. '|' .. username .. ']]'
	return foo
	--if p.isPingeable(username) == 0 then
	    --pingtext = '<span style="display:none;>' .. pingtext .. '</span>'
   -- --else
    --	--pingtext = pingtext .. ', '
--	end
--	return pingtext
end

return p