064616b5d872bd2233ce17ab28779e7735a2c320
[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 = count( $parts ) > 0 ? $parts[0] : null;
65 $this->mValue = count( $parts ) > 1 ? $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 if ( $user->isHidden() && !MediaWikiServices::getInstance()->getPermissionManager()
85 ->userHasRight( $this->getUser(), 'hideuser' )
86 ) {
87 throw new PermissionsError( null, [ 'badaccess-group0' ] );
88 }
89 $userpage = Title::makeTitle( NS_USER, $username );
90
91 return Status::newGood( $userpage->getFullURL( '', false, PROTO_CURRENT ) );
92 }
93
94 /**
95 * Handle Special:Redirect/file/xxxx
96 *
97 * @return Status A good status contains the url to redirect to
98 */
99 function dispatchFile() {
100 try {
101 $title = Title::newFromTextThrow( $this->mValue, NS_FILE );
102 if ( $title && !$title->inNamespace( NS_FILE ) ) {
103 // If the given value contains a namespace enforce file namespace
104 $title = Title::newFromTextThrow( Title::makeName( NS_FILE, $this->mValue ) );
105 }
106 } catch ( MalformedTitleException $e ) {
107 return Status::newFatal( $e->getMessageObject() );
108 }
109 $file = wfFindFile( $title );
110
111 if ( !$file || !$file->exists() ) {
112 // Message: redirect-not-exists
113 return Status::newFatal( $this->getMessagePrefix() . '-not-exists' );
114 }
115 // Default behavior: Use the direct link to the file.
116 $url = $file->getUrl();
117 $request = $this->getRequest();
118 $width = $request->getInt( 'width', -1 );
119 $height = $request->getInt( 'height', -1 );
120
121 // If a width is requested...
122 if ( $width != -1 ) {
123 $mto = $file->transform( [ 'width' => $width, 'height' => $height ] );
124 // ... and we can
125 if ( $mto && !$mto->isError() ) {
126 // ... change the URL to point to a thumbnail.
127 $url = $mto->getUrl();
128 }
129 }
130
131 return Status::newGood( $url );
132 }
133
134 /**
135 * Handle Special:Redirect/revision/xxx
136 * (by redirecting to index.php?oldid=xxx)
137 *
138 * @return Status A good status contains the url to redirect to
139 */
140 function dispatchRevision() {
141 $oldid = $this->mValue;
142 if ( !ctype_digit( $oldid ) ) {
143 // Message: redirect-not-numeric
144 return Status::newFatal( $this->getMessagePrefix() . '-not-numeric' );
145 }
146 $oldid = (int)$oldid;
147 if ( $oldid === 0 ) {
148 // Message: redirect-not-exists
149 return Status::newFatal( $this->getMessagePrefix() . '-not-exists' );
150 }
151
152 return Status::newGood( wfAppendQuery( wfScript( 'index' ), [
153 'oldid' => $oldid
154 ] ) );
155 }
156
157 /**
158 * Handle Special:Redirect/page/xxx (by redirecting to index.php?curid=xxx)
159 *
160 * @return Status A good status contains the url to redirect to
161 */
162 function dispatchPage() {
163 $curid = $this->mValue;
164 if ( !ctype_digit( $curid ) ) {
165 // Message: redirect-not-numeric
166 return Status::newFatal( $this->getMessagePrefix() . '-not-numeric' );
167 }
168 $curid = (int)$curid;
169 if ( $curid === 0 ) {
170 // Message: redirect-not-exists
171 return Status::newFatal( $this->getMessagePrefix() . '-not-exists' );
172 }
173
174 return Status::newGood( wfAppendQuery( wfScript( 'index' ), [
175 'curid' => $curid
176 ] ) );
177 }
178
179 /**
180 * Handle Special:Redirect/logid/xxx
181 * (by redirecting to index.php?title=Special:Log&logid=xxx)
182 *
183 * @since 1.27
184 * @return Status A good status contains the url to redirect to
185 */
186 function dispatchLog() {
187 $logid = $this->mValue;
188 if ( !ctype_digit( $logid ) ) {
189 // Message: redirect-not-numeric
190 return Status::newFatal( $this->getMessagePrefix() . '-not-numeric' );
191 }
192 $logid = (int)$logid;
193 if ( $logid === 0 ) {
194 // Message: redirect-not-exists
195 return Status::newFatal( $this->getMessagePrefix() . '-not-exists' );
196 }
197 $query = [ 'title' => 'Special:Log', 'logid' => $logid ];
198 return Status::newGood( wfAppendQuery( wfScript( 'index' ), $query ) );
199 }
200
201 /**
202 * Use appropriate dispatch* method to obtain a redirection URL,
203 * and either: redirect, set a 404 error code and error message,
204 * or do nothing (if $mValue wasn't set) allowing the form to be
205 * displayed.
206 *
207 * @return Status|bool True if a redirect was successfully handled.
208 */
209 function dispatch() {
210 // the various namespaces supported by Special:Redirect
211 switch ( $this->mType ) {
212 case 'user':
213 $status = $this->dispatchUser();
214 break;
215 case 'file':
216 $status = $this->dispatchFile();
217 break;
218 case 'revision':
219 $status = $this->dispatchRevision();
220 break;
221 case 'page':
222 $status = $this->dispatchPage();
223 break;
224 case 'logid':
225 $status = $this->dispatchLog();
226 break;
227 default:
228 $status = null;
229 break;
230 }
231 if ( $status && $status->isGood() ) {
232 $this->getOutput()->redirect( $status->getValue() );
233
234 return true;
235 }
236 if ( !is_null( $this->mValue ) ) {
237 $this->getOutput()->setStatusCode( 404 );
238
239 return $status;
240 }
241
242 return false;
243 }
244
245 protected function getFormFields() {
246 $mp = $this->getMessagePrefix();
247 $ns = [
248 // subpage => message
249 // Messages: redirect-user, redirect-page, redirect-revision,
250 // redirect-file, redirect-logid
251 'user' => $mp . '-user',
252 'page' => $mp . '-page',
253 'revision' => $mp . '-revision',
254 'file' => $mp . '-file',
255 'logid' => $mp . '-logid',
256 ];
257 $a = [];
258 $a['type'] = [
259 'type' => 'select',
260 'label-message' => $mp . '-lookup', // Message: redirect-lookup
261 'options' => [],
262 'default' => current( array_keys( $ns ) ),
263 ];
264 foreach ( $ns as $n => $m ) {
265 $m = $this->msg( $m )->text();
266 $a['type']['options'][$m] = $n;
267 }
268 $a['value'] = [
269 'type' => 'text',
270 'label-message' => $mp . '-value' // Message: redirect-value
271 ];
272 // set the defaults according to the parsed subpage path
273 if ( !empty( $this->mType ) ) {
274 $a['type']['default'] = $this->mType;
275 }
276 if ( !empty( $this->mValue ) ) {
277 $a['value']['default'] = $this->mValue;
278 }
279
280 return $a;
281 }
282
283 public function onSubmit( array $data ) {
284 if ( !empty( $data['type'] ) && !empty( $data['value'] ) ) {
285 $this->setParameter( $data['type'] . '/' . $data['value'] );
286 }
287
288 /* if this returns false, will show the form */
289 return $this->dispatch();
290 }
291
292 public function onSuccess() {
293 /* do nothing, we redirect in $this->dispatch if successful. */
294 }
295
296 protected function alterForm( HTMLForm $form ) {
297 /* display summary at top of page */
298 $this->outputHeader();
299 // tweak label on submit button
300 // Message: redirect-submit
301 $form->setSubmitTextMsg( $this->getMessagePrefix() . '-submit' );
302 /* submit form every time */
303 $form->setMethod( 'get' );
304 }
305
306 protected function getDisplayFormat() {
307 return 'ooui';
308 }
309
310 /**
311 * Return an array of subpages that this special page will accept.
312 *
313 * @return string[] subpages
314 */
315 protected function getSubpagesForPrefixSearch() {
316 return [
317 'file',
318 'page',
319 'revision',
320 'user',
321 'logid',
322 ];
323 }
324
325 /**
326 * @return bool
327 */
328 public function requiresWrite() {
329 return false;
330 }
331
332 /**
333 * @return bool
334 */
335 public function requiresUnblock() {
336 return false;
337 }
338
339 protected function getGroupName() {
340 return 'redirects';
341 }
342 }