node.jsmediawiki-extensions

Call NodeJS program in a MediaWiki extension


I'm creating a MediaWiki extension to enable support for Argdown (https://argdown.org/) on my wiki. The Argdown parser is written in NodeJS; what's the best way to have my extension's PHP file run a NodeJS program?

I see some examples on the web saying to do it with exec, but my sysadmin says that's a bad idea - he says exec creates security and performance problems. Is there a better way?


Solution

  • MediaWiki has a shell framework for just this purpose. After putting these at the top of my file...

    use MediaWiki\Shell\Shell;
    use MediaWiki\Logger\LoggerFactory;
    

    ...here's what I put in my PHP function:

    global $IP;
    $result = Shell::command( "/usr/local/bin/node", "$IP/extensions/Argdown/helloWorld.js" )->execute();
    $stdout = $result->getStdout();
    $stderr = $result->getStderr();
    $ret = "<p>Stdout: $stdout</p>";
    $ret .= "<p>Stderr: $stderr</p>";
    return $ret;
    

    I should be able to just put "node" instead of "/usr/local/bin/node", but that's not working and I haven't figured out why yet - writing node on the command line works fine.

    Thanks to the volunteers on the MediaWiki Discord for their help!