3 * Implements Special:Redirect
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
22 * @ingroup SpecialPage
26 * A special page that redirects to: the user for a numeric user id,
27 * the file for a given filename, or the page for a given revision id.
29 * @ingroup SpecialPage
32 class SpecialRedirect
extends FormSpecialPage
{
35 * The type of the redirect (user/file/revision)
43 * The identifier/value for the redirect (which id, which file)
50 function __construct() {
51 parent
::__construct( 'Redirect' );
57 * Set $mType and $mValue based on parsed value of $subpage.
59 function setParameter( $subpage ) {
60 // parse $subpage to pull out the parts
61 $parts = explode( '/', $subpage, 2 );
62 $this->mType
= count( $parts ) > 0 ?
$parts[0] : null;
63 $this->mValue
= count( $parts ) > 1 ?
$parts[1] : null;
67 * Handle Special:Redirect/user/xxxx (by redirecting to User:YYYY)
69 * @return string|null url to redirect to, or null if $mValue is invalid.
71 function dispatchUser() {
72 if ( !ctype_digit( $this->mValue
) ) {
75 $user = User
::newFromId( (int)$this->mValue
);
76 $username = $user->getName(); // load User as side-effect
77 if ( $user->isAnon() ) {
80 $userpage = Title
::makeTitle( NS_USER
, $username );
81 return $userpage->getFullURL( '', false, PROTO_CURRENT
);
85 * Handle Special:Redirect/file/xxxx
87 * @return string|null url to redirect to, or null if $mValue is not found.
89 function dispatchFile() {
90 $title = Title
::makeTitleSafe( NS_FILE
, $this->mValue
);
92 if ( ! $title instanceof Title
) {
95 $file = wfFindFile( $title );
97 if ( !$file ||
!$file->exists() ) {
100 // Default behavior: Use the direct link to the file.
101 $url = $file->getURL();
102 $request = $this->getRequest();
103 $width = $request->getInt( 'width', -1 );
104 $height = $request->getInt( 'height', -1 );
106 // If a width is requested...
107 if ( $width != -1 ) {
108 $mto = $file->transform( array( 'width' => $width, 'height' => $height ) );
110 if ( $mto && !$mto->isError() ) {
111 // ... change the URL to point to a thumbnail.
112 $url = $mto->getURL();
119 * Handle Special:Redirect/revision/xxx
120 * (by redirecting to index.php?oldid=xxx)
122 * @return string|null url to redirect to, or null if $mValue is invalid.
124 function dispatchRevision() {
125 $oldid = $this->mValue
;
126 if ( !ctype_digit( $oldid ) ) {
129 $oldid = (int)$oldid;
130 if ( $oldid === 0 ) {
133 return wfAppendQuery( wfScript( 'index' ), array(
139 * Use appropriate dispatch* method to obtain a redirection URL,
140 * and either: redirect, set a 404 error code and error message,
141 * or do nothing (if $mValue wasn't set) allowing the form to be
144 * @return bool true if a redirect was successfully handled.
146 function dispatch() {
147 // the various namespaces supported by Special:Redirect
148 switch ( $this->mType
) {
150 $url = $this->dispatchUser();
153 $url = $this->dispatchFile();
156 $url = $this->dispatchRevision();
159 $this->getOutput()->setStatusCode( 404 );
164 $this->getOutput()->redirect( $url );
167 if ( !is_null( $this->mValue
) ) {
168 $this->getOutput()->setStatusCode( 404 );
169 $msg = $this->getMessagePrefix() . '-not-exists';
170 return Status
::newFatal( $msg );
175 protected function getFormFields() {
176 $mp = $this->getMessagePrefix();
178 // subpage => message
179 'user' => $mp . '-user',
180 'revision' => $mp . '-revision',
181 'file' => $mp . '-file',
186 'label-message' => $mp . '-lookup',
187 'options' => array(),
188 'default' => current( array_keys( $ns ) ),
190 foreach ( $ns as $n => $m ) {
191 $m = $this->msg( $m )->text();
192 $a['type']['options'][$m] = $n;
196 'label-message' => $mp . '-value'
198 // set the defaults according to the parsed subpage path
199 if ( !empty( $this->mType
) ) {
200 $a['type']['default'] = $this->mType
;
202 if ( !empty( $this->mValue
) ) {
203 $a['value']['default'] = $this->mValue
;
208 public function onSubmit( array $data ) {
209 if ( !empty( $data['type'] ) && !empty( $data['value'] ) ) {
210 $this->setParameter( $data['type'] . '/' . $data['value'] );
212 /* if this returns false, will show the form */
213 return $this->dispatch();
216 public function onSuccess() {
217 /* do nothing, we redirect in $this->dispatch if successful. */
220 protected function alterForm( HTMLForm
$form ) {
221 /* display summary at top of page */
222 $this->outputHeader();
223 /* tweak label on submit button */
224 $form->setSubmitTextMsg( $this->getMessagePrefix() . '-submit' );
225 /* submit form every time */
226 $form->setMethod( 'get' );
229 protected function getGroupName() {