2012-01-28 16:22:18 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2012-04-06 05:04:30 +00:00
|
|
|
* Static function collection for general extension support.
|
2012-01-28 16:22:18 +00:00
|
|
|
*/
|
2012-04-06 05:04:30 +00:00
|
|
|
class Scribunto {
|
2012-04-04 06:10:32 +00:00
|
|
|
const LOCAL = 'local';
|
2012-01-28 16:22:18 +00:00
|
|
|
|
2012-04-05 07:58:02 +00:00
|
|
|
/**
|
|
|
|
* Create a new engine object with specified parameters.
|
2014-10-10 09:02:03 +00:00
|
|
|
*
|
|
|
|
* @param array $options
|
2012-06-15 23:34:35 +00:00
|
|
|
* @return ScribuntoEngineBase
|
2012-04-05 07:58:02 +00:00
|
|
|
*/
|
|
|
|
public static function newEngine( $options ) {
|
|
|
|
$class = $options['class'];
|
|
|
|
return new $class( $options );
|
|
|
|
}
|
2012-01-28 16:46:15 +00:00
|
|
|
|
2012-04-05 07:58:02 +00:00
|
|
|
/**
|
|
|
|
* Create a new engine object with default parameters
|
2014-10-10 09:02:03 +00:00
|
|
|
*
|
2015-06-26 16:37:34 +00:00
|
|
|
* @param $extraOptions array Extra options to pass to the constructor,
|
|
|
|
* in addition to the configured options
|
2012-06-15 23:34:35 +00:00
|
|
|
* @throws MWException
|
|
|
|
* @return ScribuntoEngineBase
|
2012-04-05 07:58:02 +00:00
|
|
|
*/
|
|
|
|
public static function newDefaultEngine( $extraOptions = array() ) {
|
2012-04-06 05:04:30 +00:00
|
|
|
global $wgScribuntoDefaultEngine, $wgScribuntoEngineConf;
|
2015-06-21 04:38:39 +00:00
|
|
|
if ( !$wgScribuntoDefaultEngine ) {
|
2012-04-06 05:04:30 +00:00
|
|
|
throw new MWException( 'Scribunto extension is enabled but $wgScribuntoDefaultEngine is not set' );
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|
2012-01-28 16:46:15 +00:00
|
|
|
|
2015-06-21 04:38:39 +00:00
|
|
|
if ( !isset( $wgScribuntoEngineConf[$wgScribuntoDefaultEngine] ) ) {
|
2012-04-06 05:04:30 +00:00
|
|
|
throw new MWException( 'Invalid scripting engine is specified in $wgScribuntoDefaultEngine' );
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|
2012-04-06 05:04:30 +00:00
|
|
|
$options = $extraOptions + $wgScribuntoEngineConf[$wgScribuntoDefaultEngine];
|
2012-04-05 07:58:02 +00:00
|
|
|
return self::newEngine( $options );
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|
2012-01-28 16:46:15 +00:00
|
|
|
|
2012-04-05 07:58:02 +00:00
|
|
|
/**
|
|
|
|
* Get an engine instance for the given parser, and cache it in the parser
|
|
|
|
* so that subsequent calls to this function for the same parser will return
|
|
|
|
* the same engine.
|
|
|
|
*
|
|
|
|
* @param Parser $parser
|
2014-10-10 09:02:03 +00:00
|
|
|
* @return ScribuntoEngineBase
|
2012-04-05 07:58:02 +00:00
|
|
|
*/
|
2014-10-10 09:02:03 +00:00
|
|
|
public static function getParserEngine( Parser $parser ) {
|
2015-06-21 04:38:39 +00:00
|
|
|
if ( empty( $parser->scribunto_engine ) ) {
|
2012-04-06 05:04:30 +00:00
|
|
|
$parser->scribunto_engine = self::newDefaultEngine( array( 'parser' => $parser ) );
|
2012-04-23 11:36:13 +00:00
|
|
|
$parser->scribunto_engine->setTitle( $parser->getTitle() );
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|
2012-04-06 05:04:30 +00:00
|
|
|
return $parser->scribunto_engine;
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|
2012-01-28 16:46:15 +00:00
|
|
|
|
2012-04-13 10:38:12 +00:00
|
|
|
/**
|
|
|
|
* Check if an engine instance is present in the given parser
|
2014-10-10 09:02:03 +00:00
|
|
|
*
|
|
|
|
* @param Parser $parser
|
|
|
|
* @return bool
|
2012-04-13 10:38:12 +00:00
|
|
|
*/
|
2014-10-10 09:02:03 +00:00
|
|
|
public static function isParserEnginePresent( Parser $parser ) {
|
2012-04-13 10:38:12 +00:00
|
|
|
return !empty( $parser->scribunto_engine );
|
|
|
|
}
|
|
|
|
|
2012-04-05 07:58:02 +00:00
|
|
|
/**
|
|
|
|
* Remove the current engine instance from the parser
|
|
|
|
*/
|
2014-10-10 09:02:03 +00:00
|
|
|
public static function resetParserEngine( Parser $parser ) {
|
2012-09-05 09:39:58 +00:00
|
|
|
if ( !empty( $parser->scribunto_engine ) ) {
|
|
|
|
$parser->scribunto_engine->destroy();
|
|
|
|
$parser->scribunto_engine = null;
|
|
|
|
}
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|
2013-02-20 22:00:42 +00:00
|
|
|
|
|
|
|
/**
|
2013-03-08 17:01:50 +00:00
|
|
|
* Test whether the page should be considered a documentation page
|
2014-10-10 09:02:03 +00:00
|
|
|
*
|
|
|
|
* @param Title $title
|
|
|
|
* @param Title &$forModule Module for which this is a doc page
|
2013-02-20 22:00:42 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2014-10-10 09:02:03 +00:00
|
|
|
public static function isDocPage( Title $title, Title &$forModule = null ) {
|
2013-03-08 17:01:50 +00:00
|
|
|
$docPage = wfMessage( 'scribunto-doc-page-name' )->inContentLanguage();
|
|
|
|
if ( $docPage->isDisabled() ) {
|
2013-02-20 22:00:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-03-08 17:01:50 +00:00
|
|
|
// Canonicalize the input pseudo-title. The unreplaced "$1" shouldn't
|
|
|
|
// cause a problem.
|
2013-08-01 15:55:37 +00:00
|
|
|
$docTitle = Title::newFromText( $docPage->plain() );
|
|
|
|
if ( !$docTitle ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$docPage = $docTitle->getPrefixedText();
|
2013-03-08 17:01:50 +00:00
|
|
|
|
|
|
|
// Make it into a regex, and match it against the input title
|
|
|
|
$docPage = str_replace( '\\$1', '(.+)', preg_quote( $docPage, '/' ) );
|
|
|
|
if ( preg_match( "/^$docPage$/", $title->getPrefixedText(), $m ) ) {
|
|
|
|
$forModule = Title::makeTitleSafe( NS_MODULE, $m[1] );
|
2013-06-07 22:21:31 +00:00
|
|
|
return $forModule !== null;
|
2013-03-08 17:01:50 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2013-02-20 22:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-03-08 17:01:50 +00:00
|
|
|
* Return the Title for the documentation page
|
2014-10-10 09:02:03 +00:00
|
|
|
*
|
|
|
|
* @param Title $title
|
2013-02-20 22:00:42 +00:00
|
|
|
* @return Title|null
|
|
|
|
*/
|
2014-10-10 09:02:03 +00:00
|
|
|
public static function getDocPage( Title $title ) {
|
2013-03-08 17:01:50 +00:00
|
|
|
$docPage = wfMessage( 'scribunto-doc-page-name', $title->getText() )->inContentLanguage();
|
|
|
|
if ( $docPage->isDisabled() ) {
|
2013-02-20 22:00:42 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-03-08 17:01:50 +00:00
|
|
|
return Title::newFromText( $docPage->plain() );
|
2013-02-20 22:00:42 +00:00
|
|
|
}
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-07-07 18:46:59 +00:00
|
|
|
* An exception class which represents an error in the script. This does not
|
2012-04-05 03:53:05 +00:00
|
|
|
* normally abort the request, instead it is caught and shown to the user.
|
2012-01-28 16:22:18 +00:00
|
|
|
*/
|
2012-04-06 05:04:30 +00:00
|
|
|
class ScribuntoException extends MWException {
|
2014-10-10 09:02:03 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $messageName;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $messageArgs;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $params;
|
2012-04-05 07:58:02 +00:00
|
|
|
|
2012-06-15 23:34:35 +00:00
|
|
|
/**
|
2014-10-10 09:02:03 +00:00
|
|
|
* @param string $messageName
|
|
|
|
* @param array $params
|
2012-06-15 23:34:35 +00:00
|
|
|
*/
|
2012-04-05 07:58:02 +00:00
|
|
|
function __construct( $messageName, $params = array() ) {
|
|
|
|
if ( isset( $params['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->messageArgs = $params['args'];
|
2012-04-05 07:58:02 +00:00
|
|
|
} else {
|
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->messageArgs = array();
|
2012-04-05 07:58:02 +00:00
|
|
|
}
|
|
|
|
if ( isset( $params['module'] ) && isset( $params['line'] ) ) {
|
2012-04-23 11:36:13 +00:00
|
|
|
$codeLocation = false;
|
|
|
|
if ( isset( $params['title'] ) ) {
|
|
|
|
$moduleTitle = Title::newFromText( $params['module'] );
|
|
|
|
if ( $moduleTitle && $moduleTitle->equals( $params['title'] ) ) {
|
2014-06-28 21:52:18 +00:00
|
|
|
$codeLocation = wfMessage( 'scribunto-line', $params['line'] )->inContentLanguage()->text();
|
2012-04-23 11:36:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( $codeLocation === false ) {
|
2012-10-06 23:42:39 +00:00
|
|
|
$codeLocation = wfMessage(
|
|
|
|
'scribunto-module-line',
|
|
|
|
$params['module'],
|
|
|
|
$params['line']
|
2014-06-28 21:52:18 +00:00
|
|
|
)->inContentLanguage()->text();
|
2012-04-23 11:36:13 +00:00
|
|
|
}
|
2012-01-28 16:22:18 +00:00
|
|
|
} else {
|
2012-04-23 11:36:13 +00:00
|
|
|
$codeLocation = '[UNKNOWN]';
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|
2012-04-23 11:36:13 +00:00
|
|
|
array_unshift( $this->messageArgs, $codeLocation );
|
2014-06-28 21:52:18 +00:00
|
|
|
$msg = wfMessage( $messageName )->params( $this->messageArgs )->inContentLanguage()->text();
|
2012-01-28 16:22:18 +00:00
|
|
|
parent::__construct( $msg );
|
|
|
|
|
2012-04-05 07:58:02 +00:00
|
|
|
$this->messageName = $messageName;
|
2012-04-05 03:53:05 +00:00
|
|
|
$this->params = $params;
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|
|
|
|
|
2012-06-15 23:34:35 +00:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-04-05 07:58:02 +00:00
|
|
|
public function getMessageName() {
|
|
|
|
return $this->messageName;
|
2012-01-28 16:22: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
|
|
|
|
|
|
|
public function toStatus() {
|
|
|
|
$args = array_merge( array( $this->messageName ), $this->messageArgs );
|
2012-04-23 11:36:13 +00:00
|
|
|
$status = call_user_func_array( array( 'Status', 'newFatal' ), $args );
|
|
|
|
$status->scribunto_error = $this;
|
|
|
|
return $status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the backtrace as HTML, or false if there is none available.
|
|
|
|
*/
|
|
|
|
public function getScriptTraceHtml( $options = array() ) {
|
|
|
|
return false;
|
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-01-28 16:22:18 +00:00
|
|
|
}
|