Module:User: Difference between revisions

From Test Wiki
Jump to navigation Jump to search
Content deleted Content added
Amos (talk | contribs)
mNo edit summary
Ahecht (talk | contribs)
add 3, 4, 6, 7, and 8
Line 4: Line 4:
-- template's performance by reducing the number of parameters called from
-- template's performance by reducing the number of parameters called from
-- wikitext, while still allowing all the features provided by
-- wikitext, while still allowing all the features provided by
-- [[Module:Userlinks]]. It is about twice as fast as the version of {{user}}
-- [[Module:UserLinks]]. It is about twice as fast as the version of {{user}}
-- that called the {{user-multi}} template from wikitext.
-- that called the {{user-multi}} template from wikitext.
--]=]
--]=]


local mUserLinks = require('Module:Userlinks')
local mUserLinks = require('Module:UserLinks')
local mShared = require('Module:Userlinks/shared')
local mShared = require('Module:UserLinks/shared')
local yesno = require('Module:Yesno')
local yesno = require('Module:Yesno')


Line 28: Line 28:
end
end


function p.main(frame)
function p.main(frame, opts)
-- Grab the user, project and lang args from wikitext.
-- Grab the user, project and lang args from wikitext.
local argKeys = {
local argKeys = {
Line 47: Line 47:
}
}
}
}
local origArgs = frame:getParent().args
local origArgs = require('Module:Arguments').getArgs(frame)
local args = {}
local args = {}
for argKey, t in pairs(argKeys) do
for argKey, t in pairs(argKeys) do
Line 71: Line 71:
-- Input the codes directly. This saves two argument lookups for each
-- Input the codes directly. This saves two argument lookups for each
-- invocation.
-- invocation.
local codes = {'t', 'c'}
local codes = type(opts) and opts or {'t', 'c'}
-- Generate mop icon for formeradmin
-- Plug the data into [[Module:Userlinks]].
local mop = (origArgs.mop and yesno(origArgs.mop)) and require('Module:Icon')._main({'mop'}) or ''
-- Plug the data into [[Module:UserLinks]].
local snippets = mUserLinks.getSnippets(args)
local snippets = mUserLinks.getSnippets(args)
local links = mUserLinks.getLinks(snippets)
local links = mUserLinks.getLinks(snippets)
local success, result = pcall(mUserLinks.export, codes, links, options)
local success, result = pcall(mUserLinks.export, codes, links, options)
if success then
if success then
return result
return mop .. result
else
else
return mShared.makeWikitextError(result, options.isDemo)
return mShared.makeWikitextError(result, options.isDemo)
end
end
end
end


function p.admin(frame)
return p.main(frame, {'t','c','bls','pr','del','m','rl','rfa'})
end

function p.eight(frame)
return p.main(frame, {'t', 'c', 'ca', 'ct', 'e'})
end

function p.five(frame)
return p.main(frame, {'t', 'c', 'dc', 'm', 'bu', 'bl'})
end

function p.four(frame)
return p.main(frame, {'t', 'c', 'e'})
end

function p.formeradmin(frame)
return p.main(frame, {'t','c','fa','bls','pr','del','rl','mr','lr'})
end

function p.seven(frame)
return p.main(frame, {'t', 'c', 'ct', 'l', 'e'})
end

function p.six(frame)
return p.main(frame, {'t', 'c', 'ct', 'l', 'm', 'bl'})
end

function p.three(frame)
return p.main(frame, {'t', 'c', 'l'})
end

function p.two(frame)
return p.main(frame, {'t', 'c', 'ct'})
end

p[''] = p.main
p['2'] = p.two
p['3'] = p.three
p['4'] = p.four
p['5'] = p.five
p['6'] = p.six
p['7'] = p.seven
p['8'] = p.eight
p['former admin'] = p.formeradmin


return p
return p

Revision as of 00:20, 28 August 2024

Documentation for this module may be created at Module:User/doc

--[=[
-- This module implements {{user}}. {{user}} is a high-use template, sometimes
-- with thousands of transclusions on a page. This module optimises the
-- template's performance by reducing the number of parameters called from
-- wikitext, while still allowing all the features provided by
-- [[Module:UserLinks]]. It is about twice as fast as the version of {{user}}
-- that called the {{user-multi}} template from wikitext.
--]=]

local mUserLinks = require('Module:UserLinks')
local mShared = require('Module:UserLinks/shared')
local yesno = require('Module:Yesno')

local p = {}

local function validateArg(arg)
	-- Validates one argument. Whitespace is stripped, and blank arguments
	-- are treated as nil.
	if not arg then
		return nil
	end
	arg = arg:match('^%s*(.-)%s*$')
	if arg ~= '' then
		return arg
	else
		return nil
	end
end

function p.main(frame, opts)
	-- Grab the user, project and lang args from wikitext.
	local argKeys = {
		user = {
			1,
			'User',
			'user'
		},
		project = {
			2,
			'Project',
			'project'
		},
		lang = {
			3,
			'Lang',
			'lang'
		}
	}
	local origArgs = require('Module:Arguments').getArgs(frame)
	local args = {}
	for argKey, t in pairs(argKeys) do
		for i, origArgKey in ipairs(t) do
			local value = origArgs[origArgKey]
			value = validateArg(value)
			if value then
				args[argKey] = value
				-- If we have found a value, break the loop. For the average
				-- invocation this saves two argument lookups.
				break
			end
		end
	end
	
	-- Generate options. Some of these need wikitext args also.
	local options = {
		span = false,
		separator = validateArg(origArgs.separator) or 'dot',
		isDemo = yesno(validateArg(origArgs.demo))
	}
	
	-- Input the codes directly. This saves two argument lookups for each
	-- invocation.
	local codes = type(opts) and opts or {'t', 'c'}
	
	-- Generate mop icon for formeradmin
	local mop = (origArgs.mop and yesno(origArgs.mop)) and require('Module:Icon')._main({'mop'}) or ''
	
	-- Plug the data into [[Module:UserLinks]].
	local snippets = mUserLinks.getSnippets(args)
	local links = mUserLinks.getLinks(snippets)
	local success, result = pcall(mUserLinks.export, codes, links, options)
	if success then
		return mop .. result
	else
		return mShared.makeWikitextError(result, options.isDemo)
	end
end


function p.admin(frame)
	return p.main(frame, {'t','c','bls','pr','del','m','rl','rfa'})
end

function p.eight(frame)
	return p.main(frame, {'t', 'c', 'ca', 'ct', 'e'})
end

function p.five(frame)
	return p.main(frame, {'t', 'c', 'dc', 'm', 'bu', 'bl'})
end

function p.four(frame)
	return p.main(frame, {'t', 'c', 'e'})
end

function p.formeradmin(frame)
	return p.main(frame, {'t','c','fa','bls','pr','del','rl','mr','lr'})
end

function p.seven(frame)
	return p.main(frame, {'t', 'c', 'ct', 'l', 'e'})
end

function p.six(frame)
	return p.main(frame, {'t', 'c', 'ct', 'l', 'm', 'bl'})
end

function p.three(frame)
	return p.main(frame, {'t', 'c', 'l'})
end

function p.two(frame)
	return p.main(frame, {'t', 'c', 'ct'})
end

p[''] = p.main
p['2'] = p.two
p['3'] = p.three
p['4'] = p.four
p['5'] = p.five
p['6'] = p.six
p['7'] = p.seven
p['8'] = p.eight
p['former admin'] = p.formeradmin

return p