added PPCustomFrame classes to restore ability to use replaceVariables with a custom...
authorDaniel Kinzler <daniel@users.mediawiki.org>
Thu, 26 Jun 2008 13:05:40 +0000 (13:05 +0000)
committerDaniel Kinzler <daniel@users.mediawiki.org>
Thu, 26 Jun 2008 13:05:40 +0000 (13:05 +0000)
includes/parser/Parser.php
includes/parser/Preprocessor.php
includes/parser/Preprocessor_DOM.php
includes/parser/Preprocessor_Hash.php

index a3b5d32..48cfebd 100644 (file)
@@ -2645,7 +2645,9 @@ class Parser
         *  self::OT_HTML: all templates and extension tags
         *
         * @param string $tex The text to transform
-        * @param PPFrame $frame Object describing the arguments passed to the template
+        * @param PPFrame $frame Object describing the arguments passed to the template. 
+        *        Arguments may also be provided as an associative array, as was the usual case before MW1.12.
+        *        Providing arguments this way may be useful for extensions wishing to perform variable replacement explicitly.
         * @param bool $argsOnly Only do argument (triple-brace) expansion, not double-brace expansion
         * @private
         */
@@ -2661,7 +2663,8 @@ class Parser
                if ( $frame === false ) {
                        $frame = $this->getPreprocessor()->newFrame();
                } elseif ( !( $frame instanceof PPFrame ) ) {
-                       throw new MWException( __METHOD__ . ' called using the old argument format' );
+                       wfDebug( __METHOD__." called using plain parameters instead of a PPFrame instance. Creating custom frame.\n" );
+                       $frame = $this->getPreprocessor()->newCustomFrame($frame);
                }
 
                $dom = $this->preprocessToDom( $text );
index 322e197..1a33ac7 100644 (file)
@@ -10,6 +10,9 @@ interface Preprocessor {
        /** Create a new top-level frame for expansion of a page */
        function newFrame();
 
+       /** Create a new custom frame for programmatic use of parameter replacement as used in some extensions */
+       function newCustomFrame( $args );
+
        /** Preprocess text to a PPNode */
        function preprocessToObj( $text, $flags = 0 );
 }
index 01776cd..34d5896 100644 (file)
@@ -23,6 +23,10 @@ class Preprocessor_DOM implements Preprocessor {
                return new PPFrame_DOM( $this );
        }
 
+       function newCustomFrame( $args ) {
+               return new PPCustomFrame_DOM( $this, $args );
+       }
+
        function memCheck() {
                if ( $this->memoryLimit === false ) {
                        return;
@@ -1253,6 +1257,44 @@ class PPTemplateFrame_DOM extends PPFrame_DOM {
        }
 }
 
+/**
+ * Expansion frame with custom arguments
+ * @ingroup Parser
+ */
+class PPCustomFrame_DOM extends PPFrame_DOM {
+       var $args;
+
+       function __construct( $preprocessor, $args ) {
+               $this->preprocessor = $preprocessor;
+               $this->parser = $preprocessor->parser;
+               $this->args = $args;
+       }
+
+       function __toString() {
+               $s = 'cstmframe{';
+               $first = true;
+               foreach ( $this->args as $name => $value ) {
+                       if ( $first ) {
+                               $first = false;
+                       } else {
+                               $s .= ', ';
+                       }
+                       $s .= "\"$name\":\"" .
+                               str_replace( '"', '\\"', $value->__toString() ) . '"';
+               }
+               $s .= '}';
+               return $s;
+       }
+
+       function isEmpty() {
+               return !count( $this->args );
+       }
+
+       function getArgument( $index ) {
+               return $this->args[$index];
+       }
+}
+
 /**
  * @ingroup Parser
  */
index dc33788..b577524 100644 (file)
@@ -17,6 +17,10 @@ class Preprocessor_Hash implements Preprocessor {
                return new PPFrame_Hash( $this );
        }
 
+       function newCustomFrame( $args ) {
+               return new PPCustomFrame_Hash( $this, $args );
+       }
+
        /**
         * Preprocess some wikitext and return the document tree.
         * This is the ghost of Parser::replace_variables().
@@ -1208,6 +1212,44 @@ class PPTemplateFrame_Hash extends PPFrame_Hash {
        }
 }
 
+/**
+ * Expansion frame with custom arguments
+ * @ingroup Parser
+ */
+class PPCustomFrame_Hash extends PPFrame_Hash {
+       var $args;
+
+       function __construct( $preprocessor, $args ) {
+               $this->preprocessor = $preprocessor;
+               $this->parser = $preprocessor->parser;
+               $this->args = $args;
+       }
+
+       function __toString() {
+               $s = 'cstmframe{';
+               $first = true;
+               foreach ( $this->args as $name => $value ) {
+                       if ( $first ) {
+                               $first = false;
+                       } else {
+                               $s .= ', ';
+                       }
+                       $s .= "\"$name\":\"" .
+                               str_replace( '"', '\\"', $value->__toString() ) . '"';
+               }
+               $s .= '}';
+               return $s;
+       }
+
+       function isEmpty() {
+               return !count( $this->args );
+       }
+
+       function getArgument( $index ) {
+               return $this->args[$index];
+       }
+}
+
 /**
  * @ingroup Parser
  */