Extension:Scribunto/zh

随附于MediaWiki 1.34及更高版本。 其余配置说明仍必须遵循。
Category:Extensions bundled with MediaWiki 1.34/zh
Category:Pages using deprecated NoteTA template#%20 本扩展會在可执行程序上运行。 为了让该扩展工作,你必须在你的主机上有运行可执行文件的权限。
Category:Extensions with unknown license/zh
Scribunto
Category:Stable extensions/zh
Category:Parser extensions/zh
描述 提供在 MediaWiki 页面裏嵌入脚本语言的框架
作者
  • Victor Vasiliev
  • Tim Starling
和​其他人
最新版本 持续更新
快照跟随MediaWiki发布。
PHP 5.5+
GPL-2.0-or-later AND MIT
下載 Category:Extensions in Wikimedia version control/zh
Module (ns:828), Talk_Module (ns:829)
  • $wgScribuntoDefaultEngine
  • $wgScribuntoSlowFunctionThreshold
  • $wgScribuntoLuaExtraTitleAttributes
  • $wgScribuntoGatherFunctionStats
  • $wgScribuntoUseCodeMirror
  • $wgScribuntoUseGeSHi
  • $wgScribuntoUseCodeEditor
  • $wgScribuntoEngineConf
前往translatewiki.net翻譯Scribunto
Vagrant角色 scribunto
問題 开启的任务 ·
Category:All extensions/zh

Scribunto拉丁语: “他们应能/让他们(在未来)书写”)扩展可以在 MediaWiki 软件中嵌入脚本。

目前唯一被的脚本语言是Lua。 Scribunto Lua脚本在一个名为的命名空间中。 模块使用 #invoke 解析器在普通的wiki页面上运行,每个模块都有一个函数的集合,可以使用wiki文本语法调用,例如:

