mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Scribunto
synced 2024-11-15 03:35:29 +00:00
925045a669
* Used Parser::fetchTemplateAndTitle() to get modules and register them in templatelinks. Most of the logic was previously duplicated. * Changed the configuration and factory functions to allow for the possibility of multiple engines coexisting on the one wiki. * Made the $parser parameter optional, to improve debugging in the case where a parser is needed but parsing has not started. Removed all $wgParser references. * Renamed Scripting::getEngine() to getParserEngine() and resetEngine() to resetParserEngine() * Removed setOptions() and updateOptions(). If you want to change the options, you can always make a new instance. * Renamed getModule() to fetchModuleFromParser() * Simplified module constructor parameters and member variable list * Fixed spelling error langauge -> language * Renamed a few variables for clarity: $module -> $moduleName, $function -> $functionName * Renamed getLimitsReport() to getLimitReport() as it is in Parser * Use an accessor for getting LuaSandboxEngineModule::$contents * Renamed configuration variable maxCPU to cpuLimit * Include the full message name as a parameter to ScriptingException. This makes it easier to find messages in the i18n file, and it makes it easier to find invocation points when a translator wants to know how a message is used. Adding the message name as a comment on the same line seems like a waste of space when you can just make it an actual parameter. * Reduce the number of formal parameters to ScriptingException::__construct(), since there is already too many and we may want to add more things later, such as backtraces with hyperlinks and other such stuff. * Include the code location as $2 unconditionally so that there is less chance of getting the parameters wrong * Shortened some message names. Wrote English text for messages without it.
184 lines
4.9 KiB
PHP
184 lines
4.9 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Wikitext scripting infrastructure for MediaWiki: base classes.
|
|
* Copyright (C) 2012 Victor Vasiliev <vasilvv@gmail.com> et al
|
|
* http://www.mediawiki.org/
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License along
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
*/
|
|
|
|
/**
|
|
* Base class for all scripting engines. Includes all code
|
|
* not related to particular modules, like tracking links between
|
|
* modules or loading module texts.
|
|
*/
|
|
abstract class ScriptingEngineBase {
|
|
protected
|
|
$parser,
|
|
$options,
|
|
$modules = array();
|
|
|
|
/**
|
|
* Creates a new module object within this engine
|
|
*/
|
|
abstract protected function newModule( $text, $chunkName );
|
|
|
|
/**
|
|
* Constructor.
|
|
*
|
|
* @param $options Associative array of options:
|
|
* - parser: A Parser object
|
|
*/
|
|
public function __construct( $options ) {
|
|
$this->options = $options;
|
|
if ( isset( $options['parser'] ) ) {
|
|
$this->parser = $options['parser'];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Load a module from some parser-defined template loading mechanism and
|
|
* register a parser output dependency.
|
|
*
|
|
* Does not initialize the module, i.e. do not expect it to complain if the module
|
|
* text is garbage or has syntax error. Returns a module or throws an exception.
|
|
*
|
|
* @param $title The title of the module
|
|
* @return ScriptingEngineModule
|
|
*/
|
|
function fetchModuleFromParser( Title $title ) {
|
|
list( $text, $finalTitle ) = $this->parser->fetchTemplateAndTitle( $title );
|
|
if ( $text === false ) {
|
|
throw new ScriptingException( 'scripting-common-nosuchmodule' );
|
|
}
|
|
|
|
$key = $finalTitle->getPrefixedDBkey();
|
|
if ( !isset( $this->modules[$key] ) ) {
|
|
$this->modules[$key] = $this->newModule( $text, $key );
|
|
}
|
|
return $this->modules[$key];
|
|
}
|
|
|
|
/**
|
|
* Validates the script and returns an array of the syntax errors for the
|
|
* given code.
|
|
*
|
|
* @param $code Code to validate
|
|
* @param $title Title of the code page
|
|
* @return array
|
|
*/
|
|
function validate( $text, $chunkName = false ) {
|
|
$module = $this->newModule( $text, $chunkName );
|
|
|
|
try {
|
|
$module->initialize();
|
|
} catch( ScriptingException $e ) {
|
|
return array( $e->getMessage() );
|
|
}
|
|
|
|
return array();
|
|
}
|
|
|
|
/**
|
|
* Allows the engine to append their information to the limits
|
|
* report.
|
|
*/
|
|
public function getLimitsReport() {
|
|
/* No-op by default */
|
|
return '';
|
|
}
|
|
|
|
/**
|
|
* Get the language for GeSHi syntax highlighter.
|
|
*/
|
|
function getGeSHiLanguage() {
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Get the language for Ace code editor.
|
|
*/
|
|
function getCodeEditorLanguage() {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Class that represents a module. Responsible for initial module parsing
|
|
* and maintaining the contents of the module.
|
|
*/
|
|
abstract class ScriptingModuleBase {
|
|
var $engine, $code, $chunkName;
|
|
|
|
public function __construct( $engine, $code, $chunkName ) {
|
|
$this->engine = $engine;
|
|
$this->code = $code;
|
|
$this->chunkName = $chunkName;
|
|
}
|
|
|
|
/** Accessors **/
|
|
public function getEngine() { return $this->engine; }
|
|
public function getCode() { return $this->code; }
|
|
public function getChunkName() { return $this->chunkName; }
|
|
|
|
/**
|
|
* Initialize the module. That means parse it and load the
|
|
* functions/constants/whatever into the object.
|
|
*
|
|
* Protection of double-initialization is the responsibility of this method.
|
|
*/
|
|
abstract function initialize();
|
|
|
|
/**
|
|
* Returns the object for a given function. Should return null if it does not exist.
|
|
*
|
|
* @return ScriptingFunctionBase or null
|
|
*/
|
|
abstract function getFunction( $name );
|
|
|
|
/**
|
|
* Returns the list of the functions in the module.
|
|
*
|
|
* @return array(string)
|
|
*/
|
|
abstract function getFunctions();
|
|
}
|
|
|
|
abstract class ScriptingFunctionBase {
|
|
protected $mName, $mContents, $mModule, $mEngine;
|
|
|
|
public function __construct( $module, $name, $contents ) {
|
|
$this->name = $name;
|
|
$this->contents = $contents;
|
|
$this->module = $module;
|
|
$this->engine = $module->getEngine();
|
|
}
|
|
|
|
/**
|
|
* Calls the function. Returns its first result or null if no result.
|
|
*
|
|
* @param $args array Arguments to the function
|
|
* @param $frame PPFrame
|
|
*/
|
|
abstract public function call( $args, $frame );
|
|
|
|
/** Accessors **/
|
|
public function getName() { return $this->name; }
|
|
public function getModule() { return $this->module; }
|
|
public function getEngine() { return $this->engine; }
|
|
}
|