July 25, 2020

👭 Knight Challenge #11 👬

Want to try your hand at these challenges? There's a couple of things you can do!
From writing, to research, to images, find your preferred way to contribute with our eleventh theme: Couples!

Latest Announcements

Guidelines:Modules

From Zelda Wiki, the Zelda encyclopedia
Jump to: navigation, search
ZW Logo White.png
Getting Involved Getting Involved.png
Getting Involved
[1] Discord Logo.svg
Discord
Knight Challenges Knight Challenges Icon.png
Knight Challenges
Image edit request and support center Gimp Logo.svg
Image Requests
[2] Trello Logo.png
Trello
Guidelines:Main Guidelines Icon.png
Guidelines


Overview

Modules are Lua scripts for making templates using a full-fledged programming language. You can ask questions about Zelda Wiki modules in the #modules Discord channel.

Templates

Main article: Help:Templates

Modules exist as a means to create more complex templates. In order to contribute to modules, you must first understand how templates are used. You should also know to create a basic template without modules to get a sense of when Lua scripts are needed and why.

Getting Started

In order to contribute to modules, you'll need to learn basic programming in Lua. You'll also need to know the specifics of Lua scripting on MediaWiki.

Lua

Beginners

There are many excellent online resources designed to teach programming fundamentals... But none of them are in Lua. Fortunately, Lua is a small and simple language. Jump in and try to learn through the Exercises.

If you find you need more guided lessons, try Codeacademy's Python 2 course, or Khan Academy's Intro to JavaScript. Python is closer to Lua than JavaScript, but Khan Academy's course has the benefit of video talk-throughs. In any case the fundamental language concepts are the same.

Developers

If you already have some programming experience in other languages, read Learn Lua in 15 minutes.

Note the following in particular:

Particularities Further reading
tables are the only built-in object type. Any use of the term "array" actually refers to a table with integer keys.
Generally arrays are 1-indexed as opposed to 0-indexed.
local tbl = {"foo"}
tbl[1]
> "foo"
  • The ipairs iterator is for integer key-value pairs in a table.
  • The pairs iterator is for all key-value pairs, integer or otherwise.
The length operator for a table is #.

# replaces the table.getn seen in the online version of Programming in Lua, which is for Lua 5.0. getn won't work here, as the wiki is on Lua 5.1 at the time of writing.

table.setn was also deprecated and is unusable in 5.1. Unfortunately, the alternative __len field is only available in Lua 5.2.

Tables, nil, and the # operator don't interact the way similar constructs do in other languages.
#{ nil, nil, "foo" }
> 3

local tbl = {}
tbl[3] = "foo"
#tbl
> 0

tbl[1] = "bar"
#tbl
> 1

tbl[2] = "baz"
#tbl
> 3

Generally, "array" functions only look at the consecutive indices starting from 1.

Scribunto

For a broader and more in-depth guide, see Gamepedia Help Wiki.
See also the full Scribunto/Lua reference manual.

Scribunto is the name of the extension that enables Lua modules. The Lua on you see on MediaWiki is almost the same as standard Lua except for some changes to library functions and packages. The main difference is in how Lua scripts are invoked.

#invoke

The #invoke parser function is what bridges the gap between templates and module scripts. For example, the transcluded content of Template:Figurine is as follows:

{{#invoke:Figurine|Main}}

This invokes Module:Figurine, which could look something like:

local p = {}
local h = {}

local Franchise = require("Module:Franchise")
...

function p.Main(frame)
    local args = frame:getParent().args
    local result = h.getFigurine(args[1], args[2])
    return result
end

function h.getFigurine(game, name)
...
end

return p

A page can invoke any function that is a field on the module's export table. The export table is the table object returned by the module, which is always named p by convention. In this case, Main is the only function in the export table.

args

Functions called via #invoke are passed a Frame object.

  • frame.args is a table of the arguments to #invoke. (There are none in the above example.)
  • frame:getParent().args is a table of the arguments to the template.
    • Example: If a page has {{Figurine|TMC|Minish Ezlo}}, then frame:getParent().args[1] evaluates to the string TMC for that invocation.

The # operator and most other table functions don't work on frame.args.

require

A module can import another module and use its exported functions. This is done with the require function, as shown in the above example with Module:Franchise.

mw

Scribunto pre-loads several MediaWiki-related libraries as the mw object. The following libraries are of note, in addition to the base functions:

Library Usage example
mw.text Module:List
mw.title Module:Subpage List
mw.html Module:Infobox

Debugging

See also gphelp:Extension:Scribunto#Debugging

You should never be in a situation where you're blindly submitting code and hoping that it works. The first two exercises of these guidelines cover how to debug with previewing, logging, and the debug console.

Always ensure that your code does not produce script errors. Please fix or revert any changes that do. Keep an eye on Category:Pages with Invalid Arguments as well.

Testing and Documentation

Main article: Module:Documentation

When coding a module, the best page to preview is often the corresponding template's documentation. If it exists, the page should have usage examples for every available feature. In fact, an effective way to write modules is actually to write the documentation examples before the code itself. When you're writing the actual code, previewing that documentation page will tell you if your code is working—and will give you material to debug with if it isn't. The practice of writing test cases before the code is called test-driven development.

You should also write module documentation for any Lua function meant to be used by other modules. Module documentation can double as automated unit tests via the expect property. If the output of the function does not equal what is expected, the page is added to Category:Modules with failing tests.

Utility Modules

A utility module is a library-type module meant to be used by other modules, rather than being invoked by a template. Most template-facing modules use at least one of these:

A list of utility modules is available at Category:Utility Modules. Leverage utility modules as much as possible so that the wiki's codebase stays DRY.

Exercises

Main article: Guidelines:Modules/Exercises

Try the available exercises to test and develop your understanding of Lua, Scribunto, and Zelda Wiki's utilities.

Style

Once you are able to produce working code, make sure it adheres to Zelda Wiki's coding standards.

It is particularly important to use a consistent naming pattern, as plain text searching is the only way to observe function usage.

Importing

Use lower camelCase for imported utility modules
TFH Red Link desperate.png
local UtilsString = require("Module:UtilsString")
TFH Green Link ok.png
local utilsString = require("Module:UtilsString")
Avoid importing submodules unless absolutely necessary
TFH Red Link desperate.png
local utilsMarkupLink = require("Module:UtilsMarkup/Link")
TFH Green Link ok.png
local utilsMarkup = require("Module:UtilsMarkup")
Don't index the result of the require expression.
TFH Red Link desperate.png
local list = require("Module:UtilsMarkup").list
TFH Green Link ok.png If you must assign the function separately:
local utilsMarkup = require("Module:UtilsMarkup")
...
local list = utilsMarkup.list

Strings

Default to double quotes
TFH Green Link ok.png
local foo = "foo"
local bar = '<span class="baz">bar</span>'
TFH Red Link desperate.png
local foo = 'foo'

Errors

Template calls must not throw script errors on purpose. Use warnings and error categories instead.
TFH Green Link ok.png
function(foo)
  if not foo then
    mw.addWarning("'foo' is required.")
    return "[[Category:Pages with Invalid Arguments]]"
  end
  local bar = foo.bar
  ...
end

Module:UtilsArg can help with this.

TFH Red Link desperate.png
function(foo)
  local bar = foo.bar -- throws an error if foo is nil
  ...
end
TFH Red Link desperate.png
function(foo)
  if not foo then
    error("foo cannot be nil")
  end
  local bar = foo.bar
  ...
end