{{#invoke: Module_name | function_name | arg1 | arg2 | arg3 ... }}

  • 下载,并将解压后的Scribunto移动到extensions/目录中。
    开发者和代码贡献人员应从Git安装扩展,输入:
    cd extensions/
    git clone https://gerrit.wikimedia.org/r/mediawiki/extensions/Scribunto
    
  • 将下列放置在您的LocalSettings.php 的底部:
    wfLoadExtension( 'Scribunto' );
    $wgScribuntoDefaultEngine = 'luastandalone';
    
  • Yes 完成 – 在您的wiki上Special:Version,以验证已成功安装


  • 如果使用Vagrant ,请vagrant roles enable scribunto --provision安装

需求

PCRE版本的兼容性

推荐使用PCRE(Perl相容的正則表達式)8.33版以上。 你可以查看PHP所使用的PCRE的版本:查看phpinfo()網頁页面、或在命令行中使用以下命令:

php -r 'echo "pcre: " . ( extension_loaded( "pcre" ) ? PCRE_VERSION : "no" ) . "\n";'

PHP mbsting 扩展

PHP需要启用 mbstring 扩展。

您可以通过查看phpinfo()网页或使用以下命令从命令行检查是否启用了mbstring支持:

php -r 'echo "mbstring: " . ( extension_loaded( "mbstring" ) ? "yes" : "no" ) . "\n";'

Lua 二进制文件

的二进制文件

對於Linux(x86和x86-64)、Mac OS X Lion和Windows(32位和64位),Scribunto与Lua的二进制发行版在一起。

Scribunto应该在以下情况下为您提供开箱即用的服务:

  1. 您的web服务器在上述平台之一上运行。
  2. PHP的proc_open函数不受限制。[1]
  3. 在PHP中,proc_terminateshell_exec 並未停用。
  4. 你的网络浏览器已被配置为允许MediaWiki树中的二进制文件的执行。
注意 注意: 可能需要设置执行权限,例如,在Linux使用:
chmod 755 /path/to/extensions/Scribunto/includes/Engines/LuaStandalone/binaries/lua5_1_5_linux_64_generic/lua
如果你在你的服务器上使用处于“Enforcing”模式的SELinux,你可能需要为此二进制文件设置合适的安全上下文(context)。 例如,在RHEL/CentOS 7中使用:
chcon -t httpd_sys_script_exec_t /path/to/extensions/Scribunto/includes/Engines/LuaStandalone/binaries/lua5_1_5_linux_64_generic/lua

P.S. Check your version of the extension to see if the name of the engines folder is capitalised or fully lowercase.[2]

额外的二进制文件

如果您的Web服务器的操作系统不在上面的列表中,则可能需要额外的Lua二进制发行版,可以从http://luabinaries.sourceforge.net/或从你的Linux发行中获得。

只支持Lua 5.1.x的二进制文件。

在你的web浏览器中安装了适当的二进制文件后,使用如下代码配置文件的地址:

# 其中,Lua是二进制文件的名称。
# 例如,SourceForge LuaBinaries 5.1.5 - Release 2 name the binary file lua5.1
$wgScribuntoEngineConf['luastandalone']['luaPath'] = '/path/to/binaries/lua5.1';

注意你不应该添加上面的行,除非你确认Scribunto的内置二进制对你来说不起作用。

LuaJIT,尽管理论上兼容,但并不受支持。

不再支持是因为幽灵漏洞和比特腐烂的问题(phab:T184156)。

可选安装

为能使用有语法高亮和自动缩进的代码编辑器,提供更舒适的用户界面,请安装下列扩展:

See the documentation for each extension for additional configuration options.

Lua 沙盒

我们开发了一个用C语言编写的PHP扩展,称为LuaSandbox。 可以用作独立二进制文件的替代,并且性能更佳。 详细信息和安装指引,参见LuaSandbox

如果你最初安装扩展以使用Lua独立二进制文件,请务必使用以下配置设置更新LocalSettings.php

$wgScribuntoDefaultEngine = 'luasandbox';

有以下几个用于配置的变量:

$wgScribuntoDefaultEngine
选择默认引擎。 有效值为$wgScribuntoEngineConf中的键,默认为'luasandbox''luastandalone'
$wgScribuntoEngineConf
引擎配置的关联数组。 键时$wgScribuntoDefaultEngine的有效值,值是配置数据的关联数组。 每个配置数组都必须包含一个'class'键,命名要使用的ScribuntoEngineBase子类。

luastandalone

$wgScribuntoEngineConf中使用以下键用于Scribunto_LuaStandaloneEngine。 通常你会将其设为类似于以下内容

$wgScribuntoEngineConf['luastandalone']['key'] = 'value';
keyvaluedescription
luaPathnull 指定到Lua解释器的路径。
errorFilenull 指定文件的路径,可由Web服务器用户写入,其中将记录来自独立解释器的错误和调试输出。

默认情况下不记录独立解释器产生的错误输出。 配置输出:

$wgScribuntoEngineConf['luastandalone']['errorFile'] = '/path/to/file.log';
memoryLimit52,428,800 (50MB) 指定内存限制,以字节为单位。
cpuLimit7 指定CPU时间限制,以秒为单位(使用ulimit强制执行)。
allowEnvFuncsfalse 设为true以允许在模块内使用setfenv和getfenv。

luasandbox

針對Scribunto_LuaSandboxEngine,在$wgScribuntoEngineConf中使用以下键。 通常你会将其设为类似于以下内容

$wgScribuntoEngineConf['luasandbox']['鍵'] = '值';
keyvaluedescription
memoryLimit52,428,800 (50MB) 指定内存限制,以字节为单位。
cpuLimit7 指定CPU时间限制,以秒为单位。
profilerPeriod0.02 在Lua分析器的部分中指定轮询之间的时间。
allowEnvFuncsfalse 设为true以允许在模块内使用setfenv和getfenv。

用法

脚本在称为模块的命名空间中。 每个模块都有一系列函数,可以在像这样的维基文本语法中调用:

{{#invoke: Module_name | function_name | arg1 | arg2 | arg3 ... }}

Lua

学习 Lua

Lua是一种简单的编程语言,用于让初学者可以使用。 Lua的快速速成课程,可以尝试Learn Lua in 15 Minutes

Lua语言的最全面的介绍是书籍《Programming in Lua》。 第一版(适用于Lua 5.0)可在线获得,主要与Scribunto使用的Lua 5.1版本相关:

参考手册也比较有用:

Lua 环境

在Lua中,所有全局变量和函数的集合称为环境(environment)。

每个{{#invoke:}}调用会在一个单独的环境中运行。 一个{{#invoke:}}中定义的变量不会在另一个中有效。 这种限制是必要的,以维持维基文本解析器实现的灵活性。

脚本运行的环境与标准Lua中的环境不太一样。 这种差别会在扩展:Scribunto/Lua参考手册中指出。

调试控制台

调试控制台的使用例子
Extension:Scribunto/Debug console

编辑Lua模块时,编辑框底部会有一个“调试控制台”。 在这个调试控制台内,不需要保存或者甚至实际上创建一个Lua模块,就可以执行Lua代码。

故障排除

使用可点击的“Script error”链接排除故障。

注意,红色的脚本错误消息是可点击的,且会提供详细信息。

Cannot execute extensions/Scribunto/includes/Engines/LuaStandalone/binaries

设置与此扩展捆绑的Lua二进制文件的执行权限:[2]

chmod a+x /path/to/extensions/Scribunto/includes/Engines/LuaStandalone/binaries/$YOUR_OS/lua

如果强制执行SELinux,请将type设置为httpd_sys_script_exec_t[2]

chcon -t httpd_sys_script_exec_t /path/to/extensions/Scribunto/includes/Engines/LuaStandalone/binaries/yourOS/lua

Lua错误:内部错误:解释器以状态1退出。

When using the LuaStandalone engine (this is the default), errors along the lines of "Lua错误:内部错误:解释器以状态1退出。" may be generated if the standalone Lua interpreter cannot be executed or runs into various runtime errors. To obtain more information, assign a file path to $wgScribuntoEngineConf['luastandalone']['errorFile']. The interpreter's error output will be logged to the specified file, which should prove more helpful in tracking down the issue. The information in the debug log includes debugging information, which is why there is so much of it. You should be able to ignore any line beginning with "TX" or "RX".

If you're setting up Scribunto and are using IIS/Windows, this appears to be solved by commenting out line 132 in $wiki/extensions/Scribunto/includes/engines/LuaStandalone/LuaStandaloneInterpreter.php. In other words, change $cmd = '"' . $cmd . '"'; to // $cmd = '"' . $cmd . '"';.

Lua错误:内部错误:解释器以状态2退出。

When using the LuaStandalone engine (this is the default), status 2 suggests memory allocation errors, probably caused by settings that allocate inadequate memory space for PHP or Lua, or both. Assigning a file path to $wgScribuntoEngineConf['luastandalone']['errorFile'] and examining that output can be valuable in diagnosing memory allocation errors.

Increase PHP allocation in your PHP configuration; add the line memory_limit = 200M. This allocation of 200MB is often sufficient (as of MediaWiki 1.24) but can be increased as required. Set Scribunto's memory allocation in LocalSettings.php as a line:

$wgScribuntoEngineConf['luastandalone']['memoryLimit'] = 209715200; # bytes

Finally, depending on the server configuration, some installations may be helped by adding another LocalSettings.php line

$wgMaxShellMemory = 204800; # in KB

Note that all 3 memory limits are given in different units.

Lua error: Internal error: 2. on ARM architecture

If you're using an ARM architecture processor like on a RaspberryPi you'll face the error Lua error: Internal error: The interpreter exited with status 2. due to wrong delivered binary format of the Lua interpreter.

Check your Lua interpreter in:

/path/to/webdir/Scribunto/includes/Engines/LuaStandalone/binaries/lua5_1_5_linux_32_generic

Check the interpreter by using:

file lua 

The result should look like :

lua: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 3.2.0

The installed default Lua interpreter shows:

lua: ELF 32-bit LSB pie executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, for GNU/Linux 2.6.9,

look at the "Intel 80386" part what definitely is not correct.

Check in /usr/bin what version of Lua is installed on your system. If you have lua5.1 installed, you can either copy the interpreter to your lua5_1_5_linux_32_generic directory or set in your LocalSettings.php:

$wgScribuntoEngineConf['luastandalone']['luaPath'] = '/usr/bin/lua5.1';

At present don't set wgScribuntoEngineConf to /usr/bin/lua5.3, it'll lead to the "Internal error 1".

Lua错误:内部错误:解释器以状态24退出。

When using the LuaStandalone engine (this is the default), status 24 suggests CPU time limit errors, although those should be generating a "The time allocated for running scripts has expired" message instead. It would be useful to file a task in Phabricator and participate in determining why the XCPU signal isn't being caught.

Lua错误:内部错误:解释器以状态126退出。

When using the LuaStandalone engine (this is the default), errors along the lines of "Lua错误:内部错误:解释器以状态126退出。" may be generated if the standalone Lua interpreter cannot be executed. This generally arises from either of two causes:

  • The Lua executable file's permissions do not include Execute.

Set permissions as described under #Installation.

  • The server does not allow execution of files from the place where the executable is installed, e.g. the filesystem is mounted with the 'noexec' flag.

This often occurs with shared hosted servers. Remedies include adjusting $wgScribuntoEngineConf['luastandalone']['luaPath'] to point to a Lua 5.1 binary installed in an executable location, or adjusting or convincing the shared host to adjust the setting preventing execution.

  • On an ARM-based processor, you might have to install Lua 5.1 for ARM (on Debian/Ubuntu, run sudo apt install lua5.1) and add to the LocalSettings.php file $wgScribuntoEngineConf['luastandalone']['luaPath'] = '/usr/bin/lua5.1';

Error condition such as: Fatal exception of type MWException

Check the MediaWiki, PHP, or webserver logs for more details on the exception, or temporarily set $wgShowExceptionDetails to true.

version 'GLIBC_2.11' not found

If the above gives you errors such as "version 'GLIBC_2.11' not found", it means the version of the standard C library on your system is too old for the binaries provided with Scribunto. You should upgrade your C library, or use a version of Lua 5.1 compiled for the C library you do have installed. To upgrade your C library, your best option is usually to follow your distribution's instructions for upgrading packages (or for upgrading to a new release of the distribution, if applicable).

If you copy the lua binaries from Scribunto master (or from gerrit:77905), that should suffice, if you can't or don't want to upgrade your C library. The distributed binaries were recently recompiled against an older version of glibc, so the minimum is now 2.3 rather than 2.11.

Scribunto文件中的Lua错误

这里的错误包括:

  • attempt to index field 'text' (a nil value)(尝试访问'text'字段,其值为nil)
  • 位于mw.html.lua的253行的Lua错误:提供了无效类:

如果你在尝试使用从WMF wiki导入的模块时遇到此类错误,则很可能你的Scribunto版本已过期。

如有可能,请升级,对于高级用户,你可能需要找到需要的新提交,然后将其优选到你的本地安装中。

preg_replace_callback(): Compilation failed: unknown property name after \P or \p at offset 7

preg_replace_callback(): Compilation failed: unknown property name after \P or \p at offset 7

  • this usually indicates an incompatible version of PCRE; you'll need to update to >= 8.10
  • @todo: link to instructions on how to upgrade

Lua错误

如果你从维基百科复制模板,然后在Scribunto调用(例如,使用了{{#invoke:}}的模板)的位置收到大量红色“Lua错误:x”的消息,这可能意味着你没有导入所需的所有内容。 确保在zh:Special:Export导出时勾选“包括模板”。

从另一个wiki导入页面时,导入数据中的模板或模块也可能覆盖具有相同标题的现有模板或模块,这可能会破坏依赖于覆盖版本的现有页面、模板和模块。

空白屏幕

确保你的扩展版本可以适用于你的MediaWiki版本。

许可协议

该扩展包含使用GNU通用公共许可证v2.0或更高版本(GPL-2.0+)的代码以及使用MIT许可证(MIT)的代码。

设计文档

其他页面

參見

一般
  • Lua Wikibase client - Scribunto扩展的功能。
  • Commons:Lua - 在Wikimedia Commons上可能有使用Lua模块的具体说明,包括安装的额外Lua扩展(例如,用于国际化的本地支持以及解析或播放媒体)。 一些通用模块可以在其他wiki中以各种语言重新使用(除了针对具有专用名称的方针、命名空间或项目/维护页面的特定调整)。 如果可能,应该在Wikimedia Commons上测试和国际化可以在wiki中广泛重新使用的模块。
  • w:Help:Lua - 维基百科可能有使用Lua模块的具体说明,包括安装的附加Lua扩展(包括用于集成维基数据和维基共享资源内容、生成复杂的信息框和导航框,或根据适用方针促进wiki内容的一般管理/维护)。 其他一些本地化的维基百科版本(或其他项目,如维基词典、维基共享资源或维基新闻)也可能有自己的需求和Lua模块。
  • d:Help:Lua - 可能会有关于在维基数据上使用Lua模块的具体说明,包括安装的额外Lua扩展(例如,用于国际化的本地支持和数据库查询)

外部链接

注释

  1. i.e. 如果服务器“php.ini”文件中的disable_functions数组中列出了proc_open,Scribunto将不起作用。 如果是,你可能收到像proc_open(): open_basedir restriction in effect. File(/dev/null) is not within the allowed path(s):这样的错误消息 如果你正在使用Plesk且拥有了足够的权限,你也许能够在PHP设置中为你的域名或者子域名设置open_basedir。 尝试将{WEBSPACEROOT}{/}{:}{TMP}{/}更换成{WEBSPACEROOT}{/}{:}{TMP}{/}{:}/dev/null{:}/bin/bash
  2. 1 2 3 The name of the engines folder changed from lowercase to capitalised in 2022.
Category:Extensions used on Wikimedia/zh#Scribunto/zh Category:Scribunto extensions/zh#%20 Category:ContentHandler extensions/zh Category:GPL licensed extensions/zh Category:MIT licensed extensions/zh
Category:All extensions/zh Category:ArticleViewHeader extensions/zh Category:CodeEditorGetPageLanguage extensions/zh Category:CodeMirrorGetMode extensions/zh Category:ContentHandlerDefaultModelFor extensions/zh Category:ContentHandler extensions/zh Category:EditFilterMergedContent extensions/zh Category:EditPage::showReadOnlyForm:initial extensions/zh Category:EditPage::showStandardInputs:options extensions/zh Category:EditPageBeforeEditButtons extensions/zh Category:Extensions available as Debian packages/zh Category:Extensions bundled with MediaWiki 1.34/zh Category:Extensions in Wikimedia version control/zh Category:Extensions included in BlueSpice/zh Category:Extensions included in Canasta/zh Category:Extensions included in Fandom/zh Category:Extensions included in Miraheze/zh Category:Extensions included in MyWikis/zh Category:Extensions included in ProWiki/zh Category:Extensions included in ShoutWiki/zh Category:Extensions included in WikiForge/zh Category:Extensions included in wiki.gg/zh Category:Extensions used on Wikimedia/zh Category:Extensions with unknown license/zh Category:GPL licensed extensions/zh Category:MIT licensed extensions/zh Category:Pages using Lc zh with unknown or deprecated arguments Category:Pages using deprecated NoteTA template Category:ParserClearState extensions/zh Category:ParserCloned extensions/zh Category:ParserFirstCallInit extensions/zh Category:ParserLimitReportFormat extensions/zh Category:ParserLimitReportPrepare extensions/zh Category:Parser extensions/zh Category:Scribunto extensions/zh Category:SoftwareInfo extensions/zh Category:Stable extensions/zh