https://www.mediawiki.org/wiki/Special:Code/MediaWiki/114736

Revision: 114736
Author:   tstarling
Date:     2012-04-05 07:58:02 +0000 (Thu, 05 Apr 2012)
Log Message:
-----------
* Removed scriptlinks table. It just seemed the same as templatelinks to me, 
and tl_namespace can be used if you want to separate out modules.
* 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.

Modified Paths:
--------------
    trunk/extensions/Scripting/Scripting.i18n.php
    trunk/extensions/Scripting/Scripting.php
    trunk/extensions/Scripting/common/Base.php
    trunk/extensions/Scripting/common/Common.php
    trunk/extensions/Scripting/common/Hooks.php
    trunk/extensions/Scripting/engines/LuaSandbox/Engine.php

Removed Paths:
-------------
    trunk/extensions/Scripting/common/LinkUpdates.php
    trunk/extensions/Scripting/scripting.sql

Modified: trunk/extensions/Scripting/Scripting.i18n.php
===================================================================
--- trunk/extensions/Scripting/Scripting.i18n.php       2012-04-05 07:24:33 UTC 
(rev 114735)
+++ trunk/extensions/Scripting/Scripting.i18n.php       2012-04-05 07:58:02 UTC 
(rev 114736)
@@ -13,7 +13,13 @@
  */
 $messages['en'] = array(
        'scripting-desc' => 'Framework for embedding scripting languages into 
MediaWiki pages',
-       
-       'scripting-exception-luasandbox-error' => 'Lua error: $1',
-       'scripting-exception-common-toofewargs' => 'Lua error: Too few 
arguments to function $1',
+       'scripting-codelocation' => 'in $1 at line $2',
+       'scripting-luasandbox-error' => 'Lua error: $2',
+       'scripting-common-toofewargs' => 'Lua error: Too few arguments to 
function $2',
+       'scripting-common-nosuchmodule' => 'Script error: No such module',
+       'scripting-luasandbox-noreturn' => 'Script error: The module did not 
return a value, it should return an export table.',
+       'scripting-luasandbox-toomanyreturns' => 'Script error: The module 
returned multiple values, it should return an export table.',
+       'scripting-luasandbox-notarrayreturn' => 'Script error: The module 
returned something other than a table, it should return an export table.',
+       'scripting-common-nofunction' => 'Script error: You must specify a 
function to call.',
+       'scripting-common-nosuchfunction' => 'Script error: The function you 
specified did not exist.',
 );

Modified: trunk/extensions/Scripting/Scripting.php
===================================================================
--- trunk/extensions/Scripting/Scripting.php    2012-04-05 07:24:33 UTC (rev 
114735)
+++ trunk/extensions/Scripting/Scripting.php    2012-04-05 07:58:02 UTC (rev 
114736)
@@ -40,48 +40,41 @@
 $wgAutoloadClasses['ScriptingModuleBase'] = $dir.'common/Base.php';
 $wgAutoloadClasses['ScriptingFunctionBase'] = $dir.'common/Base.php';
 $wgAutoloadClasses['ScriptingHooks'] = $dir.'common/Hooks.php';
-$wgAutoloadClasses['ScriptLinksUpdateHooks'] = $dir.'common/LinkUpdates.php';
 $wgAutoloadClasses['ScriptingException'] = $dir.'common/Common.php';
 $wgAutoloadClasses['Scripting'] = $dir.'common/Common.php';
 
 $wgHooks['ParserFirstCallInit'][] = 'ScriptingHooks::setupParserHook';
 $wgHooks['ParserLimitReport'][] = 'ScriptingHooks::reportLimits';
 $wgHooks['ParserClearState'][] = 'ScriptingHooks::clearState';
-$wgHooks['ParserTestTables'][] = 'ScriptingHooks::addTestTables';
 
 $wgHooks['CanonicalNamespaces'][] = 'ScriptingHooks::addCanonicalNamespaces';
 $wgHooks['ArticleViewCustom'][] = 'ScriptingHooks::handleScriptView';
 $wgHooks['TitleIsWikitextPage'][] = 'ScriptingHooks::isWikitextPage';
-$wgHooks['CodeEditorGetPageLanguage'][] = 'ScriptingHooks::getCodeLangauge';
+$wgHooks['CodeEditorGetPageLanguage'][] = 'ScriptingHooks::getCodeLanguage';
 $wgHooks['EditFilter'][] = 'ScriptingHooks::validateScript';
 
