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.
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
* @param $extraOptions Extra options to pass to the constructor, in addition to the configured options
|
|
|
|
*/
|
|
|
|
public static function newDefaultEngine( $extraOptions = array() ) {
|
2012-04-06 05:04:30 +00:00
|
|
|
global $wgScribuntoDefaultEngine, $wgScribuntoEngineConf;
|
|
|
|
if( !$wgScribuntoDefaultEngine ) {
|
|
|
|
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
|
|
|
|
2012-04-06 05:04:30 +00:00
|
|
|
if( !isset( $wgScribuntoEngineConf[$wgScribuntoDefaultEngine] ) ) {
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public static function getParserEngine( $parser ) {
|
2012-04-06 05:04:30 +00:00
|
|
|
if( !isset( $parser->scribunto_engine ) || !$parser->scribunto_engine ) {
|
|
|
|
$parser->scribunto_engine = self::newDefaultEngine( array( 'parser' => $parser ) );
|
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-05 07:58:02 +00:00
|
|
|
/**
|
|
|
|
* Remove the current engine instance from the parser
|
|
|
|
*/
|
|
|
|
public static function resetParserEngine( $parser ) {
|
2012-04-06 05:04:30 +00:00
|
|
|
$parser->scribunto_engine = null;
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-04-05 03:53:05 +00:00
|
|
|
* 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.
|
2012-01-28 16:22:18 +00:00
|
|
|
*/
|
2012-04-06 05:04:30 +00:00
|
|
|
class ScribuntoException extends MWException {
|
2012-04-05 07:58:02 +00:00
|
|
|
var $messageName, $params;
|
|
|
|
|
|
|
|
function __construct( $messageName, $params = array() ) {
|
|
|
|
if ( isset( $params['args'] ) ) {
|
|
|
|
$args = $params['args'];
|
|
|
|
} else {
|
|
|
|
$args = array();
|
|
|
|
}
|
|
|
|
if ( isset( $params['module'] ) && isset( $params['line'] ) ) {
|
2012-04-06 05:04:30 +00:00
|
|
|
$codelocation = wfMsg( 'scribunto-codelocation', $params['module'], $params['line'] );
|
2012-01-28 16:22:18 +00:00
|
|
|
} else {
|
2012-04-05 07:58:02 +00:00
|
|
|
$codelocation = '[UNKNOWN]'; // should never happen
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|
2012-04-05 07:58:02 +00:00
|
|
|
array_unshift( $args, $codelocation );
|
|
|
|
$msg = wfMsgExt( $messageName, array(), $args );
|
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-04-05 07:58:02 +00:00
|
|
|
public function getMessageName() {
|
|
|
|
return $this->messageName;
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|
|
|
|
}
|