1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
|
<?php
/**
* Parser to extract query parameters out of REQUEST_URI paths.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
namespace MediaWiki\Request;
use FatalError;
use MediaWiki\Utils\UrlUtils;
use stdClass;
/**
* MediaWiki\Request\PathRouter class.
* This class can take patterns such as /wiki/$1 and use them to
* parse query parameters out of REQUEST_URI paths.
*
* $router->add( "/wiki/$1" );
* - Matches /wiki/Foo style urls and extracts the title
* $router->add( [ 'edit' => "/edit/$key" ], [ 'action' => '$key' ] );
* - Matches /edit/Foo style urls and sets action=edit
* $router->add( '/$2/$1',
* [ 'variant' => '$2' ],
* [ '$2' => [ 'zh-hant', 'zh-hans' ] ]
* );
* - Matches /zh-hant/Foo or /zh-hans/Foo
* $router->addStrict( "/foo/Bar", [ 'title' => 'Baz' ] );
* - Matches /foo/Bar explicitly and uses "Baz" as the title
* $router->add( '/help/$1', [ 'title' => 'Help:$1' ] );
* - Matches /help/Foo with "Help:Foo" as the title
* $router->add( '/$1', [ 'foo' => [ 'value' => 'bar$2' ] ] );
* - Matches /Foo and sets 'foo' to 'bar$2' without $2 being replaced
* $router->add( '/$1', [ 'data:foo' => 'bar' ], [ 'callback' => 'functionname' ] );
* - Matches /Foo, adds the key 'foo' with the value 'bar' to the data array
* and calls functionname( &$matches, $data );
*
* Path patterns:
* - Paths may contain $# patterns such as $1, $2, etc...
* - $1 will match 0 or more while the rest will match 1 or more
* - Unless you use addStrict "/wiki" and "/wiki/" will be expanded to "/wiki/$1"
*
* Params:
* - In a pattern $1, $2, etc... will be replaced with the relevant contents
* - If you used a keyed array as a path pattern, $key will be replaced with
* the relevant contents
* - The default behavior is equivalent to `[ 'title' => '$1' ]`,
* if you don't want the title parameter you can explicitly use `[ 'title' => false ]`
* - You can specify a value that won't have replacements in it
* using `'foo' => [ 'value' => 'bar' ];`
*
* Options:
* - The option keys $1, $2, etc... can be specified to restrict the possible values
* of that variable. A string can be used for a single value, or an array for multiple.
* - When the option key 'strict' is set (Using addStrict is simpler than doing this directly)
* the path won't have $1 implicitly added to it.
* - The option key 'callback' can specify a callback that will be run when a path is matched.
* The callback will have the arguments ( &$matches, $data ) and the matches array can
* be modified.
*
* @since 1.19
* @author Daniel Friesen
*/
class PathRouter {
/**
* @var stdClass[]
*/
private $patterns = [];
/**
* Protected helper to do the actual bulk work of adding a single pattern.
* This is in a separate method so that add() can handle the difference between
* a single string $path and an array $path that contains multiple path
* patterns each with an associated $key to pass on.
* @param string $path
* @param array $params
* @param array $options
* @param null|string $key
*/
protected function doAdd( $path, $params, $options, $key = null ) {
// Make sure all paths start with a /
if ( $path[0] !== '/' ) {
$path = '/' . $path;
}
if ( !isset( $options['strict'] ) || !$options['strict'] ) {
// Unless this is a strict path make sure that the path has a $1
if ( strpos( $path, '$1' ) === false ) {
if ( $path[-1] !== '/' ) {
$path .= '/';
}
$path .= '$1';
}
}
// If 'title' is not specified and our path pattern contains a $1
// Add a default 'title' => '$1' rule to the parameters.
if ( !isset( $params['title'] ) && strpos( $path, '$1' ) !== false ) {
$params['title'] = '$1';
}
// If the user explicitly marked 'title' as false then omit it from the matches
if ( isset( $params['title'] ) && $params['title'] === false ) {
unset( $params['title'] );
}
// Loop over our parameters and convert basic key => string
// patterns into fully descriptive array form
foreach ( $params as $paramName => $paramData ) {
if ( is_string( $paramData ) ) {
if ( preg_match( '/\$(\d+|key)/u', $paramData ) ) {
$paramArrKey = 'pattern';
} else {
// If there's no replacement use a value instead
// of a pattern for a little more efficiency
$paramArrKey = 'value';
}
$params[$paramName] = [
$paramArrKey => $paramData
];
}
}
// Loop over our options and convert any single value $# restrictions
// into an array so we only have to do in_array tests.
foreach ( $options as $optionName => $optionData ) {
if ( preg_match( '/^\$\d+$/u', $optionName ) && !is_array( $optionData ) ) {
$options[$optionName] = [ $optionData ];
}
}
$pattern = (object)[
'path' => $path,
'params' => $params,
'options' => $options,
'key' => $key,
];
$pattern->weight = self::makeWeight( $pattern );
$this->patterns[] = $pattern;
}
/**
* Add a new path pattern to the path router
*
* @param string|array $path The path pattern to add
* @param array $params The params for this path pattern
* @param array $options The options for this path pattern
*/
public function add( $path, $params = [], $options = [] ) {
if ( is_array( $path ) ) {
foreach ( $path as $key => $onePath ) {
$this->doAdd( $onePath, $params, $options, $key );
}
} else {
$this->doAdd( $path, $params, $options );
}
}
/**
* @param string $path To be given to add()
* @param string $varName Full name of configuration variable for use
* in error message and url to mediawiki.org Manual (e.g. "wgExample").
* @throws FatalError If path is invalid
* @internal For use by WebRequest::getPathInfo
*/
public function validateRoute( $path, $varName ) {
if ( $path && !preg_match( '/^(https?:\/\/|\/)/', $path ) ) {
// T48998: Bail out early if path is non-absolute
throw new FatalError(
"If you use a relative URL for \$$varName, it must start " .
'with a slash (<code>/</code>).<br><br>See ' .
"<a href=\"https://www.mediawiki.org/wiki/Manual:\$$varName\">" .
"https://www.mediawiki.org/wiki/Manual:\$$varName</a>."
);
}
}
/**
* Add a new path pattern to the path router with the strict option on
* @param string|array $path
* @param array $params
* @param array $options
* @see self::add
*/
public function addStrict( $path, $params = [], $options = [] ) {
$options['strict'] = true;
$this->add( $path, $params, $options );
}
/**
* Protected helper to re-sort our patterns so that the most specific
* (most heavily weighted) patterns are at the start of the array.
*/
protected function sortByWeight() {
$weights = [];
foreach ( $this->patterns as $key => $pattern ) {
$weights[$key] = $pattern->weight;
}
array_multisort( $weights, SORT_DESC, SORT_NUMERIC, $this->patterns );
}
/**
* @param stdClass $pattern
* @return float|int
*/
protected static function makeWeight( $pattern ) {
# Start with a weight of 0
$weight = 0;
// Explode the path to work with
$path = explode( '/', $pattern->path );
# For each level of the path
foreach ( $path as $piece ) {
if ( preg_match( '/^\$(\d+|key)$/u', $piece ) ) {
# For a piece that is only a $1 variable add 1 points of weight
$weight += 1;
} elseif ( preg_match( '/\$(\d+|key)/u', $piece ) ) {
# For a piece that simply contains a $1 variable add 2 points of weight
$weight += 2;
} else {
# For a solid piece add a full 3 points of weight
$weight += 3;
}
}
foreach ( $pattern->options as $key => $option ) {
if ( preg_match( '/^\$\d+$/u', $key ) ) {
# Add 0.5 for restrictions to values
# This way given two separate "/$2/$1" patterns the
# one with a limited set of $2 values will dominate
# the one that'll match more loosely
$weight += 0.5;
}
}
return $weight;
}
/**
* Parse a path and return the query matches for the path
*
* @param string $path The path to parse
* @return array The array of matches for the path
*/
public function parse( $path ) {
// Make sure our patterns are sorted by weight so the most specific
// matches are tested first
$this->sortByWeight();
$matches = $this->internalParse( $path );
if ( $matches === null ) {
// Try with the normalized path (T100782)
$path = UrlUtils::removeDotSegments( $path );
$path = preg_replace( '#/+#', '/', $path );
$matches = $this->internalParse( $path );
}
// We know the difference between null (no matches) and
// [] (a match with no data) but our WebRequest caller
// expects [] even when we have no matches so return
// a [] when we have null
return $matches ?? [];
}
/**
* Match a path against each defined pattern
*
* @param string $path
* @return array|null
*/
protected function internalParse( $path ) {
$matches = null;
foreach ( $this->patterns as $pattern ) {
$matches = self::extractTitle( $path, $pattern );
if ( $matches !== null ) {
break;
}
}
return $matches;
}
/**
* @param string $path
* @param stdClass $pattern
* @return array|null
*/
protected static function extractTitle( $path, $pattern ) {
// Convert the path pattern into a regexp we can match with
$regexp = preg_quote( $pattern->path, '#' );
// .* for the $1
$regexp = preg_replace( '#\\\\\$1#u', '(?P<par1>.*)', $regexp );
// .+ for the rest of the parameter numbers
$regexp = preg_replace( '#\\\\\$(\d+)#u', '(?P<par$1>.+?)', $regexp );
$regexp = "#^{$regexp}$#";
$matches = [];
$data = [];
// Try to match the path we were asked to parse with our regexp
if ( preg_match( $regexp, $path, $m ) ) {
// Ensure that any $# restriction we have set in our {$option}s
// matches properly here.
foreach ( $pattern->options as $key => $option ) {
if ( preg_match( '/^\$\d+$/u', $key ) ) {
$n = intval( substr( $key, 1 ) );
$value = rawurldecode( $m["par{$n}"] );
if ( !in_array( $value, $option ) ) {
// If any restriction does not match return null
// to signify that this rule did not match.
return null;
}
}
}
// Give our $data array a copy of every $# that was matched
foreach ( $m as $matchKey => $matchValue ) {
if ( preg_match( '/^par\d+$/u', $matchKey ) ) {
$n = intval( substr( $matchKey, 3 ) );
$data['$' . $n] = rawurldecode( $matchValue );
}
}
// If present give our $data array a $key as well
if ( isset( $pattern->key ) ) {
$data['$key'] = $pattern->key;
}
// Go through our parameters for this match and add data to our matches and data arrays
foreach ( $pattern->params as $paramName => $paramData ) {
$value = null;
// Differentiate data: from normal parameters and keep the correct
// array key around (ie: foo for data:foo)
if ( preg_match( '/^data:/u', $paramName ) ) {
$isData = true;
$key = substr( $paramName, 5 );
} else {
$isData = false;
$key = $paramName;
}
if ( isset( $paramData['value'] ) ) {
// For basic values just set the raw data as the value
$value = $paramData['value'];
} elseif ( isset( $paramData['pattern'] ) ) {
// For patterns we have to make value replacements on the string
$value = self::expandParamValue( $m, $pattern->key ?? null,
$paramData['pattern'] );
if ( $value === false ) {
// Pattern required data that wasn't available, abort
return null;
}
}
// Send things that start with data: to $data, the rest to $matches
if ( $isData ) {
$data[$key] = $value;
} else {
$matches[$key] = $value;
}
}
// If this match includes a callback, execute it
if ( isset( $pattern->options['callback'] ) ) {
call_user_func_array( $pattern->options['callback'], [ &$matches, $data ] );
}
} else {
// Our regexp didn't match, return null to signify no match.
return null;
}
// Fall through, everything went ok, return our matches array
return $matches;
}
/**
* Replace $key etc. in param values with the matched strings from the path.
*
* @param array $pathMatches The match results from the path
* @param string|null $key The key of the matching pattern
* @param string $value The param value to be expanded
* @return string|false
*/
protected static function expandParamValue( $pathMatches, $key, $value ) {
$error = false;
$replacer = static function ( $m ) use ( $pathMatches, $key, &$error ) {
if ( $m[1] == "key" ) {
if ( $key === null ) {
$error = true;
return '';
}
return $key;
} else {
$d = $m[1];
if ( !isset( $pathMatches["par$d"] ) ) {
$error = true;
return '';
}
return rawurldecode( $pathMatches["par$d"] );
}
};
$value = preg_replace_callback( '/\$(\d+|key)/u', $replacer, $value );
if ( $error ) {
return false;
}
return $value;
}
/**
* @param array $actionPaths
* @param string $articlePath
* @return string[]|false
* @internal For use by Title and WebRequest only.
*/
public static function getActionPaths( array $actionPaths, $articlePath ) {
if ( !$actionPaths ) {
return false;
}
// Processing of urls for this feature requires that 'view' is set.
// By default, set it to the pretty article path.
if ( !isset( $actionPaths['view'] ) ) {
$actionPaths['view'] = $articlePath;
}
return $actionPaths;
}
}
/** @deprecated class alias since 1.40 */
class_alias( PathRouter::class, 'PathRouter' );
|