https://www.mediawiki.org/wiki/Special:Code/MediaWiki/112316
Revision: 112316
Author: tstarling
Date: 2012-02-24 12:01:15 +0000 (Fri, 24 Feb 2012)
Log Message:
-----------
MFT r112313: fix dollar signs in PATH_INFO
Modified Paths:
--------------
branches/wmf/1.19wmf1/includes/AutoLoader.php
branches/wmf/1.19wmf1/includes/PathRouter.php
branches/wmf/1.19wmf1/tests/phpunit/includes/PathRouterTest.php
Property Changed:
----------------
branches/wmf/1.19wmf1/includes/AutoLoader.php
branches/wmf/1.19wmf1/includes/PathRouter.php
branches/wmf/1.19wmf1/tests/phpunit/includes/PathRouterTest.php
Modified: branches/wmf/1.19wmf1/includes/AutoLoader.php
===================================================================
--- branches/wmf/1.19wmf1/includes/AutoLoader.php 2012-02-24 11:47:26 UTC
(rev 112315)
+++ branches/wmf/1.19wmf1/includes/AutoLoader.php 2012-02-24 12:01:15 UTC
(rev 112316)
@@ -162,6 +162,7 @@
'Pager' => 'includes/Pager.php',
'PasswordError' => 'includes/User.php',
'PathRouter' => 'includes/PathRouter.php',
+ 'PathRouterPatternReplacer' => 'includes/PathRouter.php',
'PermissionsError' => 'includes/Exception.php',
'PhpHttpRequest' => 'includes/HttpFunctions.php',
'PoolCounter' => 'includes/PoolCounter.php',
Property changes on: branches/wmf/1.19wmf1/includes/AutoLoader.php
___________________________________________________________________
Modified: svn:mergeinfo
- /branches/FileBackend/phase3/includes/AutoLoader.php:99972-106750
/branches/JSTesting/includes/AutoLoader.php:100352-107913
/branches/REL1_15/phase3/includes/AutoLoader.php:51646
/branches/REL1_17/phase3/includes/AutoLoader.php:81448
/branches/new-installer/phase3/includes/AutoLoader.php:43664-66004
/branches/sqlite/includes/AutoLoader.php:58211-58321
/branches/uploadwizard/phase3/includes/AutoLoader.php:73550-75905
/branches/wmf-deployment/includes/AutoLoader.php:53381
/trunk/phase3/includes/AutoLoader.php:111029,111034,111067,111085,111128,111144
+ /branches/FileBackend/phase3/includes/AutoLoader.php:99972-106750
/branches/JSTesting/includes/AutoLoader.php:100352-107913
/branches/REL1_15/phase3/includes/AutoLoader.php:51646
/branches/REL1_17/phase3/includes/AutoLoader.php:81448
/branches/new-installer/phase3/includes/AutoLoader.php:43664-66004
/branches/sqlite/includes/AutoLoader.php:58211-58321
/branches/uploadwizard/phase3/includes/AutoLoader.php:73550-75905
/branches/wmf/1.19wmf1/includes/AutoLoader.php:112313
/branches/wmf-deployment/includes/AutoLoader.php:53381
/trunk/phase3/includes/AutoLoader.php:111029,111034,111067,111085,111128,111144,112313
Modified: branches/wmf/1.19wmf1/includes/PathRouter.php
===================================================================
--- branches/wmf/1.19wmf1/includes/PathRouter.php 2012-02-24 11:47:26 UTC
(rev 112315)
+++ branches/wmf/1.19wmf1/includes/PathRouter.php 2012-02-24 12:01:15 UTC
(rev 112316)
@@ -278,20 +278,14 @@
} elseif ( isset( $paramData['pattern'] ) ) {
// For patterns we have to make value
replacements on the string
$value = $paramData['pattern'];
- // For each $# match replace any $#
within the value
- foreach ( $m as $matchKey =>
$matchValue ) {
- if ( preg_match( '/^par\d+$/u',
$matchKey ) ) {
- $n = intval( substr(
$matchKey, 3 ) );
- $value = str_replace(
'$' . $n, rawurldecode( $matchValue ), $value );
- }
- }
- // If a key was set replace any $key
within the value
+ $replacer = new
PathRouterPatternReplacer;
+ $replacer->params = $m;
if ( isset( $pattern->key ) ) {
- $value = str_replace( '$key',
$pattern->key, $value );
+ $replacer->key = $pattern->key;
}
- if ( preg_match( '/\$(\d+|key)/u',
$value ) ) {
- // Still contains $# or $key
patterns after replacement
- // Seams like we don't have all
the data, abort
+ $value = $replacer->replace( $value );
+ if ( $value === false ) {
+ // Pattern required data that
wasn't available, abort
return null;
}
}
@@ -317,3 +311,41 @@
}
}
+
+class PathRouterPatternReplacer {
+
+ public $key, $params, $error;
+
+ /**
+ * Replace keys inside path router patterns with text.
+ * We do this inside of a replacement callback because after
replacement we can't tell the
+ * difference between a $1 that was not replaced and a $1 that was part
of
+ * the content a $1 was replaced with.
+ */
+ public function replace( $value ) {
+ $this->error = false;
+ $value = preg_replace_callback( '/\$(\d+|key)/u', array( $this,
'callback' ), $value );
+ if ( $this->error ) {
+ return false;
+ }
+ return $value;
+ }
+
+ protected function callback( $m ) {
+ if ( $m[1] == "key" ) {
+ if ( is_null( $this->key ) ) {
+ $this->error = true;
+ return '';
+ }
+ return $this->key;
+ } else {
+ $d = $m[1];
+ if ( !isset( $this->params["par$d"] ) ) {
+ $this->error = true;
+ return '';
+ }
+ return rawurldecode( $this->params["par$d"] );
+ }
+ }
+
+}
\ No newline at end of file
Property changes on: branches/wmf/1.19wmf1/includes/PathRouter.php
___________________________________________________________________
Added: svn:mergeinfo
+ /branches/JSTesting/includes/PathRouter.php:100352-107913
/branches/REL1_15/phase3/includes/PathRouter.php:51646
/branches/new-installer/phase3/includes/PathRouter.php:43664-66004
/branches/sqlite/includes/PathRouter.php:58211-58321
/branches/wmf/1.18wmf1/includes/PathRouter.php:97508
/branches/wmf-deployment/includes/PathRouter.php:53381
/trunk/phase3/includes/PathRouter.php:111029,111034,111067,111085,111128,111144,111251,111397,111571,111574,111597,112313
Modified: branches/wmf/1.19wmf1/tests/phpunit/includes/PathRouterTest.php
===================================================================
--- branches/wmf/1.19wmf1/tests/phpunit/includes/PathRouterTest.php
2012-02-24 11:47:26 UTC (rev 112315)
+++ branches/wmf/1.19wmf1/tests/phpunit/includes/PathRouterTest.php
2012-02-24 12:01:15 UTC (rev 112316)
@@ -125,6 +125,16 @@
}
/**
+ * Test to ensure that matches are not made if a parameter expects
nonexistent input
+ */
+ public function testFail() {
+ $router = new PathRouter;
+ $router->add( "/wiki/$1", array( 'title' => "$1$2" ) );
+ $matches = $router->parse( "/wiki/A" );
+ $this->assertEquals( array(), $matches );
+ }
+
+ /**
* Test to ensure weight of paths is handled correctly
*/
public function testWeight() {
@@ -172,12 +182,30 @@
$this->assertEquals( $matches, array( 'title' => "Title_With
Space" ) );
}
+ public function dataRegexpChars() {
+ return array(
+ array( "$" ),
+ array( "$1" ),
+ array( "\\" ),
+ array( "\\$1" ),
+ );
+ }
+
/**
+ * Make sure the router doesn't break on special characters like $ used
in regexp replacements
+ * @dataProvider dataRegexpChars
+ */
+ public function testRegexpChars( $char ) {
+ $matches = $this->basicRouter->parse( "/wiki/$char" );
+ $this->assertEquals( $matches, array( 'title' => "$char" ) );
+ }
+
+ /**
* Make sure the router handles characters like +&() properly
*/
public function testCharacters() {
- $matches = $this->basicRouter->parse( "/wiki/Plus+And&Stuff()"
);
- $this->assertEquals( $matches, array( 'title' =>
"Plus+And&Stuff()" ) );
+ $matches = $this->basicRouter->parse(
"/wiki/Plus+And&Dollar\\Stuff();[]{}*" );
+ $this->assertEquals( $matches, array( 'title' =>
"Plus+And&Dollar\\Stuff();[]{}*" ) );
}
/**
Property changes on:
branches/wmf/1.19wmf1/tests/phpunit/includes/PathRouterTest.php
___________________________________________________________________
Added: svn:mergeinfo
+ /branches/JSTesting/tests/phpunit/includes/PathRouterTest.php:100352-107913
/branches/REL1_15/phase3/tests/phpunit/includes/PathRouterTest.php:51646
/branches/REL1_17/phase3/tests/phpunit/includes/PathRouterTest.php:81445,81448
/branches/new-installer/phase3/tests/phpunit/includes/PathRouterTest.php:43664-66004
/branches/sqlite/tests/phpunit/includes/PathRouterTest.php:58211-58321
/branches/wmf/1.18wmf1/tests/phpunit/includes/PathRouterTest.php:97508
/trunk/phase3/tests/phpunit/includes/PathRouterTest.php:111002,111029,111034,111062,111067,111076,111085,111128,111144,111251,111397,111571,111574,111597,112313
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs