Lua/Title Library
< LuaLua modules based on the Scribunto/Lua extension are stored in resource pages using the Module: namespace. Each module uses a table to hold functions and variables, and that containing table is returned at the end of the module code.[1] This lesson will show you how to use the Lua Title library in your scripts.
Prerequisites
This lesson assumes you have already completed the Tables lesson.
Create a Lua Script that Uses the Title Library
To create a Lua script that uses the Title library:
- Navigate to Module:Sandbox.
- Clear all existing code.
- It's a sandbox. Everyone is free to play in the sandbox. But if you find another user is actively editing the sandbox at the same time, you may also use Module:Sandbox/Username, where Username is your Wikiversity username.
- Add the following code and save the page:
local p = {} function p.canTalk(frame) local title = mw.title.getCurrentTitle() return ';canTalk\n:' .. tostring(title.canTalk) .. '\n' end function p.baseText(frame) local title = mw.title.getCurrentTitle() return ';baseText\n:' .. tostring(title.baseText) .. '\n' end function p.exists(frame) local title = mw.title.getCurrentTitle() return ';exists\n:' .. tostring(title.exists) .. '\n' end function p.fileExists(frame) local title = mw.title.getCurrentTitle() return ';fileExists\n:' .. tostring(title.fileExists) .. '\n' end function p.fragment(frame) local title = mw.title.getCurrentTitle() return ';fragment\n:' .. title.fragment .. '\n' end function p.fullText(frame) local title = mw.title.getCurrentTitle() return ';fullText\n:' .. title.fullText .. '\n' end function p.getContent(frame) local text = mw.text.trim(frame.args[1]) local namespace = mw.text.trim(frame.args[2]) local title = mw.title.new(text, namespace) return ';getContent\n<blockquote>' .. title:getContent() .. '</blockquote>\n' end function p.id(frame) local title = mw.title.getCurrentTitle(); return ';id\n:' .. title.id .. '\n' end function p.inNamespace(frame) local title = mw.title.getCurrentTitle(); return ';inNamespace\n:' .. tostring(title:inNamespace(0)) .. '\n' end function p.inNamespaces(frame) local title = mw.title.getCurrentTitle(); return ';inNamespaces\n:' .. tostring(title:inNamespaces(0)) .. '\n' end function p.interwiki(frame) local title = mw.title.getCurrentTitle(); return ';interwiki\n:' .. title.interwiki .. '\n' end function p.isContentPage(frame) local title = mw.title.getCurrentTitle(); return ';isContentPage\n:' .. tostring(title.isContentPage) .. '\n' end function p.isExternal(frame) local title = mw.title.getCurrentTitle(); return ';isExternal\n:' .. tostring(title.isExternal) .. '\n' end function p.isLocal(frame) local title = mw.title.getCurrentTitle(); return ';isLocal\n:' .. tostring(title.isLocal) .. '\n' end function p.isRedirect(frame) local title = mw.title.getCurrentTitle(); return ';isRedirect\n:' .. tostring(title.isRedirect) .. '\n' end function p.isSpecialPage(frame) local title = mw.title.getCurrentTitle(); return ';isSpecialPage\n:' .. tostring(title.isSpecialPage) .. '\n' end function p.isSubpage(frame) local title = mw.title.getCurrentTitle(); return ';isSubpage\n:' .. tostring(title.isSubpage) .. '\n' end function p.isTalkPage(frame) local title = mw.title.getCurrentTitle(); return ';isTalkPage\n:' .. tostring(title.isTalkPage) .. '\n' end function p.isSubpageOf(frame) local title = mw.title.getCurrentTitle(); local text = mw.text.trim(frame.args[1]) local namespace = mw.text.trim(frame.args[2]) local title2 = mw.title.new(text, namespace) return ';isSubpageOf\n:' .. tostring(title:isSubpageOf(title2)) .. '\n' end function p.new(frame) local text = mw.text.trim(frame.args[1]) local namespace = mw.text.trim(frame.args[2]) local title = mw.title.new(text, namespace) return ';new\n:' .. title.id .. '\n' end function p.nsText(frame) local title = mw.title.getCurrentTitle(); return ';nsText\n:' .. title.nsText .. '\n' end function p.prefixedText(frame) local title = mw.title.getCurrentTitle() return ';prefixedText\n:' .. title.prefixedText .. '\n' end function p.rootText(frame) local title = mw.title.getCurrentTitle() return ';rootText\n:' .. title.rootText .. '\n' end function p.subjectNsText(frame) local title = mw.title.getCurrentTitle() return ';subjectNsText\n:' .. title.subjectNsText .. '\n' end function p.subpageText(frame) local title = mw.title.getCurrentTitle() return ';subpageText\n:' .. title.subpageText .. '\n' end function p.text(frame) local title = mw.title.getCurrentTitle() return ';text\n:' .. title.text .. '\n' end return p
Test Your Lua Script
To test your Lua script:
- Navigate to either the Module_talk:Sandbox page, the Wikiversity:Sandbox page, or your own user or sandbox page.
- Add the following code and save the page:
{{#invoke:Sandbox|baseText}} {{#invoke:Sandbox|canTalk}} {{#invoke:Sandbox|exists}} {{#invoke:Sandbox|fileExists}} {{#invoke:Sandbox|fragment}} {{#invoke:Sandbox|fullText}} {{#invoke:Sandbox|getContent|Sandbox|Wikiversity}} {{#invoke:Sandbox|id}} {{#invoke:Sandbox|inNamespace}} {{#invoke:Sandbox|inNamespaces}} {{#invoke:Sandbox|interwiki}} {{#invoke:Sandbox|isContentPage}} {{#invoke:Sandbox|isExternal}} {{#invoke:Sandbox|isLocal}} {{#invoke:Sandbox|isRedirect}} {{#invoke:Sandbox|isSpecialPage}} {{#invoke:Sandbox|isSubpage}} {{#invoke:Sandbox|isTalkPage}} {{#invoke:Sandbox|isSubpageOf|Sandbox|Module}} {{#invoke:Sandbox|new|Sandbox|Module}} {{#invoke:Sandbox|nsText}} {{#invoke:Sandbox|prefixedText}} {{#invoke:Sandbox|rootText}} {{#invoke:Sandbox|subjectNsText}} {{#invoke:Sandbox|subpageText}} {{#invoke:Sandbox|text}}
The result should be similar to:
- baseText
- Sandbox
- canTalk
- true
- exists
- true
- fileExists
- false
- fragment
- fullText
- Module talk:Sandbox
- getContent
- == Welcome == Welcome to the Wikiversity Sandbox. Feel free to experiment with edits on this page.
- id
- 150785
- inNamespace
- false
- inNamespaces
- false
- interwiki
- isContentPage
- false
- isExternal
- false
- isLocal
- true
- isRedirect
- false
- isSpecialPage
- false
- isSubpage
- false
- isTalkPage
- true
- isSubpageOf
- false
- new
- 150784
- nsText
- Module_talk
- prefixedText
- Module talk:Sandbox
- rootText
- Sandbox
- subjectNsText
- Module
- subpageText
- Sandbox
- text
- Sandbox
Understand Your Lua Script
To understand your Lua script:
-
local title = mw.title.getCurrentTitle()
gets the title object for the current page and assigns it to the variable namedtitle
. -
title.canTalk
returns whether the title can have a talk page. -
title.baseText
returns the base title text or parent page text if this is a subpage. -
title.exists
returns whether the title exists. -
title.fileExists
returns whether the file or image exists. -
title.fragment
returns the title fragment. -
title.fullText
returns the full text of the page title. -
title:getContent()
returns the page content for the title. -
title.id
returns the title id. -
title:inNamespace(0)
returns whether the title is in the given namespace. -
title:inNamespaces(0)
returns whether the title is in the given namespaces. -
title.interwiki
returns the title interwiki prefix, if any. -
title.isContentPage
returns whether the title is in a content namespace. -
title.isExternal
returns whether the title has an interwiki prefix. -
title.isLocal
returns whether the title is in this wiki project. -
title.isRedirect
returns whether the title is a redirect. -
title.isSpecialPage
returns whether the title is a special page. -
title.isSubpage
returns whether the title is a subpage. -
title.isTalkPage
returns whether the title is a talk page. -
title:isSubpageOf(title2))
returns whether the title is a subpage oftitle2
. -
mw.title.new(text, namespace)
gets the title object for the given page and namespace. -
title.nsText
returns the namespace text for the title. -
title.prefixedText
returns the title of the page, with the namespace and interwiki prefixes. -
title.rootText
returns the title of the root page without prefixes. -
title.subjectNsText
returns the text of the subject namespace for the title. -
title.subpageText
returns the subpage name for the title. -
title.text
returns The title text without namespace or interwiki prefixes.
Conclusion
Congratulations! You've now created, tested, and understood a Lua script that uses the Title library. Return to the main Lua page to learn about other Lua code libraries.
See Also
References
This article is issued from Wikiversity - version of the Tuesday, March 08, 2016. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.