Index: trunk/phase3/includes/parser/Preprocessor_DOM.php |
— | — | @@ -23,6 +23,10 @@ |
24 | 24 | return new PPFrame_DOM( $this ); |
25 | 25 | } |
26 | 26 | |
| 27 | + function newCustomFrame( $args ) { |
| 28 | + return new PPCustomFrame_DOM( $this, $args ); |
| 29 | + } |
| 30 | + |
27 | 31 | function memCheck() { |
28 | 32 | if ( $this->memoryLimit === false ) { |
29 | 33 | return; |
— | — | @@ -1254,8 +1258,46 @@ |
1255 | 1259 | } |
1256 | 1260 | |
1257 | 1261 | /** |
| 1262 | + * Expansion frame with custom arguments |
1258 | 1263 | * @ingroup Parser |
1259 | 1264 | */ |
| 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 | + */ |
1260 | 1302 | class PPNode_DOM implements PPNode { |
1261 | 1303 | var $node; |
1262 | 1304 | |
Index: trunk/phase3/includes/parser/Preprocessor.php |
— | — | @@ -10,6 +10,9 @@ |
11 | 11 | /** Create a new top-level frame for expansion of a page */ |
12 | 12 | function newFrame(); |
13 | 13 | |
| 14 | + /** Create a new custom frame for programmatic use of parameter replacement as used in some extensions */ |
| 15 | + function newCustomFrame( $args ); |
| 16 | + |
14 | 17 | /** Preprocess text to a PPNode */ |
15 | 18 | function preprocessToObj( $text, $flags = 0 ); |
16 | 19 | } |
Index: trunk/phase3/includes/parser/Preprocessor_Hash.php |
— | — | @@ -17,6 +17,10 @@ |
18 | 18 | return new PPFrame_Hash( $this ); |
19 | 19 | } |
20 | 20 | |
| 21 | + function newCustomFrame( $args ) { |
| 22 | + return new PPCustomFrame_Hash( $this, $args ); |
| 23 | + } |
| 24 | + |
21 | 25 | /** |
22 | 26 | * Preprocess some wikitext and return the document tree. |
23 | 27 | * This is the ghost of Parser::replace_variables(). |
— | — | @@ -1209,8 +1213,46 @@ |
1210 | 1214 | } |
1211 | 1215 | |
1212 | 1216 | /** |
| 1217 | + * Expansion frame with custom arguments |
1213 | 1218 | * @ingroup Parser |
1214 | 1219 | */ |
| 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 | + */ |
1215 | 1257 | class PPNode_Hash_Tree implements PPNode { |
1216 | 1258 | var $name, $firstChild, $lastChild, $nextSibling; |
1217 | 1259 | |
Index: trunk/phase3/includes/parser/Parser.php |
— | — | @@ -2645,7 +2645,9 @@ |
2646 | 2646 | * self::OT_HTML: all templates and extension tags |
2647 | 2647 | * |
2648 | 2648 | * @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. |
2650 | 2652 | * @param bool $argsOnly Only do argument (triple-brace) expansion, not double-brace expansion |
2651 | 2653 | * @private |
2652 | 2654 | */ |
— | — | @@ -2661,7 +2663,8 @@ |
2662 | 2664 | if ( $frame === false ) { |
2663 | 2665 | $frame = $this->getPreprocessor()->newFrame(); |
2664 | 2666 | } 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); |
2666 | 2669 | } |
2667 | 2670 | |
2668 | 2671 | $dom = $this->preprocessToDom( $text ); |