Extension talk:Scribunto/Example extension

From mediawiki.org
Jump to navigation Jump to search


I was interested to do try the same socket extension in my privat wiki without success.

Did you arrange the extension to work on your wiki ?

Regards G

Hi! Sorry, I haven't had any luck. I ended up getting frustrated with the lack of documentation and lack of response and just implementing the functionality I needed as a separate extension. I guess Lua's useful if you want to write in something better than wiki markup, but for anything more complicated than loops and builtin string processing functions I guess you still have to go to PHP.
  • Hacking into luasandbox/library.c could help. You have to whitelist your lua library in luasandbox_allowed_globals [] and include the C headers file: #include <lua-your-lib.h>, which ought to be present in /usr/include/lua5.1, after you have installed the -dev package. Then, within luasandbox_lib_register(lua_State * L TSRMLS_DC) you have to register that library:
lua_pushcfunction(L, luaopen_your_lib);
lua_call(L, 0, 0);

luaopen_your_lib is the external function declaration in lua-your-lib.h.

Unfortunately, this is not enough. When I remake luasandbox, I get error "undefined symbol luaopen_your_lib", although the .h file is successfully included and .so file is present in the system. So, something more has yet to be done. If this information helps you to load your library, please let me know how.
Alex Mashin (talk) 14:55, 22 February 2014 (UTC)

  • I managed to advance somewhat further. Instead of including <lua-your-lib.h>, I downloaded source code for the lua library into luasandbox directory and included in luasandbox/library.c all C files from the library needed for successful compilation. Now luasandbox compiles and Scribunto works, but by the time package.lua starts, the global Lua variable that should contain the library is nil and so it is in Scribunto modules.
    Alex Mashin (talk) 17:48, 22 February 2014 (UTC)


And finally, I managed to connect an external library — the PCRE part of lrexlib in my case. The library code was copied into luasandbox/include directory, necessary files included in luasandbox/library.c:

#include "include/lrexlib-2.6.0/src/common.c"
#include "include/lrexlib-2.6.0/src/pcre/lpcre_f.c"
#include "include/lrexlib-2.6.0/src/pcre/lpcre.c"

Then after line 153 I inserted

lua_pushcfunction(L, luaopen_rex_pcre);
lua_setglobal(L, "rex_pcre");

After that I rebuilt luasandbox. Now, the library can be invoked in a Lua module thus:

local rex = rex_pcre ()
local locale = rex.maketables ()
local regex = rex.new ('\\d', 'ix', locale)

Alex Mashin (talk) 17:15, 6 March 2014 (UTC)

  • And now luasandbox moved to C++11 and this will not compile, thank you very much.
    Alex Mashin (talk) 14:52, 28 February 2015 (UTC)
    • I managed to shove rec_pcre from lrexlib down luasandbox's throat again. Added extern "C" int luaopen_rex_pcre(lua_State*); in the beginning of library.c, added lua_pushcfunction(L, luaopen_rex_pcre); lua_setglobal(L, "rex_pcre"); after purge of non-whitelisted globals (about line 153), added ;/usr/lib/x86_64-linux-gnu/liblua5.1-rex-pcre.so in line 26 of config.cmake, then cmake . && sudo make install.
      Alex Mashin (talk) 22:42, 14 May 2015 (UTC)

Some comments on the extension[edit]

The idea of writing a MediaWiki extension in PHP to load a Lua C library seems wrong. If even it had worked it was likely to cause overhead by involving PHP. Anyway, require in Lua files will not work because global require is overloaded in package.lua.
Alex Mashin (talk) 17:55, 22 February 2014 (UTC)