First-pass cleanup:

* Removed ScriptingEngineBase::load(), inappropriate interface specification, only used by child classes and more properly defined by them
* Fixed inappropriate use of final
* Fixed case of a class constant to conform with MediaWiki conventions
* Use a factory function interface for module creation instead of a class name accessor
* Don't pass unnecessary $engine parameter to ScriptingFunctionBase::__construct(). Pass parent object as the first parameter per convention.
* Fixed unnecessary reference parameter in doRunHook()
* Have LuaSandboxEngineFunction::call() return the first result or null, per the base class documentation, instead of imploding. 
* Use strval() to avoid a warning in case call() returns an array or object
* Improved some comments
This commit is contained in:
Tim Starling 2012-04-04 06:10:32 +00:00
parent be72db112a
commit c8f4d0a213
5 changed files with 39 additions and 41 deletions

View file

@ -3,7 +3,6 @@
/**
* Wikitext scripting infrastructure for MediaWiki: base classes.
* Copyright (C) 2012 Victor Vasiliev <vasilvv@gmail.com> et al
* Based on MediaWiki file LinksUpdate.php
* http://www.mediawiki.org/
*
* This program is free software; you can redistribute it and/or modify
@ -31,19 +30,12 @@ abstract class ScriptingEngineBase {
protected
$mParser,
$mModules = array(),
$mModuleTitles = array(),
$mLoaded = false;
$mModuleTitles = array();
/**
* Required for the lazy-loading of the engine. Should have a sentinel
* inside checking whether it is already loaded.
* Creates a new module object within this engine
*/
abstract public function load();
/**
* Returns the name of your module class.
*/
abstract protected function getModuleClassName();
abstract protected function newModule( $title, $code, $revisionID, $source );
/**
* Returns the default options of the engine.
@ -61,7 +53,7 @@ abstract class ScriptingEngineBase {
*
* @param $parser Parser Wikitext parser
*/
public final function __construct( $parser ) {
public function __construct( $parser ) {
$this->mParser = $parser;
}
@ -74,7 +66,7 @@ abstract class ScriptingEngineBase {
* @param $source string Source of the module
* @return ScriptingEngineModule
*/
public function getModule( $title, $source = Scripting::Local ) {
public function getModule( $title, $source = Scripting::LOCAL ) {
// Convert string to title
if( !$title instanceof Title ) {
$titleobj = Title::newFromText( (string)$title, NS_MODULE );
@ -97,8 +89,7 @@ abstract class ScriptingEngineBase {
}
// Create the class
$class = $this->getModuleClassName();
$this->mModules[$key] = new $class( $this, $title, $rev->getText(), $rev->getID(), $source );
$this->mModules[$key] = $this->newModule( $title, $rev->getText(), $rev->getID(), $source );
$this->mModuleTitles[] = $title;
}
return $this->mModules[$key];
@ -108,7 +99,7 @@ abstract class ScriptingEngineBase {
* Fetches the revision for given module title.
*/
private function getModuleRev( $title, $source ) {
if( $source != Scripting::Local ) {
if( $source != Scripting::LOCAL ) {
throw new MWException( 'Non-local scripts are not supported at this point' );
}
@ -128,7 +119,7 @@ abstract class ScriptingEngineBase {
}
/**
* Validates the script and returns an array of the syntax erros for the
* Validates the script and returns an array of the syntax errors for the
* given code.
*
* @param $code Code to validate
@ -136,8 +127,7 @@ abstract class ScriptingEngineBase {
* @return array
*/
function validate( $code, $title ) {
$class = $this->getModuleClassName();
$module = new $class( $this, $title, $code, 0, Scripting::Local );
$module = $this->newModule( $title, $code, 0, Scripting::LOCAL );
try {
$module->initialize();
@ -195,7 +185,7 @@ abstract class ScriptingEngineBase {
abstract class ScriptingModuleBase {
var $mEngine, $mTitle, $mCode, $mRevisionID, $mSource;
public final function __construct( $engine, $title, $code, $revisionID, $source ) {
public function __construct( $engine, $title, $code, $revisionID, $source ) {
$this->mEngine = $engine;
$this->mTitle = $title;
$this->mCode = $code;
@ -236,11 +226,11 @@ abstract class ScriptingModuleBase {
abstract class ScriptingFunctionBase {
protected $mName, $mContents, $mModule, $mEngine;
public final function __construct( $name, $contents, $module, $engine ) {
public function __construct( $module, $name, $contents ) {
$this->mName = $name;
$this->mContents = $contents;
$this->mModule = $module;
$this->mEngine = $engine;
$this->mEngine = $module->getEngine();
}
/**

View file

@ -4,7 +4,7 @@
* Generic scripting functions.
*/
class Scripting {
const Local = 'local';
const LOCAL = 'local';
protected static function getEngineClass() {
global $wgScriptingEngine, $wgScriptingEngines;

View file

@ -21,7 +21,7 @@
*/
/**
* Hooks for Scripting extension.
* Hooks for the Scripting extension.
*/
class ScriptingHooks {
/**
@ -35,7 +35,7 @@ class ScriptingHooks {
}
/**
* Called when interpreter is to be reset.
* Called when the interpreter is to be reset.
*
* @static
* @param $parser Parser
@ -47,7 +47,7 @@ class ScriptingHooks {
}
/**
* Adds scriptlinks table to parser tests.
* Add scriptlinks table to parser tests.
*/
public static function addTestTables( &$tables ) {
$tables[] = 'scriptlinks';
@ -55,7 +55,7 @@ class ScriptingHooks {
}
/**
* Handles the {{#invoke:module|func}} construction.
* Hook function for {{#invoke:module|func}}
*
* @param $parser Parser
* @param $frame PPFrame
@ -73,7 +73,7 @@ class ScriptingHooks {
}
/**
* Handles the transclusion of the script ({{script:module}} hook).
* Hook function for {{script:module}}
*
* @param $parser Parser
* @param $frame PPFrame
@ -94,7 +94,7 @@ class ScriptingHooks {
* @return string
* @throws ScriptingException
*/
private static function doRunHook( &$parser, $frame, $module, $function, $args ) {
private static function doRunHook( $parser, $frame, $module, $function, $args ) {
wfProfileIn( __METHOD__ );
try {
@ -104,7 +104,7 @@ class ScriptingHooks {
$arg = $frame->expand( $arg );
}
$module = $engine->getModule( $module, Scripting::Local );
$module = $engine->getModule( $module, Scripting::LOCAL );
$functionObj = $module->getFunction( $function );
if( !$functionObj ) {
@ -114,7 +114,7 @@ class ScriptingHooks {
$result = $functionObj->call( $args, $frame );
wfProfileOut( __METHOD__ );
return trim( $result );
return trim( strval( $result ) );
} catch( ScriptingException $e ) {
$msg = $e->getMessage();
wfProfileOut( __METHOD__ );

View file

@ -31,7 +31,7 @@ if( !defined( 'MEDIAWIKI' ) )
*/
class ScriptLinksUpdateHooks {
/**
* Appends script links to the output.
* Append script links to the output.
*/
public static function appendToOutput( &$parser, &$text ) {
if( isset( $parser->scripting_engine ) ) {
@ -41,7 +41,7 @@ class ScriptLinksUpdateHooks {
}
/**
* Runs the link updater.
* Run the link updater.
*/
public static function updateLinks( &$update ) {
$output = $update->mParserOutput;
@ -57,7 +57,7 @@ class ScriptLinksUpdateHooks {
}
/**
* Purges cache for all the pages where the script is used.
* Purge cache for all the pages where the script is used.
* @param $article Article
* @param $editInfo
* @param $changed
@ -71,7 +71,7 @@ class ScriptLinksUpdateHooks {
$engine = Scripting::getEngine( $wgParser );
$engine->invalidateModuleCache( $article->getTitle() );
// Invalidate caches of articles which include the script
// Invalidate the caches of articles which include the script
$wgDeferredUpdateList[] = new HTMLCacheUpdate( $article->getTitle(), 'scriptlinks' );
}
@ -79,7 +79,7 @@ class ScriptLinksUpdateHooks {
}
/**
* Adds scriptlinks to the list of tables supported by BacklinkCache.
* Add scriptlinks to the list of tables supported by BacklinkCache.
*/
public static function getBacklinkCachePrefix( $table, &$prefix ) {
if( $table == 'scriptlinks' ) {
@ -91,7 +91,7 @@ class ScriptLinksUpdateHooks {
}
/**
* Adds scriptlinks to the list of tables supported by BacklinkCache.
* Add scriptlinks to the list of tables supported by BacklinkCache.
* @param $table
* @param $title Title
* @param $conds
@ -111,7 +111,7 @@ class ScriptLinksUpdateHooks {
}
/**
* A class that updates links on scripts like phase3/includes/LinksUpdate.php does that
* A class that updates links on scripts like what phase3/includes/LinksUpdate.php does
* with templates.
*/
class ScriptLinksUpdate {

View file

@ -1,7 +1,11 @@
<?php
class LuaSandboxEngine extends ScriptingEngineBase {
public $mSandbox;
public $mSandbox, $mLoaded = false;
public function newModule( $title, $code, $revisionID, $source ) {
return new LuaSandboxEngineModule( $this, $title, $code, $revisionID, $source );
}
public function load() {
if( $this->mLoaded ) {
@ -119,7 +123,7 @@ class LuaSandboxEngineModule extends ScriptingModuleBase {
$this->initialize();
if( isset( $this->mContents[$name] ) ) {
return new LuaSandboxEngineFunction( $name, $this->mContents[$name], $this, $this->mEngine );
return new LuaSandboxEngineFunction( $this, $name, $this->mContents[$name] );
} else {
return null;
}
@ -139,6 +143,10 @@ class LuaSandboxEngineFunction extends ScriptingFunctionBase {
throw new ScriptingException( 'error', 'luasandbox', null, null, array( $e->getMessage() ) );
}
return implode( '', $result );
if ( isset( $result[0] ) ) {
return $result[0];
} else {
return null;
}
}
}