Jump to content
 







Main menu
   


Navigation  



Main page
Contents
Current events
Random article
About Wikipedia
Contact us
Donate
 




Contribute  



Help
Learn to edit
Community portal
Recent changes
Upload file
 








Search  

































Create account

Log in
 









Create account
 Log in
 




Pages for logged out editors learn more  



Contributions
Talk
 



















Contents

   



(Top)
 


1 Technical details  














Module:User






العربية


Cebuano
Chi-Chewa
فارسی
Føroyskt


Jawa

Latviešu
ि

Bahasa Melayu



پښتو
Português
سرائیکی
Scots
Shqip

Simple English
کوردی
ி


اردو
Tiếng Vit

 

Edit links
 









Module
Talk
 

















Read
View source
View history
 








Tools
   


Actions  



Read
View source
View history
 




General  



What links here
Related changes
Upload file
Special pages
Permanent link
Page information
Get shortened URL
Download QR code
Wikidata item
 




Print/export  



Download as PDF
Printable version
 
















Appearance
   

 





Permanently protected module

From Wikipedia, the free encyclopedia
 


Technical details

While the {{user}} template is only a wrapper for Module:UserLinks, by implementing it as a Lua module we make significant gains in performance. It is roughly twice as fast as accessing Module:UserLinks from wikitext. For example, this module rendered a test page containing nothing but 1000 {{user}} transclusions in 3.9 seconds, whereas the version that called Module:UserLinks from wikitext rendered it in 7.7 seconds.

The performance gains are achieved by reducing the number of parameters that are looked up from wikitext. In particular, the link codes, "t" and "c", along with the "span=no" option, are passed directly to UserLinks. Also, UserLinks looks up the arguments "user", "User", "project", "Project", "lang", and "Lang". From wikitext, each parameter is checked twice – once for the #invoke text and once for the calling template – and each check is relatively resource-heavy. From Lua, each parameter is only checked once, and the individual checks take much less time. This means that on average, maybe nine argument lookups are saved in each invocation by using this module. On pages with hundreds or thousands of invocations, this adds up to a lot of argument lookups saved.

--[=[
-- 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)
 -- 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 = frame:getParent().args
 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 = {'t', 'c'}
 
 -- 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 result
 else
  return mShared.makeWikitextError(result, options.isDemo)
 end
end

return p

Retrieved from "https://en.wikipedia.org/w/index.php?title=Module:User&oldid=603017465"

Hidden category: 
Wikipedia template-protected modules
 



This page was last edited on 6 April 2014, at 15:43 (UTC).

Text is available under the Creative Commons Attribution-ShareAlike License 4.0; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.



Privacy policy

About Wikipedia

Disclaimers

Contact Wikipedia

Code of Conduct

Developers

Statistics

Cookie statement

Mobile view



Wikimedia Foundation
Powered by MediaWiki