사용자:하늘/루아

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
{{#switch: {{{1}}} | foo = Foo | Bar }}
local cases = {
    default = "Bar",
    foo = "Foo",
}

local pframe = frame: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

Math[편집 | 원본 편집]

Unlike in wikicode, lua does not use functions like ifexpr and expr. Add the lua operators and functions straight into your module.

Grouping

Wikicode Lua
() ()

Numbers

Wikicode Lua
1234.5 1234.5
2e3 2e3
pi math.pi
e
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

틀:Clear

Magic words[편집 | 원본 편집]

Date and time[편집 | 원본 편집]

Wikicode Lua
{{CURRENTYEAR}}
os.date( "%Y" )
{{CURRENTMONTH}}
os.date( "%m" )
{{CURRENTMONTHNAME}}
mw.getContentLanguage():formatDate( 'F' )
{{CURRENTMONTHNAMEGEN}}
mw.getContentLanguage():formatDate( 'xg' )
{{CURRENTMONTHABBREV}}
mw.getContentLanguage():formatDate( 'M' )
{{CURRENTDAY}}
os.date( "%e" )
{{CURRENTDAY2}}
os.date( "%d" )
{{CURRENTDOW}}
os.date( "%w" )
{{CURRENTDAYNAME}}
mw.getContentLanguage():formatDate( 'l' )
{{CURRENTTIME}}
os.date( "%R" )
{{CURRENTHOUR}}
os.date( "%H" )
{{CURRENTWEEK}}
mw.getContentLanguage():formatDate( 'W' )
{{CURRENTTIMESTAMP}}

Technical metadata[편집 | 원본 편집]

Wikicode Lua
{{SITENAME}}
mw.site.siteName
{{SERVER}}
mw.site.server
{{SERVERNAME}}
{{DIRMARK}}
mw.language.getContentLanguage():getDirMark()
{{SCRIPTPATH}}
mw.site.scriptPath
{{STYLEPATH}}
mw.site.stylePath
{{CURRENTVERSION}}
mw.site.currentVersion
{{CONTENTLANGUAGE}}
mw.getContentLanguage():getCode()
{{PAGEID}}
mw.title.getCurrentTitle().id
{{PAGESIZE:page name}}
{{PROTECTIONLEVEL:edit}} [Expensive]
local protection = mw.title.getCurrentTitle().protectionLevels["edit"]
table.concat(protection)
{{REVISIONID}}
{{REVISIONDAY}}
{{REVISIONDAY}}
{{REVISIONMONTH}}
{{REVISIONMONTH1}}
{{REVISIONYEAR}}
{{REVISIONTIMESTAMP}}
{{REVISIONUSER}}
{{DISPLAYTITLE:title}}
{{DEFAULTSORT:sortkey}}

Statistics[편집 | 원본 편집]

Wikicode Lua
{{NUMBEROFPAGES}}
mw.site.stats.pages
{{NUMBEROFARTICLES}}
mw.site.stats.articles
{{NUMBEROFFILES}}
mw.site.stats.files
{{NUMBEROFEDITS}}
mw.site.stats.edits
{{NUMBEROFVIEWS}}
mw.site.stats.views
{{NUMBEROFUSERS}}
mw.site.stats.users
{{NUMBEROFADMINS}}
mw.site.stats.admins
{{NUMBEROFACTIVEUSERS}}
mw.site.stats.activeUsers
[Expensive]
{{PAGESINCATEGORY:categoryname}}
[Expensive]
mw.site.stats.pagesInCategory( 'categoryname' )
{{NUMBERINGROUP:groupname}}
mw.site.stats.usersInGroup( 'groupname' )

Page names[편집 | 원본 편집]

Wikicode Lua
{{FULLPAGENAME}}
mw.title.getCurrentTitle().prefixedText
{{PAGENAME}}
mw.title.getCurrentTitle().text
{{BASEPAGENAME}}
mw.title.getCurrentTitle().baseText
{{SUBPAGENAME}}
mw.title.getCurrentTitle().subpageText
{{SUBJECTPAGENAME}} [Expensive]
mw.title.getCurrentTitle().subjectPageTitle

or an non expensive alternative:

mw.title.getCurrentTitle().subjectNsText .. ":" .. mw.title.getCurrentTitle().text
{{TALKPAGENAME}} [Expensive]
mw.title.getCurrentTitle().talkPageTitle

Namespaces[편집 | 원본 편집]

Wikicode Lua
{{NAMESPACE}}
mw.title.getCurrentTitle().nsText
{{NAMESPACENUMBER}}
mw.title.getCurrentTitle().namespace
{{SUBJECTSPACE}}
mw.title.getCurrentTitle().subjectNsText
{{TALKSPACE}}

URL data[편집 | 원본 편집]

Wikicode Lua
{{localurl:page|query}}
mw.uri.localUrl( 'page', 'query' )
{{fullurl:page|query}}
mw.uri.fullUrl( 'page', 'query' )
{{canonicalurl:page|query}}
mw.uri.canonicalUrl( 'page', 'query' )
{{filepath:file name}}
{{urlencode:string|QUERY}}
mw.uri.encode( 'string', QUERY )
{{anchorencode:string}}
mw.uri.anchorEncode( 'string' )

Namespaces[편집 | 원본 편집]

Wikicode Lua
{{ns:0}}
mw.site.namespaces[0].name
{{ns:Project}}
mw.site.namespaces.Project.name

Formatting[편집 | 원본 편집]

Wikicode Lua
{{formatnum:number}}
mw.getContentLanguage():formatNum( number )
#dateformat
{{lc:string}}
mw.ustring.lower( 'string' )
{{lcfirst:string}}
mw.getContentLanguage():lcfirst( 'string' )
{{uc:string}}
mw.ustring.upper( 'string' )
{{ucfirst:string}}
mw.getContentLanguage():ucfirst( 'string' )
{{padleft:xyz|stringlength}}
{{padright:xyz|stringlength}}

Localisation[편집 | 원본 편집]

Wikicode Lua
{{plural:2|is|are}}
mw.getContentLanguage():plural( 2, 'is', 'are' )
{{grammar:N|noun}}
mw.getContentLanguage():grammar( 'N', 'noun' )
{{gender:username|male|female|neutral }}
mw.getContentLanguage():gender( 'username', { 'male', 'female', 'neutral' } )
{{int:message}}
mw.message.new( 'message' ):plain()
{{int:editsectionhint|MediaWiki}}
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 }}
frame:extensionTag( 'tagname', 'some text', {
    attribute1 = 'value1', attribute2 = 'value2'
} )

