Topic on Extension talk:Scribunto

Squeak24 (talkcontribs)

Hi,

I was wondering if it is possible to use the XML2LUA plugin to convert XML content into Lua, which can then in turn display XML content into Scribuntu.


I have tried to install XML2LUA into the Standalone Lua, the 64bit version on my Windows Server 2012. But when I run luarocks make, it doesn't install.


Is this possible, or should I give up?


Any help is appreciated.


Gary

FeRDNYC (talkcontribs)

@Squeak24 : It might be possible, with a lot of work, but it's not trivially possible. You can't import arbitrary Lua modules into the Scribunto environment.

For starters, it doesn't support filesystem access at all — the require statement "Can fetch certain built-in modules distributed with Scribunto, as well as modules present in the Module namespace of the wiki. To fetch wiki modules, use the full page name including the namespace. Cannot otherwise access the local filesystem."

The Lua standard library modules that are available are only present because they've been explicitly wrapped into the sandboxed Lua environment by the Scribunto setup code. At runtime, as the quote above notes, the only namespace accessible to Module code is the rest of the Module namespace on-wiki.

That being said, the fact that xml2lua is a pure-Lua package does mean that there are some options open to you. First off, you'd have to get a copy of the package itself — luarocks is unusable in the Scribunto environment because it uses local filesystem access to download and install packages. You could get a copy of the module from the author's Github repo at manoelcampos/xml2lua, but even then you need luarocks to build it into a loadable package.

I'm on Fedora Linux, which distributes lua-5.4, and its luarocks package is configured to use that environment by default. But Fedora's compat-lua package provides a legacy lua-5.1 build. After installing that, I created an /etc/luarocks/config-5.1.lua configuration using the existing /etc/luarocks/config-5.4.lua file as a guide, and with that in place I was able to run luarocks --local --lua-version=5.1 install xml2lua to get a copy of the package installed into $HOME/.luarocks/share/lua/5.1/.

Now, this is where it being a pure Lua package helps, because we know that the package doesn't contain any C-library calls, which the sandbox environment doesn't support. Hopefully there are no filesystem accesses or other invalid code either. I haven't looked, to be honest, you'll have to do that part of the investigation if you're interested in trying to make this work.

Because the package is pure Lua code, there are two possibilities for making it available in Scribunto:

  1. You could hack xml2lua into the Scribunto environment as a Lua library package exported into the sandbox. That'd involve at the very least...
    1. Editing the Scribunto/includes/engines/LuaStandalone/MWServer.lua code and adding the pieces of xml2lua to the allowedGlobals object in MWServer:newEnvironment. (You'll see where tables and math are already listed as allowed global libraries.)
    2. (I think...) Modifying Scribunto/includes/engines/LuaCommon/lualib/package.lua and adding the pieces of xml2lua as package.loaded.module.
    3. Possibly having to write some wrapper PHP code in Scribunto/includes/engines/LuaCommon/, to wire up the functions made available by the xml2lua modules.
    4. Doing a lot of finger-crossing, along with some finding of religion and/or ritual sacrifice, as appropriate.
  2. You could import the library's modules into your wiki as Module:-namespace pages that run under the Scribunto environment. (IMHO, the far more sane option, though it's still a big "may or may not work" hail-Mary.) To do that, you'd...
    1. Create a new Module: namespace page for each of the xml2lua library's .lua files, and paste in the code.
    2. Edit the code so that (for instance) when Module:xml2lua loads its parser module, instead of using local XmlParser = require("XmlParser") as the original xml2lua.lua file does, it uses local XmlParser = require("Module:XmlParser") to load your imported copy of the code (which you naturally created by pasting in the contents of XmlParser.lua).
    3. Sanitize the rest of the code to remove any problem Lua code that may be in there; I can't even begin to guess what might constitute problem Lua code, but the sandbox's parser will tell you pretty quick once you've pasted it into the editor and tried to run it.
    4. Do a lot of the same finger-crossing, prayer-and-or-ritual-sacrifice stuff, probably.

Is it worth it? Only you can decide that. But it might actually be an interesting challenge, and completely permissible under the terms of the package's MIT license.

Reply to "XML2LUA"