1<?php 2/** 3 * Shortcuts to construct a special page alias. 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; either version 2 of the License, or 8 * (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License along 16 * with this program; if not, write to the Free Software Foundation, Inc., 17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 18 * http://www.gnu.org/copyleft/gpl.html 19 * 20 * @file 21 * @ingroup SpecialPage 22 */ 23 24/** 25 * Shortcut to construct a special page alias. 26 * 27 * @stable to extend 28 * 29 * @ingroup SpecialPage 30 */ 31abstract class RedirectSpecialPage extends UnlistedSpecialPage { 32 /** @var array Query parameters that can be passed through redirects */ 33 protected $mAllowedRedirectParams = []; 34 35 /** @var array Query parameters added by redirects */ 36 protected $mAddedRedirectParams = []; 37 38 /** 39 * @stable to override 40 * @param string|null $subpage 41 */ 42 public function execute( $subpage ) { 43 $redirect = $this->getRedirect( $subpage ); 44 $query = $this->getRedirectQuery( $subpage ); 45 46 if ( $redirect instanceof Title ) { 47 // Redirect to a page title with possible query parameters 48 $url = $redirect->getFullUrlForRedirect( $query ); 49 $this->getOutput()->redirect( $url ); 50 } elseif ( $redirect === true ) { 51 // Redirect to index.php with query parameters 52 $url = wfAppendQuery( wfScript( 'index' ), $query ); 53 $this->getOutput()->redirect( $url ); 54 } else { 55 $this->showNoRedirectPage(); 56 } 57 } 58 59 /** 60 * If the special page is a redirect, then get the Title object it redirects to. 61 * False otherwise. 62 * 63 * @param string|null $subpage 64 * @return Title|bool 65 */ 66 abstract public function getRedirect( $subpage ); 67 68 /** 69 * Return part of the request string for a special redirect page 70 * This allows passing, e.g. action=history to Special:Mypage, etc. 71 * 72 * @stable to override 73 * @param string|null $subpage 74 * @return array|bool 75 */ 76 public function getRedirectQuery( $subpage ) { 77 $params = []; 78 $request = $this->getRequest(); 79 80 foreach ( array_merge( $this->mAllowedRedirectParams, 81 [ 'uselang', 'useskin', 'debug', 'safemode' ] // parameters which can be passed to all pages 82 ) as $arg ) { 83 if ( $request->getVal( $arg, null ) !== null ) { 84 $params[$arg] = $request->getVal( $arg ); 85 } elseif ( $request->getArray( $arg, null ) !== null ) { 86 $params[$arg] = $request->getArray( $arg ); 87 } 88 } 89 90 foreach ( $this->mAddedRedirectParams as $arg => $val ) { 91 $params[$arg] = $val; 92 } 93 94 return count( $params ) 95 ? $params 96 : false; 97 } 98 99 /** 100 * Indicate if the target of this redirect can be used to identify 101 * a particular user of this wiki (e.g., if the redirect is to the 102 * user page of a User). See T109724. 103 * 104 * @stable to override 105 * @since 1.27 106 * @return bool 107 */ 108 public function personallyIdentifiableTarget() { 109 return false; 110 } 111 112 /** 113 * @stable to override 114 */ 115 protected function showNoRedirectPage() { 116 $class = static::class; 117 throw new MWException( "RedirectSpecialPage $class doesn't redirect!" ); 118 } 119} 120