70 private $patterns = [];
82 protected function doAdd(
$path, $params, $options, $key =
null ) {
84 if (
$path[0] !==
'/' ) {
88 if ( !isset( $options[
'strict'] ) || !$options[
'strict'] ) {
90 if ( !str_contains(
$path,
'$1' ) ) {
91 if (
$path[-1] !==
'/' ) {
100 if ( !isset( $params[
'title'] ) && str_contains(
$path,
'$1' ) ) {
101 $params[
'title'] =
'$1';
104 if ( isset( $params[
'title'] ) && $params[
'title'] ===
false ) {
105 unset( $params[
'title'] );
110 foreach ( $params as $paramName => $paramData ) {
111 if ( is_string( $paramData ) ) {
112 if ( preg_match(
'/\$(\d+|key)/u', $paramData ) ) {
113 $paramArrKey =
'pattern';
117 $paramArrKey =
'value';
119 $params[$paramName] = [
120 $paramArrKey => $paramData
127 foreach ( $options as $optionName => $optionData ) {
128 if ( preg_match(
'/^\$\d+$/u', $optionName ) && !is_array( $optionData ) ) {
129 $options[$optionName] = [ $optionData ];
136 'options' => $options,
140 $this->patterns[] = $pattern;
150 public function add(
$path, $params = [], $options = [] ) {
151 if ( is_array(
$path ) ) {
152 foreach (
$path as $key => $onePath ) {
153 $this->
doAdd( $onePath, $params, $options, $key );
168 if (
$path && !preg_match(
'/^(https?:\/\/|\/)/',
$path ) ) {
171 "If you use a relative URL for \$$varName, it must start " .
172 'with a slash (<code>/</code>).<br><br>See ' .
173 "<a href=\"https://www.mediawiki.org/wiki/Manual:\$$varName\">" .
174 "https://www.mediawiki.org/wiki/Manual:\$$varName</a>."
187 $options[
'strict'] =
true;
188 $this->
add(
$path, $params, $options );
197 foreach ( $this->patterns as $key => $pattern ) {
198 $weights[$key] = $pattern->weight;
200 array_multisort( $weights, SORT_DESC, SORT_NUMERIC, $this->patterns );
208 # Start with a weight of 0
212 $path = explode(
'/', $pattern->path );
214 # For each level of the path
215 foreach (
$path as $piece ) {
216 if ( preg_match(
'/^\$(\d+|key)$/u', $piece ) ) {
217 # For a piece that is only a $1 variable add 1 points of weight
219 } elseif ( preg_match(
'/\$(\d+|key)/u', $piece ) ) {
220 # For a piece that simply contains a $1 variable add 2 points of weight
223 # For a solid piece add a full 3 points of weight
228 foreach ( $pattern->options as $key => $option ) {
229 if ( preg_match(
'/^\$\d+$/u', $key ) ) {
230 # Add 0.5 for restrictions to values
231 # This way given two separate "/$2/$1" patterns the
232 # one with a limited set of $2 values will dominate
233 # the one that'll match more loosely
276 foreach ( $this->patterns as $pattern ) {
292 $regexp = preg_quote( $pattern->path,
'#' );
294 $regexp = preg_replace(
'#\\\\\$1#u',
'(?P<par1>.*)', $regexp );
296 $regexp = preg_replace(
'#\\\\\$(\d+)#u',
'(?P<par$1>.+?)', $regexp );
297 $regexp =
"#^{$regexp}$#";
303 if ( preg_match( $regexp,
$path, $m ) ) {
306 foreach ( $pattern->options as $key => $option ) {
307 if ( preg_match(
'/^\$\d+$/u', $key ) ) {
308 $n = intval( substr( $key, 1 ) );
309 $value = rawurldecode( $m[
"par{$n}"] );
310 if ( !in_array( $value, $option ) ) {
319 foreach ( $m as $matchKey => $matchValue ) {
320 if ( preg_match(
'/^par\d+$/u', $matchKey ) ) {
321 $n = intval( substr( $matchKey, 3 ) );
322 $data[
'$' . $n] = rawurldecode( $matchValue );
326 if ( isset( $pattern->key ) ) {
327 $data[
'$key'] = $pattern->key;
331 foreach ( $pattern->params as $paramName => $paramData ) {
335 if ( preg_match(
'/^data:/u', $paramName ) ) {
337 $key = substr( $paramName, 5 );
343 if ( isset( $paramData[
'value'] ) ) {
345 $value = $paramData[
'value'];
346 } elseif ( isset( $paramData[
'pattern'] ) ) {
349 $paramData[
'pattern'] );
350 if ( $value ===
false ) {
358 $data[$key] = $value;
365 if ( isset( $pattern->options[
'callback'] ) ) {
366 $pattern->options[
'callback'](
$matches, $data );
387 $replacer =
static function ( $m ) use ( $pathMatches, $key, &$error ) {
388 if ( $m[1] ==
"key" ) {
389 if ( $key ===
null ) {
398 if ( !isset( $pathMatches[
"par$d"] ) ) {
404 return rawurldecode( $pathMatches[
"par$d"] );
408 $value = preg_replace_callback(
'/\$(\d+|key)/u', $replacer, $value );
423 if ( !$actionPaths ) {
428 if ( !isset( $actionPaths[
'view'] ) ) {
429 $actionPaths[
'view'] = $articlePath;