From 24d5e941a06f5b493bbba962b82036461d92dc87 Mon Sep 17 00:00:00 2001 From: Daniel Kinzler Date: Thu, 26 Jun 2008 13:05:40 +0000 Subject: [PATCH] added PPCustomFrame classes to restore ability to use replaceVariables with a custom map of values. This should unbreak some extensions that were broken by the new PP stuff, like the News extension. --- includes/parser/Parser.php | 7 +++-- includes/parser/Preprocessor.php | 3 ++ includes/parser/Preprocessor_DOM.php | 42 +++++++++++++++++++++++++++ includes/parser/Preprocessor_Hash.php | 42 +++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 2 deletions(-) diff --git a/includes/parser/Parser.php b/includes/parser/Parser.php index a3b5d32004..48cfebdc27 100644 --- a/includes/parser/Parser.php +++ b/includes/parser/Parser.php @@ -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 ); diff --git a/includes/parser/Preprocessor.php b/includes/parser/Preprocessor.php index 322e197f7e..1a33ac7fc1 100644 --- a/includes/parser/Preprocessor.php +++ b/includes/parser/Preprocessor.php @@ -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 ); } diff --git a/includes/parser/Preprocessor_DOM.php b/includes/parser/Preprocessor_DOM.php index 01776cdb47..34d589676f 100644 --- a/includes/parser/Preprocessor_DOM.php +++ b/includes/parser/Preprocessor_DOM.php @@ -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 */ diff --git a/includes/parser/Preprocessor_Hash.php b/includes/parser/Preprocessor_Hash.php index dc3378832a..b57752437d 100644 --- a/includes/parser/Preprocessor_Hash.php +++ b/includes/parser/Preprocessor_Hash.php @@ -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 */ -- 2.20.1