Debian Codegraph @

includes/Request/PathRouter.php

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