r112313 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r112312‎ | r112313 | r112314 >
Date:11:31, 24 February 2012
Author:dantman
Status:ok
Tags:
Comment:
Fix bug 34684 in my PathRouter code:
- Update the tests to test extra characters and patterns like like \\ and $1
- Also update the tests to make sure that matches that don't have enough data to work fail
- Replace the str_replace and preg_match based code with code based on preg_replace_callback.
Modified paths:
  • /trunk/phase3/includes/AutoLoader.php (modified) (history)
  • /trunk/phase3/includes/PathRouter.php (modified) (history)
  • /trunk/phase3/tests/phpunit/includes/PathRouterTest.php (modified) (history)

Diff [purge]

Index: trunk/phase3/tests/phpunit/includes/PathRouterTest.php
@@ -125,6 +125,16 @@
126126 }
127127
128128 /**
 129+ * Test to ensure that matches are not made if a parameter expects nonexistent input
 130+ */
 131+ public function testFail() {
 132+ $router = new PathRouter;
 133+ $router->add( "/wiki/$1", array( 'title' => "$1$2" ) );
 134+ $matches = $router->parse( "/wiki/A" );
 135+ $this->assertEquals( array(), $matches );
 136+ }
 137+
 138+ /**
129139 * Test to ensure weight of paths is handled correctly
130140 */
131141 public function testWeight() {
@@ -172,12 +182,30 @@
173183 $this->assertEquals( $matches, array( 'title' => "Title_With Space" ) );
174184 }
175185
 186+ public function dataRegexpChars() {
 187+ return array(
 188+ array( "$" ),
 189+ array( "$1" ),
 190+ array( "\\" ),
 191+ array( "\\$1" ),
 192+ );
 193+ }
 194+
176195 /**
 196+ * Make sure the router doesn't break on special characters like $ used in regexp replacements
 197+ * @dataProvider dataRegexpChars
 198+ */
 199+ public function testRegexpChars( $char ) {
 200+ $matches = $this->basicRouter->parse( "/wiki/$char" );
 201+ $this->assertEquals( $matches, array( 'title' => "$char" ) );
 202+ }
 203+
 204+ /**
177205 * Make sure the router handles characters like +&() properly
178206 */
179207 public function testCharacters() {
180 - $matches = $this->basicRouter->parse( "/wiki/Plus+And&Stuff()" );
181 - $this->assertEquals( $matches, array( 'title' => "Plus+And&Stuff()" ) );
 208+ $matches = $this->basicRouter->parse( "/wiki/Plus+And&Dollar\\Stuff();[]{}*" );
 209+ $this->assertEquals( $matches, array( 'title' => "Plus+And&Dollar\\Stuff();[]{}*" ) );
182210 }
183211
184212 /**
Index: trunk/phase3/includes/PathRouter.php
@@ -278,20 +278,14 @@
279279 } elseif ( isset( $paramData['pattern'] ) ) {
280280 // For patterns we have to make value replacements on the string
281281 $value = $paramData['pattern'];
282 - // For each $# match replace any $# within the value
283 - foreach ( $m as $matchKey => $matchValue ) {
284 - if ( preg_match( '/^par\d+$/u', $matchKey ) ) {
285 - $n = intval( substr( $matchKey, 3 ) );
286 - $value = str_replace( '$' . $n, rawurldecode( $matchValue ), $value );
287 - }
288 - }
289 - // If a key was set replace any $key within the value
 282+ $replacer = new PathRouterPatternReplacer;
 283+ $replacer->params = $m;
290284 if ( isset( $pattern->key ) ) {
291 - $value = str_replace( '$key', $pattern->key, $value );
 285+ $replacer->key = $pattern->key;
292286 }
293 - if ( preg_match( '/\$(\d+|key)/u', $value ) ) {
294 - // Still contains $# or $key patterns after replacement
295 - // Seams like we don't have all the data, abort
 287+ $value = $replacer->replace( $value );
 288+ if ( $value === false ) {
 289+ // Pattern required data that wasn't available, abort
296290 return null;
297291 }
298292 }
@@ -317,3 +311,41 @@
318312 }
319313
320314 }
 315+
 316+class PathRouterPatternReplacer {
 317+
 318+ public $key, $params, $error;
 319+
 320+ /**
 321+ * Replace keys inside path router patterns with text.
 322+ * We do this inside of a replacement callback because after replacement we can't tell the
 323+ * difference between a $1 that was not replaced and a $1 that was part of
 324+ * the content a $1 was replaced with.
 325+ */
 326+ public function replace( $value ) {
 327+ $this->error = false;
 328+ $value = preg_replace_callback( '/\$(\d+|key)/u', array( $this, 'callback' ), $value );
 329+ if ( $this->error ) {
 330+ return false;
 331+ }
 332+ return $value;
 333+ }
 334+
 335+ protected function callback( $m ) {
 336+ if ( $m[1] == "key" ) {
 337+ if ( is_null( $this->key ) ) {
 338+ $this->error = true;
 339+ return '';
 340+ }
 341+ return $this->key;
 342+ } else {
 343+ $d = $m[1];
 344+ if ( !isset( $this->params["par$d"] ) ) {
 345+ $this->error = true;
 346+ return '';
 347+ }
 348+ return rawurldecode( $this->params["par$d"] );
 349+ }
 350+ }
 351+
 352+}
\ No newline at end of file
Index: trunk/phase3/includes/AutoLoader.php
@@ -165,6 +165,7 @@
166166 'Pager' => 'includes/Pager.php',
167167 'PasswordError' => 'includes/User.php',
168168 'PathRouter' => 'includes/PathRouter.php',
 169+ 'PathRouterPatternReplacer' => 'includes/PathRouter.php',
169170 'PermissionsError' => 'includes/Exception.php',
170171 'PhpHttpRequest' => 'includes/HttpFunctions.php',
171172 'PoolCounter' => 'includes/PoolCounter.php',

Follow-up revisions

RevisionCommit summaryAuthorDate
r112316MFT r112313: fix dollar signs in PATH_INFOtstarling12:01, 24 February 2012
r112644MFT r112169, r112170, r112172, r112173, r112179, r112184, r112290, r112313reedy21:13, 28 February 2012

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r104689Followup r104688, reintroduce the full PathRouter code now that the bug with ...dantman15:12, 30 November 2011

Status & tagging log