Merge "Exclude redirects from Special:Fewestrevisions"
[lhc/web/wiklou.git] / includes / specialpage / RedirectSpecialPage.php
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 * @ingroup SpecialPage
28 */
29 abstract class RedirectSpecialPage extends UnlistedSpecialPage {
30 // Query parameters that can be passed through redirects
31 protected $mAllowedRedirectParams = [];
32
33 // Query parameters added by redirects
34 protected $mAddedRedirectParams = [];
35
36 /**
37 * @param string|null $subpage
38 */
39 public function execute( $subpage ) {
40 $redirect = $this->getRedirect( $subpage );
41 $query = $this->getRedirectQuery( $subpage );
42
43 if ( $redirect instanceof Title ) {
44 // Redirect to a page title with possible query parameters
45 $url = $redirect->getFullUrlForRedirect( $query );
46 $this->getOutput()->redirect( $url );
47 } elseif ( $redirect === true ) {
48 // Redirect to index.php with query parameters
49 $url = wfAppendQuery( wfScript( 'index' ), $query );
50 $this->getOutput()->redirect( $url );
51 } else {
52 $this->showNoRedirectPage();
53 }
54 }
55
56 /**
57 * If the special page is a redirect, then get the Title object it redirects to.
58 * False otherwise.
59 *
60 * @param string|null $subpage
61 * @return Title|bool
62 */
63 abstract public function getRedirect( $subpage );
64
65 /**
66 * Return part of the request string for a special redirect page
67 * This allows passing, e.g. action=history to Special:Mypage, etc.
68 *
69 * @param string|null $subpage
70 * @return array|bool
71 */
72 public function getRedirectQuery( $subpage ) {
73 $params = [];
74 $request = $this->getRequest();
75
76 foreach ( array_merge( $this->mAllowedRedirectParams,
77 [ 'uselang', 'useskin', 'debug', 'safemode' ] // parameters which can be passed to all pages
78 ) as $arg ) {
79 if ( $request->getVal( $arg, null ) !== null ) {
80 $params[$arg] = $request->getVal( $arg );
81 } elseif ( $request->getArray( $arg, null ) !== null ) {
82 $params[$arg] = $request->getArray( $arg );
83 }
84 }
85
86 foreach ( $this->mAddedRedirectParams as $arg => $val ) {
87 $params[$arg] = $val;
88 }
89
90 return count( $params )
91 ? $params
92 : false;
93 }
94
95 /**
96 * Indicate if the target of this redirect can be used to identify
97 * a particular user of this wiki (e.g., if the redirect is to the
98 * user page of a User). See T109724.
99 *
100 * @since 1.27
101 * @return bool
102 */
103 public function personallyIdentifiableTarget() {
104 return false;
105 }
106
107 protected function showNoRedirectPage() {
108 $class = static::class;
109 throw new MWException( "RedirectSpecialPage $class doesn't redirect!" );
110 }
111 }