MediaWiki  1.23.2
PathRouter Class Reference

PathRouter class. More...

Public Member Functions

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

Protected Member Functions

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

Static Protected Member Functions

static extractTitle ( $path, $pattern)
 
static makeWeight ( $pattern)
 

Private Attributes

array $patterns = array()
 

Detailed Description

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( array( 'edit' => "/edit/$key" ), array( 'action' => '$key' ) );
  • Matches /edit/Foo style urls and sets action=edit $router->add( '/$2/$1', array( 'variant' => '$2' ), array( '$2' => array( 'zh-hant', 'zh-hans' ) );
  • Matches /zh-hant/Foo or /zh-hans/Foo $router->addStrict( "/foo/Bar", array( 'title' => 'Baz' ) );
  • Matches /foo/Bar explicitly and uses "Baz" as the title $router->add( '/help/$1', array( 'title' => 'Help:$1' ) );
  • Matches /help/Foo with "Help:Foo" as the title $router->add( '/$1', array( 'foo' => array( 'value' => 'bar$2' ) );
  • Matches /Foo and sets 'foo' to 'bar$2' without $2 being replaced $router->add( '/$1', array( 'data:foo' => 'bar' ), array( '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 ‘array( 'title’ => '$1' ), if you don't want the title parameter you can explicitly usearray( 'title' => false )`
  • You can specify a value that won't have replacements in it using ‘'foo’ => array( '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 73 of file PathRouter.php.

Member Function Documentation

◆ add()

PathRouter::add (   $path,
  $params = array(),
  $options = array() 
)

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 159 of file PathRouter.php.

References $options, $params, $path, as, and doAdd().

Referenced by addStrict(), WebRequest\getPathInfo(), PathRouterTest\setUp(), PathRouterTest\testAdditionalParameter(), PathRouterTest\testCallback(), PathRouterTest\testFail(), PathRouterTest\testKeyParameter(), PathRouterTest\testLoose(), PathRouterTest\testOrder(), PathRouterTest\testPatternUrlencoding(), PathRouterTest\testRawParamValue(), and PathRouterTest\testRestrictedValue().

◆ addStrict()

PathRouter::addStrict (   $path,
  $params = array(),
  $options = array() 
)

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

See also
self::add
Parameters
$pathstring|array
$paramsarray
$optionsarray

Definition at line 176 of file PathRouter.php.

References $options, $params, $path, and add().

Referenced by PathRouterTest\testWeight().

◆ doAdd()

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
$pathstring
$paramsarray
$optionsarray
$keynull|string

Definition at line 89 of file PathRouter.php.

References $options, $params, $path, array(), as, makeWeight(), and object.

Referenced by add().

◆ extractTitle()

static PathRouter::extractTitle (   $path,
  $pattern 
)
staticprotected
Parameters
$pathstring
$patternstring
Returns
array|null

Definition at line 263 of file PathRouter.php.

References $matches, $n, $path, $regexp, $value, array(), and as.

Referenced by parse().

◆ makeWeight()

static PathRouter::makeWeight (   $pattern)
staticprotected
Parameters
$patternobject
Returns
float|int

Definition at line 197 of file PathRouter.php.

References $path, and as.

Referenced by doAdd().

◆ parse()

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 237 of file PathRouter.php.

References $matches, $path, array(), as, extractTitle(), and sortByWeight().

◆ sortByWeight()

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 185 of file PathRouter.php.

References array(), and as.

Referenced by parse().

Member Data Documentation

◆ $patterns

array PathRouter::$patterns = array()
private

Definition at line 77 of file PathRouter.php.


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