User:Duesentrieb/PPCustomFrame

From mediawiki.org
Index: includes/parser/Preprocessor_Hash.php
===================================================================
--- includes/parser/Preprocessor_Hash.php	(Revision 36636)
+++ includes/parser/Preprocessor_Hash.php	(Arbeitskopie)
@@ -17,6 +17,10 @@
 		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().
@@ -1209,8 +1213,46 @@
 }
 
 /**
+ * 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
+ */
 class PPNode_Hash_Tree implements PPNode {
 	var $name, $firstChild, $lastChild, $nextSibling;
 
Index: includes/parser/Parser.php
===================================================================
--- includes/parser/Parser.php	(Revision 36636)
+++ includes/parser/Parser.php	(Arbeitskopie)
@@ -2645,7 +2645,9 @@
 	 *  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 @@
 		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: includes/parser/Preprocessor_DOM.php
===================================================================
--- includes/parser/Preprocessor_DOM.php	(Revision 36636)
+++ includes/parser/Preprocessor_DOM.php	(Arbeitskopie)
@@ -23,6 +23,10 @@
 		return new PPFrame_DOM( $this );
 	}
 
+	function newCustomFrame( $args ) {
+		return new PPCustomFrame_DOM( $this, $args );
+	}
+
 	function memCheck() {
 		if ( $this->memoryLimit === false ) {
 			return;
@@ -1254,8 +1258,46 @@
 }
 
 /**
+ * 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
+ */
 class PPNode_DOM implements PPNode {
 	var $node;
 
Index: includes/parser/Preprocessor.php
===================================================================
--- includes/parser/Preprocessor.php	(Revision 36636)
+++ includes/parser/Preprocessor.php	(Arbeitskopie)
@@ -10,6 +10,9 @@
 	/** 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 );
 }