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