84 private $patterns = [];
98 if (
$path[0] !==
'/' ) {
102 if ( !isset( $options[
'strict'] ) || !$options[
'strict'] ) {
104 if ( strpos(
$path,
'$1' ) ===
false ) {
105 if (
$path[-1] !==
'/' ) {
114 if ( !isset(
$params[
'title'] ) && strpos(
$path,
'$1' ) !==
false ) {
124 foreach (
$params as $paramName => $paramData ) {
125 if ( is_string( $paramData ) ) {
126 if ( preg_match(
'/\$(\d+|key)/u', $paramData ) ) {
127 $paramArrKey =
'pattern';
131 $paramArrKey =
'value';
134 $paramArrKey => $paramData
141 foreach ( $options as $optionName => $optionData ) {
142 if ( preg_match(
'/^\$\d+$/u', $optionName ) && !is_array( $optionData ) ) {
143 $options[$optionName] = [ $optionData ];
150 'options' => $options,
154 $this->patterns[] = $pattern;
165 if ( is_array(
$path ) ) {
166 foreach (
$path as $key => $onePath ) {
182 if (
$path && !preg_match(
'/^(https?:\/\/|\/)/',
$path ) ) {
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>."
201 $options[
'strict'] =
true;
211 foreach ( $this->patterns as $key => $pattern ) {
212 $weights[$key] = $pattern->weight;
214 array_multisort( $weights, SORT_DESC, SORT_NUMERIC, $this->patterns );
222 # Start with a weight of 0
226 $path = explode(
'/', $pattern->path );
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
233 } elseif ( preg_match(
'/\$(\d+|key)/u', $piece ) ) {
234 # For a piece that simply contains a $1 variable add 2 points of weight
237 # For a solid piece add a full 3 points of weight
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
290 foreach ( $this->patterns as $pattern ) {
306 $regexp = preg_quote( $pattern->path,
'#' );
308 $regexp = preg_replace(
'#\\\\\$1#u',
'(?P<par1>.*)', $regexp );
310 $regexp = preg_replace(
'#\\\\\$(\d+)#u',
'(?P<par$1>.+?)', $regexp );
311 $regexp =
"#^{$regexp}$#";
317 if ( preg_match( $regexp,
$path, $m ) ) {
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 ) ) {
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 );
340 if ( isset( $pattern->key ) ) {
341 $data[
'$key'] = $pattern->key;
345 foreach ( $pattern->params as $paramName => $paramData ) {
349 if ( preg_match(
'/^data:/u', $paramName ) ) {
351 $key = substr( $paramName, 5 );
357 if ( isset( $paramData[
'value'] ) ) {
359 $value = $paramData[
'value'];
360 } elseif ( isset( $paramData[
'pattern'] ) ) {
363 $paramData[
'pattern'] );
364 if ( $value ===
false ) {
372 $data[$key] = $value;
379 if ( isset( $pattern->options[
'callback'] ) ) {
380 call_user_func_array( $pattern->options[
'callback'], [ &
$matches, $data ] );
401 $replacer =
static function ( $m ) use ( $pathMatches, $key, &$error ) {
402 if ( $m[1] ==
"key" ) {
403 if ( $key ===
null ) {
412 if ( !isset( $pathMatches[
"par$d"] ) ) {
418 return rawurldecode( $pathMatches[
"par$d"] );
422 $value = preg_replace_callback(
'/\$(\d+|key)/u', $replacer, $value );
437 if ( !$actionPaths ) {
442 if ( !isset( $actionPaths[
'view'] ) ) {
443 $actionPaths[
'view'] = $articlePath;
450class_alias( PathRouter::class,
'PathRouter' );
array $params
The job parameters.
Abort the web request with a custom HTML string that will represent the entire response.