-$wgHooks['LinksUpdate'][] = 'ScriptLinksUpdateHooks::updateLinks';
-$wgHooks['ArticleEditUpdates'][] = 'ScriptLinksUpdateHooks::purgeCache';
-$wgHooks['ParserAfterTidy'][] = 'ScriptLinksUpdateHooks::appendToOutput';
-$wgHooks['BacklinkCacheGetPrefix'][] = 
'ScriptLinksUpdateHooks::getBacklinkCachePrefix';
-$wgHooks['BacklinkCacheGetConditions'][] = 
'ScriptLinksUpdateHooks::getBacklinkCacheConditions';
-
 /***** Individual engines and their configurations *****/
 
-/**
- * Available scripting engines.
- */
-$wgScriptingEngines = array(
-       'luasandbox' => 'LuaSandboxEngine',
-);
-
 $wgAutoloadClasses['LuaSandboxEngine'] = $dir.'engines/LuaSandbox/Engine.php';
 
 /***** Configuration *****/
 
 /**
- * The name of the scripting engine.
+ * The name of the default scripting engine.
  */
-$wgScriptingEngine = false;
+$wgScriptingDefaultEngine = 'luasandbox';
 
+/**
+ * Configuration for each scripting engine
+ */
+$wgScriptingEngineConf = array(
+       'luasandbox' => array(
+               'class' => 'LuaSandboxEngine',
+               'memoryLimit' => 50 * 1024 * 1024,
+               'cpuLimit' => 7,
+       ),
+);
 
-$wgScriptingEngineConf = array();
-
 /**
  * Script namespace numbers.
  */

