Debian Codegraph @

extensions/Scribunto/includes/Engines/LuaCommon/LuaModule.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Scribunto\Engines\LuaCommon;
4
5 use MediaWiki\Extension\Scribunto\ScribuntoException;
6 use MediaWiki\Extension\Scribunto\ScribuntoModuleBase;
7 use MediaWiki\Parser\PPFrame;
8 use MediaWiki\Status\Status;
9
10 class LuaModule extends ScribuntoModuleBase {
11 /**
12 * @var mixed
13 */
14 protected $initChunk;
15
16 /**
17 * @var LuaEngine
18 */
19 protected $engine;
20
21 /**
22 * @param LuaEngine $engine
23 * @param string $code
24 * @param string|bool $chunkName
25 */
26 public function __construct( LuaEngine $engine, $code, $chunkName ) {
27 parent::__construct( $engine, $code, $chunkName );
28 }
29
30 public function validate() {
31 try {
32 $this->getInitChunk();
33 } catch ( ScribuntoException $e ) {
34 return $e->toStatus();
35 }
36 return Status::newGood();
37 }
38
39 /**
40 * Get the chunk which, when called, will return the export table.
41 * @return mixed
42 */
43 public function getInitChunk() {
44 if ( !$this->initChunk ) {
45 $this->initChunk = $this->engine->getInterpreter()->loadString(
46 $this->code,
47 // Prepending an "=" to the chunk name avoids truncation or a "[string" prefix
48 '=' . $this->chunkName );
49 }
50 return $this->initChunk;
51 }
52
53 /**
54 * Invoke a function within the module. Return the expanded wikitext result.
55 *
56 * @param string $name
57 * @param PPFrame $frame
58 * @throws ScribuntoException
59 * @return string|null
60 */
61 public function invoke( $name, $frame ) {
62 $ret = $this->engine->executeModule( $this->getInitChunk(), $name, $frame );
63
64 if ( !isset( $ret ) ) {
65 throw $this->engine->newException(
66 'scribunto-common-nosuchfunction', [ 'args' => [ $name ] ]
67 );
68 }
69 if ( !$this->engine->getInterpreter()->isLuaFunction( $ret ) ) {
70 throw $this->engine->newException(
71 'scribunto-common-notafunction', [ 'args' => [ $name ] ]
72 );
73 }
74
75 $result = $this->engine->executeFunctionChunk( $ret, $frame );
76 return $result[0] ?? null;
77 }
78 }