Lua/Title Library

< Lua

Lua 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 edit

This lesson assumes you have already completed the Tables lesson.

Create a Lua Script that Uses the Title Library edit

To create a Lua script that uses the Title library:

  1. Navigate to Module:Sandbox.
  2. 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.
  3. 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 edit

To test your Lua script:

  1. Navigate to either the Module_talk:Sandbox page, the Wikiversity:Sandbox page, or your own user or sandbox page.
  2. 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 edit

To understand your Lua script:

  1. local title = mw.title.getCurrentTitle() gets the title object for the current page and assigns it to the variable named title.
  2. title.canTalk returns whether the title can have a talk page.
  3. title.baseText returns the base title text or parent page text if this is a subpage.
  4. title.exists returns whether the title exists.
  5. title.fileExists returns whether the file or image exists.
  6. title.fragment returns the title fragment.
  7. title.fullText returns the full text of the page title, with the namespace prefix.
  8. title:getContent() returns the page content for the title.
  9. title.id returns the title id.
  10. title:inNamespace(0) returns whether the title is in the given namespace.
  11. title:inNamespaces(0) returns whether the title is in the given namespaces.
  12. title.interwiki returns the title interwiki prefix, if any.
  13. title.isContentPage returns whether the title is in a content namespace.
  14. title.isExternal returns whether the title has an interwiki prefix.
  15. title.isLocal returns whether the title is in this wiki project.
  16. title.isRedirect returns whether the title is a redirect.
  17. title.isSpecialPage returns whether the title is a special page.
  18. title.isSubpage returns whether the title is a subpage.
  19. title.isTalkPage returns whether the title is a talk page.
  20. title:isSubpageOf(title2)) returns whether the title is a subpage of title2.
  21. mw.title.new(text, namespace) gets the title object for the given page and namespace.
  22. title.nsText returns the namespace text for the title.
  23. title.prefixedText returns the title of the page, with the namespace and interwiki prefixes.
  24. title.rootText returns the title of the root page without prefixes.
  25. title.subjectNsText returns the text of the subject namespace for the title.
  26. title.subpageText returns the subpage name for the title.
  27. title.text returns The title text without namespace or interwiki prefixes.

Conclusion edit

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 edit

References edit