r36668 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r36667‎ | r36668 | r36669 >
Date:13:05, 26 June 2008
Author:daniel
Status:old
Tags:
Comment:
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.
Modified paths:
  • /trunk/phase3/includes/parser/Parser.php (modified) (history)
  • /trunk/phase3/includes/parser/Preprocessor.php (modified) (history)
  • /trunk/phase3/includes/parser/Preprocessor_DOM.php (modified) (history)
  • /trunk/phase3/includes/parser/Preprocessor_Hash.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/parser/Preprocessor_DOM.php
@@ -23,6 +23,10 @@
2424 return new PPFrame_DOM( $this );
2525 }
2626
 27+ function newCustomFrame( $args ) {
 28+ return new PPCustomFrame_DOM( $this, $args );
 29+ }
 30+
2731 function memCheck() {
2832 if ( $this->memoryLimit === false ) {
2933 return;
@@ -1254,8 +1258,46 @@
12551259 }
12561260
12571261 /**
 1262+ * Expansion frame with custom arguments
12581263 * @ingroup Parser
12591264 */
 1265+class PPCustomFrame_DOM extends PPFrame_DOM {
 1266+ var $args;
 1267+
 1268+ function __construct( $preprocessor, $args ) {
 1269+ $this->preprocessor = $preprocessor;
 1270+ $this->parser = $preprocessor->parser;
 1271+ $this->args = $args;
 1272+ }
 1273+
 1274+ function __toString() {
 1275+ $s = 'cstmframe{';
 1276+ $first = true;
 1277+ foreach ( $this->args as $name => $value ) {
 1278+ if ( $first ) {
 1279+ $first = false;
 1280+ } else {
 1281+ $s .= ', ';
 1282+ }
 1283+ $s .= "\"$name\":\"" .
 1284+ str_replace( '"', '\\"', $value->__toString() ) . '"';
 1285+ }
 1286+ $s .= '}';
 1287+ return $s;
 1288+ }
 1289+
 1290+ function isEmpty() {
 1291+ return !count( $this->args );
 1292+ }
 1293+
 1294+ function getArgument( $index ) {
 1295+ return $this->args[$index];
 1296+ }
 1297+}
 1298+
 1299+/**
 1300+ * @ingroup Parser
 1301+ */
12601302 class PPNode_DOM implements PPNode {
12611303 var $node;
12621304
Index: trunk/phase3/includes/parser/Preprocessor.php
@@ -10,6 +10,9 @@
1111 /** Create a new top-level frame for expansion of a page */
1212 function newFrame();
1313
 14+ /** Create a new custom frame for programmatic use of parameter replacement as used in some extensions */
 15+ function newCustomFrame( $args );
 16+
1417 /** Preprocess text to a PPNode */
1518 function preprocessToObj( $text, $flags = 0 );
1619 }
Index: trunk/phase3/includes/parser/Preprocessor_Hash.php
@@ -17,6 +17,10 @@
1818 return new PPFrame_Hash( $this );
1919 }
2020
 21+ function newCustomFrame( $args ) {
 22+ return new PPCustomFrame_Hash( $this, $args );
 23+ }
 24+
2125 /**
2226 * Preprocess some wikitext and return the document tree.
2327 * This is the ghost of Parser::replace_variables().
@@ -1209,8 +1213,46 @@
12101214 }
12111215
12121216 /**
 1217+ * Expansion frame with custom arguments
12131218 * @ingroup Parser
12141219 */
 1220+class PPCustomFrame_Hash extends PPFrame_Hash {
 1221+ var $args;
 1222+
 1223+ function __construct( $preprocessor, $args ) {
 1224+ $this->preprocessor = $preprocessor;
 1225+ $this->parser = $preprocessor->parser;
 1226+ $this->args = $args;
 1227+ }
 1228+
 1229+ function __toString() {
 1230+ $s = 'cstmframe{';
 1231+ $first = true;
 1232+ foreach ( $this->args as $name => $value ) {
 1233+ if ( $first ) {
 1234+ $first = false;
 1235+ } else {
 1236+ $s .= ', ';
 1237+ }
 1238+ $s .= "\"$name\":\"" .
 1239+ str_replace( '"', '\\"', $value->__toString() ) . '"';
 1240+ }
 1241+ $s .= '}';
 1242+ return $s;
 1243+ }
 1244+
 1245+ function isEmpty() {
 1246+ return !count( $this->args );
 1247+ }
 1248+
 1249+ function getArgument( $index ) {
 1250+ return $this->args[$index];
 1251+ }
 1252+}
 1253+
 1254+/**
 1255+ * @ingroup Parser
 1256+ */
12151257 class PPNode_Hash_Tree implements PPNode {
12161258 var $name, $firstChild, $lastChild, $nextSibling;
12171259
Index: trunk/phase3/includes/parser/Parser.php
@@ -2645,7 +2645,9 @@
26462646 * self::OT_HTML: all templates and extension tags
26472647 *
26482648 * @param string $tex The text to transform
2649 - * @param PPFrame $frame Object describing the arguments passed to the template
 2649+ * @param PPFrame $frame Object describing the arguments passed to the template.
 2650+ * Arguments may also be provided as an associative array, as was the usual case before MW1.12.
 2651+ * Providing arguments this way may be useful for extensions wishing to perform variable replacement explicitly.
26502652 * @param bool $argsOnly Only do argument (triple-brace) expansion, not double-brace expansion
26512653 * @private
26522654 */
@@ -2661,7 +2663,8 @@
26622664 if ( $frame === false ) {
26632665 $frame = $this->getPreprocessor()->newFrame();
26642666 } elseif ( !( $frame instanceof PPFrame ) ) {
2665 - throw new MWException( __METHOD__ . ' called using the old argument format' );
 2667+ wfDebug( __METHOD__." called using plain parameters instead of a PPFrame instance. Creating custom frame.\n" );
 2668+ $frame = $this->getPreprocessor()->newCustomFrame($frame);
26662669 }
26672670
26682671 $dom = $this->preprocessToDom( $text );

Status & tagging log