Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 28 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| RedirectSpecialPage | |
0.00% |
0 / 27 |
|
0.00% |
0 / 4 |
132 | |
0.00% |
0 / 1 |
| execute | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
| getRedirect | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| getRedirectQuery | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
42 | |||
| personallyIdentifiableTarget | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| showNoRedirectPage | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | */ |
| 6 | |
| 7 | namespace MediaWiki\SpecialPage; |
| 8 | |
| 9 | use LogicException; |
| 10 | use MediaWiki\Title\Title; |
| 11 | |
| 12 | /** |
| 13 | * Shortcut to construct a special page alias. |
| 14 | * |
| 15 | * @stable to extend |
| 16 | * |
| 17 | * @ingroup SpecialPage |
| 18 | */ |
| 19 | abstract class RedirectSpecialPage extends UnlistedSpecialPage { |
| 20 | /** @var array Query parameters that can be passed through redirects */ |
| 21 | protected $mAllowedRedirectParams = []; |
| 22 | |
| 23 | /** @var array Query parameters added by redirects */ |
| 24 | protected $mAddedRedirectParams = []; |
| 25 | |
| 26 | /** |
| 27 | * @stable to override |
| 28 | * @param string|null $subpage |
| 29 | */ |
| 30 | public function execute( $subpage ) { |
| 31 | $redirect = $this->getRedirect( $subpage ); |
| 32 | $query = $this->getRedirectQuery( $subpage ); |
| 33 | |
| 34 | if ( $redirect instanceof Title ) { |
| 35 | // Redirect to a page title with possible query parameters |
| 36 | $url = $redirect->getFullUrlForRedirect( $query ); |
| 37 | $this->getOutput()->redirect( $url ); |
| 38 | } elseif ( $redirect === true ) { |
| 39 | // Redirect to index.php with query parameters |
| 40 | $url = wfAppendQuery( wfScript( 'index' ), $query ); |
| 41 | $this->getOutput()->redirect( $url ); |
| 42 | } else { |
| 43 | $this->showNoRedirectPage(); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * If the special page is a redirect, then get the Title object it redirects to. |
| 49 | * False otherwise. |
| 50 | * |
| 51 | * @param string|null $subpage |
| 52 | * @return Title|bool |
| 53 | */ |
| 54 | abstract public function getRedirect( $subpage ); |
| 55 | |
| 56 | /** |
| 57 | * Return part of the request string for a special redirect page |
| 58 | * This allows passing, e.g. action=history to Special:Mypage, etc. |
| 59 | * |
| 60 | * @stable to override |
| 61 | * @param string|null $subpage |
| 62 | * @return array|false |
| 63 | */ |
| 64 | public function getRedirectQuery( $subpage ) { |
| 65 | $params = []; |
| 66 | $request = $this->getRequest(); |
| 67 | |
| 68 | foreach ( array_merge( |
| 69 | $this->mAllowedRedirectParams, |
| 70 | // parameters which can be passed to all pages |
| 71 | [ 'uselang', 'useskin', 'useformat', 'variant', 'debug', 'safemode' ] |
| 72 | ) as $arg ) { |
| 73 | if ( $request->getVal( $arg ) !== null ) { |
| 74 | $params[$arg] = $request->getVal( $arg ); |
| 75 | } elseif ( $request->getArray( $arg ) !== null ) { |
| 76 | $params[$arg] = $request->getArray( $arg ); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | foreach ( $this->mAddedRedirectParams as $arg => $val ) { |
| 81 | $params[$arg] = $val; |
| 82 | } |
| 83 | |
| 84 | return count( $params ) |
| 85 | ? $params |
| 86 | : false; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Indicate if the target of this redirect can be used to identify |
| 91 | * a particular user of this wiki (e.g., if the redirect is to the |
| 92 | * user page of a User). See T109724. |
| 93 | * |
| 94 | * @stable to override |
| 95 | * @since 1.27 |
| 96 | * @return bool |
| 97 | */ |
| 98 | public function personallyIdentifiableTarget() { |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * @stable to override |
| 104 | */ |
| 105 | protected function showNoRedirectPage() { |
| 106 | $class = static::class; |
| 107 | throw new LogicException( "RedirectSpecialPage $class doesn't redirect!" ); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /** @deprecated class alias since 1.41 */ |
| 112 | class_alias( RedirectSpecialPage::class, 'RedirectSpecialPage' ); |