Merge "registration: Only allow one extension to set a specific config setting"
[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 * @return Title|bool
39 */
40 public function execute( $subpage ) {
41 $redirect = $this->getRedirect( $subpage );
42 $query = $this->getRedirectQuery();
43 // Redirect to a page title with possible query parameters
44 if ( $redirect instanceof Title ) {
45 $url = $redirect->getFullUrlForRedirect( $query );
46 $this->getOutput()->redirect( $url );
47
48 return $redirect;
49 } elseif ( $redirect === true ) {
50 // Redirect to index.php with query parameters
51 $url = wfAppendQuery( wfScript( 'index' ), $query );
52 $this->getOutput()->redirect( $url );
53
54 return $redirect;
55 } else {
56 $this->showNoRedirectPage();
57 }
58 }
59
60 /**
61 * If the special page is a redirect, then get the Title object it redirects to.
62 * False otherwise.
63 *
64 * @param string|null $subpage
65 * @return Title|bool
66 */
67 abstract public function getRedirect( $subpage );
68
69 /**
70 * Return part of the request string for a special redirect page
71 * This allows passing, e.g. action=history to Special:Mypage, etc.
72 *
73 * @return array|bool
74 */
75 public function getRedirectQuery() {
76 $params = [];
77 $request = $this->getRequest();
78
79 foreach ( array_merge( $this->mAllowedRedirectParams,
80 [ 'uselang', 'useskin', 'debug' ] // parameters which can be passed to all pages
81 ) as $arg ) {
82 if ( $request->getVal( $arg, null ) !== null ) {
83 $params[$arg] = $request->getVal( $arg );
84 } elseif ( $request->getArray( $arg, null ) !== null ) {
85 $params[$arg] = $request->getArray( $arg );
86 }
87 }
88
89 foreach ( $this->mAddedRedirectParams as $arg => $val ) {
90 $params[$arg] = $val;
91 }
92
93 return count( $params )
94 ? $params
95 : false;
96 }
97
98 /**
99 * Indicate if the target of this redirect can be used to identify
100 * a particular user of this wiki (e.g., if the redirect is to the
101 * user page of a User). See T109724.
102 *
103 * @since 1.27
104 * @return bool
105 */
106 public function personallyIdentifiableTarget() {
107 return false;
108 }
109
110 protected function showNoRedirectPage() {
111 $class = static::class;
112 throw new MWException( "RedirectSpecialPage $class doesn't redirect!" );
113 }
114 }
115
116 /**
117 * @ingroup SpecialPage
118 */
119 abstract class SpecialRedirectToSpecial extends RedirectSpecialPage {
120 /** @var string Name of redirect target */
121 protected $redirName;
122
123 /** @var string Name of subpage of redirect target */
124 protected $redirSubpage;
125
126 function __construct(
127 $name, $redirName, $redirSubpage = false,
128 $allowedRedirectParams = [], $addedRedirectParams = []
129 ) {
130 parent::__construct( $name );
131 $this->redirName = $redirName;
132 $this->redirSubpage = $redirSubpage;
133 $this->mAllowedRedirectParams = $allowedRedirectParams;
134 $this->mAddedRedirectParams = $addedRedirectParams;
135 }
136
137 /**
138 * @param string|null $subpage
139 * @return Title|bool
140 */
141 public function getRedirect( $subpage ) {
142 if ( $this->redirSubpage === false ) {
143 return SpecialPage::getTitleFor( $this->redirName, $subpage );
144 }
145
146 return SpecialPage::getTitleFor( $this->redirName, $this->redirSubpage );
147 }
148 }
149
150 /**
151 * Superclass for any RedirectSpecialPage which redirects the user
152 * to a particular article (as opposed to user contributions, logs, etc.).
153 *
154 * For security reasons these special pages are restricted to pass on
155 * the following subset of GET parameters to the target page while
156 * removing all others:
157 *
158 * - useskin, uselang, printable: to alter the appearance of the resulting page
159 *
160 * - redirect: allows viewing one's user page or talk page even if it is a
161 * redirect.
162 *
163 * - rdfrom: allows redirecting to one's user page or talk page from an
164 * external wiki with the "Redirect from..." notice.
165 *
166 * - limit, offset: Useful for linking to history of one's own user page or
167 * user talk page. For example, this would be a link to "the last edit to your
168 * user talk page in the year 2010":
169 * https://en.wikipedia.org/wiki/Special:MyPage?offset=20110000000000&limit=1&action=history
170 *
171 * - feed: would allow linking to the current user's RSS feed for their user
172 * talk page:
173 * https://en.wikipedia.org/w/index.php?title=Special:MyTalk&action=history&feed=rss
174 *
175 * - preloadtitle: Can be used to provide a default section title for a
176 * preloaded new comment on one's own talk page.
177 *
178 * - summary : Can be used to provide a default edit summary for a preloaded
179 * edit to one's own user page or talk page.
180 *
181 * - preview: Allows showing/hiding preview on first edit regardless of user
182 * preference, useful for preloaded edits where you know preview wouldn't be
183 * useful.
184 *
185 * - redlink: Affects the message the user sees if their talk page/user talk
186 * page does not currently exist. Avoids confusion for newbies with no user
187 * pages over why they got a "permission error" following this link:
188 * https://en.wikipedia.org/w/index.php?title=Special:MyPage&redlink=1
189 *
190 * - debug: determines whether the debug parameter is passed to load.php,
191 * which disables reformatting and allows scripts to be debugged. Useful
192 * when debugging scripts that manipulate one's own user page or talk page.
193 *
194 * @par Hook extension:
195 * Extensions can add to the redirect parameters list by using the hook
196 * RedirectSpecialArticleRedirectParams
197 *
198 * This hook allows extensions which add GET parameters like FlaggedRevs to
199 * retain those parameters when redirecting using special pages.
200 *
201 * @par Hook extension example:
202 * @code
203 * $wgHooks['RedirectSpecialArticleRedirectParams'][] =
204 * 'MyExtensionHooks::onRedirectSpecialArticleRedirectParams';
205 * public static function onRedirectSpecialArticleRedirectParams( &$redirectParams ) {
206 * $redirectParams[] = 'stable';
207 * return true;
208 * }
209 * @endcode
210 *
211 * @ingroup SpecialPage
212 */
213 abstract class RedirectSpecialArticle extends RedirectSpecialPage {
214 function __construct( $name ) {
215 parent::__construct( $name );
216 $redirectParams = [
217 'action',
218 'redirect', 'rdfrom',
219 # Options for preloaded edits
220 'preload', 'preloadparams', 'editintro', 'preloadtitle', 'summary', 'nosummary',
221 # Options for overriding user settings
222 'preview', 'minor', 'watchthis',
223 # Options for history/diffs
224 'section', 'oldid', 'diff', 'dir',
225 'limit', 'offset', 'feed',
226 # Misc options
227 'redlink',
228 # Options for action=raw; missing ctype can break JS or CSS in some browsers
229 'ctype', 'maxage', 'smaxage',
230 ];
231
232 Hooks::run( "RedirectSpecialArticleRedirectParams", [ &$redirectParams ] );
233 $this->mAllowedRedirectParams = $redirectParams;
234 }
235 }