[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/includes/ -> PathRouter.php (summary)

Parser to extract query parameters out of REQUEST_URI paths. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

File Size: 399 lines (13 kb)
Included or required:0 times
Referenced: 2 times
Includes or requires: 0 files

Defines 2 classes

PathRouter:: (7 methods):
  doAdd()
  add()
  addStrict()
  sortByWeight()
  makeWeight()
  parse()
  extractTitle()

PathRouterPatternReplacer:: (2 methods):
  replace()
  callback()


Class: PathRouter  - X-Ref

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 use `array( '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.

doAdd( $path, $params, $options, $key = null )   X-Ref
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.

param: string $path
param: array $params
param: array $options
param: null|string $key

add( $path, $params = array()   X-Ref
Add a new path pattern to the path router

param: string|array $path The path pattern to add
param: array $params The params for this path pattern
param: array $options The options for this path pattern

addStrict( $path, $params = array()   X-Ref
Add a new path pattern to the path router with the strict option on

param: string|array $path
param: array $params
param: array $options

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


makeWeight( $pattern )   X-Ref

param: object $pattern
return: float|int

parse( $path )   X-Ref
Parse a path and return the query matches for the path

param: string $path The path to parse
return: array The array of matches for the path

extractTitle( $path, $pattern )   X-Ref

param: string $path
param: string $pattern
return: array|null

Class: PathRouterPatternReplacer  - X-Ref

replace( $value )   X-Ref
Replace keys inside path router patterns with text.
We do this inside of a replacement callback because after replacement we can't tell the
difference between a $1 that was not replaced and a $1 that was part of
the content a $1 was replaced with.

param: string $value
return: string

callback( $m )   X-Ref

param: array $m
return: string



Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1