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