3 * Implements Special:Redirect
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.
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.
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
21 * @ingroup SpecialPage
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.
28 * @ingroup SpecialPage
31 class SpecialRedirect
extends FormSpecialPage
{
34 * The type of the redirect (user/file/revision)
36 * Example value: `'user'`
43 * The identifier/value for the redirect (which id, which file)
45 * Example value: `'42'`
51 function __construct() {
52 parent
::__construct( 'Redirect' );
58 * Set $mType and $mValue based on parsed value of $subpage.
59 * @param string $subpage
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;
69 * Handle Special:Redirect/user/xxxx (by redirecting to User:YYYY)
71 * @return Status A good status contains the url to redirect to
73 function dispatchUser() {
74 if ( !ctype_digit( $this->mValue
) ) {
75 // Message: redirect-not-numeric
76 return Status
::newFatal( $this->getMessagePrefix() . '-not-numeric' );
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' );
84 if ( $user->isHidden() && !MediaWikiServices
::getInstance()->getPermissionManager()
85 ->userHasRight( $this->getUser(), 'hideuser' )
87 throw new PermissionsError( null, [ 'badaccess-group0' ] );
89 $userpage = Title
::makeTitle( NS_USER
, $username );
91 return Status
::newGood( $userpage->getFullURL( '', false, PROTO_CURRENT
) );
95 * Handle Special:Redirect/file/xxxx
97 * @return Status A good status contains the url to redirect to
99 function dispatchFile() {
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
) );
106 } catch ( MalformedTitleException
$e ) {
107 return Status
::newFatal( $e->getMessageObject() );
109 $file = wfFindFile( $title );
111 if ( !$file ||
!$file->exists() ) {
112 // Message: redirect-not-exists
113 return Status
::newFatal( $this->getMessagePrefix() . '-not-exists' );
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 );
121 // If a width is requested...
122 if ( $width != -1 ) {
123 $mto = $file->transform( [ 'width' => $width, 'height' => $height ] );
125 if ( $mto && !$mto->isError() ) {
126 // ... change the URL to point to a thumbnail.
127 // Note: This url is more temporary as can change
128 // if file is reuploaded and has different aspect ratio.
129 $url = [ $mto->getUrl(), $height === -1 ?
301 : 302 ];
133 return Status
::newGood( $url );
137 * Handle Special:Redirect/revision/xxx
138 * (by redirecting to index.php?oldid=xxx)
140 * @return Status A good status contains the url to redirect to
142 function dispatchRevision() {
143 $oldid = $this->mValue
;
144 if ( !ctype_digit( $oldid ) ) {
145 // Message: redirect-not-numeric
146 return Status
::newFatal( $this->getMessagePrefix() . '-not-numeric' );
148 $oldid = (int)$oldid;
149 if ( $oldid === 0 ) {
150 // Message: redirect-not-exists
151 return Status
::newFatal( $this->getMessagePrefix() . '-not-exists' );
154 return Status
::newGood( wfAppendQuery( wfScript( 'index' ), [
160 * Handle Special:Redirect/page/xxx (by redirecting to index.php?curid=xxx)
162 * @return Status A good status contains the url to redirect to
164 function dispatchPage() {
165 $curid = $this->mValue
;
166 if ( !ctype_digit( $curid ) ) {
167 // Message: redirect-not-numeric
168 return Status
::newFatal( $this->getMessagePrefix() . '-not-numeric' );
170 $curid = (int)$curid;
171 if ( $curid === 0 ) {
172 // Message: redirect-not-exists
173 return Status
::newFatal( $this->getMessagePrefix() . '-not-exists' );
176 return Status
::newGood( wfAppendQuery( wfScript( 'index' ), [
182 * Handle Special:Redirect/logid/xxx
183 * (by redirecting to index.php?title=Special:Log&logid=xxx)
186 * @return Status A good status contains the url to redirect to
188 function dispatchLog() {
189 $logid = $this->mValue
;
190 if ( !ctype_digit( $logid ) ) {
191 // Message: redirect-not-numeric
192 return Status
::newFatal( $this->getMessagePrefix() . '-not-numeric' );
194 $logid = (int)$logid;
195 if ( $logid === 0 ) {
196 // Message: redirect-not-exists
197 return Status
::newFatal( $this->getMessagePrefix() . '-not-exists' );
199 $query = [ 'title' => 'Special:Log', 'logid' => $logid ];
200 return Status
::newGood( wfAppendQuery( wfScript( 'index' ), $query ) );
204 * Use appropriate dispatch* method to obtain a redirection URL,
205 * and either: redirect, set a 404 error code and error message,
206 * or do nothing (if $mValue wasn't set) allowing the form to be
209 * @return Status|bool True if a redirect was successfully handled.
211 function dispatch() {
212 // the various namespaces supported by Special:Redirect
213 switch ( $this->mType
) {
215 $status = $this->dispatchUser();
218 $status = $this->dispatchFile();
221 $status = $this->dispatchRevision();
224 $status = $this->dispatchPage();
227 $status = $this->dispatchLog();
233 if ( $status && $status->isGood() ) {
234 // These urls can sometimes be linked from prominent places,
236 $value = $status->getValue();
237 if ( is_array( $value ) ) {
238 list( $url, $code ) = $value;
243 if ( $code === 301 ) {
244 $this->getOutput()->setCdnMaxage( 60 * 60 );
246 $this->getOutput()->setCdnMaxage( 10 );
248 $this->getOutput()->redirect( $url, $code );
252 if ( !is_null( $this->mValue
) ) {
253 $this->getOutput()->setStatusCode( 404 );
261 protected function getFormFields() {
262 $mp = $this->getMessagePrefix();
264 // subpage => message
265 // Messages: redirect-user, redirect-page, redirect-revision,
266 // redirect-file, redirect-logid
267 'user' => $mp . '-user',
268 'page' => $mp . '-page',
269 'revision' => $mp . '-revision',
270 'file' => $mp . '-file',
271 'logid' => $mp . '-logid',
276 'label-message' => $mp . '-lookup', // Message: redirect-lookup
278 'default' => current( array_keys( $ns ) ),
280 foreach ( $ns as $n => $m ) {
281 $m = $this->msg( $m )->text();
282 $a['type']['options'][$m] = $n;
286 'label-message' => $mp . '-value' // Message: redirect-value
288 // set the defaults according to the parsed subpage path
289 if ( !empty( $this->mType
) ) {
290 $a['type']['default'] = $this->mType
;
292 if ( !empty( $this->mValue
) ) {
293 $a['value']['default'] = $this->mValue
;
299 public function onSubmit( array $data ) {
300 if ( !empty( $data['type'] ) && !empty( $data['value'] ) ) {
301 $this->setParameter( $data['type'] . '/' . $data['value'] );
304 /* if this returns false, will show the form */
305 return $this->dispatch();
308 public function onSuccess() {
309 /* do nothing, we redirect in $this->dispatch if successful. */
312 protected function alterForm( HTMLForm
$form ) {
313 /* display summary at top of page */
314 $this->outputHeader();
315 // tweak label on submit button
316 // Message: redirect-submit
317 $form->setSubmitTextMsg( $this->getMessagePrefix() . '-submit' );
318 /* submit form every time */
319 $form->setMethod( 'get' );
322 protected function getDisplayFormat() {
327 * Return an array of subpages that this special page will accept.
329 * @return string[] subpages
331 protected function getSubpagesForPrefixSearch() {
344 public function requiresWrite() {
351 public function requiresUnblock() {
355 protected function getGroupName() {