2012-04-13 10:38:12 +00:00
|
|
|
<?php
|
|
|
|
|
2022-08-03 12:31:15 +00:00
|
|
|
namespace MediaWiki\Extension\Scribunto\Engines\LuaCommon;
|
|
|
|
|
|
|
|
use Exception;
|
2022-07-30 18:22:25 +00:00
|
|
|
use MediaWiki\Extension\Scribunto\Engines\LuaSandbox\LuaSandboxInterpreter;
|
2022-04-07 23:12:32 +00:00
|
|
|
use MediaWiki\Extension\Scribunto\Scribunto;
|
2020-02-21 20:49:51 +00:00
|
|
|
use MediaWiki\Extension\Scribunto\ScribuntoContent;
|
2022-04-07 23:12:32 +00:00
|
|
|
use MediaWiki\Extension\Scribunto\ScribuntoEngineBase;
|
|
|
|
use MediaWiki\Extension\Scribunto\ScribuntoException;
|
2024-01-05 18:24:06 +00:00
|
|
|
use MediaWiki\Html\Html;
|
2024-10-20 09:54:11 +00:00
|
|
|
use MediaWiki\Json\FormatJson;
|
2021-06-03 16:33:54 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2024-06-10 18:41:33 +00:00
|
|
|
use MediaWiki\Parser\Parser;
|
2024-10-20 09:54:11 +00:00
|
|
|
use MediaWiki\Parser\PPFrame;
|
2023-08-19 04:19:07 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2023-06-09 12:43:44 +00:00
|
|
|
use RuntimeException;
|
2017-08-21 21:06:34 +00:00
|
|
|
use Wikimedia\ScopedCallback;
|
|
|
|
|
2022-08-03 12:31:15 +00:00
|
|
|
abstract class LuaEngine extends ScribuntoEngineBase {
|
2013-02-15 23:41:20 +00:00
|
|
|
/**
|
|
|
|
* Libraries to load. See also the 'ScribuntoExternalLibraries' hook.
|
2024-08-26 15:24:52 +00:00
|
|
|
* @var array<string,class-string<LibraryBase>> Maps module names to PHP classes or definition arrays
|
2013-02-15 23:41:20 +00:00
|
|
|
*/
|
2017-06-15 17:19:00 +00:00
|
|
|
protected static $libraryClasses = [
|
2022-08-03 12:31:15 +00:00
|
|
|
'mw.site' => SiteLibrary::class,
|
|
|
|
'mw.uri' => UriLibrary::class,
|
|
|
|
'mw.ustring' => UstringLibrary::class,
|
|
|
|
'mw.language' => LanguageLibrary::class,
|
|
|
|
'mw.message' => MessageLibrary::class,
|
|
|
|
'mw.title' => TitleLibrary::class,
|
|
|
|
'mw.text' => TextLibrary::class,
|
|
|
|
'mw.html' => HtmlLibrary::class,
|
|
|
|
'mw.hash' => HashLibrary::class,
|
2017-06-15 17:19:00 +00:00
|
|
|
];
|
2013-01-25 17:53:18 +00:00
|
|
|
|
2013-02-15 23:41:20 +00:00
|
|
|
/**
|
|
|
|
* Paths for modules that may be loaded from Lua. See also the
|
|
|
|
* 'ScribuntoExternalLibraryPaths' hook.
|
2024-08-26 15:24:52 +00:00
|
|
|
* @var string[] Paths
|
2013-02-15 23:41:20 +00:00
|
|
|
*/
|
2017-06-15 17:19:00 +00:00
|
|
|
protected static $libraryPaths = [
|
2013-02-14 17:38:03 +00:00
|
|
|
'.',
|
|
|
|
'luabit',
|
|
|
|
'ustring',
|
2017-06-15 17:19:00 +00:00
|
|
|
];
|
2013-02-14 17:38:03 +00:00
|
|
|
|
2020-12-18 19:04:17 +00:00
|
|
|
/** @var bool */
|
Added more Lua environment features
Package library:
* Added a simulation of the Lua 5.1 package library.
* Removed mw.import(), replaced it with a package loader. Packages can be
retrieved from the wiki, using require('Module:Foo'), or from files
distributed with Scribunto, using require('foo'). The "Module:" prefix allows
for source compatibility with existing Lua code.
* Added a couple of libraries from LuaForge: luabit and stringtools.
* Made fetchModuleFromParser() return null on error instead of throwing an
exception, to more easily support the desired behaviour of the package loader,
which needs to return null on error.
* Renamed mw.setupEnvironment() to mw.setup() since it is setting up things
other than the environment now.
* In MWServer:handleRegisterLibrary(), remove the feature which interprets dots
in library names, since LuaSandbox doesn't support this.
Improved module isolation and related refactoring:
* Expose restricted versions of getfenv() and setfenv() to user Lua code.
Requires luasandbox r114952.
* Don't cache the export list returned by module execution for later function
calls. This breaks isolation of #invoke calls, since the local variables are
persistent.
* Removed ScribuntoFunctionBase and its children, since it doesn't really have
a purpose if it can't cache anything. Instead, invoke functions using a module
method called invoke().
* Removed Module::initialize(), replaced it with a validate() function. This is
a more elegant interface and works better with the new module caching scheme.
* Use a Status object for the return value of Engine::validate() instead of an
array. Use the formatting facilities of the Status class.
Other:
* Removed "too many returns" error, doesn't fit in with Lua conventions.
* Use the standalone engine by default, so that the extension will work without
configuration for more people.
* Added an accessor for $engine->interpreter
* Fix mw.clone() to correctly clone metatables
* If the standalone interpreter exits due to an error, there are some contexts
where the initial error will be caught and ignored, and the user will see the
error from checkValid() instead. In this case, rethrow the original error for
a more informative message.
* Load mw.lua into the initial standalone environment, to reduce code
duplication between mw.lua and MWServer.lua.
* Fixed a bug in Scribunto_LuaStandaloneInterpreter::handleCall() for functions
that return no results.
* Fixed a bug in encodeLuaVar() for strings with "\r". Added test case.
* In MWServer.lua, don't call error() for internal errors, instead just print
the error and exit. This avoids a protocol violation when an error is
encountered from within handleCall().
* Added lots of documentation. Lua doc comments are in LuaDoc format.
Change-Id: Ie2fd572c362bedf02f45d3fa5352a5280e034740
2012-04-18 03:46:18 +00:00
|
|
|
protected $loaded = false;
|
2014-06-18 17:17:04 +00:00
|
|
|
|
|
|
|
/**
|
2022-08-03 12:31:15 +00:00
|
|
|
* @var LuaInterpreter|null
|
2014-06-18 17:17:04 +00:00
|
|
|
*/
|
2013-04-12 14:38:14 +00:00
|
|
|
protected $interpreter;
|
2014-06-18 17:17:04 +00:00
|
|
|
|
2014-10-10 09:02:03 +00:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2012-05-22 03:56:07 +00:00
|
|
|
protected $mw;
|
2014-10-09 19:49:40 +00:00
|
|
|
|
|
|
|
/**
|
2024-08-26 15:24:52 +00:00
|
|
|
* @var array<string,?PPFrame>
|
2014-10-09 19:49:40 +00:00
|
|
|
*/
|
2017-06-15 17:19:00 +00:00
|
|
|
protected $currentFrames = [];
|
2020-12-18 19:04:17 +00:00
|
|
|
/**
|
2024-08-26 15:24:52 +00:00
|
|
|
* @var array<string,string>|null
|
2020-12-18 19:04:17 +00:00
|
|
|
*/
|
2017-06-15 17:19:00 +00:00
|
|
|
protected $expandCache = [];
|
2020-12-18 19:04:17 +00:00
|
|
|
/**
|
2024-08-26 15:24:52 +00:00
|
|
|
* @var array<string,array|class-string<LibraryBase>>
|
2020-12-18 19:04:17 +00:00
|
|
|
*/
|
2017-06-15 17:19:00 +00:00
|
|
|
protected $availableLibraries = [];
|
2012-05-22 03:56:07 +00:00
|
|
|
|
2020-05-30 00:32:16 +00:00
|
|
|
private const MAX_EXPAND_CACHE_SIZE = 100;
|
2012-04-13 10:38:12 +00:00
|
|
|
|
2017-06-24 21:58:12 +00:00
|
|
|
/**
|
|
|
|
* If luasandbox is installed and usable then use it,
|
|
|
|
* otherwise
|
|
|
|
*
|
|
|
|
* @param array $options
|
2022-08-03 12:31:15 +00:00
|
|
|
* @return LuaEngine
|
2017-06-24 21:58:12 +00:00
|
|
|
*/
|
|
|
|
public static function newAutodetectEngine( array $options ) {
|
2024-03-10 12:42:04 +00:00
|
|
|
$engineConf = MediaWikiServices::getInstance()->getMainConfig()->get( 'ScribuntoEngineConf' );
|
2017-06-24 21:58:12 +00:00
|
|
|
$engine = 'luastandalone';
|
|
|
|
try {
|
2022-07-30 18:22:25 +00:00
|
|
|
LuaSandboxInterpreter::checkLuaSandboxVersion();
|
2017-06-24 21:58:12 +00:00
|
|
|
$engine = 'luasandbox';
|
2022-08-03 12:31:15 +00:00
|
|
|
} catch ( LuaInterpreterNotFoundError | LuaInterpreterBadVersionError $e ) {
|
2017-06-24 21:58:12 +00:00
|
|
|
// pass
|
|
|
|
}
|
|
|
|
|
|
|
|
unset( $options['factory'] );
|
|
|
|
|
2021-09-09 22:24:04 +00:00
|
|
|
// @phan-suppress-next-line PhanTypeMismatchReturnSuperType
|
2024-03-10 12:42:04 +00:00
|
|
|
return Scribunto::newEngine( $options + $engineConf[$engine] );
|
2017-06-24 21:58:12 +00:00
|
|
|
}
|
|
|
|
|
Added more Lua environment features
Package library:
* Added a simulation of the Lua 5.1 package library.
* Removed mw.import(), replaced it with a package loader. Packages can be
retrieved from the wiki, using require('Module:Foo'), or from files
distributed with Scribunto, using require('foo'). The "Module:" prefix allows
for source compatibility with existing Lua code.
* Added a couple of libraries from LuaForge: luabit and stringtools.
* Made fetchModuleFromParser() return null on error instead of throwing an
exception, to more easily support the desired behaviour of the package loader,
which needs to return null on error.
* Renamed mw.setupEnvironment() to mw.setup() since it is setting up things
other than the environment now.
* In MWServer:handleRegisterLibrary(), remove the feature which interprets dots
in library names, since LuaSandbox doesn't support this.
Improved module isolation and related refactoring:
* Expose restricted versions of getfenv() and setfenv() to user Lua code.
Requires luasandbox r114952.
* Don't cache the export list returned by module execution for later function
calls. This breaks isolation of #invoke calls, since the local variables are
persistent.
* Removed ScribuntoFunctionBase and its children, since it doesn't really have
a purpose if it can't cache anything. Instead, invoke functions using a module
method called invoke().
* Removed Module::initialize(), replaced it with a validate() function. This is
a more elegant interface and works better with the new module caching scheme.
* Use a Status object for the return value of Engine::validate() instead of an
array. Use the formatting facilities of the Status class.
Other:
* Removed "too many returns" error, doesn't fit in with Lua conventions.
* Use the standalone engine by default, so that the extension will work without
configuration for more people.
* Added an accessor for $engine->interpreter
* Fix mw.clone() to correctly clone metatables
* If the standalone interpreter exits due to an error, there are some contexts
where the initial error will be caught and ignored, and the user will see the
error from checkValid() instead. In this case, rethrow the original error for
a more informative message.
* Load mw.lua into the initial standalone environment, to reduce code
duplication between mw.lua and MWServer.lua.
* Fixed a bug in Scribunto_LuaStandaloneInterpreter::handleCall() for functions
that return no results.
* Fixed a bug in encodeLuaVar() for strings with "\r". Added test case.
* In MWServer.lua, don't call error() for internal errors, instead just print
the error and exit. This avoids a protocol violation when an error is
encountered from within handleCall().
* Added lots of documentation. Lua doc comments are in LuaDoc format.
Change-Id: Ie2fd572c362bedf02f45d3fa5352a5280e034740
2012-04-18 03:46:18 +00:00
|
|
|
/**
|
|
|
|
* Create a new interpreter object
|
2022-08-03 12:31:15 +00:00
|
|
|
* @return LuaInterpreter
|
Added more Lua environment features
Package library:
* Added a simulation of the Lua 5.1 package library.
* Removed mw.import(), replaced it with a package loader. Packages can be
retrieved from the wiki, using require('Module:Foo'), or from files
distributed with Scribunto, using require('foo'). The "Module:" prefix allows
for source compatibility with existing Lua code.
* Added a couple of libraries from LuaForge: luabit and stringtools.
* Made fetchModuleFromParser() return null on error instead of throwing an
exception, to more easily support the desired behaviour of the package loader,
which needs to return null on error.
* Renamed mw.setupEnvironment() to mw.setup() since it is setting up things
other than the environment now.
* In MWServer:handleRegisterLibrary(), remove the feature which interprets dots
in library names, since LuaSandbox doesn't support this.
Improved module isolation and related refactoring:
* Expose restricted versions of getfenv() and setfenv() to user Lua code.
Requires luasandbox r114952.
* Don't cache the export list returned by module execution for later function
calls. This breaks isolation of #invoke calls, since the local variables are
persistent.
* Removed ScribuntoFunctionBase and its children, since it doesn't really have
a purpose if it can't cache anything. Instead, invoke functions using a module
method called invoke().
* Removed Module::initialize(), replaced it with a validate() function. This is
a more elegant interface and works better with the new module caching scheme.
* Use a Status object for the return value of Engine::validate() instead of an
array. Use the formatting facilities of the Status class.
Other:
* Removed "too many returns" error, doesn't fit in with Lua conventions.
* Use the standalone engine by default, so that the extension will work without
configuration for more people.
* Added an accessor for $engine->interpreter
* Fix mw.clone() to correctly clone metatables
* If the standalone interpreter exits due to an error, there are some contexts
where the initial error will be caught and ignored, and the user will see the
error from checkValid() instead. In this case, rethrow the original error for
a more informative message.
* Load mw.lua into the initial standalone environment, to reduce code
duplication between mw.lua and MWServer.lua.
* Fixed a bug in Scribunto_LuaStandaloneInterpreter::handleCall() for functions
that return no results.
* Fixed a bug in encodeLuaVar() for strings with "\r". Added test case.
* In MWServer.lua, don't call error() for internal errors, instead just print
the error and exit. This avoids a protocol violation when an error is
encountered from within handleCall().
* Added lots of documentation. Lua doc comments are in LuaDoc format.
Change-Id: Ie2fd572c362bedf02f45d3fa5352a5280e034740
2012-04-18 03:46:18 +00:00
|
|
|
*/
|
2018-11-09 19:31:08 +00:00
|
|
|
abstract protected function newInterpreter();
|
2012-04-13 10:38:12 +00:00
|
|
|
|
2014-06-18 17:17:04 +00:00
|
|
|
/**
|
2014-10-10 09:02:03 +00:00
|
|
|
* @param string $text
|
|
|
|
* @param string|bool $chunkName
|
2022-08-03 12:31:15 +00:00
|
|
|
* @return LuaModule
|
2014-06-18 17:17:04 +00:00
|
|
|
*/
|
2012-04-13 10:38:12 +00:00
|
|
|
protected function newModule( $text, $chunkName ) {
|
2022-08-03 12:31:15 +00:00
|
|
|
return new LuaModule( $this, $text, $chunkName );
|
2012-04-13 10:38:12 +00:00
|
|
|
}
|
|
|
|
|
2014-06-18 17:17:04 +00:00
|
|
|
/**
|
2014-10-10 09:02:03 +00:00
|
|
|
* @param string $message
|
2014-06-18 17:17:04 +00:00
|
|
|
* @param array $params
|
2022-08-03 12:31:15 +00:00
|
|
|
* @return LuaError
|
2014-06-18 17:17:04 +00:00
|
|
|
*/
|
2017-06-15 17:19:00 +00:00
|
|
|
public function newLuaError( $message, $params = [] ) {
|
2022-08-03 12:31:15 +00:00
|
|
|
return new LuaError( $message, $this->getDefaultExceptionParams() + $params );
|
2012-04-23 11:36:13 +00:00
|
|
|
}
|
|
|
|
|
2012-09-05 09:39:58 +00:00
|
|
|
public function destroy() {
|
|
|
|
// Break reference cycles
|
|
|
|
$this->interpreter = null;
|
2019-11-07 19:37:46 +00:00
|
|
|
$this->mw = [];
|
2012-09-05 09:39:58 +00:00
|
|
|
$this->expandCache = null;
|
|
|
|
parent::destroy();
|
|
|
|
}
|
|
|
|
|
Added more Lua environment features
Package library:
* Added a simulation of the Lua 5.1 package library.
* Removed mw.import(), replaced it with a package loader. Packages can be
retrieved from the wiki, using require('Module:Foo'), or from files
distributed with Scribunto, using require('foo'). The "Module:" prefix allows
for source compatibility with existing Lua code.
* Added a couple of libraries from LuaForge: luabit and stringtools.
* Made fetchModuleFromParser() return null on error instead of throwing an
exception, to more easily support the desired behaviour of the package loader,
which needs to return null on error.
* Renamed mw.setupEnvironment() to mw.setup() since it is setting up things
other than the environment now.
* In MWServer:handleRegisterLibrary(), remove the feature which interprets dots
in library names, since LuaSandbox doesn't support this.
Improved module isolation and related refactoring:
* Expose restricted versions of getfenv() and setfenv() to user Lua code.
Requires luasandbox r114952.
* Don't cache the export list returned by module execution for later function
calls. This breaks isolation of #invoke calls, since the local variables are
persistent.
* Removed ScribuntoFunctionBase and its children, since it doesn't really have
a purpose if it can't cache anything. Instead, invoke functions using a module
method called invoke().
* Removed Module::initialize(), replaced it with a validate() function. This is
a more elegant interface and works better with the new module caching scheme.
* Use a Status object for the return value of Engine::validate() instead of an
array. Use the formatting facilities of the Status class.
Other:
* Removed "too many returns" error, doesn't fit in with Lua conventions.
* Use the standalone engine by default, so that the extension will work without
configuration for more people.
* Added an accessor for $engine->interpreter
* Fix mw.clone() to correctly clone metatables
* If the standalone interpreter exits due to an error, there are some contexts
where the initial error will be caught and ignored, and the user will see the
error from checkValid() instead. In this case, rethrow the original error for
a more informative message.
* Load mw.lua into the initial standalone environment, to reduce code
duplication between mw.lua and MWServer.lua.
* Fixed a bug in Scribunto_LuaStandaloneInterpreter::handleCall() for functions
that return no results.
* Fixed a bug in encodeLuaVar() for strings with "\r". Added test case.
* In MWServer.lua, don't call error() for internal errors, instead just print
the error and exit. This avoids a protocol violation when an error is
encountered from within handleCall().
* Added lots of documentation. Lua doc comments are in LuaDoc format.
Change-Id: Ie2fd572c362bedf02f45d3fa5352a5280e034740
2012-04-18 03:46:18 +00:00
|
|
|
/**
|
|
|
|
* Initialise the interpreter and the base environment
|
|
|
|
*/
|
2012-04-13 10:38:12 +00:00
|
|
|
public function load() {
|
2015-06-21 04:38:39 +00:00
|
|
|
if ( $this->loaded ) {
|
2012-04-13 10:38:12 +00:00
|
|
|
return;
|
|
|
|
}
|
Added more Lua environment features
Package library:
* Added a simulation of the Lua 5.1 package library.
* Removed mw.import(), replaced it with a package loader. Packages can be
retrieved from the wiki, using require('Module:Foo'), or from files
distributed with Scribunto, using require('foo'). The "Module:" prefix allows
for source compatibility with existing Lua code.
* Added a couple of libraries from LuaForge: luabit and stringtools.
* Made fetchModuleFromParser() return null on error instead of throwing an
exception, to more easily support the desired behaviour of the package loader,
which needs to return null on error.
* Renamed mw.setupEnvironment() to mw.setup() since it is setting up things
other than the environment now.
* In MWServer:handleRegisterLibrary(), remove the feature which interprets dots
in library names, since LuaSandbox doesn't support this.
Improved module isolation and related refactoring:
* Expose restricted versions of getfenv() and setfenv() to user Lua code.
Requires luasandbox r114952.
* Don't cache the export list returned by module execution for later function
calls. This breaks isolation of #invoke calls, since the local variables are
persistent.
* Removed ScribuntoFunctionBase and its children, since it doesn't really have
a purpose if it can't cache anything. Instead, invoke functions using a module
method called invoke().
* Removed Module::initialize(), replaced it with a validate() function. This is
a more elegant interface and works better with the new module caching scheme.
* Use a Status object for the return value of Engine::validate() instead of an
array. Use the formatting facilities of the Status class.
Other:
* Removed "too many returns" error, doesn't fit in with Lua conventions.
* Use the standalone engine by default, so that the extension will work without
configuration for more people.
* Added an accessor for $engine->interpreter
* Fix mw.clone() to correctly clone metatables
* If the standalone interpreter exits due to an error, there are some contexts
where the initial error will be caught and ignored, and the user will see the
error from checkValid() instead. In this case, rethrow the original error for
a more informative message.
* Load mw.lua into the initial standalone environment, to reduce code
duplication between mw.lua and MWServer.lua.
* Fixed a bug in Scribunto_LuaStandaloneInterpreter::handleCall() for functions
that return no results.
* Fixed a bug in encodeLuaVar() for strings with "\r". Added test case.
* In MWServer.lua, don't call error() for internal errors, instead just print
the error and exit. This avoids a protocol violation when an error is
encountered from within handleCall().
* Added lots of documentation. Lua doc comments are in LuaDoc format.
Change-Id: Ie2fd572c362bedf02f45d3fa5352a5280e034740
2012-04-18 03:46:18 +00:00
|
|
|
$this->loaded = true;
|
2012-04-13 10:38:12 +00:00
|
|
|
|
2013-07-05 14:05:12 +00:00
|
|
|
try {
|
|
|
|
$this->interpreter = $this->newInterpreter();
|
Added more Lua environment features
Package library:
* Added a simulation of the Lua 5.1 package library.
* Removed mw.import(), replaced it with a package loader. Packages can be
retrieved from the wiki, using require('Module:Foo'), or from files
distributed with Scribunto, using require('foo'). The "Module:" prefix allows
for source compatibility with existing Lua code.
* Added a couple of libraries from LuaForge: luabit and stringtools.
* Made fetchModuleFromParser() return null on error instead of throwing an
exception, to more easily support the desired behaviour of the package loader,
which needs to return null on error.
* Renamed mw.setupEnvironment() to mw.setup() since it is setting up things
other than the environment now.
* In MWServer:handleRegisterLibrary(), remove the feature which interprets dots
in library names, since LuaSandbox doesn't support this.
Improved module isolation and related refactoring:
* Expose restricted versions of getfenv() and setfenv() to user Lua code.
Requires luasandbox r114952.
* Don't cache the export list returned by module execution for later function
calls. This breaks isolation of #invoke calls, since the local variables are
persistent.
* Removed ScribuntoFunctionBase and its children, since it doesn't really have
a purpose if it can't cache anything. Instead, invoke functions using a module
method called invoke().
* Removed Module::initialize(), replaced it with a validate() function. This is
a more elegant interface and works better with the new module caching scheme.
* Use a Status object for the return value of Engine::validate() instead of an
array. Use the formatting facilities of the Status class.
Other:
* Removed "too many returns" error, doesn't fit in with Lua conventions.
* Use the standalone engine by default, so that the extension will work without
configuration for more people.
* Added an accessor for $engine->interpreter
* Fix mw.clone() to correctly clone metatables
* If the standalone interpreter exits due to an error, there are some contexts
where the initial error will be caught and ignored, and the user will see the
error from checkValid() instead. In this case, rethrow the original error for
a more informative message.
* Load mw.lua into the initial standalone environment, to reduce code
duplication between mw.lua and MWServer.lua.
* Fixed a bug in Scribunto_LuaStandaloneInterpreter::handleCall() for functions
that return no results.
* Fixed a bug in encodeLuaVar() for strings with "\r". Added test case.
* In MWServer.lua, don't call error() for internal errors, instead just print
the error and exit. This avoids a protocol violation when an error is
encountered from within handleCall().
* Added lots of documentation. Lua doc comments are in LuaDoc format.
Change-Id: Ie2fd572c362bedf02f45d3fa5352a5280e034740
2012-04-18 03:46:18 +00:00
|
|
|
|
2017-06-15 17:19:00 +00:00
|
|
|
$funcs = [
|
2013-07-05 14:05:12 +00:00
|
|
|
'loadPackage',
|
2014-09-26 18:11:21 +00:00
|
|
|
'loadPHPLibrary',
|
2013-07-05 14:05:12 +00:00
|
|
|
'frameExists',
|
|
|
|
'newChildFrame',
|
|
|
|
'getExpandedArgument',
|
|
|
|
'getAllExpandedArguments',
|
|
|
|
'expandTemplate',
|
|
|
|
'callParserFunction',
|
|
|
|
'preprocess',
|
|
|
|
'incrementExpensiveFunctionCount',
|
2013-11-01 02:05:00 +00:00
|
|
|
'isSubsting',
|
2013-12-06 18:10:39 +00:00
|
|
|
'getFrameTitle',
|
2014-06-01 03:15:42 +00:00
|
|
|
'setTTL',
|
2016-06-16 20:20:35 +00:00
|
|
|
'addWarning',
|
2022-09-22 07:45:47 +00:00
|
|
|
'loadJsonData',
|
2017-06-15 17:19:00 +00:00
|
|
|
];
|
2013-01-25 17:53:18 +00:00
|
|
|
|
2017-06-15 17:19:00 +00:00
|
|
|
$lib = [];
|
2013-07-05 14:05:12 +00:00
|
|
|
foreach ( $funcs as $name ) {
|
2017-06-15 17:19:00 +00:00
|
|
|
$lib[$name] = [ $this, $name ];
|
2013-07-05 14:05:12 +00:00
|
|
|
}
|
2012-05-22 03:56:07 +00:00
|
|
|
|
2017-06-15 17:19:00 +00:00
|
|
|
$this->registerInterface( 'mwInit.lua', [] );
|
2013-07-05 14:05:12 +00:00
|
|
|
$this->mw = $this->registerInterface( 'mw.lua', $lib,
|
2017-06-15 17:19:00 +00:00
|
|
|
[ 'allowEnvFuncs' => $this->options['allowEnvFuncs'] ] );
|
2013-01-25 17:53:18 +00:00
|
|
|
|
2014-09-26 18:11:21 +00:00
|
|
|
$this->availableLibraries = $this->getLibraries( 'lua', self::$libraryClasses );
|
|
|
|
foreach ( $this->availableLibraries as $name => $def ) {
|
|
|
|
$this->instantiatePHPLibrary( $name, $def, false );
|
2013-07-05 14:05:12 +00:00
|
|
|
}
|
|
|
|
} catch ( Exception $ex ) {
|
|
|
|
$this->loaded = false;
|
|
|
|
$this->interpreter = null;
|
|
|
|
throw $ex;
|
2013-01-25 17:53:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-15 23:41:20 +00:00
|
|
|
/**
|
|
|
|
* Register a Lua Library
|
|
|
|
*
|
|
|
|
* This should be called from the library's PHP module's register() method.
|
|
|
|
*
|
|
|
|
* The value for $interfaceFuncs is used to populate the mw_interface
|
|
|
|
* global that is defined when the library's Lua module is loaded. Values
|
|
|
|
* must be PHP callables, which will be seen in Lua as functions.
|
|
|
|
*
|
2017-09-24 17:38:14 +00:00
|
|
|
* @param string $moduleFileName The path to the Lua portion of the library
|
2013-02-15 23:41:20 +00:00
|
|
|
* (absolute, or relative to $this->getLuaLibDir())
|
2024-08-26 15:24:52 +00:00
|
|
|
* @param array<string,callable> $interfaceFuncs Populates mw_interface
|
2017-09-24 17:38:14 +00:00
|
|
|
* @param array $setupOptions Passed to the modules setupInterface() method.
|
2014-10-16 23:40:15 +00:00
|
|
|
* @return array Lua package
|
2013-02-15 23:41:20 +00:00
|
|
|
*/
|
2017-06-15 17:19:00 +00:00
|
|
|
public function registerInterface( $moduleFileName, $interfaceFuncs, $setupOptions = [] ) {
|
2013-01-25 17:53:18 +00:00
|
|
|
$this->interpreter->registerLibrary( 'mw_interface', $interfaceFuncs );
|
2013-02-14 17:38:03 +00:00
|
|
|
$moduleFileName = $this->normalizeModuleFileName( $moduleFileName );
|
|
|
|
$package = $this->loadLibraryFromFile( $moduleFileName );
|
2013-12-09 00:39:13 +00:00
|
|
|
if ( !empty( $package['setupInterface'] ) ) {
|
2013-01-25 17:53:18 +00:00
|
|
|
$this->interpreter->callFunction( $package['setupInterface'], $setupOptions );
|
|
|
|
}
|
|
|
|
return $package;
|
2012-04-13 10:38:12 +00:00
|
|
|
}
|
|
|
|
|
2013-02-15 23:41:20 +00:00
|
|
|
/**
|
|
|
|
* Return the base path for Lua modules.
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-05 09:39:58 +00:00
|
|
|
public function getLuaLibDir() {
|
2013-12-16 15:00:37 +00:00
|
|
|
return __DIR__ . '/lualib';
|
2012-09-05 09:39:58 +00:00
|
|
|
}
|
|
|
|
|
2013-02-14 17:38:03 +00:00
|
|
|
/**
|
2013-03-29 11:28:08 +00:00
|
|
|
* Normalize a lua module to its full path. If path does not look like an
|
|
|
|
* absolute path (i.e. begins with DIRECTORY_SEPARATOR or "X:"), prepend
|
|
|
|
* getLuaLibDir()
|
2013-02-14 17:38:03 +00:00
|
|
|
*
|
2017-09-24 17:38:14 +00:00
|
|
|
* @param string $fileName name of the lua module file
|
2013-02-14 17:38:03 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function normalizeModuleFileName( $fileName ) {
|
2013-03-29 11:28:08 +00:00
|
|
|
if ( !preg_match( '<^(?:[a-zA-Z]:)?' . preg_quote( DIRECTORY_SEPARATOR ) . '>', $fileName ) ) {
|
|
|
|
$fileName = "{$this->getLuaLibDir()}/{$fileName}";
|
|
|
|
}
|
|
|
|
return $fileName;
|
2013-02-14 17:38:03 +00:00
|
|
|
}
|
|
|
|
|
2013-01-25 17:53:18 +00:00
|
|
|
/**
|
|
|
|
* Get performance characteristics of the Lua engine/interpreter
|
|
|
|
*
|
|
|
|
* phpCallsRequireSerialization: boolean
|
|
|
|
* whether calls between PHP and Lua functions require (slow)
|
|
|
|
* serialization of parameters and return values
|
2013-02-15 23:41:20 +00:00
|
|
|
*
|
|
|
|
* @return array
|
2013-01-25 17:53:18 +00:00
|
|
|
*/
|
2015-06-26 16:37:34 +00:00
|
|
|
abstract public function getPerformanceCharacteristics();
|
2013-01-25 17:53:18 +00:00
|
|
|
|
Added more Lua environment features
Package library:
* Added a simulation of the Lua 5.1 package library.
* Removed mw.import(), replaced it with a package loader. Packages can be
retrieved from the wiki, using require('Module:Foo'), or from files
distributed with Scribunto, using require('foo'). The "Module:" prefix allows
for source compatibility with existing Lua code.
* Added a couple of libraries from LuaForge: luabit and stringtools.
* Made fetchModuleFromParser() return null on error instead of throwing an
exception, to more easily support the desired behaviour of the package loader,
which needs to return null on error.
* Renamed mw.setupEnvironment() to mw.setup() since it is setting up things
other than the environment now.
* In MWServer:handleRegisterLibrary(), remove the feature which interprets dots
in library names, since LuaSandbox doesn't support this.
Improved module isolation and related refactoring:
* Expose restricted versions of getfenv() and setfenv() to user Lua code.
Requires luasandbox r114952.
* Don't cache the export list returned by module execution for later function
calls. This breaks isolation of #invoke calls, since the local variables are
persistent.
* Removed ScribuntoFunctionBase and its children, since it doesn't really have
a purpose if it can't cache anything. Instead, invoke functions using a module
method called invoke().
* Removed Module::initialize(), replaced it with a validate() function. This is
a more elegant interface and works better with the new module caching scheme.
* Use a Status object for the return value of Engine::validate() instead of an
array. Use the formatting facilities of the Status class.
Other:
* Removed "too many returns" error, doesn't fit in with Lua conventions.
* Use the standalone engine by default, so that the extension will work without
configuration for more people.
* Added an accessor for $engine->interpreter
* Fix mw.clone() to correctly clone metatables
* If the standalone interpreter exits due to an error, there are some contexts
where the initial error will be caught and ignored, and the user will see the
error from checkValid() instead. In this case, rethrow the original error for
a more informative message.
* Load mw.lua into the initial standalone environment, to reduce code
duplication between mw.lua and MWServer.lua.
* Fixed a bug in Scribunto_LuaStandaloneInterpreter::handleCall() for functions
that return no results.
* Fixed a bug in encodeLuaVar() for strings with "\r". Added test case.
* In MWServer.lua, don't call error() for internal errors, instead just print
the error and exit. This avoids a protocol violation when an error is
encountered from within handleCall().
* Added lots of documentation. Lua doc comments are in LuaDoc format.
Change-Id: Ie2fd572c362bedf02f45d3fa5352a5280e034740
2012-04-18 03:46:18 +00:00
|
|
|
/**
|
|
|
|
* Get the current interpreter object
|
2022-08-03 12:31:15 +00:00
|
|
|
* @return LuaInterpreter
|
Added more Lua environment features
Package library:
* Added a simulation of the Lua 5.1 package library.
* Removed mw.import(), replaced it with a package loader. Packages can be
retrieved from the wiki, using require('Module:Foo'), or from files
distributed with Scribunto, using require('foo'). The "Module:" prefix allows
for source compatibility with existing Lua code.
* Added a couple of libraries from LuaForge: luabit and stringtools.
* Made fetchModuleFromParser() return null on error instead of throwing an
exception, to more easily support the desired behaviour of the package loader,
which needs to return null on error.
* Renamed mw.setupEnvironment() to mw.setup() since it is setting up things
other than the environment now.
* In MWServer:handleRegisterLibrary(), remove the feature which interprets dots
in library names, since LuaSandbox doesn't support this.
Improved module isolation and related refactoring:
* Expose restricted versions of getfenv() and setfenv() to user Lua code.
Requires luasandbox r114952.
* Don't cache the export list returned by module execution for later function
calls. This breaks isolation of #invoke calls, since the local variables are
persistent.
* Removed ScribuntoFunctionBase and its children, since it doesn't really have
a purpose if it can't cache anything. Instead, invoke functions using a module
method called invoke().
* Removed Module::initialize(), replaced it with a validate() function. This is
a more elegant interface and works better with the new module caching scheme.
* Use a Status object for the return value of Engine::validate() instead of an
array. Use the formatting facilities of the Status class.
Other:
* Removed "too many returns" error, doesn't fit in with Lua conventions.
* Use the standalone engine by default, so that the extension will work without
configuration for more people.
* Added an accessor for $engine->interpreter
* Fix mw.clone() to correctly clone metatables
* If the standalone interpreter exits due to an error, there are some contexts
where the initial error will be caught and ignored, and the user will see the
error from checkValid() instead. In this case, rethrow the original error for
a more informative message.
* Load mw.lua into the initial standalone environment, to reduce code
duplication between mw.lua and MWServer.lua.
* Fixed a bug in Scribunto_LuaStandaloneInterpreter::handleCall() for functions
that return no results.
* Fixed a bug in encodeLuaVar() for strings with "\r". Added test case.
* In MWServer.lua, don't call error() for internal errors, instead just print
the error and exit. This avoids a protocol violation when an error is
encountered from within handleCall().
* Added lots of documentation. Lua doc comments are in LuaDoc format.
Change-Id: Ie2fd572c362bedf02f45d3fa5352a5280e034740
2012-04-18 03:46:18 +00:00
|
|
|
*/
|
|
|
|
public function getInterpreter() {
|
|
|
|
$this->load();
|
|
|
|
return $this->interpreter;
|
|
|
|
}
|
|
|
|
|
2014-07-07 16:30:44 +00:00
|
|
|
/**
|
|
|
|
* Replaces the list of current frames, and return a ScopedCallback that
|
|
|
|
* will reset them when it goes out of scope.
|
|
|
|
*
|
|
|
|
* @param PPFrame|null $frame If null, an empty frame with no parent will be used
|
|
|
|
* @return ScopedCallback
|
|
|
|
*/
|
|
|
|
private function setupCurrentFrames( PPFrame $frame = null ) {
|
|
|
|
if ( !$frame ) {
|
|
|
|
$frame = $this->getParser()->getPreprocessor()->newFrame();
|
|
|
|
}
|
|
|
|
|
|
|
|
$oldFrames = $this->currentFrames;
|
2014-05-31 17:42:45 +00:00
|
|
|
$oldExpandCache = $this->expandCache;
|
2017-06-15 17:19:00 +00:00
|
|
|
$this->currentFrames = [
|
2014-07-07 16:30:44 +00:00
|
|
|
'current' => $frame,
|
2019-03-21 04:16:08 +00:00
|
|
|
'parent' => $frame->parent ?? null,
|
2017-06-15 17:19:00 +00:00
|
|
|
];
|
|
|
|
$this->expandCache = [];
|
2014-07-07 16:30:44 +00:00
|
|
|
|
2017-08-21 21:16:32 +00:00
|
|
|
return new ScopedCallback( function () use ( $oldFrames, $oldExpandCache ) {
|
|
|
|
$this->currentFrames = $oldFrames;
|
|
|
|
$this->expandCache = $oldExpandCache;
|
2014-07-07 16:30:44 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
Added more Lua environment features
Package library:
* Added a simulation of the Lua 5.1 package library.
* Removed mw.import(), replaced it with a package loader. Packages can be
retrieved from the wiki, using require('Module:Foo'), or from files
distributed with Scribunto, using require('foo'). The "Module:" prefix allows
for source compatibility with existing Lua code.
* Added a couple of libraries from LuaForge: luabit and stringtools.
* Made fetchModuleFromParser() return null on error instead of throwing an
exception, to more easily support the desired behaviour of the package loader,
which needs to return null on error.
* Renamed mw.setupEnvironment() to mw.setup() since it is setting up things
other than the environment now.
* In MWServer:handleRegisterLibrary(), remove the feature which interprets dots
in library names, since LuaSandbox doesn't support this.
Improved module isolation and related refactoring:
* Expose restricted versions of getfenv() and setfenv() to user Lua code.
Requires luasandbox r114952.
* Don't cache the export list returned by module execution for later function
calls. This breaks isolation of #invoke calls, since the local variables are
persistent.
* Removed ScribuntoFunctionBase and its children, since it doesn't really have
a purpose if it can't cache anything. Instead, invoke functions using a module
method called invoke().
* Removed Module::initialize(), replaced it with a validate() function. This is
a more elegant interface and works better with the new module caching scheme.
* Use a Status object for the return value of Engine::validate() instead of an
array. Use the formatting facilities of the Status class.
Other:
* Removed "too many returns" error, doesn't fit in with Lua conventions.
* Use the standalone engine by default, so that the extension will work without
configuration for more people.
* Added an accessor for $engine->interpreter
* Fix mw.clone() to correctly clone metatables
* If the standalone interpreter exits due to an error, there are some contexts
where the initial error will be caught and ignored, and the user will see the
error from checkValid() instead. In this case, rethrow the original error for
a more informative message.
* Load mw.lua into the initial standalone environment, to reduce code
duplication between mw.lua and MWServer.lua.
* Fixed a bug in Scribunto_LuaStandaloneInterpreter::handleCall() for functions
that return no results.
* Fixed a bug in encodeLuaVar() for strings with "\r". Added test case.
* In MWServer.lua, don't call error() for internal errors, instead just print
the error and exit. This avoids a protocol violation when an error is
encountered from within handleCall().
* Added lots of documentation. Lua doc comments are in LuaDoc format.
Change-Id: Ie2fd572c362bedf02f45d3fa5352a5280e034740
2012-04-18 03:46:18 +00:00
|
|
|
/**
|
2014-05-23 15:43:22 +00:00
|
|
|
* Execute a module chunk in a new isolated environment, and return the specified function
|
2022-08-03 12:31:15 +00:00
|
|
|
* @param mixed $chunk As accepted by LuaInterpreter::callFunction()
|
2018-01-10 17:45:07 +00:00
|
|
|
* @param string $functionName
|
|
|
|
* @param PPFrame|null $frame
|
|
|
|
* @return mixed
|
|
|
|
* @throws ScribuntoException
|
Added more Lua environment features
Package library:
* Added a simulation of the Lua 5.1 package library.
* Removed mw.import(), replaced it with a package loader. Packages can be
retrieved from the wiki, using require('Module:Foo'), or from files
distributed with Scribunto, using require('foo'). The "Module:" prefix allows
for source compatibility with existing Lua code.
* Added a couple of libraries from LuaForge: luabit and stringtools.
* Made fetchModuleFromParser() return null on error instead of throwing an
exception, to more easily support the desired behaviour of the package loader,
which needs to return null on error.
* Renamed mw.setupEnvironment() to mw.setup() since it is setting up things
other than the environment now.
* In MWServer:handleRegisterLibrary(), remove the feature which interprets dots
in library names, since LuaSandbox doesn't support this.
Improved module isolation and related refactoring:
* Expose restricted versions of getfenv() and setfenv() to user Lua code.
Requires luasandbox r114952.
* Don't cache the export list returned by module execution for later function
calls. This breaks isolation of #invoke calls, since the local variables are
persistent.
* Removed ScribuntoFunctionBase and its children, since it doesn't really have
a purpose if it can't cache anything. Instead, invoke functions using a module
method called invoke().
* Removed Module::initialize(), replaced it with a validate() function. This is
a more elegant interface and works better with the new module caching scheme.
* Use a Status object for the return value of Engine::validate() instead of an
array. Use the formatting facilities of the Status class.
Other:
* Removed "too many returns" error, doesn't fit in with Lua conventions.
* Use the standalone engine by default, so that the extension will work without
configuration for more people.
* Added an accessor for $engine->interpreter
* Fix mw.clone() to correctly clone metatables
* If the standalone interpreter exits due to an error, there are some contexts
where the initial error will be caught and ignored, and the user will see the
error from checkValid() instead. In this case, rethrow the original error for
a more informative message.
* Load mw.lua into the initial standalone environment, to reduce code
duplication between mw.lua and MWServer.lua.
* Fixed a bug in Scribunto_LuaStandaloneInterpreter::handleCall() for functions
that return no results.
* Fixed a bug in encodeLuaVar() for strings with "\r". Added test case.
* In MWServer.lua, don't call error() for internal errors, instead just print
the error and exit. This avoids a protocol violation when an error is
encountered from within handleCall().
* Added lots of documentation. Lua doc comments are in LuaDoc format.
Change-Id: Ie2fd572c362bedf02f45d3fa5352a5280e034740
2012-04-18 03:46:18 +00:00
|
|
|
*/
|
2014-07-07 16:30:44 +00:00
|
|
|
public function executeModule( $chunk, $functionName, $frame ) {
|
2019-10-01 20:30:26 +00:00
|
|
|
// $resetFrames is a ScopedCallback, so it has a purpose even though it appears unused.
|
|
|
|
$resetFrames = $this->setupCurrentFrames( $frame );
|
2014-07-07 16:30:44 +00:00
|
|
|
|
2016-05-17 14:52:05 +00:00
|
|
|
$retval = $this->getInterpreter()->callFunction(
|
|
|
|
$this->mw['executeModule'], $chunk, $functionName
|
|
|
|
);
|
2014-05-23 15:43:22 +00:00
|
|
|
if ( !$retval[0] ) {
|
|
|
|
// If we get here, it means we asked for an element from the table the module returned,
|
|
|
|
// but it returned something other than a table. In this case, $retval[1] contains the type
|
|
|
|
// of what it did returned, instead of the value we asked for.
|
2016-05-17 14:52:05 +00:00
|
|
|
throw $this->newException(
|
2017-06-15 17:19:00 +00:00
|
|
|
'scribunto-lua-notarrayreturn', [ 'args' => [ $retval[1] ] ]
|
2016-05-17 14:52:05 +00:00
|
|
|
);
|
2014-05-23 15:43:22 +00:00
|
|
|
}
|
|
|
|
return $retval[1];
|
2012-05-22 03:56:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute a module function chunk
|
2022-08-03 12:31:15 +00:00
|
|
|
* @param mixed $chunk As accepted by LuaInterpreter::callFunction()
|
2018-01-10 17:45:07 +00:00
|
|
|
* @param PPFrame|null $frame
|
|
|
|
* @return array
|
2012-05-22 03:56:07 +00:00
|
|
|
*/
|
|
|
|
public function executeFunctionChunk( $chunk, $frame ) {
|
2014-07-07 18:46:59 +00:00
|
|
|
// $resetFrames is a ScopedCallback, so it has a purpose even though it appears unused.
|
2014-07-07 16:30:44 +00:00
|
|
|
$resetFrames = $this->setupCurrentFrames( $frame );
|
|
|
|
|
|
|
|
return $this->getInterpreter()->callFunction(
|
|
|
|
$this->mw['executeFunction'],
|
|
|
|
$chunk );
|
2012-04-13 10:38:12 +00:00
|
|
|
}
|
|
|
|
|
2013-03-25 15:56:38 +00:00
|
|
|
/**
|
|
|
|
* Get data logged by modules
|
|
|
|
* @return string Logged data
|
|
|
|
*/
|
|
|
|
protected function getLogBuffer() {
|
|
|
|
if ( !$this->loaded ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
$log = $this->getInterpreter()->callFunction( $this->mw['getLogBuffer'] );
|
|
|
|
return $log[0];
|
|
|
|
} catch ( ScribuntoException $ex ) {
|
|
|
|
// Probably time expired, ignore it.
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Format the logged data for HTML output
|
|
|
|
* @param string $logs Logged data
|
2017-08-11 04:28:16 +00:00
|
|
|
* @param bool $localize Whether to localize the message key
|
2013-03-25 15:56:38 +00:00
|
|
|
* @return string HTML
|
|
|
|
*/
|
|
|
|
protected function formatHtmlLogs( $logs, $localize ) {
|
|
|
|
$keyMsg = wfMessage( 'scribunto-limitreport-logs' );
|
|
|
|
if ( !$localize ) {
|
|
|
|
$keyMsg->inLanguage( 'en' )->useDatabase( false );
|
|
|
|
}
|
|
|
|
return Html::openElement( 'tr' ) .
|
2017-06-15 17:19:00 +00:00
|
|
|
Html::rawElement( 'th', [ 'colspan' => 2 ], $keyMsg->parse() ) .
|
2013-03-25 15:56:38 +00:00
|
|
|
Html::closeElement( 'tr' ) .
|
|
|
|
Html::openElement( 'tr' ) .
|
2017-06-15 17:19:00 +00:00
|
|
|
Html::openElement( 'td', [ 'colspan' => 2 ] ) .
|
|
|
|
Html::openElement( 'div', [ 'class' => 'mw-collapsible mw-collapsed' ] ) .
|
|
|
|
Html::element( 'pre', [ 'class' => 'scribunto-limitreport-logs' ], $logs ) .
|
2013-03-25 15:56:38 +00:00
|
|
|
Html::closeElement( 'div' ) .
|
|
|
|
Html::closeElement( 'td' ) .
|
|
|
|
Html::closeElement( 'tr' );
|
|
|
|
}
|
|
|
|
|
Added more Lua environment features
Package library:
* Added a simulation of the Lua 5.1 package library.
* Removed mw.import(), replaced it with a package loader. Packages can be
retrieved from the wiki, using require('Module:Foo'), or from files
distributed with Scribunto, using require('foo'). The "Module:" prefix allows
for source compatibility with existing Lua code.
* Added a couple of libraries from LuaForge: luabit and stringtools.
* Made fetchModuleFromParser() return null on error instead of throwing an
exception, to more easily support the desired behaviour of the package loader,
which needs to return null on error.
* Renamed mw.setupEnvironment() to mw.setup() since it is setting up things
other than the environment now.
* In MWServer:handleRegisterLibrary(), remove the feature which interprets dots
in library names, since LuaSandbox doesn't support this.
Improved module isolation and related refactoring:
* Expose restricted versions of getfenv() and setfenv() to user Lua code.
Requires luasandbox r114952.
* Don't cache the export list returned by module execution for later function
calls. This breaks isolation of #invoke calls, since the local variables are
persistent.
* Removed ScribuntoFunctionBase and its children, since it doesn't really have
a purpose if it can't cache anything. Instead, invoke functions using a module
method called invoke().
* Removed Module::initialize(), replaced it with a validate() function. This is
a more elegant interface and works better with the new module caching scheme.
* Use a Status object for the return value of Engine::validate() instead of an
array. Use the formatting facilities of the Status class.
Other:
* Removed "too many returns" error, doesn't fit in with Lua conventions.
* Use the standalone engine by default, so that the extension will work without
configuration for more people.
* Added an accessor for $engine->interpreter
* Fix mw.clone() to correctly clone metatables
* If the standalone interpreter exits due to an error, there are some contexts
where the initial error will be caught and ignored, and the user will see the
error from checkValid() instead. In this case, rethrow the original error for
a more informative message.
* Load mw.lua into the initial standalone environment, to reduce code
duplication between mw.lua and MWServer.lua.
* Fixed a bug in Scribunto_LuaStandaloneInterpreter::handleCall() for functions
that return no results.
* Fixed a bug in encodeLuaVar() for strings with "\r". Added test case.
* In MWServer.lua, don't call error() for internal errors, instead just print
the error and exit. This avoids a protocol violation when an error is
encountered from within handleCall().
* Added lots of documentation. Lua doc comments are in LuaDoc format.
Change-Id: Ie2fd572c362bedf02f45d3fa5352a5280e034740
2012-04-18 03:46:18 +00:00
|
|
|
/**
|
|
|
|
* Load a library from the given file and execute it in the base environment.
|
2017-09-24 17:38:14 +00:00
|
|
|
* @param string $fileName File name/path to load
|
|
|
|
* @return array|null the export list, or null if there isn't one.
|
Added more Lua environment features
Package library:
* Added a simulation of the Lua 5.1 package library.
* Removed mw.import(), replaced it with a package loader. Packages can be
retrieved from the wiki, using require('Module:Foo'), or from files
distributed with Scribunto, using require('foo'). The "Module:" prefix allows
for source compatibility with existing Lua code.
* Added a couple of libraries from LuaForge: luabit and stringtools.
* Made fetchModuleFromParser() return null on error instead of throwing an
exception, to more easily support the desired behaviour of the package loader,
which needs to return null on error.
* Renamed mw.setupEnvironment() to mw.setup() since it is setting up things
other than the environment now.
* In MWServer:handleRegisterLibrary(), remove the feature which interprets dots
in library names, since LuaSandbox doesn't support this.
Improved module isolation and related refactoring:
* Expose restricted versions of getfenv() and setfenv() to user Lua code.
Requires luasandbox r114952.
* Don't cache the export list returned by module execution for later function
calls. This breaks isolation of #invoke calls, since the local variables are
persistent.
* Removed ScribuntoFunctionBase and its children, since it doesn't really have
a purpose if it can't cache anything. Instead, invoke functions using a module
method called invoke().
* Removed Module::initialize(), replaced it with a validate() function. This is
a more elegant interface and works better with the new module caching scheme.
* Use a Status object for the return value of Engine::validate() instead of an
array. Use the formatting facilities of the Status class.
Other:
* Removed "too many returns" error, doesn't fit in with Lua conventions.
* Use the standalone engine by default, so that the extension will work without
configuration for more people.
* Added an accessor for $engine->interpreter
* Fix mw.clone() to correctly clone metatables
* If the standalone interpreter exits due to an error, there are some contexts
where the initial error will be caught and ignored, and the user will see the
error from checkValid() instead. In this case, rethrow the original error for
a more informative message.
* Load mw.lua into the initial standalone environment, to reduce code
duplication between mw.lua and MWServer.lua.
* Fixed a bug in Scribunto_LuaStandaloneInterpreter::handleCall() for functions
that return no results.
* Fixed a bug in encodeLuaVar() for strings with "\r". Added test case.
* In MWServer.lua, don't call error() for internal errors, instead just print
the error and exit. This avoids a protocol violation when an error is
encountered from within handleCall().
* Added lots of documentation. Lua doc comments are in LuaDoc format.
Change-Id: Ie2fd572c362bedf02f45d3fa5352a5280e034740
2012-04-18 03:46:18 +00:00
|
|
|
*/
|
2012-04-13 10:38:12 +00:00
|
|
|
protected function loadLibraryFromFile( $fileName ) {
|
2015-08-25 23:25:24 +00:00
|
|
|
static $cache = null;
|
|
|
|
|
2024-05-08 23:26:58 +00:00
|
|
|
$objectcachefactory = MediaWikiServices::getInstance()->getObjectCacheFactory();
|
|
|
|
|
2015-08-25 23:25:24 +00:00
|
|
|
if ( !$cache ) {
|
2024-05-08 23:26:58 +00:00
|
|
|
$cache = $objectcachefactory->getLocalServerInstance( CACHE_HASH );
|
2015-08-25 23:25:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$mtime = filemtime( $fileName );
|
|
|
|
if ( $mtime === false ) {
|
2023-06-09 12:43:44 +00:00
|
|
|
throw new RuntimeException( 'Lua file does not exist: ' . $fileName );
|
2015-08-25 23:25:24 +00:00
|
|
|
}
|
|
|
|
|
2018-01-10 14:20:06 +00:00
|
|
|
$cacheKey = $cache->makeGlobalKey( __CLASS__, $fileName );
|
2015-08-25 23:25:24 +00:00
|
|
|
$fileData = $cache->get( $cacheKey );
|
2015-08-26 00:36:26 +00:00
|
|
|
|
|
|
|
$code = false;
|
2015-08-25 23:25:24 +00:00
|
|
|
if ( $fileData ) {
|
2024-03-10 22:46:30 +00:00
|
|
|
[ $code, $cachedMtime ] = $fileData;
|
2015-08-26 00:36:26 +00:00
|
|
|
if ( $cachedMtime < $mtime ) {
|
|
|
|
$code = false;
|
2015-08-25 23:25:24 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-26 00:36:26 +00:00
|
|
|
if ( !$code ) {
|
|
|
|
$code = file_get_contents( $fileName );
|
|
|
|
if ( $code === false ) {
|
2023-06-09 12:43:44 +00:00
|
|
|
throw new RuntimeException( 'Lua file does not exist: ' . $fileName );
|
2015-08-26 00:36:26 +00:00
|
|
|
}
|
2017-06-15 17:19:00 +00:00
|
|
|
$cache->set( $cacheKey, [ $code, $mtime ], 60 * 5 );
|
2012-04-13 10:38:12 +00:00
|
|
|
}
|
2015-08-25 23:25:24 +00:00
|
|
|
|
2012-04-19 07:40:56 +00:00
|
|
|
# Prepending an "@" to the chunk name makes Lua think it is a filename
|
|
|
|
$module = $this->getInterpreter()->loadString( $code, '@' . basename( $fileName ) );
|
|
|
|
$ret = $this->getInterpreter()->callFunction( $module );
|
2019-03-21 04:16:08 +00:00
|
|
|
return $ret[0] ?? null;
|
2012-04-13 10:38:12 +00:00
|
|
|
}
|
|
|
|
|
2020-01-14 18:50:34 +00:00
|
|
|
/** @inheritDoc */
|
2012-04-13 10:38:12 +00:00
|
|
|
public function getGeSHiLanguage() {
|
|
|
|
return 'lua';
|
|
|
|
}
|
2012-05-22 03:56:07 +00:00
|
|
|
|
2020-01-14 18:50:34 +00:00
|
|
|
/** @inheritDoc */
|
2012-04-13 10:38:12 +00:00
|
|
|
public function getCodeEditorLanguage() {
|
|
|
|
return 'lua';
|
|
|
|
}
|
|
|
|
|
2020-01-14 18:50:34 +00:00
|
|
|
/** @inheritDoc */
|
2014-10-09 19:49:40 +00:00
|
|
|
public function runConsole( array $params ) {
|
2014-07-07 18:46:59 +00:00
|
|
|
// $resetFrames is a ScopedCallback, so it has a purpose even though it appears unused.
|
2014-07-07 16:30:44 +00:00
|
|
|
$resetFrames = $this->setupCurrentFrames();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TODO: provide some means for giving correct line numbers for errors
|
|
|
|
* in console input, and for producing an informative error message
|
|
|
|
* if there is an error in prevQuestions.
|
|
|
|
*
|
2014-07-07 18:46:59 +00:00
|
|
|
* Maybe each console line could be evaluated as a different chunk,
|
2014-07-07 16:30:44 +00:00
|
|
|
* apparently that's what lua.c does.
|
|
|
|
*/
|
|
|
|
$code = "return function (__init, exe)\n" .
|
2014-04-07 21:58:31 +00:00
|
|
|
"if not exe then exe = function(...) return true, ... end end\n" .
|
|
|
|
"local p = select(2, exe(__init) )\n" .
|
|
|
|
"__init, exe = nil, nil\n" .
|
2014-07-07 16:30:44 +00:00
|
|
|
"local print = mw.log\n";
|
|
|
|
foreach ( $params['prevQuestions'] as $q ) {
|
|
|
|
if ( substr( $q, 0, 1 ) === '=' ) {
|
|
|
|
$code .= "print(" . substr( $q, 1 ) . ")";
|
2012-07-14 04:23:42 +00:00
|
|
|
} else {
|
2014-07-07 16:30:44 +00:00
|
|
|
$code .= $q;
|
2012-07-14 04:23:42 +00:00
|
|
|
}
|
2014-07-07 16:30:44 +00:00
|
|
|
$code .= "\n";
|
|
|
|
}
|
|
|
|
$code .= "mw.clearLogBuffer()\n";
|
|
|
|
if ( substr( $params['question'], 0, 1 ) === '=' ) {
|
|
|
|
// Treat a statement starting with "=" as a return statement, like in lua.c
|
|
|
|
$code .= "local ret = mw.allToString(" . substr( $params['question'], 1 ) . ")\n" .
|
|
|
|
"return ret, mw.getLogBuffer()\n";
|
|
|
|
} else {
|
|
|
|
$code .= $params['question'] . "\n" .
|
|
|
|
"return nil, mw.getLogBuffer()\n";
|
|
|
|
}
|
|
|
|
$code .= "end\n";
|
2013-02-19 00:49:08 +00:00
|
|
|
|
2014-09-16 03:25:53 +00:00
|
|
|
if ( $params['title']->hasContentModel( CONTENT_MODEL_SCRIBUNTO ) ) {
|
2014-04-07 21:58:31 +00:00
|
|
|
$contentModule = $this->newModule(
|
|
|
|
$params['content'], $params['title']->getPrefixedDBkey() );
|
|
|
|
$contentInit = $contentModule->getInitChunk();
|
|
|
|
$contentExe = $this->mw['executeModule'];
|
|
|
|
} else {
|
|
|
|
$contentInit = $params['content'];
|
|
|
|
$contentExe = null;
|
|
|
|
}
|
2013-02-19 00:49:08 +00:00
|
|
|
|
2014-07-07 16:30:44 +00:00
|
|
|
$consoleModule = $this->newModule(
|
|
|
|
$code,
|
|
|
|
wfMessage( 'scribunto-console-current-src' )->text()
|
|
|
|
);
|
|
|
|
$consoleInit = $consoleModule->getInitChunk();
|
|
|
|
$ret = $this->getInterpreter()->callFunction( $this->mw['executeModule'], $consoleInit, false );
|
|
|
|
$func = $ret[1];
|
2014-04-07 21:58:31 +00:00
|
|
|
$ret = $this->getInterpreter()->callFunction( $func, $contentInit, $contentExe );
|
2012-07-14 04:23:42 +00:00
|
|
|
|
2017-06-15 17:19:00 +00:00
|
|
|
return [
|
2019-03-21 04:16:08 +00:00
|
|
|
'return' => $ret[0] ?? null,
|
|
|
|
'print' => $ret[1] ?? '',
|
2017-06-15 17:19:00 +00:00
|
|
|
];
|
2012-07-14 04:23:42 +00:00
|
|
|
}
|
2014-06-18 17:17:04 +00:00
|
|
|
|
2012-04-13 10:38:12 +00:00
|
|
|
/**
|
|
|
|
* Workalike for luaL_checktype()
|
|
|
|
*
|
2014-06-18 17:17:04 +00:00
|
|
|
* @param string $funcName The Lua function name, for use in error messages
|
|
|
|
* @param array $args The argument array
|
|
|
|
* @param int $index0 The zero-based argument index
|
2024-08-26 15:24:52 +00:00
|
|
|
* @param string|string[] $type The allowed type names as given by gettype()
|
2014-06-18 17:17:04 +00:00
|
|
|
* @param string $msgType The type name used in the error message
|
2022-08-03 12:31:15 +00:00
|
|
|
* @throws LuaError
|
2012-04-13 10:38:12 +00:00
|
|
|
*/
|
|
|
|
public function checkType( $funcName, $args, $index0, $type, $msgType ) {
|
2014-06-01 03:15:42 +00:00
|
|
|
if ( !is_array( $type ) ) {
|
2017-06-15 17:19:00 +00:00
|
|
|
$type = [ $type ];
|
2014-06-01 03:15:42 +00:00
|
|
|
}
|
|
|
|
if ( !isset( $args[$index0] ) || !in_array( gettype( $args[$index0] ), $type, true ) ) {
|
2012-04-13 10:38:12 +00:00
|
|
|
$index1 = $index0 + 1;
|
2022-08-03 12:31:15 +00:00
|
|
|
throw new LuaError( "bad argument #$index1 to '$funcName' ($msgType expected)" );
|
2012-04-13 10:38:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Workalike for luaL_checkstring()
|
|
|
|
*
|
2014-06-18 17:17:04 +00:00
|
|
|
* @param string $funcName The Lua function name, for use in error messages
|
|
|
|
* @param array $args The argument array
|
|
|
|
* @param int $index0 The zero-based argument index
|
2012-04-13 10:38:12 +00:00
|
|
|
*/
|
|
|
|
public function checkString( $funcName, $args, $index0 ) {
|
|
|
|
$this->checkType( $funcName, $args, $index0, 'string', 'string' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Workalike for luaL_checknumber()
|
|
|
|
*
|
2014-06-18 17:17:04 +00:00
|
|
|
* @param string $funcName The Lua function name, for use in error messages
|
|
|
|
* @param array $args The argument array
|
|
|
|
* @param int $index0 The zero-based argument index
|
2012-04-13 10:38:12 +00:00
|
|
|
*/
|
|
|
|
public function checkNumber( $funcName, $args, $index0 ) {
|
2017-06-15 17:19:00 +00:00
|
|
|
$this->checkType( $funcName, $args, $index0, [ 'integer', 'double' ], 'number' );
|
2012-04-13 10:38:12 +00:00
|
|
|
}
|
|
|
|
|
2014-09-26 18:11:21 +00:00
|
|
|
/**
|
|
|
|
* Instantiate and register a library.
|
|
|
|
* @param string $name
|
2024-08-26 15:24:52 +00:00
|
|
|
* @param array|class-string<LibraryBase> $def
|
2014-09-26 18:11:21 +00:00
|
|
|
* @param bool $loadDeferred
|
|
|
|
* @return array|null
|
|
|
|
*/
|
|
|
|
private function instantiatePHPLibrary( $name, $def, $loadDeferred ) {
|
|
|
|
$def = $this->availableLibraries[$name];
|
|
|
|
if ( is_string( $def ) ) {
|
2024-08-26 15:24:52 +00:00
|
|
|
/** @var LibraryBase $class */
|
2014-09-26 18:11:21 +00:00
|
|
|
$class = new $def( $this );
|
|
|
|
} else {
|
|
|
|
if ( !$loadDeferred && !empty( $def['deferLoad'] ) ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if ( isset( $def['class'] ) ) {
|
2024-08-26 15:24:52 +00:00
|
|
|
/** @var LibraryBase $class */
|
2014-09-26 18:11:21 +00:00
|
|
|
$class = new $def['class']( $this );
|
|
|
|
} else {
|
2023-06-09 12:43:44 +00:00
|
|
|
throw new RuntimeException( "No class for library \"$name\"" );
|
2014-09-26 18:11:21 +00:00
|
|
|
}
|
|
|
|
}
|
2014-10-05 19:52:23 +00:00
|
|
|
return $class->register();
|
2014-09-26 18:11:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for the loadPHPLibrary() callback. Register the specified
|
|
|
|
* library and return its function table. It's not necessary to cache the
|
|
|
|
* function table in the object instance, since there is caching in a
|
|
|
|
* wrapper on the Lua side.
|
2018-11-09 19:31:08 +00:00
|
|
|
* @internal
|
2017-09-24 17:38:14 +00:00
|
|
|
* @param string $name
|
|
|
|
* @return array
|
2014-09-26 18:11:21 +00:00
|
|
|
*/
|
2018-11-09 19:31:08 +00:00
|
|
|
public function loadPHPLibrary( $name ) {
|
2014-09-26 18:11:21 +00:00
|
|
|
$args = func_get_args();
|
|
|
|
$this->checkString( 'loadPHPLibrary', $args, 0 );
|
|
|
|
|
|
|
|
$ret = null;
|
|
|
|
if ( isset( $this->availableLibraries[$name] ) ) {
|
|
|
|
$ret = $this->instantiatePHPLibrary( $name, $this->availableLibraries[$name], true );
|
|
|
|
}
|
|
|
|
|
2017-06-15 17:19:00 +00:00
|
|
|
return [ $ret ];
|
2014-09-26 18:11:21 +00:00
|
|
|
}
|
|
|
|
|
Added more Lua environment features
Package library:
* Added a simulation of the Lua 5.1 package library.
* Removed mw.import(), replaced it with a package loader. Packages can be
retrieved from the wiki, using require('Module:Foo'), or from files
distributed with Scribunto, using require('foo'). The "Module:" prefix allows
for source compatibility with existing Lua code.
* Added a couple of libraries from LuaForge: luabit and stringtools.
* Made fetchModuleFromParser() return null on error instead of throwing an
exception, to more easily support the desired behaviour of the package loader,
which needs to return null on error.
* Renamed mw.setupEnvironment() to mw.setup() since it is setting up things
other than the environment now.
* In MWServer:handleRegisterLibrary(), remove the feature which interprets dots
in library names, since LuaSandbox doesn't support this.
Improved module isolation and related refactoring:
* Expose restricted versions of getfenv() and setfenv() to user Lua code.
Requires luasandbox r114952.
* Don't cache the export list returned by module execution for later function
calls. This breaks isolation of #invoke calls, since the local variables are
persistent.
* Removed ScribuntoFunctionBase and its children, since it doesn't really have
a purpose if it can't cache anything. Instead, invoke functions using a module
method called invoke().
* Removed Module::initialize(), replaced it with a validate() function. This is
a more elegant interface and works better with the new module caching scheme.
* Use a Status object for the return value of Engine::validate() instead of an
array. Use the formatting facilities of the Status class.
Other:
* Removed "too many returns" error, doesn't fit in with Lua conventions.
* Use the standalone engine by default, so that the extension will work without
configuration for more people.
* Added an accessor for $engine->interpreter
* Fix mw.clone() to correctly clone metatables
* If the standalone interpreter exits due to an error, there are some contexts
where the initial error will be caught and ignored, and the user will see the
error from checkValid() instead. In this case, rethrow the original error for
a more informative message.
* Load mw.lua into the initial standalone environment, to reduce code
duplication between mw.lua and MWServer.lua.
* Fixed a bug in Scribunto_LuaStandaloneInterpreter::handleCall() for functions
that return no results.
* Fixed a bug in encodeLuaVar() for strings with "\r". Added test case.
* In MWServer.lua, don't call error() for internal errors, instead just print
the error and exit. This avoids a protocol violation when an error is
encountered from within handleCall().
* Added lots of documentation. Lua doc comments are in LuaDoc format.
Change-Id: Ie2fd572c362bedf02f45d3fa5352a5280e034740
2012-04-18 03:46:18 +00:00
|
|
|
/**
|
2013-01-25 17:53:18 +00:00
|
|
|
* Handler for the loadPackage() callback. Load the specified
|
Added more Lua environment features
Package library:
* Added a simulation of the Lua 5.1 package library.
* Removed mw.import(), replaced it with a package loader. Packages can be
retrieved from the wiki, using require('Module:Foo'), or from files
distributed with Scribunto, using require('foo'). The "Module:" prefix allows
for source compatibility with existing Lua code.
* Added a couple of libraries from LuaForge: luabit and stringtools.
* Made fetchModuleFromParser() return null on error instead of throwing an
exception, to more easily support the desired behaviour of the package loader,
which needs to return null on error.
* Renamed mw.setupEnvironment() to mw.setup() since it is setting up things
other than the environment now.
* In MWServer:handleRegisterLibrary(), remove the feature which interprets dots
in library names, since LuaSandbox doesn't support this.
Improved module isolation and related refactoring:
* Expose restricted versions of getfenv() and setfenv() to user Lua code.
Requires luasandbox r114952.
* Don't cache the export list returned by module execution for later function
calls. This breaks isolation of #invoke calls, since the local variables are
persistent.
* Removed ScribuntoFunctionBase and its children, since it doesn't really have
a purpose if it can't cache anything. Instead, invoke functions using a module
method called invoke().
* Removed Module::initialize(), replaced it with a validate() function. This is
a more elegant interface and works better with the new module caching scheme.
* Use a Status object for the return value of Engine::validate() instead of an
array. Use the formatting facilities of the Status class.
Other:
* Removed "too many returns" error, doesn't fit in with Lua conventions.
* Use the standalone engine by default, so that the extension will work without
configuration for more people.
* Added an accessor for $engine->interpreter
* Fix mw.clone() to correctly clone metatables
* If the standalone interpreter exits due to an error, there are some contexts
where the initial error will be caught and ignored, and the user will see the
error from checkValid() instead. In this case, rethrow the original error for
a more informative message.
* Load mw.lua into the initial standalone environment, to reduce code
duplication between mw.lua and MWServer.lua.
* Fixed a bug in Scribunto_LuaStandaloneInterpreter::handleCall() for functions
that return no results.
* Fixed a bug in encodeLuaVar() for strings with "\r". Added test case.
* In MWServer.lua, don't call error() for internal errors, instead just print
the error and exit. This avoids a protocol violation when an error is
encountered from within handleCall().
* Added lots of documentation. Lua doc comments are in LuaDoc format.
Change-Id: Ie2fd572c362bedf02f45d3fa5352a5280e034740
2012-04-18 03:46:18 +00:00
|
|
|
* module and return its chunk. It's not necessary to cache the resulting
|
|
|
|
* chunk in the object instance, since there is caching in a wrapper on the
|
|
|
|
* Lua side.
|
2018-11-09 19:31:08 +00:00
|
|
|
* @internal
|
2017-09-24 17:38:14 +00:00
|
|
|
* @param string $name
|
|
|
|
* @return array
|
Added more Lua environment features
Package library:
* Added a simulation of the Lua 5.1 package library.
* Removed mw.import(), replaced it with a package loader. Packages can be
retrieved from the wiki, using require('Module:Foo'), or from files
distributed with Scribunto, using require('foo'). The "Module:" prefix allows
for source compatibility with existing Lua code.
* Added a couple of libraries from LuaForge: luabit and stringtools.
* Made fetchModuleFromParser() return null on error instead of throwing an
exception, to more easily support the desired behaviour of the package loader,
which needs to return null on error.
* Renamed mw.setupEnvironment() to mw.setup() since it is setting up things
other than the environment now.
* In MWServer:handleRegisterLibrary(), remove the feature which interprets dots
in library names, since LuaSandbox doesn't support this.
Improved module isolation and related refactoring:
* Expose restricted versions of getfenv() and setfenv() to user Lua code.
Requires luasandbox r114952.
* Don't cache the export list returned by module execution for later function
calls. This breaks isolation of #invoke calls, since the local variables are
persistent.
* Removed ScribuntoFunctionBase and its children, since it doesn't really have
a purpose if it can't cache anything. Instead, invoke functions using a module
method called invoke().
* Removed Module::initialize(), replaced it with a validate() function. This is
a more elegant interface and works better with the new module caching scheme.
* Use a Status object for the return value of Engine::validate() instead of an
array. Use the formatting facilities of the Status class.
Other:
* Removed "too many returns" error, doesn't fit in with Lua conventions.
* Use the standalone engine by default, so that the extension will work without
configuration for more people.
* Added an accessor for $engine->interpreter
* Fix mw.clone() to correctly clone metatables
* If the standalone interpreter exits due to an error, there are some contexts
where the initial error will be caught and ignored, and the user will see the
error from checkValid() instead. In this case, rethrow the original error for
a more informative message.
* Load mw.lua into the initial standalone environment, to reduce code
duplication between mw.lua and MWServer.lua.
* Fixed a bug in Scribunto_LuaStandaloneInterpreter::handleCall() for functions
that return no results.
* Fixed a bug in encodeLuaVar() for strings with "\r". Added test case.
* In MWServer.lua, don't call error() for internal errors, instead just print
the error and exit. This avoids a protocol violation when an error is
encountered from within handleCall().
* Added lots of documentation. Lua doc comments are in LuaDoc format.
Change-Id: Ie2fd572c362bedf02f45d3fa5352a5280e034740
2012-04-18 03:46:18 +00:00
|
|
|
*/
|
2018-11-09 19:31:08 +00:00
|
|
|
public function loadPackage( $name ) {
|
2012-04-13 10:38:12 +00:00
|
|
|
$args = func_get_args();
|
Added more Lua environment features
Package library:
* Added a simulation of the Lua 5.1 package library.
* Removed mw.import(), replaced it with a package loader. Packages can be
retrieved from the wiki, using require('Module:Foo'), or from files
distributed with Scribunto, using require('foo'). The "Module:" prefix allows
for source compatibility with existing Lua code.
* Added a couple of libraries from LuaForge: luabit and stringtools.
* Made fetchModuleFromParser() return null on error instead of throwing an
exception, to more easily support the desired behaviour of the package loader,
which needs to return null on error.
* Renamed mw.setupEnvironment() to mw.setup() since it is setting up things
other than the environment now.
* In MWServer:handleRegisterLibrary(), remove the feature which interprets dots
in library names, since LuaSandbox doesn't support this.
Improved module isolation and related refactoring:
* Expose restricted versions of getfenv() and setfenv() to user Lua code.
Requires luasandbox r114952.
* Don't cache the export list returned by module execution for later function
calls. This breaks isolation of #invoke calls, since the local variables are
persistent.
* Removed ScribuntoFunctionBase and its children, since it doesn't really have
a purpose if it can't cache anything. Instead, invoke functions using a module
method called invoke().
* Removed Module::initialize(), replaced it with a validate() function. This is
a more elegant interface and works better with the new module caching scheme.
* Use a Status object for the return value of Engine::validate() instead of an
array. Use the formatting facilities of the Status class.
Other:
* Removed "too many returns" error, doesn't fit in with Lua conventions.
* Use the standalone engine by default, so that the extension will work without
configuration for more people.
* Added an accessor for $engine->interpreter
* Fix mw.clone() to correctly clone metatables
* If the standalone interpreter exits due to an error, there are some contexts
where the initial error will be caught and ignored, and the user will see the
error from checkValid() instead. In this case, rethrow the original error for
a more informative message.
* Load mw.lua into the initial standalone environment, to reduce code
duplication between mw.lua and MWServer.lua.
* Fixed a bug in Scribunto_LuaStandaloneInterpreter::handleCall() for functions
that return no results.
* Fixed a bug in encodeLuaVar() for strings with "\r". Added test case.
* In MWServer.lua, don't call error() for internal errors, instead just print
the error and exit. This avoids a protocol violation when an error is
encountered from within handleCall().
* Added lots of documentation. Lua doc comments are in LuaDoc format.
Change-Id: Ie2fd572c362bedf02f45d3fa5352a5280e034740
2012-04-18 03:46:18 +00:00
|
|
|
$this->checkString( 'loadPackage', $args, 0 );
|
|
|
|
|
2013-02-08 19:20:32 +00:00
|
|
|
# This is what Lua does for its built-in loaders
|
|
|
|
$luaName = str_replace( '.', '/', $name ) . '.lua';
|
2013-02-14 17:38:03 +00:00
|
|
|
$paths = $this->getLibraryPaths( 'lua', self::$libraryPaths );
|
|
|
|
foreach ( $paths as $path ) {
|
|
|
|
$fileName = $this->normalizeModuleFileName( "$path/$luaName" );
|
Added more Lua environment features
Package library:
* Added a simulation of the Lua 5.1 package library.
* Removed mw.import(), replaced it with a package loader. Packages can be
retrieved from the wiki, using require('Module:Foo'), or from files
distributed with Scribunto, using require('foo'). The "Module:" prefix allows
for source compatibility with existing Lua code.
* Added a couple of libraries from LuaForge: luabit and stringtools.
* Made fetchModuleFromParser() return null on error instead of throwing an
exception, to more easily support the desired behaviour of the package loader,
which needs to return null on error.
* Renamed mw.setupEnvironment() to mw.setup() since it is setting up things
other than the environment now.
* In MWServer:handleRegisterLibrary(), remove the feature which interprets dots
in library names, since LuaSandbox doesn't support this.
Improved module isolation and related refactoring:
* Expose restricted versions of getfenv() and setfenv() to user Lua code.
Requires luasandbox r114952.
* Don't cache the export list returned by module execution for later function
calls. This breaks isolation of #invoke calls, since the local variables are
persistent.
* Removed ScribuntoFunctionBase and its children, since it doesn't really have
a purpose if it can't cache anything. Instead, invoke functions using a module
method called invoke().
* Removed Module::initialize(), replaced it with a validate() function. This is
a more elegant interface and works better with the new module caching scheme.
* Use a Status object for the return value of Engine::validate() instead of an
array. Use the formatting facilities of the Status class.
Other:
* Removed "too many returns" error, doesn't fit in with Lua conventions.
* Use the standalone engine by default, so that the extension will work without
configuration for more people.
* Added an accessor for $engine->interpreter
* Fix mw.clone() to correctly clone metatables
* If the standalone interpreter exits due to an error, there are some contexts
where the initial error will be caught and ignored, and the user will see the
error from checkValid() instead. In this case, rethrow the original error for
a more informative message.
* Load mw.lua into the initial standalone environment, to reduce code
duplication between mw.lua and MWServer.lua.
* Fixed a bug in Scribunto_LuaStandaloneInterpreter::handleCall() for functions
that return no results.
* Fixed a bug in encodeLuaVar() for strings with "\r". Added test case.
* In MWServer.lua, don't call error() for internal errors, instead just print
the error and exit. This avoids a protocol violation when an error is
encountered from within handleCall().
* Added lots of documentation. Lua doc comments are in LuaDoc format.
Change-Id: Ie2fd572c362bedf02f45d3fa5352a5280e034740
2012-04-18 03:46:18 +00:00
|
|
|
if ( !file_exists( $fileName ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$code = file_get_contents( $fileName );
|
2013-02-08 19:20:32 +00:00
|
|
|
$init = $this->interpreter->loadString( $code, "@$luaName" );
|
2017-06-15 17:19:00 +00:00
|
|
|
return [ $init ];
|
2012-04-13 10:38:12 +00:00
|
|
|
}
|
Added more Lua environment features
Package library:
* Added a simulation of the Lua 5.1 package library.
* Removed mw.import(), replaced it with a package loader. Packages can be
retrieved from the wiki, using require('Module:Foo'), or from files
distributed with Scribunto, using require('foo'). The "Module:" prefix allows
for source compatibility with existing Lua code.
* Added a couple of libraries from LuaForge: luabit and stringtools.
* Made fetchModuleFromParser() return null on error instead of throwing an
exception, to more easily support the desired behaviour of the package loader,
which needs to return null on error.
* Renamed mw.setupEnvironment() to mw.setup() since it is setting up things
other than the environment now.
* In MWServer:handleRegisterLibrary(), remove the feature which interprets dots
in library names, since LuaSandbox doesn't support this.
Improved module isolation and related refactoring:
* Expose restricted versions of getfenv() and setfenv() to user Lua code.
Requires luasandbox r114952.
* Don't cache the export list returned by module execution for later function
calls. This breaks isolation of #invoke calls, since the local variables are
persistent.
* Removed ScribuntoFunctionBase and its children, since it doesn't really have
a purpose if it can't cache anything. Instead, invoke functions using a module
method called invoke().
* Removed Module::initialize(), replaced it with a validate() function. This is
a more elegant interface and works better with the new module caching scheme.
* Use a Status object for the return value of Engine::validate() instead of an
array. Use the formatting facilities of the Status class.
Other:
* Removed "too many returns" error, doesn't fit in with Lua conventions.
* Use the standalone engine by default, so that the extension will work without
configuration for more people.
* Added an accessor for $engine->interpreter
* Fix mw.clone() to correctly clone metatables
* If the standalone interpreter exits due to an error, there are some contexts
where the initial error will be caught and ignored, and the user will see the
error from checkValid() instead. In this case, rethrow the original error for
a more informative message.
* Load mw.lua into the initial standalone environment, to reduce code
duplication between mw.lua and MWServer.lua.
* Fixed a bug in Scribunto_LuaStandaloneInterpreter::handleCall() for functions
that return no results.
* Fixed a bug in encodeLuaVar() for strings with "\r". Added test case.
* In MWServer.lua, don't call error() for internal errors, instead just print
the error and exit. This avoids a protocol violation when an error is
encountered from within handleCall().
* Added lots of documentation. Lua doc comments are in LuaDoc format.
Change-Id: Ie2fd572c362bedf02f45d3fa5352a5280e034740
2012-04-18 03:46:18 +00:00
|
|
|
|
|
|
|
$title = Title::newFromText( $name );
|
2014-09-16 03:25:53 +00:00
|
|
|
if ( !$title || !$title->hasContentModel( CONTENT_MODEL_SCRIBUNTO ) ) {
|
2017-06-15 17:19:00 +00:00
|
|
|
return [];
|
Added more Lua environment features
Package library:
* Added a simulation of the Lua 5.1 package library.
* Removed mw.import(), replaced it with a package loader. Packages can be
retrieved from the wiki, using require('Module:Foo'), or from files
distributed with Scribunto, using require('foo'). The "Module:" prefix allows
for source compatibility with existing Lua code.
* Added a couple of libraries from LuaForge: luabit and stringtools.
* Made fetchModuleFromParser() return null on error instead of throwing an
exception, to more easily support the desired behaviour of the package loader,
which needs to return null on error.
* Renamed mw.setupEnvironment() to mw.setup() since it is setting up things
other than the environment now.
* In MWServer:handleRegisterLibrary(), remove the feature which interprets dots
in library names, since LuaSandbox doesn't support this.
Improved module isolation and related refactoring:
* Expose restricted versions of getfenv() and setfenv() to user Lua code.
Requires luasandbox r114952.
* Don't cache the export list returned by module execution for later function
calls. This breaks isolation of #invoke calls, since the local variables are
persistent.
* Removed ScribuntoFunctionBase and its children, since it doesn't really have
a purpose if it can't cache anything. Instead, invoke functions using a module
method called invoke().
* Removed Module::initialize(), replaced it with a validate() function. This is
a more elegant interface and works better with the new module caching scheme.
* Use a Status object for the return value of Engine::validate() instead of an
array. Use the formatting facilities of the Status class.
Other:
* Removed "too many returns" error, doesn't fit in with Lua conventions.
* Use the standalone engine by default, so that the extension will work without
configuration for more people.
* Added an accessor for $engine->interpreter
* Fix mw.clone() to correctly clone metatables
* If the standalone interpreter exits due to an error, there are some contexts
where the initial error will be caught and ignored, and the user will see the
error from checkValid() instead. In this case, rethrow the original error for
a more informative message.
* Load mw.lua into the initial standalone environment, to reduce code
duplication between mw.lua and MWServer.lua.
* Fixed a bug in Scribunto_LuaStandaloneInterpreter::handleCall() for functions
that return no results.
* Fixed a bug in encodeLuaVar() for strings with "\r". Added test case.
* In MWServer.lua, don't call error() for internal errors, instead just print
the error and exit. This avoids a protocol violation when an error is
encountered from within handleCall().
* Added lots of documentation. Lua doc comments are in LuaDoc format.
Change-Id: Ie2fd572c362bedf02f45d3fa5352a5280e034740
2012-04-18 03:46:18 +00:00
|
|
|
}
|
|
|
|
|
2012-04-13 10:38:12 +00:00
|
|
|
$module = $this->fetchModuleFromParser( $title );
|
Added more Lua environment features
Package library:
* Added a simulation of the Lua 5.1 package library.
* Removed mw.import(), replaced it with a package loader. Packages can be
retrieved from the wiki, using require('Module:Foo'), or from files
distributed with Scribunto, using require('foo'). The "Module:" prefix allows
for source compatibility with existing Lua code.
* Added a couple of libraries from LuaForge: luabit and stringtools.
* Made fetchModuleFromParser() return null on error instead of throwing an
exception, to more easily support the desired behaviour of the package loader,
which needs to return null on error.
* Renamed mw.setupEnvironment() to mw.setup() since it is setting up things
other than the environment now.
* In MWServer:handleRegisterLibrary(), remove the feature which interprets dots
in library names, since LuaSandbox doesn't support this.
Improved module isolation and related refactoring:
* Expose restricted versions of getfenv() and setfenv() to user Lua code.
Requires luasandbox r114952.
* Don't cache the export list returned by module execution for later function
calls. This breaks isolation of #invoke calls, since the local variables are
persistent.
* Removed ScribuntoFunctionBase and its children, since it doesn't really have
a purpose if it can't cache anything. Instead, invoke functions using a module
method called invoke().
* Removed Module::initialize(), replaced it with a validate() function. This is
a more elegant interface and works better with the new module caching scheme.
* Use a Status object for the return value of Engine::validate() instead of an
array. Use the formatting facilities of the Status class.
Other:
* Removed "too many returns" error, doesn't fit in with Lua conventions.
* Use the standalone engine by default, so that the extension will work without
configuration for more people.
* Added an accessor for $engine->interpreter
* Fix mw.clone() to correctly clone metatables
* If the standalone interpreter exits due to an error, there are some contexts
where the initial error will be caught and ignored, and the user will see the
error from checkValid() instead. In this case, rethrow the original error for
a more informative message.
* Load mw.lua into the initial standalone environment, to reduce code
duplication between mw.lua and MWServer.lua.
* Fixed a bug in Scribunto_LuaStandaloneInterpreter::handleCall() for functions
that return no results.
* Fixed a bug in encodeLuaVar() for strings with "\r". Added test case.
* In MWServer.lua, don't call error() for internal errors, instead just print
the error and exit. This avoids a protocol violation when an error is
encountered from within handleCall().
* Added lots of documentation. Lua doc comments are in LuaDoc format.
Change-Id: Ie2fd572c362bedf02f45d3fa5352a5280e034740
2012-04-18 03:46:18 +00:00
|
|
|
if ( $module ) {
|
2019-03-21 04:19:37 +00:00
|
|
|
// @phan-suppress-next-line PhanUndeclaredMethod
|
2017-06-15 17:19:00 +00:00
|
|
|
return [ $module->getInitChunk() ];
|
Added more Lua environment features
Package library:
* Added a simulation of the Lua 5.1 package library.
* Removed mw.import(), replaced it with a package loader. Packages can be
retrieved from the wiki, using require('Module:Foo'), or from files
distributed with Scribunto, using require('foo'). The "Module:" prefix allows
for source compatibility with existing Lua code.
* Added a couple of libraries from LuaForge: luabit and stringtools.
* Made fetchModuleFromParser() return null on error instead of throwing an
exception, to more easily support the desired behaviour of the package loader,
which needs to return null on error.
* Renamed mw.setupEnvironment() to mw.setup() since it is setting up things
other than the environment now.
* In MWServer:handleRegisterLibrary(), remove the feature which interprets dots
in library names, since LuaSandbox doesn't support this.
Improved module isolation and related refactoring:
* Expose restricted versions of getfenv() and setfenv() to user Lua code.
Requires luasandbox r114952.
* Don't cache the export list returned by module execution for later function
calls. This breaks isolation of #invoke calls, since the local variables are
persistent.
* Removed ScribuntoFunctionBase and its children, since it doesn't really have
a purpose if it can't cache anything. Instead, invoke functions using a module
method called invoke().
* Removed Module::initialize(), replaced it with a validate() function. This is
a more elegant interface and works better with the new module caching scheme.
* Use a Status object for the return value of Engine::validate() instead of an
array. Use the formatting facilities of the Status class.
Other:
* Removed "too many returns" error, doesn't fit in with Lua conventions.
* Use the standalone engine by default, so that the extension will work without
configuration for more people.
* Added an accessor for $engine->interpreter
* Fix mw.clone() to correctly clone metatables
* If the standalone interpreter exits due to an error, there are some contexts
where the initial error will be caught and ignored, and the user will see the
error from checkValid() instead. In this case, rethrow the original error for
a more informative message.
* Load mw.lua into the initial standalone environment, to reduce code
duplication between mw.lua and MWServer.lua.
* Fixed a bug in Scribunto_LuaStandaloneInterpreter::handleCall() for functions
that return no results.
* Fixed a bug in encodeLuaVar() for strings with "\r". Added test case.
* In MWServer.lua, don't call error() for internal errors, instead just print
the error and exit. This avoids a protocol violation when an error is
encountered from within handleCall().
* Added lots of documentation. Lua doc comments are in LuaDoc format.
Change-Id: Ie2fd572c362bedf02f45d3fa5352a5280e034740
2012-04-18 03:46:18 +00:00
|
|
|
} else {
|
2017-06-15 17:19:00 +00:00
|
|
|
return [];
|
Added more Lua environment features
Package library:
* Added a simulation of the Lua 5.1 package library.
* Removed mw.import(), replaced it with a package loader. Packages can be
retrieved from the wiki, using require('Module:Foo'), or from files
distributed with Scribunto, using require('foo'). The "Module:" prefix allows
for source compatibility with existing Lua code.
* Added a couple of libraries from LuaForge: luabit and stringtools.
* Made fetchModuleFromParser() return null on error instead of throwing an
exception, to more easily support the desired behaviour of the package loader,
which needs to return null on error.
* Renamed mw.setupEnvironment() to mw.setup() since it is setting up things
other than the environment now.
* In MWServer:handleRegisterLibrary(), remove the feature which interprets dots
in library names, since LuaSandbox doesn't support this.
Improved module isolation and related refactoring:
* Expose restricted versions of getfenv() and setfenv() to user Lua code.
Requires luasandbox r114952.
* Don't cache the export list returned by module execution for later function
calls. This breaks isolation of #invoke calls, since the local variables are
persistent.
* Removed ScribuntoFunctionBase and its children, since it doesn't really have
a purpose if it can't cache anything. Instead, invoke functions using a module
method called invoke().
* Removed Module::initialize(), replaced it with a validate() function. This is
a more elegant interface and works better with the new module caching scheme.
* Use a Status object for the return value of Engine::validate() instead of an
array. Use the formatting facilities of the Status class.
Other:
* Removed "too many returns" error, doesn't fit in with Lua conventions.
* Use the standalone engine by default, so that the extension will work without
configuration for more people.
* Added an accessor for $engine->interpreter
* Fix mw.clone() to correctly clone metatables
* If the standalone interpreter exits due to an error, there are some contexts
where the initial error will be caught and ignored, and the user will see the
error from checkValid() instead. In this case, rethrow the original error for
a more informative message.
* Load mw.lua into the initial standalone environment, to reduce code
duplication between mw.lua and MWServer.lua.
* Fixed a bug in Scribunto_LuaStandaloneInterpreter::handleCall() for functions
that return no results.
* Fixed a bug in encodeLuaVar() for strings with "\r". Added test case.
* In MWServer.lua, don't call error() for internal errors, instead just print
the error and exit. This avoids a protocol violation when an error is
encountered from within handleCall().
* Added lots of documentation. Lua doc comments are in LuaDoc format.
Change-Id: Ie2fd572c362bedf02f45d3fa5352a5280e034740
2012-04-18 03:46:18 +00:00
|
|
|
}
|
2012-04-13 10:38:12 +00:00
|
|
|
}
|
2012-05-22 03:56:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function for the implementation of frame methods
|
2014-06-18 17:17:04 +00:00
|
|
|
*
|
2014-10-09 19:49:40 +00:00
|
|
|
* @param string $frameId
|
2014-06-18 17:17:04 +00:00
|
|
|
* @return PPFrame
|
|
|
|
*
|
2022-08-03 12:31:15 +00:00
|
|
|
* @throws LuaError
|
2012-05-22 03:56:07 +00:00
|
|
|
*/
|
|
|
|
protected function getFrameById( $frameId ) {
|
2014-07-07 16:30:44 +00:00
|
|
|
if ( $frameId === 'empty' ) {
|
2015-06-26 16:37:34 +00:00
|
|
|
return $this->getParser()->getPreprocessor()->newFrame();
|
2014-07-07 16:30:44 +00:00
|
|
|
} elseif ( isset( $this->currentFrames[$frameId] ) ) {
|
2024-08-26 15:24:52 +00:00
|
|
|
// @phan-suppress-next-line PhanTypeMismatchReturnNullable False positive
|
2013-02-19 00:49:08 +00:00
|
|
|
return $this->currentFrames[$frameId];
|
2012-05-22 03:56:07 +00:00
|
|
|
} else {
|
2022-08-03 12:31:15 +00:00
|
|
|
throw new LuaError( 'invalid frame ID' );
|
2012-05-22 03:56:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-02-19 00:49:08 +00:00
|
|
|
* Handler for frameExists()
|
2014-10-09 19:49:40 +00:00
|
|
|
*
|
2018-11-09 19:31:08 +00:00
|
|
|
* @internal
|
2014-10-09 19:49:40 +00:00
|
|
|
* @param string $frameId
|
2014-06-18 17:17:04 +00:00
|
|
|
* @return array
|
2013-02-19 00:49:08 +00:00
|
|
|
*/
|
2018-11-09 19:31:08 +00:00
|
|
|
public function frameExists( $frameId ) {
|
2017-06-15 17:19:00 +00:00
|
|
|
return [ $frameId === 'empty' || isset( $this->currentFrames[$frameId] ) ];
|
2013-02-19 00:49:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for newChildFrame()
|
2014-06-18 17:17:04 +00:00
|
|
|
*
|
2018-11-09 19:31:08 +00:00
|
|
|
* @internal
|
2018-01-10 17:45:07 +00:00
|
|
|
* @param string $frameId
|
|
|
|
* @param string $title
|
|
|
|
* @param array $args
|
|
|
|
* @return array
|
2022-08-03 12:31:15 +00:00
|
|
|
* @throws LuaError
|
2012-05-22 03:56:07 +00:00
|
|
|
*/
|
2018-11-09 19:31:08 +00:00
|
|
|
public function newChildFrame( $frameId, $title, array $args ) {
|
2013-02-19 00:49:08 +00:00
|
|
|
if ( count( $this->currentFrames ) > 100 ) {
|
2022-08-03 12:31:15 +00:00
|
|
|
throw new LuaError( 'newChild: too many frames' );
|
2013-02-19 00:49:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$frame = $this->getFrameById( $frameId );
|
|
|
|
if ( $title === false ) {
|
|
|
|
$title = $frame->getTitle();
|
|
|
|
} else {
|
|
|
|
$title = Title::newFromText( $title );
|
|
|
|
if ( !$title ) {
|
2022-08-03 12:31:15 +00:00
|
|
|
throw new LuaError( 'newChild: invalid title' );
|
2013-02-19 00:49:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$args = $this->getParser()->getPreprocessor()->newPartNodeArray( $args );
|
|
|
|
$newFrame = $frame->newChild( $args, $title );
|
|
|
|
$newFrameId = 'frame' . count( $this->currentFrames );
|
|
|
|
$this->currentFrames[$newFrameId] = $newFrame;
|
2017-06-15 17:19:00 +00:00
|
|
|
return [ $newFrameId ];
|
2012-05-22 03:56:07 +00:00
|
|
|
}
|
|
|
|
|
2013-12-06 18:10:39 +00:00
|
|
|
/**
|
|
|
|
* Handler for getTitle()
|
2014-06-18 17:17:04 +00:00
|
|
|
*
|
2018-11-09 19:31:08 +00:00
|
|
|
* @internal
|
2017-09-24 17:38:14 +00:00
|
|
|
* @param string $frameId
|
2014-06-18 17:17:04 +00:00
|
|
|
*
|
|
|
|
* @return array
|
2013-12-06 18:10:39 +00:00
|
|
|
*/
|
2018-11-09 19:31:08 +00:00
|
|
|
public function getFrameTitle( $frameId ) {
|
2013-12-06 18:10:39 +00:00
|
|
|
$frame = $this->getFrameById( $frameId );
|
2017-06-15 17:19:00 +00:00
|
|
|
return [ $frame->getTitle()->getPrefixedText() ];
|
2013-12-06 18:10:39 +00:00
|
|
|
}
|
|
|
|
|
2014-06-01 03:15:42 +00:00
|
|
|
/**
|
|
|
|
* Handler for setTTL()
|
2018-11-09 19:31:08 +00:00
|
|
|
* @internal
|
2018-01-10 17:45:07 +00:00
|
|
|
* @param int $ttl
|
2014-06-01 03:15:42 +00:00
|
|
|
*/
|
2018-11-09 19:31:08 +00:00
|
|
|
public function setTTL( $ttl ) {
|
2014-06-01 03:15:42 +00:00
|
|
|
$args = func_get_args();
|
|
|
|
$this->checkNumber( 'setTTL', $args, 0 );
|
|
|
|
|
|
|
|
$frame = $this->getFrameById( 'current' );
|
2016-10-13 15:07:44 +00:00
|
|
|
$frame->setTTL( $ttl );
|
2014-06-01 03:15:42 +00:00
|
|
|
}
|
|
|
|
|
2012-05-22 03:56:07 +00:00
|
|
|
/**
|
2013-01-25 17:53:18 +00:00
|
|
|
* Handler for getExpandedArgument()
|
2018-11-09 19:31:08 +00:00
|
|
|
* @internal
|
2018-01-10 17:45:07 +00:00
|
|
|
* @param string $frameId
|
|
|
|
* @param string $name
|
|
|
|
* @return array
|
2012-05-22 03:56:07 +00:00
|
|
|
*/
|
2018-11-09 19:31:08 +00:00
|
|
|
public function getExpandedArgument( $frameId, $name ) {
|
2012-05-22 03:56:07 +00:00
|
|
|
$args = func_get_args();
|
|
|
|
$this->checkString( 'getExpandedArgument', $args, 0 );
|
|
|
|
|
|
|
|
$frame = $this->getFrameById( $frameId );
|
2013-03-06 23:28:24 +00:00
|
|
|
$this->getInterpreter()->pauseUsageTimer();
|
2012-05-22 03:56:07 +00:00
|
|
|
$result = $frame->getArgument( $name );
|
|
|
|
if ( $result === false ) {
|
2017-06-15 17:19:00 +00:00
|
|
|
return [];
|
2012-05-22 03:56:07 +00:00
|
|
|
} else {
|
2017-06-15 17:19:00 +00:00
|
|
|
return [ $result ];
|
2012-05-22 03:56:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-01-25 17:53:18 +00:00
|
|
|
* Handler for getAllExpandedArguments()
|
2018-11-09 19:31:08 +00:00
|
|
|
* @internal
|
2018-01-10 17:45:07 +00:00
|
|
|
* @param string $frameId
|
|
|
|
* @return array
|
2012-05-22 03:56:07 +00:00
|
|
|
*/
|
2018-11-09 19:31:08 +00:00
|
|
|
public function getAllExpandedArguments( $frameId ) {
|
2012-05-22 03:56:07 +00:00
|
|
|
$frame = $this->getFrameById( $frameId );
|
2013-03-06 23:28:24 +00:00
|
|
|
$this->getInterpreter()->pauseUsageTimer();
|
2017-06-15 17:19:00 +00:00
|
|
|
return [ $frame->getArguments() ];
|
2012-05-22 03:56:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-01-25 17:53:18 +00:00
|
|
|
* Handler for expandTemplate()
|
2018-11-09 19:31:08 +00:00
|
|
|
* @internal
|
2018-01-10 17:45:07 +00:00
|
|
|
* @param string $frameId
|
|
|
|
* @param string $titleText
|
|
|
|
* @param array $args
|
|
|
|
* @return array
|
2022-08-03 12:31:15 +00:00
|
|
|
* @throws LuaError
|
2012-05-22 03:56:07 +00:00
|
|
|
*/
|
2018-11-09 19:31:08 +00:00
|
|
|
public function expandTemplate( $frameId, $titleText, $args ) {
|
2012-05-22 03:56:07 +00:00
|
|
|
$frame = $this->getFrameById( $frameId );
|
|
|
|
$title = Title::newFromText( $titleText, NS_TEMPLATE );
|
|
|
|
if ( !$title ) {
|
2022-08-03 12:31:15 +00:00
|
|
|
throw new LuaError( "expandTemplate: invalid title \"$titleText\"" );
|
2012-05-22 03:56:07 +00:00
|
|
|
}
|
|
|
|
|
2022-12-27 11:53:12 +00:00
|
|
|
if ( $frame->depth >= $this->parser->getOptions()->getMaxTemplateDepth() ) {
|
2022-08-03 12:31:15 +00:00
|
|
|
throw new LuaError( 'expandTemplate: template depth limit exceeded' );
|
2012-05-22 03:56:07 +00:00
|
|
|
}
|
2021-06-03 16:33:54 +00:00
|
|
|
if ( MediaWikiServices::getInstance()->getNamespaceInfo()->isNonincludable( $title->getNamespace() ) ) {
|
2022-08-03 12:31:15 +00:00
|
|
|
throw new LuaError( 'expandTemplate: template inclusion denied' );
|
2012-05-22 03:56:07 +00:00
|
|
|
}
|
|
|
|
|
2024-03-10 22:46:30 +00:00
|
|
|
[ $dom, $finalTitle ] = $this->parser->getTemplateDom( $title );
|
2012-05-22 03:56:07 +00:00
|
|
|
if ( $dom === false ) {
|
2022-08-03 12:31:15 +00:00
|
|
|
throw new LuaError( "expandTemplate: template \"$titleText\" does not exist" );
|
2012-05-22 03:56:07 +00:00
|
|
|
}
|
|
|
|
if ( !$frame->loopCheck( $finalTitle ) ) {
|
2022-08-03 12:31:15 +00:00
|
|
|
throw new LuaError( 'expandTemplate: template loop detected' );
|
2012-05-22 03:56:07 +00:00
|
|
|
}
|
|
|
|
|
2013-10-10 15:18:13 +00:00
|
|
|
$fargs = $this->getParser()->getPreprocessor()->newPartNodeArray( $args );
|
|
|
|
$newFrame = $frame->newChild( $fargs, $finalTitle );
|
2014-07-07 18:46:59 +00:00
|
|
|
$text = $this->doCachedExpansion( $newFrame, $dom,
|
2017-06-15 17:19:00 +00:00
|
|
|
[
|
2014-05-31 17:42:45 +00:00
|
|
|
'frameId' => $frameId,
|
2012-05-22 03:56:07 +00:00
|
|
|
'template' => $finalTitle->getPrefixedDBkey(),
|
|
|
|
'args' => $args
|
2017-06-15 17:19:00 +00:00
|
|
|
] );
|
|
|
|
return [ $text ];
|
2012-05-22 03:56:07 +00:00
|
|
|
}
|
|
|
|
|
2013-03-04 05:42:17 +00:00
|
|
|
/**
|
|
|
|
* Handler for callParserFunction()
|
2018-11-09 19:31:08 +00:00
|
|
|
* @internal
|
2017-09-24 17:38:14 +00:00
|
|
|
* @param string $frameId
|
|
|
|
* @param string $function
|
|
|
|
* @param array $args
|
2022-08-03 12:31:15 +00:00
|
|
|
* @throws LuaError
|
2014-06-18 17:17:04 +00:00
|
|
|
* @return array
|
2019-12-22 02:14:03 +00:00
|
|
|
* @suppress PhanImpossibleCondition
|
2013-03-04 05:42:17 +00:00
|
|
|
*/
|
2018-11-09 19:31:08 +00:00
|
|
|
public function callParserFunction( $frameId, $function, $args ) {
|
2013-03-04 05:42:17 +00:00
|
|
|
$frame = $this->getFrameById( $frameId );
|
|
|
|
|
|
|
|
# Make zero-based, without screwing up named args
|
2017-06-15 17:19:00 +00:00
|
|
|
$args = array_merge( [], $args );
|
2013-03-04 05:42:17 +00:00
|
|
|
|
|
|
|
# Sort, since we can't rely on the order coming in from Lua
|
2021-05-13 08:16:18 +00:00
|
|
|
uksort( $args, static function ( $a, $b ) {
|
2013-03-04 05:42:17 +00:00
|
|
|
if ( is_int( $a ) !== is_int( $b ) ) {
|
|
|
|
return is_int( $a ) ? -1 : 1;
|
|
|
|
}
|
|
|
|
if ( is_int( $a ) ) {
|
|
|
|
return $a - $b;
|
|
|
|
}
|
|
|
|
return strcmp( $a, $b );
|
|
|
|
} );
|
|
|
|
|
|
|
|
# Be user-friendly
|
|
|
|
$colonPos = strpos( $function, ':' );
|
|
|
|
if ( $colonPos !== false ) {
|
|
|
|
array_unshift( $args, trim( substr( $function, $colonPos + 1 ) ) );
|
|
|
|
$function = substr( $function, 0, $colonPos );
|
|
|
|
}
|
2014-04-07 16:23:16 +00:00
|
|
|
if ( !isset( $args[0] ) ) {
|
|
|
|
# It's impossible to call a parser function from wikitext without
|
|
|
|
# supplying an arg 0. Insist that one be provided via Lua, too.
|
2022-08-03 12:31:15 +00:00
|
|
|
throw new LuaError( 'callParserFunction: At least one unnamed parameter ' .
|
2014-04-07 16:23:16 +00:00
|
|
|
'(the parameter that comes after the colon in wikitext) ' .
|
|
|
|
'must be provided'
|
|
|
|
);
|
|
|
|
}
|
2013-03-04 05:42:17 +00:00
|
|
|
|
|
|
|
$result = $this->parser->callParserFunction( $frame, $function, $args );
|
|
|
|
if ( !$result['found'] ) {
|
2022-08-03 12:31:15 +00:00
|
|
|
throw new LuaError( "callParserFunction: function \"$function\" was not found" );
|
2013-03-04 05:42:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Set defaults for various flags
|
2017-06-15 17:19:00 +00:00
|
|
|
$result += [
|
2013-03-04 05:42:17 +00:00
|
|
|
'nowiki' => false,
|
|
|
|
'isChildObj' => false,
|
|
|
|
'isLocalObj' => false,
|
|
|
|
'isHTML' => false,
|
|
|
|
'title' => false,
|
2017-06-15 17:19:00 +00:00
|
|
|
];
|
2013-03-04 05:42:17 +00:00
|
|
|
|
|
|
|
$text = $result['text'];
|
|
|
|
if ( $result['isChildObj'] ) {
|
2013-07-07 11:39:52 +00:00
|
|
|
$fargs = $this->getParser()->getPreprocessor()->newPartNodeArray( $args );
|
|
|
|
$newFrame = $frame->newChild( $fargs, $result['title'] );
|
2013-03-04 05:42:17 +00:00
|
|
|
if ( $result['nowiki'] ) {
|
|
|
|
$text = $newFrame->expand( $text, PPFrame::RECOVER_ORIG );
|
|
|
|
} else {
|
|
|
|
$text = $newFrame->expand( $text );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( $result['isLocalObj'] && $result['nowiki'] ) {
|
|
|
|
$text = $frame->expand( $text, PPFrame::RECOVER_ORIG );
|
|
|
|
$result['isLocalObj'] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Replace raw HTML by a placeholder
|
|
|
|
if ( $result['isHTML'] ) {
|
|
|
|
$text = $this->parser->insertStripItem( $text );
|
|
|
|
} elseif ( $result['nowiki'] ) {
|
|
|
|
# Escape nowiki-style return values
|
|
|
|
$text = wfEscapeWikiText( $text );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $result['isLocalObj'] ) {
|
|
|
|
$text = $frame->expand( $text );
|
|
|
|
}
|
|
|
|
|
2017-06-15 17:19:00 +00:00
|
|
|
return [ "$text" ];
|
2013-03-04 05:42:17 +00:00
|
|
|
}
|
|
|
|
|
2012-05-22 03:56:07 +00:00
|
|
|
/**
|
2013-01-25 17:53:18 +00:00
|
|
|
* Handler for preprocess()
|
2018-11-09 19:31:08 +00:00
|
|
|
* @internal
|
2018-01-10 17:45:07 +00:00
|
|
|
* @param string $frameId
|
|
|
|
* @param string $text
|
|
|
|
* @return array
|
2022-08-03 12:31:15 +00:00
|
|
|
* @throws LuaError
|
2012-05-22 03:56:07 +00:00
|
|
|
*/
|
2018-11-09 19:31:08 +00:00
|
|
|
public function preprocess( $frameId, $text ) {
|
2012-05-22 03:56:07 +00:00
|
|
|
$args = func_get_args();
|
|
|
|
$this->checkString( 'preprocess', $args, 0 );
|
|
|
|
|
|
|
|
$frame = $this->getFrameById( $frameId );
|
|
|
|
|
|
|
|
if ( !$frame ) {
|
2022-08-03 12:31:15 +00:00
|
|
|
throw new LuaError( 'attempt to call mw.preprocess with no frame' );
|
2012-05-22 03:56:07 +00:00
|
|
|
}
|
2013-03-06 23:28:24 +00:00
|
|
|
|
|
|
|
// Don't count the time for expanding all the frame arguments against
|
|
|
|
// the Lua time limit.
|
|
|
|
$this->getInterpreter()->pauseUsageTimer();
|
2014-05-31 17:42:45 +00:00
|
|
|
$frame->getArguments();
|
2013-03-06 23:28:24 +00:00
|
|
|
$this->getInterpreter()->unpauseUsageTimer();
|
|
|
|
|
2012-08-27 01:11:32 +00:00
|
|
|
$text = $this->doCachedExpansion( $frame, $text,
|
2017-06-15 17:19:00 +00:00
|
|
|
[
|
2014-05-31 17:42:45 +00:00
|
|
|
'frameId' => $frameId,
|
|
|
|
'inputText' => $text
|
2017-06-15 17:19:00 +00:00
|
|
|
] );
|
|
|
|
return [ $text ];
|
2012-05-22 03:56:07 +00:00
|
|
|
}
|
|
|
|
|
2013-02-01 20:43:32 +00:00
|
|
|
/**
|
|
|
|
* Increment the expensive function count, and throw if limit exceeded
|
|
|
|
*
|
2018-11-09 19:31:08 +00:00
|
|
|
* @internal
|
2022-08-03 12:31:15 +00:00
|
|
|
* @throws LuaError
|
2013-02-01 20:43:32 +00:00
|
|
|
* @return null
|
|
|
|
*/
|
|
|
|
public function incrementExpensiveFunctionCount() {
|
|
|
|
if ( !$this->getParser()->incrementExpensiveFunctionCount() ) {
|
2022-08-03 12:31:15 +00:00
|
|
|
throw new LuaError( "too many expensive function calls" );
|
2013-02-01 20:43:32 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-06-16 20:20:35 +00:00
|
|
|
/**
|
|
|
|
* Adds a warning to be displayed upon preview
|
|
|
|
*
|
2018-11-09 19:31:08 +00:00
|
|
|
* @internal
|
2016-06-16 20:20:35 +00:00
|
|
|
* @param string $text wikitext
|
|
|
|
*/
|
|
|
|
public function addWarning( $text ) {
|
2020-04-03 15:26:59 +00:00
|
|
|
$args = func_get_args();
|
|
|
|
$this->checkString( 'addWarning', $args, 0 );
|
|
|
|
|
2021-10-15 20:18:48 +00:00
|
|
|
// Message localization has to happen on the Lua side
|
|
|
|
$this->getParser()->getOutput()->addWarningMsg(
|
|
|
|
'scribunto-lua-warning',
|
|
|
|
$text
|
|
|
|
);
|
2016-06-16 20:20:35 +00:00
|
|
|
}
|
|
|
|
|
2013-11-01 02:05:00 +00:00
|
|
|
/**
|
|
|
|
* Return whether the parser is currently substing
|
|
|
|
*
|
2018-11-09 19:31:08 +00:00
|
|
|
* @internal
|
2013-11-01 02:05:00 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function isSubsting() {
|
|
|
|
// See Parser::braceSubstitution, OT_WIKI is the switch
|
2020-04-16 16:43:21 +00:00
|
|
|
return [ $this->getParser()->getOutputType() === Parser::OT_WIKI ];
|
2013-11-01 02:05:00 +00:00
|
|
|
}
|
|
|
|
|
2020-01-14 18:50:34 +00:00
|
|
|
/**
|
|
|
|
* @param PPFrame $frame
|
|
|
|
* @param string|array $input
|
|
|
|
* @param mixed $cacheKey
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-11-09 19:31:08 +00:00
|
|
|
private function doCachedExpansion( $frame, $input, $cacheKey ) {
|
2012-05-22 03:56:07 +00:00
|
|
|
$hash = md5( serialize( $cacheKey ) );
|
2013-12-06 23:25:58 +00:00
|
|
|
if ( isset( $this->expandCache[$hash] ) ) {
|
|
|
|
return $this->expandCache[$hash];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( is_scalar( $input ) ) {
|
2022-11-12 05:01:55 +00:00
|
|
|
$input = str_replace( [ "\r\n", "\r" ], "\n", $input );
|
2013-12-06 23:25:58 +00:00
|
|
|
$dom = $this->parser->getPreprocessor()->preprocessToObj(
|
2014-06-20 20:12:01 +00:00
|
|
|
$input, $frame->depth ? Parser::PTD_FOR_INCLUSION : 0 );
|
2013-12-06 23:25:58 +00:00
|
|
|
} else {
|
|
|
|
$dom = $input;
|
|
|
|
}
|
|
|
|
$ret = $frame->expand( $dom );
|
2016-10-13 15:07:44 +00:00
|
|
|
if ( !$frame->isVolatile() ) {
|
2012-05-22 03:56:07 +00:00
|
|
|
if ( count( $this->expandCache ) > self::MAX_EXPAND_CACHE_SIZE ) {
|
|
|
|
reset( $this->expandCache );
|
|
|
|
$oldHash = key( $this->expandCache );
|
|
|
|
unset( $this->expandCache[$oldHash] );
|
|
|
|
}
|
2013-12-06 23:25:58 +00:00
|
|
|
$this->expandCache[$hash] = $ret;
|
2012-05-22 03:56:07 +00:00
|
|
|
}
|
2013-12-06 23:25:58 +00:00
|
|
|
return $ret;
|
2012-05-22 03:56:07 +00:00
|
|
|
}
|
2022-09-22 07:45:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements mw.loadJsonData()
|
|
|
|
*
|
|
|
|
* @param string $title Title text, type-checked in Lua
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function loadJsonData( $title ) {
|
|
|
|
$this->incrementExpensiveFunctionCount();
|
|
|
|
|
|
|
|
$titleObj = Title::newFromText( $title );
|
|
|
|
if ( !$titleObj || !$titleObj->exists() || !$titleObj->hasContentModel( CONTENT_MODEL_JSON ) ) {
|
|
|
|
throw new LuaError(
|
|
|
|
"bad argument #1 to 'mw.loadJsonData' ('$title' is not a valid JSON page)"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$parser = $this->getParser();
|
2024-03-10 22:46:30 +00:00
|
|
|
[ $text, $finalTitle ] = $parser->fetchTemplateAndTitle( $titleObj );
|
2022-09-22 07:45:47 +00:00
|
|
|
|
|
|
|
$json = FormatJson::decode( $text, true );
|
|
|
|
if ( is_array( $json ) ) {
|
|
|
|
$json = TextLibrary::reindexArrays( $json, false );
|
|
|
|
}
|
|
|
|
// We'll throw an error for non-tables on the Lua side
|
|
|
|
|
|
|
|
return [ $json ];
|
|
|
|
}
|
2020-02-21 20:49:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Content::updateRedirect
|
|
|
|
*
|
|
|
|
* @param ScribuntoContent $content
|
|
|
|
* @param Title $target
|
|
|
|
* @return ScribuntoContent
|
|
|
|
*/
|
|
|
|
public function updateRedirect( ScribuntoContent $content, Title $target ): ScribuntoContent {
|
|
|
|
if ( !$content->isRedirect() ) {
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
return $this->makeRedirectContent( $target );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Content::getRedirectTarget
|
|
|
|
*
|
|
|
|
* @param ScribuntoContent $content
|
|
|
|
* @return Title|null
|
|
|
|
*/
|
|
|
|
public function getRedirectTarget( ScribuntoContent $content ) {
|
|
|
|
$text = $content->getText();
|
|
|
|
preg_match( '/^return require \[\[(.*?)\]\]/', $text, $matches );
|
|
|
|
if ( isset( $matches[1] ) ) {
|
|
|
|
$title = Title::newFromText( $matches[1] );
|
|
|
|
// Can only redirect to other Scribunto pages
|
|
|
|
if ( $title && $title->hasContentModel( CONTENT_MODEL_SCRIBUNTO ) ) {
|
|
|
|
// Have a title, check that the current content equals what
|
|
|
|
// the redirect content should be
|
|
|
|
if ( $content->equals( $this->makeRedirectContent( $title ) ) ) {
|
|
|
|
return $title;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see ContentHandler::makeRedirectContent
|
|
|
|
* @param Title $destination
|
|
|
|
* @param string $text
|
|
|
|
* @return ScribuntoContent
|
|
|
|
*/
|
|
|
|
public function makeRedirectContent( Title $destination, $text = '' ) {
|
|
|
|
$targetPage = $destination->getPrefixedText();
|
|
|
|
$redirectText = "return require [[$targetPage]]";
|
|
|
|
return new ScribuntoContent( $redirectText );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see ContentHandler::supportsRedirects
|
|
|
|
* @return bool true
|
|
|
|
*/
|
|
|
|
public function supportsRedirects(): bool {
|
|
|
|
return true;
|
|
|
|
}
|
2012-04-13 10:38:12 +00:00
|
|
|
}
|
2022-09-30 01:54:23 +00:00
|
|
|
|
|
|
|
class_alias( LuaEngine::class, 'Scribunto_LuaEngine' );
|