Merge "Add action=query&meta=languageinfo API module"
[lhc/web/wiklou.git] / includes / specials / SpecialRedirect.php
1 <?php
2 /**
3 * Implements Special:Redirect
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 * A special page that redirects to: the user for a numeric user id,
26 * the file for a given filename, or the page for a given revision id.
27 *
28 * @ingroup SpecialPage
29 * @since 1.22
30 */
31 class SpecialRedirect extends FormSpecialPage {
32
33 /**
34 * The type of the redirect (user/file/revision)
35 *
36 * Example value: `'user'`
37 *
38 * @var string $mType
39 */
40 protected $mType;
41
42 /**
43 * The identifier/value for the redirect (which id, which file)
44 *
45 * Example value: `'42'`
46 *
47 * @var string $mValue
48 */
49 protected $mValue;
50
51 function __construct() {
52 parent::__construct( 'Redirect' );
53 $this->mType = null;
54 $this->mValue = null;
55 }
56
57 /**
58 * Set $mType and $mValue based on parsed value of $subpage.
59 * @param string $subpage
60 */
61 function setParameter( $subpage ) {
62 // parse $subpage to pull out the parts
63 $parts = explode( '/', $subpage, 2 );
64 $this->mType = $parts[0];
65 $this->mValue = $parts[1] ?? null;
66 }
67
68 /**
69 * Handle Special:Redirect/user/xxxx (by redirecting to User:YYYY)
70 *
71 * @return Status A good status contains the url to redirect to
72 */
73 function dispatchUser() {
74 if ( !ctype_digit( $this->mValue ) ) {
75 // Message: redirect-not-numeric
76 return Status::newFatal( $this->getMessagePrefix() . '-not-numeric' );
77 }
78 $user = User::newFromId( (int)$this->mValue );
79 $username = $user->getName(); // load User as side-effect
80 if ( $user->isAnon() ) {
81 // Message: redirect-not-exists
82 return Status::newFatal( $this->getMessagePrefix() . '-not-exists' );
83 }
84 $userpage = Title::makeTitle( NS_USER, $username );
85
86 return Status::newGood( $userpage->getFullURL( '', false, PROTO_CURRENT ) );
87 }
88
89 /**
90 * Handle Special:Redirect/file/xxxx
91 *
92 * @return Status A good status contains the url to redirect to
93 */
94 function dispatchFile() {
95 try {
96 $title = Title::newFromTextThrow( $this->mValue, NS_FILE );
97 if ( $title && !$title->inNamespace( NS_FILE ) ) {
98 // If the given value contains a namespace enforce file namespace
99 $title = Title::newFromTextThrow( Title::makeName( NS_FILE, $this->mValue ) );
100 }
101 } catch ( MalformedTitleException $e ) {
102 return Status::newFatal( $e->getMessageObject() );
103 }
104 $file = wfFindFile( $title );
105
106 if ( !$file || !$file->exists() ) {
107 // Message: redirect-not-exists
108 return Status::newFatal( $this->getMessagePrefix() . '-not-exists' );
109 }
110 // Default behavior: Use the direct link to the file.
111 $url = $file->getUrl();
112 $request = $this->getRequest();
113 $width = $request->getInt( 'width', -1 );
114 $height = $request->getInt( 'height', -1 );
115
116 // If a width is requested...
117 if ( $width != -1 ) {
118 $mto = $file->transform( [ 'width' => $width, 'height' => $height ] );
119 // ... and we can
120 if ( $mto && !$mto->isError() ) {
121 // ... change the URL to point to a thumbnail.
122 // Note: This url is more temporary as can change
123 // if file is reuploaded and has different aspect ratio.
124 $url = [ $mto->getUrl(), $height === -1 ? 301 : 302 ];
125 }
126 }
127
128 return Status::newGood( $url );
129 }
130
131 /**
132 * Handle Special:Redirect/revision/xxx
133 * (by redirecting to index.php?oldid=xxx)
134 *
135 * @return Status A good status contains the url to redirect to
136 */
137 function dispatchRevision() {
138 $oldid = $this->mValue;
139 if ( !ctype_digit( $oldid ) ) {
140 // Message: redirect-not-numeric
141 return Status::newFatal( $this->getMessagePrefix() . '-not-numeric' );
142 }
143 $oldid = (int)$oldid;
144 if ( $oldid === 0 ) {
145 // Message: redirect-not-exists
146 return Status::newFatal( $this->getMessagePrefix() . '-not-exists' );
147 }
148
149 return Status::newGood( wfAppendQuery( wfScript( 'index' ), [
150 'oldid' => $oldid
151 ] ) );
152 }
153
154 /**
155 * Handle Special:Redirect/page/xxx (by redirecting to index.php?curid=xxx)
156 *
157 * @return Status A good status contains the url to redirect to
158 */
159 function dispatchPage() {
160 $curid = $this->mValue;
161 if ( !ctype_digit( $curid ) ) {
162 // Message: redirect-not-numeric
163 return Status::newFatal( $this->getMessagePrefix() . '-not-numeric' );
164 }
165 $curid = (int)$curid;
166 if ( $curid === 0 ) {
167 // Message: redirect-not-exists
168 return Status::newFatal( $this->getMessagePrefix() . '-not-exists' );
169 }
170
171 return Status::newGood( wfAppendQuery( wfScript( 'index' ), [
172 'curid' => $curid
173 ] ) );
174 }
175
176 /**
177 * Handle Special:Redirect/logid/xxx
178 * (by redirecting to index.php?title=Special:Log&logid=xxx)
179 *
180 * @since 1.27
181 * @return Status A good status contains the url to redirect to
182 */
183 function dispatchLog() {
184 $logid = $this->mValue;
185 if ( !ctype_digit( $logid ) ) {
186 // Message: redirect-not-numeric
187 return Status::newFatal( $this->getMessagePrefix() . '-not-numeric' );
188 }
189 $logid = (int)$logid;
190 if ( $logid === 0 ) {
191 // Message: redirect-not-exists
192 return Status::newFatal( $this->getMessagePrefix() . '-not-exists' );
193 }
194 $query = [ 'title' => 'Special:Log', 'logid' => $logid ];
195 return Status::newGood( wfAppendQuery( wfScript( 'index' ), $query ) );
196 }
197
198 /**
199 * Use appropriate dispatch* method to obtain a redirection URL,
200 * and either: redirect, set a 404 error code and error message,
201 * or do nothing (if $mValue wasn't set) allowing the form to be
202 * displayed.
203 *
204 * @return Status|bool True if a redirect was successfully handled.
205 */
206 function dispatch() {
207 // the various namespaces supported by Special:Redirect
208 switch ( $this->mType ) {
209 case 'user':
210 $status = $this->dispatchUser();
211 break;
212 case 'file':
213 $status = $this->dispatchFile();
214 break;
215 case 'revision':
216 $status = $this->dispatchRevision();
217 break;
218 case 'page':
219 $status = $this->dispatchPage();
220 break;
221 case 'logid':
222 $status = $this->dispatchLog();
223 break;
224 default:
225 $status = null;
226 break;
227 }
228 if ( $status && $status->isGood() ) {
229 // These urls can sometimes be linked from prominent places,
230 // so varnish cache.
231 $value = $status->getValue();
232 if ( is_array( $value ) ) {
233 list( $url, $code ) = $value;
234 } else {
235 $url = $value;
236 $code = 301;
237 }
238 if ( $code === 301 ) {
239 $this->getOutput()->setCdnMaxage( 60 * 60 );
240 } else {
241 $this->getOutput()->setCdnMaxage( 10 );
242 }
243 $this->getOutput()->redirect( $url, $code );
244
245 return true;
246 }
247 if ( !is_null( $this->mValue ) ) {
248 $this->getOutput()->setStatusCode( 404 );
249
250 return $status;
251 }
252
253 return false;
254 }
255
256 protected function getFormFields() {
257 $mp = $this->getMessagePrefix();
258 $ns = [
259 // subpage => message
260 // Messages: redirect-user, redirect-page, redirect-revision,
261 // redirect-file, redirect-logid
262 'user' => $mp . '-user',
263 'page' => $mp . '-page',
264 'revision' => $mp . '-revision',
265 'file' => $mp . '-file',
266 'logid' => $mp . '-logid',
267 ];
268 $a = [];
269 $a['type'] = [
270 'type' => 'select',
271 'label-message' => $mp . '-lookup', // Message: redirect-lookup
272 'options' => [],
273 'default' => current( array_keys( $ns ) ),
274 ];
275 foreach ( $ns as $n => $m ) {
276 $m = $this->msg( $m )->text();
277 $a['type']['options'][$m] = $n;
278 }
279 $a['value'] = [
280 'type' => 'text',
281 'label-message' => $mp . '-value' // Message: redirect-value
282 ];
283 // set the defaults according to the parsed subpage path
284 if ( !empty( $this->mType ) ) {
285 $a['type']['default'] = $this->mType;
286 }
287 if ( !empty( $this->mValue ) ) {
288 $a['value']['default'] = $this->mValue;
289 }
290
291 return $a;
292 }
293
294 public function onSubmit( array $data ) {
295 if ( !empty( $data['type'] ) && !empty( $data['value'] ) ) {
296 $this->setParameter( $data['type'] . '/' . $data['value'] );
297 }
298
299 /* if this returns false, will show the form */
300 return $this->dispatch();
301 }
302
303 public function onSuccess() {
304 /* do nothing, we redirect in $this->dispatch if successful. */
305 }
306
307 protected function alterForm( HTMLForm $form ) {
308 /* display summary at top of page */
309 $this->outputHeader();
310 // tweak label on submit button
311 // Message: redirect-submit
312 $form->setSubmitTextMsg( $this->getMessagePrefix() . '-submit' );
313 /* submit form every time */
314 $form->setMethod( 'get' );
315 }
316
317 protected function getDisplayFormat() {
318 return 'ooui';
319 }
320
321 /**
322 * Return an array of subpages that this special page will accept.
323 *
324 * @return string[] subpages
325 */
326 protected function getSubpagesForPrefixSearch() {
327 return [
328 'file',
329 'page',
330 'revision',
331 'user',
332 'logid',
333 ];
334 }
335
336 /**
337 * @return bool
338 */
339 public function requiresWrite() {
340 return false;
341 }
342
343 /**
344 * @return bool
345 */
346 public function requiresUnblock() {
347 return false;
348 }
349
350 protected function getGroupName() {
351 return 'redirects';
352 }
353 }