Source: Wikipedia, the free encyclopedia.
This is a list of Lua functions and the wikitext parser functions and magic words that they are comparable to.
It is slightly out of date for what wikitext parser functions can do.
Parser functions
Wikitext
Lua
{{#if:x |not empty |empty}}
if x then
'not empty'
else
'empty'
end
{{#ifeq:x |y |identical |different}}
if x == y then
'identical'
else
'different'
end
{{#iferror:function |bad |good}}
if tostring ( mw . getCurrentFrame (): preprocess ( 'function' )): find ( '<strong class="error"' ) then
'bad'
else
'good'
end
{{#ifexpr:1+2=3 |true|false}}
if 1 + 2 == 3 then
'true'
else
'false'
end
[Expensive] {{#ifexist:namespace:title |exists |doesn't exist}}
[Expensive]
if mw . title . new ( 'title' , 'namespace' ). exists == true then
"exists"
else
"doesn't exist"
end
{{#rel2abs:path |basepath}}
{{#switch:{{{1}}} |foo=Foo |Bar}}
local cases = {
default = "Bar" ,
foo = "Foo" ,
}
local pframe = mw . getCurrentFrame (): getParent ()
local arg = pframe . args [ 1 ]
cases [ arg ] or cases [ "default" ]
{{#time:format |timestamp}}
mw . getContentLanguage (): formatDate ( 'format' , 'timestamp' )
{{#timel:format |timestamp}}
mw . getContentLanguage (): formatDate ( 'format' , 'timestamp' , 1 )
{{#titleparts:pagename |number of segments to return |first segment to return}}
Math
Unlike in wikicode, Lua does not use functions like #ifexpr
and #expr
. Add the Lua operators and functions straight into your module. See mw:Help:Calculation for details on how #expr
and mw . ext . ParserFunctions . expr ()
evaluates expressions.
Grouping
Wikicode
Lua
()
()
Numbers
Wikicode
Lua
1234.5
1234.5
2e3
2e3
pi
math.pi
e
math.exp(1)
2+2
2+2
2-1
2-1
Unary
Wikicode
Lua
not
not
ceil1.2
math.ceil(1.2)
trunc
floor1.2
math.floor(1.2)
abs-2
math.abs(-2)
exp43
math.exp(43)
ln2
math.log(2)
cos0.1
math.cos(0.1)
tan0.1
math.tan(0.1)
acos0.1
math.acos(0.1)
asin0.1
math.asin(0.1)
atan0.1
math.atan(0.1)
Binary
Wikicode
Lua
2^3
2^3
2*3
2*3
2/3 2div3
2/3
30mod7
30%7
+2
2
-2
-2
Logic
Wikicode
Data type
Lua
Data type
3.0=3
Integer (0 or 1)
3.0==3
Boolean (true or false)
3!=4 3<>4
Integer
3~=4
Boolean
4>3
Integer
4>3
Boolean
4>=3
Integer
4>=3
Boolean
3<=4
Integer
3<=4
Boolean
3<=4
Integer
3<=4
Boolean
and
Integer
and
Boolean
or
Integer
or
Boolean
Magic words
Date and time
Wikicode
Lua
mw . getContentLanguage (): formatDate ( "Y" )
or
mw . getContentLanguage (): formatDate ( "m" )
or
mw . getContentLanguage (): formatDate ( "n" )
or( "%d" ): format ( os.date ( "!%m" ))
mw . getContentLanguage (): formatDate ( "F" )
or (if the wiki's language is set to English):
mw . getContentLanguage (): formatDate ( "xg" )
or (if the wiki's language is set to English):
mw . getContentLanguage (): formatDate ( "M" )
or (if the wiki's language is set to English):
mw . getContentLanguage (): formatDate ( "j" )
or( "%d" ): format ( os.date ( "!%d" ))
mw . getContentLanguage (): formatDate ( "d" )
or
mw . getContentLanguage (): formatDate ( "w" )
or
mw . getContentLanguage (): formatDate ( "l" )
or (if the wiki's language is set to English):
mw . getContentLanguage (): formatDate ( "H:i" )
or
mw . getContentLanguage (): formatDate ( "H" )
or
( "%d" ): format ( mw . getContentLanguage (): formatDate ( "W" ))
or( "%d" ): format ( os.date ( "!%V" ))
mw . getContentLanguage (): formatDate ( "YmdHis" )
or
mw . getContentLanguage (): formatDate ( "Y" , nil , true )
or
mw . getContentLanguage (): formatDate ( "m" , nil , true )
or
mw . getContentLanguage (): formatDate ( "n" , nil , true )
or( "%d" ): format ( os.date ( "%m" ))
mw . getContentLanguage (): formatDate ( "F" , nil , true )
or (if the wiki's language is set to English):
mw . getContentLanguage (): formatDate ( "xg" , nil , true )
or (if the wiki's language is set to English):
mw . getContentLanguage (): formatDate ( "M" , nil , true )
or (if the wiki's language is set to English):
mw . getContentLanguage (): formatDate ( "j" , nil , true )
or( "%d" ): format ( os.date ( "%d" ))
mw . getContentLanguage (): formatDate ( "d" , nil , true )
or
mw . getContentLanguage (): formatDate ( "w" , nil , true )
or
mw . getContentLanguage (): formatDate ( "l" , nil , true )
or (if the wiki's language is set to English):
mw . getContentLanguage (): formatDate ( "H:i" , nil , true )
or
mw . getContentLanguage (): formatDate ( "H" , nil , true )
or
( "%d" ): format ( mw . getContentLanguage (): formatDate ( "W" , nil , true ))
or( "%d" ): format ( os.date ( "%V" ))
mw . getContentLanguage (): formatDate ( "YmdHis" , nil , true )
or
Technical metadata
Wikicode
Lua
mw . language . getContentLanguage (): getDirMark ()
mw . getContentLanguage (): getCode ()
mw . title . getCurrentTitle (). id
mw . getContentLanguage (): formatNum ( mw . title . new ( pagename ): getContent (): len ())
mw . title . new ( pagename ): getContent (): len ()
[Expensive] {{PROTECTIONLEVEL:action|pagename}}
table.concat ( mw . title . getCurrentTitle (). protectionLevels [ "edit" ])
[Expensive] table.concat ( mw . title . new ( pagename ). protectionLevels [ action ])
[Expensive] {{CASCADINGSOURCES:pagename}}
[Expensive] table.concat ( mw . title . getCurrentTitle (). cascadingProtection . sources , "|" )
table.concat ( mw . title . new ( pagename ). cascadingProtection . sources , "|" )
Statistics
Wikicode
Lua
mw . site . stats . activeUsers
[Expensive] {{PAGESINCATEGORY:categoryname}}
[Expensive] mw . site . stats . pagesInCategory ( 'categoryname' )
{{NUMBERINGROUP:groupname}}
mw . site . stats . usersInGroup ( 'groupname' )
Page names
Wikicode
Lua
mw . title . getCurrentTitle (). prefixedText
mw . uri . encode ( mw . title . getCurrentTitle (). prefixedText , "WIKI" )
mw . title . getCurrentTitle (). text
mw . uri . encode ( mw . title . getCurrentTitle (). text , "WIKI" )
mw . title . getCurrentTitle (). baseText
mw . uri . encode ( mw . title . getCurrentTitle (). baseText , "WIKI" )
mw . title . getCurrentTitle (). rootText
mw . uri . encode ( mw . title . getCurrentTitle (). rootText , "WIKI" )
mw . title . getCurrentTitle (). subpageText
mw . uri . encode ( mw . title . getCurrentTitle (). subpageText , "WIKI" )
[Expensive] mw . title . getCurrentTitle (). subjectPageTitle
or a non-expensive alternative:
mw . title . getCurrentTitle (). subjectNsText .. ':' .. mw . title . getCurrentTitle (). text
[Expensive] mw . uri . encode ( mw . title . getCurrentTitle (). subjectPageTitle , "WIKI" )
or a non-expensive alternative:
mw . uri . encode ( mw . title . getCurrentTitle (). subjectNsText .. ':' .. mw . title . getCurrentTitle (). text , "WIKI" )
[Expensive] mw . title . getCurrentTitle (). talkPageTitle
mw . uri . encode ( mw . title . getCurrentTitle (). talkPageTitle , "WIKI" )
Namespaces
Wikicode
Lua
mw . title . getCurrentTitle (). nsText
mw . uri . encode ( mw . title . getCurrentTitle (). nsText , "WIKI" )
mw . title . getCurrentTitle (). namespace
mw . title . getCurrentTitle (). subjectNsText
mw . uri . encode ( mw . title . getCurrentTitle (). subjectNsText , "WIKI" )
mw . site . namespaces [ title . namespace ]. talk . canonicalName
mw . uri . encode ( mw . site . namespaces [ title . namespace ]. talk . canonicalName , "WIKI" )
URL data
Wikicode
Lua
mw . uri . localUrl ( 'page' , 'query' )
mw . uri . fullUrl ( 'page' , 'query' )
{{canonicalurl:page|query}}
mw . uri . canonicalUrl ( 'page' , 'query' )
{{urlencode:string|QUERY}}
mw . uri . encode ( 'string' , QUERY )
mw . uri . anchorEncode ( 'string' )
Namespaces
Wikicode
Lua
mw . site . namespaces [ 0 ]. name
mw . site . namespaces . Project . name
Formatting
Wikicode
Lua
mw . getContentLanguage (): formatNum ( number )
{{#dateformat:date|format}}
mw . ustring . lower ( 'string' )
mw . getContentLanguage (): lcfirst ( 'string' )
mw . ustring . upper ( 'string' )
mw . getContentLanguage (): ucfirst ( 'string' )
{{padleft:xyz|stringlength}}
{{padright:xyz|stringlength}}
Localisation
Wikicode
Lua
mw . getContentLanguage (): plural ( 2 , 'is' , 'are' )
mw . getContentLanguage (): grammar ( 'N' , 'noun' )
{{gender:username|male|female|neutral}}
mw . getContentLanguage (): gender ( 'username' , { 'male' , 'female' , 'neutral' })
{{int:editsectionhint|MediaWiki}}
mw . message . new ( 'message' ): plain ()
mw . message . new ( 'editsectionhint' , 'MediaWiki' ): plain ()
Miscellaneous
Wikicode
Lua
{{#language:code|inlanguage}}
mw . language . fetchLanguageName ( 'code' , 'inLanguage' )
{{#special:special page name}}
{{#speciale:special page name}}
{{#tag:tagname |some text |attribute1=value1 |attribute2=value2}}
mw . getCurrentFrame (): callParserFunction ( '#tag' , { 'tagname' , 'some text' , attribute1 = 'value1' , attribute2 = 'value2' })
mw . getCurrentFrame (): extensionTag ( 'tagname' , 'some text' , { attribute1 = 'value1' , attribute2 = 'value2' })