83 private $patterns = [];
95 protected function doAdd(
$path, $params, $options, $key =
null ) {
97 if (
$path[0] !==
'/' ) {
101 if ( !isset( $options[
'strict'] ) || !$options[
'strict'] ) {
103 if ( strpos(
$path,
'$1' ) ===
false ) {
104 if ( substr(
$path, -1 ) !==
'/' ) {
113 if ( !isset( $params[
'title'] ) && strpos(
$path,
'$1' ) !==
false ) {
114 $params[
'title'] =
'$1';
117 if ( isset( $params[
'title'] ) && $params[
'title'] ===
false ) {
118 unset( $params[
'title'] );
123 foreach ( $params as $paramName => $paramData ) {
124 if ( is_string( $paramData ) ) {
125 if ( preg_match(
'/\$(\d+|key)/u', $paramData ) ) {
126 $paramArrKey =
'pattern';
130 $paramArrKey =
'value';
132 $params[$paramName] = [
133 $paramArrKey => $paramData
140 foreach ( $options as $optionName => $optionData ) {
141 if ( preg_match(
'/^\$\d+$/u', $optionName ) && !is_array( $optionData ) ) {
142 $options[$optionName] = [ $optionData ];
149 'options' => $options,
153 $this->patterns[] = $pattern;
163 public function add(
$path, $params = [], $options = [] ) {
164 if ( is_array(
$path ) ) {
165 foreach (
$path as $key => $onePath ) {
166 $this->
doAdd( $onePath, $params, $options, $key );
181 if (
$path && !preg_match(
'/^(https?:\/\/|\/)/',
$path ) ) {
184 "If you use a relative URL for \$$varName, it must start " .
185 'with a slash (<code>/</code>).<br><br>See ' .
186 "<a href=\"https://www.mediawiki.org/wiki/Manual:\$$varName\">" .
187 "https://www.mediawiki.org/wiki/Manual:\$$varName</a>."
200 $options[
'strict'] =
true;
201 $this->
add(
$path, $params, $options );
210 foreach ( $this->patterns as $key => $pattern ) {
211 $weights[$key] = $pattern->weight;
213 array_multisort( $weights, SORT_DESC, SORT_NUMERIC, $this->patterns );
221 # Start with a weight of 0
225 $path = explode(
'/', $pattern->path );
227 # For each level of the path
228 foreach (
$path as $piece ) {
229 if ( preg_match(
'/^\$(\d+|key)$/u', $piece ) ) {
230 # For a piece that is only a $1 variable add 1 points of weight
232 } elseif ( preg_match(
'/\$(\d+|key)/u', $piece ) ) {
233 # For a piece that simply contains a $1 variable add 2 points of weight
236 # For a solid piece add a full 3 points of weight
241 foreach ( $pattern->options as $key => $option ) {
242 if ( preg_match(
'/^\$\d+$/u', $key ) ) {
243 # Add 0.5 for restrictions to values
244 # This way given two separate "/$2/$1" patterns the
245 # one with a limited set of $2 values will dominate
246 # the one that'll match more loosely
289 foreach ( $this->patterns as $pattern ) {
305 $regexp = preg_quote( $pattern->path,
'#' );
307 $regexp = preg_replace(
'#\\\\\$1#u',
'(?P<par1>.*)', $regexp );
309 $regexp = preg_replace(
'#\\\\\$(\d+)#u',
'(?P<par$1>.+?)', $regexp );
310 $regexp =
"#^{$regexp}$#";
316 if ( preg_match( $regexp,
$path, $m ) ) {
319 foreach ( $pattern->options as $key => $option ) {
320 if ( preg_match(
'/^\$\d+$/u', $key ) ) {
321 $n = intval( substr( $key, 1 ) );
322 $value = rawurldecode( $m[
"par{$n}"] );
323 if ( !in_array( $value, $option ) ) {
332 foreach ( $m as $matchKey => $matchValue ) {
333 if ( preg_match(
'/^par\d+$/u', $matchKey ) ) {
334 $n = intval( substr( $matchKey, 3 ) );
335 $data[
'$' . $n] = rawurldecode( $matchValue );
339 if ( isset( $pattern->key ) ) {
340 $data[
'$key'] = $pattern->key;
344 foreach ( $pattern->params as $paramName => $paramData ) {
348 if ( preg_match(
'/^data:/u', $paramName ) ) {
350 $key = substr( $paramName, 5 );
356 if ( isset( $paramData[
'value'] ) ) {
358 $value = $paramData[
'value'];
359 } elseif ( isset( $paramData[
'pattern'] ) ) {
362 $paramData[
'pattern'] );
363 if ( $value ===
false ) {
371 $data[$key] = $value;
378 if ( isset( $pattern->options[
'callback'] ) ) {
379 call_user_func_array( $pattern->options[
'callback'], [ &
$matches, $data ] );
400 $replacer =
static function ( $m ) use ( $pathMatches, $key, &$error ) {
401 if ( $m[1] ==
"key" ) {
402 if ( $key ===
null ) {
411 if ( !isset( $pathMatches[
"par$d"] ) ) {
417 return rawurldecode( $pathMatches[
"par$d"] );
421 $value = preg_replace_callback(
'/\$(\d+|key)/u', $replacer, $value );
436 if ( !$actionPaths ) {
441 if ( !isset( $actionPaths[
'view'] ) ) {
442 $actionPaths[
'view'] = $articlePath;
448 class_alias( PathRouter::class,
'PathRouter' );
wfRemoveDotSegments( $urlPath)
Remove all dot-segments in the provided URL path.
Abort the web request with a custom HTML string that will represent the entire response.