HTML library[편집 | 원본 편집]

mw.html is a fluent interface for building complex HTML from Lua. A mw.html object can be created using [[<tvar|anchor>#mw.html.create</>|mw.html.create]].


틀:Anchor


Functions documented as mw.html.name are available on the global mw.html table; functions documented as mw.html:name and html:name are methods of an mw.html object (see [[<tvar|anchor>#mw.html.create</>|mw.html.create]]).


A basic example could look like this:


local div = mw.html.create( 'div' )
div
     :attr( 'id', 'testdiv' )
     :css( 'width', '100%' )
     :wikitext( 'Some text' )
     :tag( 'hr' )
return tostring( div )
-- Output: <div id="testdiv" style="width:100%;">Some text<hr /></div>

mw.html.create[편집 | 원본 편집]

태그 생성 mw.html.create( tagName, args )


Creates a new mw.html object containing a tagName html element. You can also pass an empty string or nil as tagName in order to create an empty mw.html object.


args can be a table with the following keys:


  • args.selfClosing: Force the current tag to be self-closing, even if mw.html doesn't recognize it as self-closing
  • args.parent: Parent of the current mw.html instance (intended for internal usage)


mw.html:node[편집 | 원본 편집]

html:node( builder )


Appends a child mw.html (builder) node to the current mw.html instance. If a nil parameter is passed, this is a no-op. A (builder) node is a string representation of an html element.


mw.html:wikitext[편집 | 원본 편집]

텍스트 출력? html:wikitext( ... )


Appends an undetermined number of wikitext strings to the mw.html object.


Note that this stops at the first nil item.


mw.html:newline[편집 | 원본 편집]

새로운 행 html:newline()


Appends a newline to the mw.html object.


mw.html:tag[편집 | 원본 편집]

자식태그 삽입 html:tag( tagName, args )


Appends a new child node with the given tagName to the builder, and returns a mw.html instance representing that new node. The args parameter is identical to that of [[<tvar|anchor>#mw.html.create</>|mw.html.create]]

mw.html:attr[편집 | 원본 편집]

속성 추가 html:attr( name, value )
html:attr( table )


Set an HTML attribute with the given name and value on the node. Alternatively a table holding name->value pairs of attributes to set can be passed. In the first form, a value of nil causes any attribute with the given name to be unset if it was previously set.

mw.html:getAttr[편집 | 원본 편집]

속성 get html:getAttr( name )


Get the value of a html attribute previously set using [[<tvar|anchor>#mw.html:attr</>|html:attr()]] with the given name.

mw.html:addClass[편집 | 원본 편집]

html:addClass( class )


Adds a class name to the node's class attribute. If a nil parameter is passed, this is a no-op.


mw.html:css[편집 | 원본 편집]

html:css( name, value )
html:css( table )


Set a CSS property with the given name and value on the node. Alternatively a table holding name->value pairs of properties to set can be passed. In the first form, a value of nil causes any property with the given name to be unset if it was previously set.


mw.html:cssText[편집 | 원본 편집]

html:cssText( css )


Add some raw css to the node's style attribute. If a nil parameter is passed, this is a no-op.


mw.html:done[편집 | 원본 편집]

html:done()


Returns the parent node under which the current node was created. Like jQuery.end, this is a convenience function to allow the construction of several child nodes to be chained together into a single statement.


mw.html:allDone[편집 | 원본 편집]

html:allDone()


Like [[<tvar|anchor>#mw.html:done</>|html:done()]], but traverses all the way to the root node of the tree and returns it.

Title library[편집 | 원본 편집]

mw.title.equals[편집 | 원본 편집]

두 제목 일치 여부 mw.title.equals( a, b )


Test for whether two titles are equal. Note that fragments are ignored in the comparison.

mw.title.compare[편집 | 원본 편집]

mw.title.compare( a, b )


Returns -1, 0, or 1 to indicate whether the title a is less than, equal to, or greater than title b.


This compares titles by interwiki prefix (if any) as strings, then by namespace number, then by the unprefixed title text as a string. These string comparisons use Lua's standard < operator.

mw.title.getCurrentTitle[편집 | 원본 편집]

현재 문서 제목 객체 얻기 mw.title.getCurrentTitle()


Returns the title object for the current page.

mw.title.new[편집 | 원본 편집]

새 제목 객체 만들기 mw.title.new( text, namespace )
mw.title.new( id )


틀:Red
Creates a new title object.
Returns type "nil" if the pagename (in parameter "text") is invalid, but succeeds if it is valid but only does not exist.


If a number id is given, an object is created for the title with that page_id. The title referenced will be counted as linked from the current page. If the page_id does not exist, returns nil. The expensive function count will be incremented if the title object created is not for a title that has already been loaded.


If a string text is given instead, an object is created for that title (even if the page does not exist). If the text string does not specify a namespace, namespace (which may be any key found in [[<tvar name=anchor>#mw.site.namespaces</tvar>|mw.site.namespaces]]) will be used. If the text is not a valid title, nil is returned.

mw.title.makeTitle[편집 | 원본 편집]

mw.title.makeTitle( namespace, title, fragment, interwiki )


Creates a title object with title title in namespace namespace, optionally with the specified fragment and interwiki prefix. namespace may be any key found in [[<tvar name=anchor>#mw.site.namespaces</tvar>|mw.site.namespaces]]. If the resulting title is not valid, returns nil.


Note that, unlike <tvar name=mw_title_new_function>mw.title.new()</tvar>, this method will always apply the specified namespace. For example, mw.title.makeTitle( 'Template', 'Module:Foo' ) will create an object for the page Template:Module:Foo, while mw.title.new( 'Module:Foo', 'Template' ) will create an object for the page Module:Foo.

Title objects[편집 | 원본 편집]

A title object has a number of properties and methods. Most of the properties are read-only.


Note that fields ending with text return titles as string values whereas the fields ending with title return title objects.


  • id: The page_id. 0 if the page does not exist. 틀:Red.


  • interwiki: The interwiki prefix, or the empty string if none.


  • namespace: The namespace number.


  • fragment: The fragment (aka section/anchor linking), or the empty string. May be assigned.


  • nsText: The text of the namespace for the page.


  • subjectNsText: The text of the subject namespace for the page.


  • text: The title of the page, without the namespace or interwiki prefixes.


  • prefixedText: The title of the page, with the namespace and interwiki prefixes.


  • fullText: The title of the page, with the namespace and interwiki prefixes and the fragment. Interwiki is not returned if equal to the current.


  • rootText: If this is a subpage, the title of the root page without prefixes. Otherwise, the same as title.text.


  • baseText: If this is a subpage, the title of the page it is a subpage of without prefixes. Otherwise, the same as title.text.


  • subpageText: If this is a subpage, just the subpage name. Otherwise, the same as title.text.


  • canTalk: Whether the page for this title could have a talk page.


  • exists: Whether the page exists. Alias for file.exists for Media-namespace titles. For File-namespace titles this checks the existence of the file description page, not the file itself. 틀:Red. It is always expensive if the title object was created by "mw.title.new" or "mw.title.makeTitle", and the call will throw an exception rather than returning "false" if the limit (500 on most wikis) is reached.



  • isContentPage: Whether this title is in a content namespace.


  • isExternal: Whether this title has an interwiki prefix.


  • isLocal: Whether this title is in this project. For example, on the English Wikipedia, any other Wikipedia is considered "local" while Wiktionary and such are not.


  • isRedirect: Whether this is the title for a page that is a redirect. 틀:Red.


  • isSpecialPage: Whether this is the title for a possible special page (i.e. a page in the Special: namespace).


  • isSubpage: Whether this title is a subpage of some other title.
  • isTalkPage: Whether this is a title for a talk page.
  • isSubpageOf( title2 ): Whether this title is a subpage of the given title.


  • inNamespace( ns ): Whether this title is in the given namespace. Namespaces may be specified by anything that is a key found in [[<tvar name=anchor3>#mw.site.namespaces</tvar>|mw.site.namespaces]].


  • inNamespaces( ... ): Whether this title is in any of the given namespaces. Namespaces may be specified by anything that is a key found in [[<tvar name=anchor4>#mw.site.namespaces</tvar>|mw.site.namespaces]].


  • hasSubjectNamespace( ns ): Whether this title's subject namespace is in the given namespace. Namespaces may be specified by anything that is a key found in [[<tvar name=anchor5>#mw.site.namespaces</tvar>|mw.site.namespaces]].


  • contentModel: The content model for this title, as a string. 틀:Red.


  • basePageTitle: The same as mw.title.makeTitle( title.namespace, title.baseText ).


  • rootPageTitle: The same as mw.title.makeTitle( title.namespace, title.rootText ).


  • talkPageTitle: The same as mw.title.makeTitle( mw.site.namespaces[title.namespace].talk.id, title.text ), or nil if this title cannot have a talk page.


  • subjectPageTitle: The same as mw.title.makeTitle( mw.site.namespaces[title.namespace].subject.id, title.text ).


  • redirectTarget: Returns a title object of the target of the redirect page if the page is a redirect and the page exists, returns false otherwise.


  • protectionLevels: The page's protection levels. This is a table with keys corresponding to each action (e.g., "edit" and "move"). The table values are arrays, the first item of which is a string containing the protection level. If the page is unprotected, either the table values or the array items will be nil. 틀:Red.


  • cascadingProtection: The cascading protections applicable to the page. This is a table with keys "restrictions" (itself a table with keys like protectionLevels has) and "sources" (an array listing titles where the protections cascade from). If no protections cascade to the page, "restrictions" and "sources" will be empty. 틀:Red.


  • subPageTitle( text ): The same as mw.title.makeTitle( title.namespace, title.text .. '/' .. text ).


  • partialUrl(): Returns title.text encoded as it would be in a URL.


  • fullUrl( query, proto ): Returns the full URL (with optional query table/string) for this title. proto may be specified to control the scheme of the resulting url: "http", "https", "relative" (the default), or "canonical".


  • localUrl( query ): Returns the local URL (with optional query table/string) for this title.


  • canonicalUrl( query ): Returns the canonical URL (with optional query table/string) for this title.


  • getContent(): Returns the (unparsed) content of the page, or nil if there is no page. The page will be recorded as a transclusion.


Title objects may be compared using [[<tvar name=anchor1>#Relational operators</tvar>|relational operators]]. [[<tvar name=anchor2>#tostring</tvar>|tostring]]( title ) will return title.prefixedText.


Since people find the fact surprising, note that accessing any expensive field on a title object records a "link" to the page (as shown on Special:WhatLinksHere, for example). Using the title object's getContent() method or accessing the redirectTarget field records it as a "틀:Int", and accessing the title object's file or fileExists fields records it as a "틀:Int".

File metadata[편집 | 원본 편집]

Title objects representing a page in the File or Media namespace will have a property called file. 틀:Red This is a table, structured as follows:


  • exists: Whether the file exists. It will be recorded as an image usage. The fileExists property on a Title object exists for backwards compatibility reasons and is an alias for this property. If this is false, all other file properties will be nil.


  • width: The width of the file. If the file contains multiple pages, this is the width of the first page.


  • height: The height of the file. If the file contains multiple pages, this is the height of the first page.


  • pages: If the file format supports multiple pages, this is a table containing tables for each page of the file; otherwise, it is nil. The # operator can be used to get the number of pages in the file. Each individual page table contains a width and height property.


  • size: The size of the file in bytes.



  • length: The length (duration) of the media file in seconds. Zero for media types which do not support length.

Expensive properties[편집 | 원본 편집]

The properties id, isRedirect, exists, and contentModel require fetching data about the title from the database. For this reason, the expensive function count is incremented the first time one of them is accessed for a page other than the current page. Subsequent accesses of any of these properties for that page will not increment the expensive function count again.


Other properties marked as expensive will always increment the expensive function count the first time they are accessed for a page other than the current page.

문서 제목[편집 | 원본 편집]

  • 제목 전체
    mw.title.new('시리즈:리브레 한자사전/漢').prefixedText 시리즈:리브레 한자사전/漢
  • 이름 공간
    mw.title.new('시리즈:리브레 한자사전/漢').nsText 시리즈
  • 이름공간 제외한 제목
    mw.title.new('시리즈:리브레 한자사전/漢').text 리브레 한자사전/漢
  • 최상위 제목
    mw.title.new('시리즈:리브레 한자사전/漢').rootText 리브레 한자사전
  • 하위문서 이름
    mw.title.new('시리즈:리브레 한자사전/漢').subpageText
  • 토론 문서가 있는지
    mw.title.new('시리즈:리브레 한자사전/漢').canTalk true