mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Scribunto
synced 2024-11-24 08:14:09 +00:00
da06273ede
* Added error backtrace collection to MWServer:handleCall() * When there is an error on parse, show a short and simple inline error message to the user, which when clicked, expands to a full error with HTML-formatted backtrace. * When an error is encountered during module validation, have the code editor jump directly to the line. Requires r115011. * Expose the code location of most errors to Scribunto, by parsing the standard error message format. * During module validation, abbreviate the error location if the error is in the same module. * Do not execute the module during validation, just parse it. Execution does not really work without an active parse operation in progress. It already caused a fatal error if you called require() from the main chunk, and problems would have become more visible as more parser-related APIs were added. * LuaSandbox does not yet provide backtraces, but this is planned. Change-Id: Id9f6564a41b310792b3fe3ebb527cbf8f8771bd1
117 lines
3.4 KiB
PHP
117 lines
3.4 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Static function collection for general extension support.
|
|
*/
|
|
class Scribunto {
|
|
const LOCAL = 'local';
|
|
|
|
/**
|
|
* Create a new engine object with specified parameters.
|
|
*/
|
|
public static function newEngine( $options ) {
|
|
$class = $options['class'];
|
|
return new $class( $options );
|
|
}
|
|
|
|
/**
|
|
* Create a new engine object with default parameters
|
|
* @param $extraOptions Extra options to pass to the constructor, in addition to the configured options
|
|
*/
|
|
public static function newDefaultEngine( $extraOptions = array() ) {
|
|
global $wgScribuntoDefaultEngine, $wgScribuntoEngineConf;
|
|
if( !$wgScribuntoDefaultEngine ) {
|
|
throw new MWException( 'Scribunto extension is enabled but $wgScribuntoDefaultEngine is not set' );
|
|
}
|
|
|
|
if( !isset( $wgScribuntoEngineConf[$wgScribuntoDefaultEngine] ) ) {
|
|
throw new MWException( 'Invalid scripting engine is specified in $wgScribuntoDefaultEngine' );
|
|
}
|
|
$options = $extraOptions + $wgScribuntoEngineConf[$wgScribuntoDefaultEngine];
|
|
return self::newEngine( $options );
|
|
}
|
|
|
|
/**
|
|
* 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
|
|
*/
|
|
public static function getParserEngine( $parser ) {
|
|
if( empty( $parser->scribunto_engine ) ) {
|
|
$parser->scribunto_engine = self::newDefaultEngine( array( 'parser' => $parser ) );
|
|
$parser->scribunto_engine->setTitle( $parser->getTitle() );
|
|
}
|
|
return $parser->scribunto_engine;
|
|
}
|
|
|
|
/**
|
|
* Check if an engine instance is present in the given parser
|
|
*/
|
|
public static function isParserEnginePresent( $parser ) {
|
|
return !empty( $parser->scribunto_engine );
|
|
}
|
|
|
|
/**
|
|
* Remove the current engine instance from the parser
|
|
*/
|
|
public static function resetParserEngine( $parser ) {
|
|
$parser->scribunto_engine = null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* An exception class which represents an error in the script. This does not
|
|
* normally abort the request, instead it is caught and shown to the user.
|
|
*/
|
|
class ScribuntoException extends MWException {
|
|
var $messageName, $messageArgs, $params;
|
|
|
|
function __construct( $messageName, $params = array() ) {
|
|
if ( isset( $params['args'] ) ) {
|
|
$this->messageArgs = $params['args'];
|
|
} else {
|
|
$this->messageArgs = array();
|
|
}
|
|
if ( isset( $params['module'] ) && isset( $params['line'] ) ) {
|
|
$codeLocation = false;
|
|
if ( isset( $params['title'] ) ) {
|
|
$moduleTitle = Title::newFromText( $params['module'] );
|
|
if ( $moduleTitle && $moduleTitle->equals( $params['title'] ) ) {
|
|
$codeLocation = wfMsg( 'scribunto-line', $params['line'] );
|
|
}
|
|
}
|
|
if ( $codeLocation === false ) {
|
|
$codeLocation = wfMsg( 'scribunto-module-line', $params['module'], $params['line'] );
|
|
}
|
|
} else {
|
|
$codeLocation = '[UNKNOWN]';
|
|
}
|
|
array_unshift( $this->messageArgs, $codeLocation );
|
|
$msg = wfMsgExt( $messageName, array(), $this->messageArgs );
|
|
parent::__construct( $msg );
|
|
|
|
$this->messageName = $messageName;
|
|
$this->params = $params;
|
|
}
|
|
|
|
public function getMessageName() {
|
|
return $this->messageName;
|
|
}
|
|
|
|
public function toStatus() {
|
|
$args = array_merge( array( $this->messageName ), $this->messageArgs );
|
|
$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;
|
|
}
|
|
}
|