Open main menu
注記: This is not the Lua extension used by Wikimedia projects. For that, see Extension:Scribunto/ja.
MediaWiki 拡張機能マニュアル
OOjs UI icon advanced.svg
Lua parser extensions
リリースの状態: 実験的
実装 Tag, Parser function
説明 Extends the parser with support for embedded blocks of Lua code
作者 Fran McCrorytalk
データベースの変更 いいえ
ライセンス Zlib
ダウンロード Template:WikimediaDownload/svn 注記: trunk may be broken. Try r71165, it's old but should work.
パラメーター
$wgLuaExternalInterpreter,
$wgLuaExternalCompiler,
$wgLuaMaxLines,
$wgLuaMaxCalls,
$wgLuaMaxTime
使用するフック
LanguageGetMagic
ParserBeforeTidy
translatewiki.net で翻訳を利用できる場合は、Lua 拡張機能の翻訳にご協力ください
使用状況とバージョン マトリクスを確認してください。

Contents

この拡張機能でできることEdit

The Lua parser extensions allow you to embed blocks of Lua code into your pages and templates to be parsed and expanded by MediaWiki.

Traditionally, extensions such as ParserFunctions have offered some degree of programmability to MediaWiki's template system; however, this was not originally its intended purpose, and its syntax and utility have proven to be rather cumbersome and limited. As an alternative approach, this extension brings the full power of the Lua programming language to wikitext - including features such as variables with lexical scoping, data control structures and functions, and mathematical and string processing.

This extension requires either the Lua PHP extension from PECL or external Lua interpreter binaries (e.g. from LuaBinaries, or the "lua" package in most modern UNIX-like distributions) be installed to function.

使用法Edit

The <lua> tag is used to embed blocks of Lua code into wikitext, which are then executed. Arguments to the <lua> tag will be available as variables to the embedded code; the standard Lua functions print() and io.write() may be used to pass output back to the parser.

<lua who="{{PAGENAME}}">
function hello(s)
  return string.format("Hello, %s!", s)
end

print(hello(who))
</lua>

The {{#luaexpr:}} parser function may be used to embed a single Lua expression which will be immediately printed to the parser as if it were wrapped in a print() statement. Its semantics are thus quite similar to the ParserFunctions {{#expr:}} function.

All variables and function declarations persist from one block/expression to the next, throughout the page. Complicated expressions and calculations can thus easily be separated from their presentation. For example,

<lua>
function bottle(x)
  if x > 1 then
    return x .. " bottles of beer"
  elseif x == 1 then
    return "One bottle of beer"
  else
    return "No more bottles of beer"
  end
end

function count(x)
  output = "<pre>"
  for beer = x, 1, -1 do
    output = output .. bottle(beer) .. " on the wall,\n"
    output = output .. bottle(beer) .. ",\n"
    output = output .. "Take one down, pass it around,\n"
    output = output .. bottle(beer-1) .. " on the wall.\n"
    output = output .. "\n\n"
  end
  return output .. "</pre>"
end
</lua>

==99 Bottles of Beer==
{{#luaexpr:count(99)}}

All Lua code is executed in a secure sandbox environment. Nearly all of the Lua standard library is exposed to this environment, necessarily minus OS, filesystem, and debug facilities; most functions have been removed from io and os tables, for example. Hard limits on code execution are also in place to prevent infinite loops and stack overflow; see below for details on their configuration.

ダウンロード手順Edit

You can download this extension via the MediaWiki extension distribution system, or via Wikimedia SVN.

インストールEdit

This extension may be configured in two separate ways, using either an external Lua interpreter or the PHP Lua extension from PECL.

外部 Lua インタープリターの使用Edit

First, download and install the Lua interpreter. Most modern UNIX-like distributions will have this in their repositories under e.g. "lua" or "lua5.1"; precompiled binaries for several operating systems are also available from the LuaBinaries project at LuaForge.

Extract the extension files to your MediaWiki extensions/ folder. Then add the following to LocalSettings.php, replacing /usr/bin/lua5.1 with the actual path to your Lua binary:

$wgLuaExternalInterpreter = "/usr/bin/lua5.1";
require_once("$IP/extensions/Lua/Lua.php");

PECL からの Lua PHP 拡張機能の使用Edit

Due to the embryonic state of the Lua PHP extension from PECL, this method is not yet recommended for most users.

First, download, compile, and install the "lua" package from PECL. Unfortunately, as it is still in a pre-alpha stage, this entails checking out a copy from CVS and compiling it yourself.

Then, to install this extension, extract the extension files to your MediaWiki extensions/ folder, and add the following to LocalSettings.php:

require_once("$IP/extensions/Lua/Lua.php");

Configuration parametersEdit

  • $wgLuaExternalInterpreter — Path to the external lua binary to use. If FALSE or unset, attempt to use the Lua PHP extension instead.
  • $wgLuaExternalCompiler — Optional path to an external luac binary to use - if present, the wrapper library will be bytecode compiled for quicker execution.
  • $wgLuaMaxLines — Hard limit on lines of Lua code to execute, to stop malfunctioning or malicious scripts from draining server resources. The default is 1000000.
  • $wgLuaMaxCalls — Hard limit on recursion in Lua code, to stop malfunctioning or malicious scripts from draining server resources. The default is 2000.
  • $wgLuaMaxTime — Hard limit on Lua code execution time in seconds. The default is 5.

関連項目Edit