Modified: trunk/extensions/Scripting/common/Base.php
===================================================================
--- trunk/extensions/Scripting/common/Base.php  2012-04-05 07:24:33 UTC (rev 
114735)
+++ trunk/extensions/Scripting/common/Base.php  2012-04-05 07:58:02 UTC (rev 
114736)
@@ -29,98 +29,51 @@
 abstract class ScriptingEngineBase {
        protected
                $parser,
-               $modules = array(),
-               $moduleTitles = array();
+               $options,
+               $modules = array();
 
        /**
         * Creates a new module object within this engine
         */
-       abstract protected function newModule( $title, $code, $revisionID, 
$source );
+       abstract protected function newModule( $text, $chunkName );
 
        /**
-        * Returns the default options of the engine.
-        */
-       public function getDefaultOptions() {
-               return array();
-       }
-
-       /**
-        * Is called by setOptions() in order to notify the engine
-        * that the options were changed.
-        */
-       protected function updateOptions() { /* No-op */ }
-
-       /**
         * Constructor.
         * 
-        * @param $parser Parser Wikitext parser
+        * @param $options Associative array of options:
+        *    - parser:            A Parser object
         */
-       public function __construct( $parser ) {
-               $this->parser = $parser;
+       public function __construct( $options ) {
+               $this->options = $options;
+               if ( isset( $options['parser'] ) ) {
+                       $this->parser = $options['parser'];
+               }
        }
 
        /**
-        * Loads the module either from instance cache or from the actual 
revision.
+        * 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 Title/string The title or the name of the module.
-        * @param $source string Source of the module
+        *
+        * @param $title The title of the module
         * @return ScriptingEngineModule
         */
-       public function getModule( $title, $source = Scripting::LOCAL ) {
-               // Convert string to title
-               if( !$title instanceof Title ) {
-                       $titleobj = Title::newFromText( (string)$title, 
NS_MODULE );
-                       if( !$titleobj || $titleobj->getNamespace() != 
NS_MODULE ) {
-                               throw new ScriptingException( 'badtitle', 
'common' );   // scripting-exceptions-common-badtitle
-                       }
-                       $title = $titleobj;
+       function fetchModuleFromParser( Title $title ) {
+               list( $text, $finalTitle ) = 
$this->parser->fetchTemplateAndTitle( $title );
+               if ( $text === false ) {
+                       throw new ScriptingException( 
'scripting-common-nosuchmodule' );
                }
 
-               // Check if it is already loaded
-               $key = $title->getPrefixedText();
-               if( !isset( $this->modules[$key] ) ) {
-                       // Fetch the text
-                       $rev = $this->getModuleRev( $title, $source );
-                       if( !$rev ) {
-                               throw new ScriptingException( 'nosuchmodule', 
'common' );       // scripting-exceptions-common-nosuchmodule
-                       }
-                       if( $rev->getTitle()->getNamespace() != NS_MODULE ) {
-                               throw new ScriptingException( 'badnamespace', 
'common' );       // scripting-exceptions-common-badnamespace
-                       }
-
-                       // Create the class
-                       $this->modules[$key] = $this->newModule( $title, 
$rev->getText(), $rev->getID(), $source );
-                       $this->moduleTitles[] = $title;
+               $key = $finalTitle->getPrefixedDBkey();
+               if ( !isset( $this->modules[$key] ) ) {
+                       $this->modules[$key] = $this->newModule( $text, $key );
                }
                return $this->modules[$key];
        }
 
        /**
-        * Fetches the revision for given module title.
-        */
-       private function getModuleRev( $title, $source ) {
-               if( $source != Scripting::LOCAL ) {
-                       throw new MWException( 'Non-local scripts are not 
supported at this point' );
-               }
-
-               $rev = Revision::newFromTitle( $title );
-               if( $rev && $real = Title::newFromRedirect( $rev->getText() ) ) 
{
-                       $rev = Revision::newFromTitle( $real );
-               }
-               return $rev;
-       }
-
-       /**
-        * Sets the engine-specific options from $wgScriptingEngineConf.
-        */
-       function setOptions( $options ) {
-               $this->options = array_merge( $this->getDefaultOptions(), 
$options );
-               $this->updateOptions();
-       }
-
-       /**
         * Validates the script and returns an array of the syntax errors for 
the
         * given code.
         * 
@@ -128,8 +81,8 @@
         * @param $title Title of the code page
         * @return array
         */
-       function validate( $code, $title ) {
-               $module = $this->newModule( $title, $code, 0, Scripting::LOCAL 
);
+       function validate( $text, $chunkName = false ) {
+               $module = $this->newModule( $text, $chunkName );
 
                try {
                        $module->initialize();
@@ -150,30 +103,14 @@
        }
 
        /**
-        * Returns the titles of all the modules used by this instance of the
-        * engine.
-        */
-       public function getUsedModules() {
-               return $this->moduleTitles;
-       }
-
-       /**
-        * Invalidates the cache of the given module by its title. Should be
-        * redefined if the engine uses any form of bytecode or other cache.
-        */
-       function invalidateModuleCache( $title ) {
-               /* No-op by default */
-       }
-
-       /**
         * Get the language for GeSHi syntax highlighter.
         */
-       function getGeSHiLangauge() {
+       function getGeSHiLanguage() {
                return false;
        }
        
        /**
-        * Get the langauge for Ace code editor.
+        * Get the language for Ace code editor.
         */
        function getCodeEditorLanguage() {
                return false;
@@ -185,23 +122,19 @@
  * and maintaining the contents of the module.
  */
 abstract class ScriptingModuleBase {
-       var $engine, $title, $code, $revisionID, $source;
+       var $engine, $code, $chunkName;
 
-       public function __construct( $engine, $title, $code, $revisionID, 
$source ) {
+       public function __construct( $engine, $code, $chunkName ) {
                $this->engine = $engine;
-               $this->title = $title;
                $this->code = $code;
-               $this->revisionID = $revisionID;
-               $this->source = $source;
+               $this->chunkName = $chunkName;
        }
 
        /** Accessors **/
        public function getEngine()     { return $this->engine; }
-       public function getTitle()      { return $this->title; }
        public function getCode()       { return $this->code; }
-       public function getRevisionID() { return $this->revisionID; }
-       public function getSource()     { return $this->source; }
-       
+       public function getChunkName()  { return $this->chunkName; }
+
        /**
         * Initialize the module. That means parse it and load the
         * functions/constants/whatever into the object.

Modified: trunk/extensions/Scripting/common/Common.php
===================================================================
--- trunk/extensions/Scripting/common/Common.php        2012-04-05 07:24:33 UTC 
(rev 114735)
+++ trunk/extensions/Scripting/common/Common.php        2012-04-05 07:58:02 UTC 
(rev 114736)
@@ -6,32 +6,49 @@
 class Scripting {
        const LOCAL = 'local';
 
-       protected static function getEngineClass() {
-               global $wgScriptingEngine, $wgScriptingEngines;
+       /**
+        * Create a new engine object with specified parameters.
+        */
+       public static function newEngine( $options ) {
+               $class = $options['class'];
+               return new $class( $options );
+       }
 
-               if( !$wgScriptingEngine ) {
-                       throw new MWException( 'Scripting extension is enabled 
but $wgScriptingEngine is not set' );
+       /**
+        * 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 $wgScriptingDefaultEngine, $wgScriptingEngineConf;
+               if( !$wgScriptingDefaultEngine ) {
+                       throw new MWException( 'Scripting extension is enabled 
but $wgScriptingDefaultEngine is not set' );
                }
 
-               if( !isset( $wgScriptingEngines[$wgScriptingEngine] ) ) {
-                       throw new MWException( 'Invalid scripting engine is 
specified in $wgScriptingEngine' );
+               if( !isset( $wgScriptingEngineConf[$wgScriptingDefaultEngine] ) 
) {
+                       throw new MWException( 'Invalid scripting engine is 
specified in $wgScriptingDefaultEngine' );
                }
-
-               return $wgScriptingEngines[$wgScriptingEngine];
+               $options = $extraOptions + 
$wgScriptingEngineConf[$wgScriptingDefaultEngine];
+               return self::newEngine( $options );
        }
 
-       public static function getEngine( $parser ) {
-               global $wgScriptingEngineConf;
-
+       /**
+        * 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( !isset( $parser->scripting_engine ) || 
!$parser->scripting_engine ) {
-                       $class = self::getEngineClass();
-                       $parser->scripting_engine = new $class( $parser );
-                       $parser->scripting_engine->setOptions( 
$wgScriptingEngineConf );
+                       $parser->scripting_engine = self::newDefaultEngine( 
array( 'parser' => $parser ) );
                }
                return $parser->scripting_engine;
        }
 
-       public static function resetEngine( $parser ) {
+       /**
+        * Remove the current engine instance from the parser
+        */
+       public static function resetParserEngine( $parser ) {
                $parser->scripting_engine = null;
        }
 }
@@ -41,22 +58,28 @@
  * normally abort the request, instead it is caught and shown to the user.
  */
 class ScriptingException extends MWException {
-       function __construct( $exceptionID, $engine, $module = null, $line = 
null, $params = array() ) {
-               if( $module ) {
-                       $codelocation = wfMsg( 'scripting-codelocation', 
$module, $line );
-                       $msg = wfMsgExt( 
"scripting-exception-{$engine}-{$exceptionID}", array(), array_merge( array( 
$codelocation ), $params ) );
+       var $messageName, $params;
+
+       function __construct( $messageName, $params = array() ) {
+               if ( isset( $params['args'] ) ) {
+                       $args = $params['args'];
                } else {
-                       $msg = wfMsgExt( 
"scripting-exception-{$engine}-{$exceptionID}", array(), $params );
+                       $args = array();
                }
+               if ( isset( $params['module'] ) && isset( $params['line'] ) ) {
+                       $codelocation = wfMsg( 'scripting-codelocation', 
$params['module'], $params['line'] );
+               } else {
+                       $codelocation = '[UNKNOWN]'; // should never happen
+               }
+               array_unshift( $args, $codelocation );
+               $msg = wfMsgExt( $messageName, array(), $args );
                parent::__construct( $msg );
 
-               $this->exceptionID = $exceptionID;
-               $this->line = $line;
-               $this->module = $module;
+               $this->messageName = $messageName;
                $this->params = $params;
        }
 
-       public function getExceptionID() {
-               return $this->exceptionID;
+       public function getMessageName() {
+               return $this->messageName;
        }
 }

Modified: trunk/extensions/Scripting/common/Hooks.php
===================================================================
--- trunk/extensions/Scripting/common/Hooks.php 2012-04-05 07:24:33 UTC (rev 
114735)
+++ trunk/extensions/Scripting/common/Hooks.php 2012-04-05 07:58:02 UTC (rev 
114736)
@@ -42,19 +42,11 @@
         * @return bool
         */
        public static function clearState( &$parser ) {
-               Scripting::resetEngine( $parser );
+               Scripting::resetParserEngine( $parser );
                return true;
        }
 
        /**
-        * Add scriptlinks table to parser tests.
-        */
-       public static function addTestTables( &$tables ) {
-               $tables[] = 'scriptlinks';
-               return true;
-       }
-
-       /**
         * Hook function for {{#invoke:module|func}}
         *
         * @param $parser Parser
@@ -64,7 +56,7 @@
         */
        public static function callHook( &$parser, $frame, $args ) {
                if( count( $args ) < 2 ) {
-                       throw new ScriptingException( 'nofunction', 'common' ); 
// scripting-exceptions-common-nofunction
+                       throw new ScriptingException( 
'scripting-common-nofunction' );
                }
 
                $module = $parser->mStripState->unstripBoth( array_shift( $args 
) );
@@ -88,29 +80,32 @@
        /**
         * @param $parser Parser
         * @param $frame PPFrame
-        * @param $module
-        * @param $function
+        * @param $moduleName
+        * @param $functionName
         * @param $args
         * @return string
         * @throws ScriptingException
         */
-       private static function doRunHook( $parser, $frame, $module, $function, 
$args ) {
+       private static function doRunHook( $parser, $frame, $moduleName, 
$functionName, $args ) {
                wfProfileIn( __METHOD__ );
-
+               
                try {
-                       $engine = Scripting::getEngine( $parser );
-
-                       foreach( $args as &$arg ) {
-                               $arg = $frame->expand( $arg );
+                       $engine = Scripting::getParserEngine( $parser );
+                       $title = Title::makeTitleSafe( NS_MODULE, $moduleName );
+                       if ( !$title ) {
+                               throw new ScriptingException( 
'scripting-common-nosuchmodule' );
                        }
+                       $module = $engine->fetchModuleFromParser( $title );
 
-                       $module = $engine->getModule( $module, Scripting::LOCAL 
);
-
-                       $functionObj = $module->getFunction( $function );
+                       $functionObj = $module->getFunction( $functionName );
                        if( !$functionObj ) {
-                               throw new ScriptingException( 'nosuchfunction', 
'common' );     // scripting-exceptions-common-nosuchfunction
+                               throw new ScriptingException( 
'scripting-common-nosuchfunction' );
                        }
 
+                       foreach( $args as &$arg ) {
+                               $arg = $frame->expand( $arg );
+                       }
+
                        $result = $functionObj->call( $args, $frame );
 
                        wfProfileOut( __METHOD__ );
@@ -132,11 +127,11 @@
         * @return bool
         */
        public static function handleScriptView( $text, $title, $output ) {
-               global $wgScriptingUseGeSHi, $wgParser;
+               global $wgScriptingUseGeSHi;
 
                if( $title->getNamespace() == NS_MODULE ) {
-                       $engine = Scripting::getEngine( $wgParser );
-                       $language = $engine->getGeSHiLangauge();
+                       $engine = Scripting::newDefaultEngine();
+                       $language = $engine->getGeSHiLanguage();
                        
                        if( $wgScriptingUseGeSHi && $language ) {
                                $geshi = SyntaxHighlight_GeSHi::prepare( $text, 
$language );
@@ -161,10 +156,10 @@
                }
        }
        
-       public static function getCodeLangauge( $title, &$lang ) {
-               global $wgParser, $wgScriptingUseCodeEditor;
+       public static function getCodeLanguage( $title, &$lang ) {
+               global $wgScriptingUseCodeEditor;
                if( $wgScriptingUseCodeEditor && $title->getNamespace() == 
NS_MODULE ) {
-                       $engine = Scripting::getEngine( $wgParser );
+                       $engine = Scripting::newDefaultEngine();
                        if( $engine->getCodeEditorLanguage() ) {
                                $lang = $engine->getCodeEditorLanguage();
                                return false;
@@ -198,8 +193,8 @@
        public static function reportLimits( $parser, &$report ) {
                # FIXME
                global $wgScriptsLimits;
-               $engine = Scripting::getEngine( $parser );
-               $report .= $engine->getLimitsReport();
+               $engine = Scripting::getParserEngine( $parser );
+               $report .= $engine->getLimitReport();
                return true;
        }
 
@@ -213,12 +208,12 @@
        }
 
        public static function validateScript( $editor, $text, $section, 
&$error ) {
-               global $wgUser, $wgParser;
+               global $wgUser;
                $title = $editor->mTitle;
 
                if( $title->getNamespace() == NS_MODULE ) {
-                       $engine = Scripting::getEngine( $wgParser );
-                       $errors = $engine->validate( $text, $title );
+                       $engine = Scripting::newDefaultEngine();
+                       $errors = $engine->validate( $text, 
$title->getPrefixedDBkey() );
                        if( !$errors ) {
                                return true;
                        }

Deleted: trunk/extensions/Scripting/common/LinkUpdates.php
===================================================================
--- trunk/extensions/Scripting/common/LinkUpdates.php   2012-04-05 07:24:33 UTC 
(rev 114735)
+++ trunk/extensions/Scripting/common/LinkUpdates.php   2012-04-05 07:58:02 UTC 
(rev 114736)
@@ -1,209 +0,0 @@
-<?php
-/**
- * Wikitext scripting infrastructure for MediaWiki: link updating code
- * Copyright (C) 2011-2012 Victor Vasiliev <[email protected]> et al
- * Based on MediaWiki file LinksUpdate.php
- * 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
- */
-
-if( !defined( 'MEDIAWIKI' ) )
-       die();
-
-
-/**
- * Class that contains hooks related to tracking links to scripts and 
invalidating
- * pages on script change.
- */
-class ScriptLinksUpdateHooks {
-       /**
-        * Append script links to the output.
-        */
-       public static function appendToOutput( &$parser, &$text ) {
-               if( isset( $parser->scripting_engine ) ) {
-                       $parser->mOutput->scripting_links = 
$parser->scripting_engine->getUsedModules();
-               }
-               return true;
-       }
-
-       /**
-        * Run the link updater.
-        */
-       public static function updateLinks( &$update ) {
-               $output = $update->mParserOutput;
-               if( isset( $output->scripting_links ) ) {
-                       $new = $output->scripting_links;
-               } else {
-                       $new = array();
-               }
-               
-               $isupdate = new ScriptLinksUpdate( $update, $new );
-               $isupdate->run();
-               return true;
-       }
-
-       /**
-        * Purge cache for all the pages where the script is used.
-        * @param $article Article
-        * @param $editInfo
-        * @param $changed
-        * @return bool
-        */
-       public static function purgeCache( &$article, &$editInfo, $changed ) {
-               global $wgDeferredUpdateList, $wgParser;
-
-               if( $article->getTitle()->getNamespace() == NS_MODULE ) {
-                       // Invalidate the script cache
-                       $engine = Scripting::getEngine( $wgParser );
-                       $engine->invalidateModuleCache( $article->getTitle() );
-                       
-                       // Invalidate the caches of articles which include the 
script
-                       $wgDeferredUpdateList[] = new HTMLCacheUpdate( 
$article->getTitle(), 'scriptlinks' );
-               }
-
-               return true;
-       }
-
-       /**
-        * Add scriptlinks to the list of tables supported by BacklinkCache.
-        */
-       public static function getBacklinkCachePrefix( $table, &$prefix ) {
-               if( $table == 'scriptlinks' ) {
-                       $prefix = 'sl';
-                       return false;
-               } else {
-                       return true;
-               }
-       }
-
-       /**
-        * Add scriptlinks to the list of tables supported by BacklinkCache.
-        * @param $table
-        * @param $title Title
-        * @param $conds
-        * @return bool
-        */
-       public static function getBacklinkCacheConditions( $table, $title, 
&$conds ) {
-               if( $table == 'scriptlinks' ) {
-                       $conds = array(
-                               'sl_to' => $title->getDBkey(),
-                               'page_id=sl_from'
-                       );
-                       return false;
-               } else {
-                       return true;
-               }
-       }
-}
-
-/**
- * A class that updates links on scripts like what 
phase3/includes/LinksUpdate.php does
- * with templates.
- */
-class ScriptLinksUpdate {
-       var $update, $id, $new;
-
-       public function __construct( $update, $new ) {
-               $this->update = $update;
-               $this->id = $update->mId;
-               $this->new = $new;
-       }
-
-       public function run() {
-               global $wgUseDumbLinkUpdate;
-
-               wfProfileIn( __METHOD__ );
-
-               if( $wgUseDumbLinkUpdate ) {
-                       $this->update->dumbTableUpdate( 'scriptlinks', 
$this->getScriptInsertions(), 'sl_from' );
-               } else {
-                       $existing = $this->getExistingScripts();
-                       $this->update->incrTableUpdate( 'scriptlinks', 'sl', 
$this->getScriptDeletions( $existing ),
-                               $this->getScriptInsertions( $existing ) );
-               }
-
-               if( $this->update->mRecursive && 
$this->update->mTitle->getNamespace() == NS_MODULE ) {
-                       $this->queueRecursiveJobs();
-               }
-
-               wfProfileOut( __METHOD__ );
-       }
-
-       protected function getExistingScripts() {
-               $result = array();
-
-               $res = $this->update->mDb->select( 'scriptlinks', array( 
'sl_to' ),
-                       array( 'sl_from' => $this->id ), __METHOD__, 
$this->update->mOptions );
-               foreach ( $res as $row ) {
-                       $result[] = $row->sl_to;
-               }
-
-               return $result;
-       }
-
-       protected function getScriptInsertions( $existing = array() ) {
-               $result = array();
-
-               foreach( array_diff( $this->new, $existing ) as $module ) {
-                       $result[] = array(
-                               'sl_from' => $this->id,
-                               'sl_to' => $module,
-                       );
-               }
-
-               return $result;
-       }
-
-       protected function getScriptDeletions( $existing = array() ) {
-               $result = array();
-
-               foreach( array_diff( $existing, $this->new ) as $module ) {
-                       $result[] = array(
-                               'sl_from' => $this->id,
-                               'sl_to' => $module,
-                       );
-               }
-
-               return $result;
-       }
-
-       protected function queueRecursiveJobs() {
-               global $wgUpdateRowsPerJob;
-               wfProfileIn( __METHOD__ );
-
-               $cache = $this->update->mTitle->getBacklinkCache();
-               $batches = $cache->partition( 'scriptlinks', 
$wgUpdateRowsPerJob );
-               if ( !$batches ) {
-                       wfProfileOut( __METHOD__ );
-                       return;
-               }
-               $jobs = array();
-               foreach ( $batches as $batch ) {
-                       list( $start, $end ) = $batch;
-                       $params = array(
-                               'table' => 'scriptlinks',
-                               'start' => $start,
-                               'end' => $end,
-                       );
-                       $jobs[] = new RefreshLinksJob2( $this->update->mTitle, 
$params );
-               }
-               Job::batchInsert( $jobs );
-
-               wfProfileOut( __METHOD__ );
-       }
-}
-

Modified: trunk/extensions/Scripting/engines/LuaSandbox/Engine.php
===================================================================
--- trunk/extensions/Scripting/engines/LuaSandbox/Engine.php    2012-04-05 
07:24:33 UTC (rev 114735)
+++ trunk/extensions/Scripting/engines/LuaSandbox/Engine.php    2012-04-05 
07:58:02 UTC (rev 114736)
@@ -1,10 +1,10 @@
 <?php
 
 class LuaSandboxEngine extends ScriptingEngineBase {
-       public $sandbox, $loaded = false;
+       public $sandbox, $options, $loaded = false;
 
-       public function newModule( $title, $code, $revisionID, $source ) {
-               return new LuaSandboxEngineModule( $this, $title, $code, 
$revisionID, $source );
+       public function newModule( $text, $chunkName ) {
+               return new LuaSandboxEngineModule( $this, $text, $chunkName );
        }
 
        public function load() {
@@ -18,31 +18,17 @@
 
                $this->sandbox = new LuaSandbox;
                $this->sandbox->setMemoryLimit( $this->options['memoryLimit'] );
-               $this->sandbox->setCPULimit( $this->options['maxCPU'] );
+               $this->sandbox->setCPULimit( $this->options['cpuLimit'] );
                $this->sandbox->registerLibrary( 'mw', array( 'import' => 
array( $this, 'importModule' ) ) );
                
                $this->loaded = true;
        }
 
-       protected function updateOptions() {
-               if( $this->loaded ) {
-                       $this->sandbox->setMemoryLimit( 
$this->options['memoryLimit'] );
-                       $this->sandbox->setCPULimit( $this->options['maxCPU'] );
-               }
-       }
-
        protected function getModuleClassName() {
                return 'LuaSandboxEngineModule';
        }
 
-       public function getDefaultOptions() {
-               return array(
-                       'memoryLimit' => 50 * 1024 * 1024,
-                       'maxCPU' => 7,
-               );
-       }
-
-       public function getGeSHiLangauge() {
+       public function getGeSHiLanguage() {
                return 'lua';
        }
        
@@ -50,7 +36,7 @@
                return 'lua';
        }
        
-       public function getLimitsReport() {
+       public function getLimitReport() {
                $this->load();
                
                $usage = $this->sandbox->getMemoryUsage();
@@ -69,12 +55,16 @@
                $args = func_get_args();
                if( count( $args ) < 1 ) {
                        // FIXME: LuaSandbox PHP extension should provide 
proper context
-                       throw new ScriptingException( 'toofewargs', 'common', 
null, null, array( 'mw.import' ) );
+                       throw new ScriptingException( 
'scripting-common-toofewargs',
+                               array( 'args' => array( 'mw.import' ) ) );
                }
 
-               $module = $this->getModule( $args[0] );
-               $module->initialize();
-               return $module->contents;
+               $title = Title::makeTitleSafe( NS_MODULE, $args[0] );
+               if ( !$title ) {
+                       throw new ScriptingException( 
'scripting-common-nosuchmodule' );
+               }
+               $module = $this->fetchModuleFromParser( $title );
+               return $module->getContents();
        }
 }
 
@@ -93,20 +83,21 @@
                        $this->body = $this->engine->sandbox->loadString(
                                $this->code, 
                                // Prepending an "@" to the chunk name makes 
Lua think it is a file name
-                               '@' . $this->getTitle()->getPrefixedDBkey() );
+                               '@' . $this->chunkName );
                        $output = $this->body->call();
                } catch( LuaSandboxError $e ) {
-                       throw new ScriptingException( 'error', 'luasandbox', 
null, null, array( $e->getMessage() ) );
+                       throw new ScriptingException( 
'scripting-luasandbox-error', 
+                               array( 'args' => array( $e->getMessage() ) ) );
                }
                
                if( !$output ) {
-                       throw new ScriptingException( 'noreturn', 'luasandbox' 
);
+                       throw new ScriptingException( 
'scripting-luasandbox-noreturn' );
                }
                if( count( $output ) > 2 ) {
-                       throw new ScriptingException( 'toomanyreturns', 
'luasandbox' );
+                       throw new ScriptingException( 
'scripting-luasandbox-toomanyreturns' );
                }
                if( !is_array( $output[0] ) ) {
-                       throw new ScriptingException( 'notarrayreturn', 
'luasandbox' );
+                       throw new ScriptingException( 
'scripting-luasandbox-notarrayreturn' );
                }
                
                $this->contents = $output[0];
@@ -133,6 +124,11 @@
                $this->initialize();
                return $this->functions;
        }
+
+       function getContents() {
+               $this->initialize();
+               return $this->contents;
+       }
 }
 
 class LuaSandboxEngineFunction extends ScriptingFunctionBase {
@@ -140,7 +136,8 @@
                try {
                        $result = call_user_func_array( array( $this->contents, 
'call' ), $args );
                } catch( LuaSandboxError $e ) {
-                       throw new ScriptingException( 'error', 'luasandbox', 
null, null, array( $e->getMessage() ) );
+                       throw new ScriptingException( 
'scripting-luasandbox-error', 
+                               array( 'args' => array( $e->getMessage() ) ) );
                }
                
                if ( isset( $result[0] ) ) {

Deleted: trunk/extensions/Scripting/scripting.sql
===================================================================
--- trunk/extensions/Scripting/scripting.sql    2012-04-05 07:24:33 UTC (rev 
114735)
+++ trunk/extensions/Scripting/scripting.sql    2012-04-05 07:58:02 UTC (rev 
114736)
@@ -1,16 +0,0 @@
---
--- Track script inclusions.
---
-CREATE TABLE /*_*/scriptlinks (
-  -- Key to the page_id of the page containing the link.
-  sl_from int unsigned NOT NULL default 0,
-
-  -- Key to page_title of the target page.
-  -- The target page may or may not exist, and due to renames
-  -- and deletions may refer to different page records as time
-  -- goes by.
-  sl_to varchar(255) binary NOT NULL default ''
-) /*$wgDBTableOptions*/;
-
-CREATE UNIQUE INDEX /*i*/sl_from ON /*_*/scriptlinks (sl_from,sl_to);
-CREATE UNIQUE INDEX /*i*/sl_title ON /*_*/scriptlinks (sl_to,sl_from);


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to