Search code examples
macosluamediawikiscribunto

Using wikipedia lua modules in a script


OK, so, let me first make it clear that I have no idea how Lua really works, never written more than 10 lines of code in it and that I'm generally confused about how MediaWiki + Scribunto + Lua works...

So...

I have this wiktionary module/template:

https://en.wiktionary.org/w/index.php?title=Module:pl-IPA&action=edit

It looks to me like perfectly valid lua code.

When I try to run some simple example (e.g. calling the function export.convert_to_IPA("something")), Lua seems to complain that it doesn't know what mw is.

From what I know, mw stands for MediaWiki and is a reference to the Scribunto module.

The question is... how do I find and import this mw module in a simple Lua script running from the terminal... with the Lua interpreter, on Mac?


Solution

  • In general, you can't use mw in standalone Lua without MediaWiki. Almost all of the functions it provides are callbacks into MediaWiki's PHP. However, it looks like you just want to use convert_to_IPA rather than template_IPA, and the only usage of mw that the former has is this line: for ch in mw.ustring.gmatch(mw.ustring.lower(word), ".") do. You're lucky: mw.ustring is the exception to the rule, and it does have a pure-Lua implementation: https://github.com/wikimedia/mediawiki-extensions-Scribunto/tree/master/includes/engines/LuaCommon/lualib/ustring

    Here's an example of how you can use it (assuming your current directory contains both pl-IPA.lua and the ustring directory I linked):

    $ lua5.1
    Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
    > mw = {ustring = require 'ustring/ustring'}
    > export = require 'pl-IPA'
    > =export.convert_to_IPA("something")
    sɔˈmɛt.xʲink
    >