MediaWiki  master
RedirectSpecialPage.php
Go to the documentation of this file.
1 <?php
24 namespace MediaWiki\SpecialPage;
25 
26 use LogicException;
28 
36 abstract class RedirectSpecialPage extends UnlistedSpecialPage {
38  protected $mAllowedRedirectParams = [];
39 
41  protected $mAddedRedirectParams = [];
42 
47  public function execute( $subpage ) {
48  $redirect = $this->getRedirect( $subpage );
49  $query = $this->getRedirectQuery( $subpage );
50 
51  if ( $redirect instanceof Title ) {
52  // Redirect to a page title with possible query parameters
53  $url = $redirect->getFullUrlForRedirect( $query );
54  $this->getOutput()->redirect( $url );
55  } elseif ( $redirect === true ) {
56  // Redirect to index.php with query parameters
57  $url = wfAppendQuery( wfScript( 'index' ), $query );
58  $this->getOutput()->redirect( $url );
59  } else {
60  $this->showNoRedirectPage();
61  }
62  }
63 
71  abstract public function getRedirect( $subpage );
72 
81  public function getRedirectQuery( $subpage ) {
82  $params = [];
83  $request = $this->getRequest();
84 
85  foreach ( array_merge( $this->mAllowedRedirectParams,
86  [ 'uselang', 'useskin', 'variant', 'debug', 'safemode' ] // parameters which can be passed to all pages
87  ) as $arg ) {
88  if ( $request->getVal( $arg, null ) !== null ) {
89  $params[$arg] = $request->getVal( $arg );
90  } elseif ( $request->getArray( $arg, null ) !== null ) {
91  $params[$arg] = $request->getArray( $arg );
92  }
93  }
94 
95  foreach ( $this->mAddedRedirectParams as $arg => $val ) {
96  $params[$arg] = $val;
97  }
98 
99  return count( $params )
100  ? $params
101  : false;
102  }
103 
113  public function personallyIdentifiableTarget() {
114  return false;
115  }
116 
120  protected function showNoRedirectPage() {
121  $class = static::class;
122  throw new LogicException( "RedirectSpecialPage $class doesn't redirect!" );
123  }
124 }
125 
130 class_alias( RedirectSpecialPage::class, 'RedirectSpecialPage' );
wfAppendQuery( $url, $query)
Append a query string to an existing URL, which may or may not already have query string parameters a...
wfScript( $script='index')
Get the URL path to a MediaWiki entry point.
Shortcut to construct a special page alias.
getRedirect( $subpage)
If the special page is a redirect, then get the Title object it redirects to.
array $mAllowedRedirectParams
Query parameters that can be passed through redirects.
personallyIdentifiableTarget()
Indicate if the target of this redirect can be used to identify a particular user of this wiki (e....
getRedirectQuery( $subpage)
Return part of the request string for a special redirect page This allows passing,...
array $mAddedRedirectParams
Query parameters added by redirects.
getRequest()
Get the WebRequest being used for this instance.
getOutput()
Get the OutputPage being used for this instance.
Shortcut to construct a special page which is unlisted by default.
Represents a title within MediaWiki.
Definition: Title.php:76