MediaWiki master
MediaWiki\Request\PathRouter Class Reference

MediaWiki\Request\PathRouter class. More...

Public Member Functions

 add ( $path, $params=[], $options=[])
 Add a new path pattern to the path router.
 
 addStrict ( $path, $params=[], $options=[])
 Add a new path pattern to the path router with the strict option on.
 
 parse ( $path)
 Parse a path and return the query matches for the path.
 
 validateRoute ( $path, $varName)
 

Static Public Member Functions

static getActionPaths (array $actionPaths, $articlePath)
 

Protected Member Functions

 doAdd ( $path, $params, $options, $key=null)
 Protected helper to do the actual bulk work of adding a single pattern.
 
 internalParse ( $path)
 Match a path against each defined pattern.
 
 sortByWeight ()
 Protected helper to re-sort our patterns so that the most specific (most heavily weighted) patterns are at the start of the array.
 

Static Protected Member Functions

static expandParamValue ( $pathMatches, $key, $value)
 Replace $key etc.
 
static extractTitle ( $path, $pattern)
 
static makeWeight ( $pattern)
 

Detailed Description

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

Definition at line 78 of file PathRouter.php.

Member Function Documentation

◆ add()

MediaWiki\Request\PathRouter::add ( $path,
$params = [],
$options = [] )

Add a new path pattern to the path router.

Parameters
string | array$pathThe path pattern to add
array$paramsThe params for this path pattern
array$optionsThe options for this path pattern

Definition at line 163 of file PathRouter.php.

References $params, $path, and MediaWiki\Request\PathRouter\doAdd().

Referenced by MediaWiki\Request\PathRouter\addStrict(), and MediaWiki\Request\WebRequest\getPathInfo().

◆ addStrict()

MediaWiki\Request\PathRouter::addStrict ( $path,
$params = [],
$options = [] )

Add a new path pattern to the path router with the strict option on.

Parameters
string | array$path
array$params
array$options
See also
self::add

Definition at line 199 of file PathRouter.php.

References $params, $path, and MediaWiki\Request\PathRouter\add().

◆ doAdd()

MediaWiki\Request\PathRouter::doAdd ( $path,
$params,
$options,
$key = null )
protected

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.

Parameters
string$path
array$params
array$options
null | string$key

Definition at line 95 of file PathRouter.php.

References $params, $path, and MediaWiki\Request\PathRouter\makeWeight().

Referenced by MediaWiki\Request\PathRouter\add().

◆ expandParamValue()

static MediaWiki\Request\PathRouter::expandParamValue ( $pathMatches,
$key,
$value )
staticprotected

Replace $key etc.

in param values with the matched strings from the path.

Parameters
array$pathMatchesThe match results from the path
string | null$keyThe key of the matching pattern
string$valueThe param value to be expanded
Returns
string|false

Definition at line 397 of file PathRouter.php.

Referenced by MediaWiki\Request\PathRouter\extractTitle().

◆ extractTitle()

static MediaWiki\Request\PathRouter::extractTitle ( $path,
$pattern )
staticprotected
Parameters
string$path
stdClass$pattern
Returns
array|null

Definition at line 303 of file PathRouter.php.

References $matches, $path, and MediaWiki\Request\PathRouter\expandParamValue().

Referenced by MediaWiki\Request\PathRouter\internalParse().

◆ getActionPaths()

static MediaWiki\Request\PathRouter::getActionPaths ( array $actionPaths,
$articlePath )
static
Parameters
array$actionPaths
string$articlePath
Returns
string[]|false
Access: internal
For use by Title and WebRequest only.

Definition at line 435 of file PathRouter.php.

Referenced by MediaWiki\Request\WebRequest\getPathInfo().

◆ internalParse()

MediaWiki\Request\PathRouter::internalParse ( $path)
protected

Match a path against each defined pattern.

Parameters
string$path
Returns
array|null

Definition at line 286 of file PathRouter.php.

References $matches, $path, and MediaWiki\Request\PathRouter\extractTitle().

Referenced by MediaWiki\Request\PathRouter\parse().

◆ makeWeight()

static MediaWiki\Request\PathRouter::makeWeight ( $pattern)
staticprotected
Parameters
stdClass$pattern
Returns
float|int

Definition at line 220 of file PathRouter.php.

References $path.

Referenced by MediaWiki\Request\PathRouter\doAdd().

◆ parse()

MediaWiki\Request\PathRouter::parse ( $path)

Parse a path and return the query matches for the path.

Parameters
string$pathThe path to parse
Returns
array The array of matches for the path

Definition at line 260 of file PathRouter.php.

References $matches, $path, MediaWiki\Request\PathRouter\internalParse(), MediaWiki\Request\PathRouter\sortByWeight(), and wfRemoveDotSegments().

◆ sortByWeight()

MediaWiki\Request\PathRouter::sortByWeight ( )
protected

Protected helper to re-sort our patterns so that the most specific (most heavily weighted) patterns are at the start of the array.

Definition at line 208 of file PathRouter.php.

Referenced by MediaWiki\Request\PathRouter\parse().

◆ validateRoute()

MediaWiki\Request\PathRouter::validateRoute ( $path,
$varName )
Parameters
string$pathTo be given to add()
string$varNameFull name of configuration variable for use in error message and url to mediawiki.org Manual (e.g. "wgExample").
Exceptions
FatalErrorIf path is invalid
Access: internal
For use by WebRequest::getPathInfo

Definition at line 180 of file PathRouter.php.

References $path.


The documentation for this class was generated from the following file: