mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Scribunto
synced 2024-11-28 10:10:04 +00:00
6bc11ff615
* Implemented the new parser interface based on a frame object, as described in the design document and wikitech-l. * Added parser tests for the new interface. * Removed {{script:}} parser function * Allow named parameters to {{#invoke:}} * Don't trim the return value * If a function invoked by #invoke returns multiple values, concatenate them into a single string. * If there is an error during parse, show the error message as an HTML comment as well as via JavaScript. This makes parser test construction easier, and probably makes debugging easier also. * Rename mw_internal to mw_php to clarify its role. It is now strictly a private Lua -> PHP interface function table. * Protect mw.setup() against multiple invocation. * Fixed a bug in Scribunto_LuaStandaloneInterpreter::receiveMessage(): large packets caused fread() to return with less than the requested amount of data, which previously caused an exception. It's necessary to check for EOF and to repeat the read to get all data. The receive function on the Lua side does not suffer from this problem. * In the standalone engine, fixed a bug in the interpretation of null return values from PHP callbacks. This should return no values to Lua. * Updated the Lua unit tests to account for the fact that functions are now forced to return strings. * Updated the getfenv and setfenv tests to account for the extra stack level introduced by mw.executeFunction(). Change-Id: If8fdecdfc91ebe7bd4b1dae8489ccbdeb6bbf5ce
177 lines
4.5 KiB
PHP
177 lines
4.5 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 script engines. Includes all code
|
|
* not related to particular modules, like tracking links between
|
|
* modules or loading module texts.
|
|
*/
|
|
abstract class ScribuntoEngineBase {
|
|
protected
|
|
$parser,
|
|
$title,
|
|
$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'];
|
|
}
|
|
if ( isset( $options['title'] ) ) {
|
|
$this->title = $options['title'];
|
|
}
|
|
}
|
|
|
|
public function setTitle( $title ) {
|
|
$this->title = $title;
|
|
}
|
|
|
|
public function getTitle() {
|
|
return $this->title;
|
|
}
|
|
|
|
public function newException( $message, $params = array() ) {
|
|
return new ScribuntoException( $message, $this->getDefaultExceptionParams() + $params );
|
|
}
|
|
|
|
public function getDefaultExceptionParams() {
|
|
$params = array();
|
|
if ( $this->title ) {
|
|
$params['title'] = $this->title;
|
|
}
|
|
return $params;
|
|
}
|
|
|
|
/**
|
|
* 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 null if it doesn't exist.
|
|
*
|
|
* @param $title The title of the module
|
|
* @return ScribuntoEngineModule
|
|
*/
|
|
function fetchModuleFromParser( Title $title ) {
|
|
list( $text, $finalTitle ) = $this->parser->fetchTemplateAndTitle( $title );
|
|
if ( $text === false ) {
|
|
return null;
|
|
}
|
|
|
|
$key = $finalTitle->getPrefixedDBkey();
|
|
if ( !isset( $this->modules[$key] ) ) {
|
|
$this->modules[$key] = $this->newModule( $text, $key );
|
|
}
|
|
return $this->modules[$key];
|
|
}
|
|
|
|
/**
|
|
* Validates the script and returns a Status object containing the syntax
|
|
* errors for the given code.
|
|
*
|
|
* @param $code Code to validate
|
|
* @param $title Title of the code page
|
|
* @return Status
|
|
*/
|
|
function validate( $text, $chunkName = false ) {
|
|
$module = $this->newModule( $text, $chunkName );
|
|
return $module->validate();
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
|
|
public function getParser() {
|
|
return $this->parser;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Class that represents a module. Responsible for initial module parsing
|
|
* and maintaining the contents of the module.
|
|
*/
|
|
abstract class ScribuntoModuleBase {
|
|
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; }
|
|
|
|
/**
|
|
* Validates the script and returns a Status object containing the syntax
|
|
* errors for the given code.
|
|
*
|
|
* @param $code Code to validate
|
|
* @param $title Title of the code page
|
|
* @return Status
|
|
*/
|
|
abstract public function validate();
|
|
|
|
/**
|
|
* Invoke the function with the specified name.
|
|
*
|
|
* @return string
|
|
*/
|
|
abstract public function invoke( $name, $frame );
|
|
}
|
|
|