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 $userpage = Title
::makeTitle( NS_USER
, $username );
86 return Status
::newGood( $userpage->getFullURL( '', false, PROTO_CURRENT
) );
90 * Handle Special:Redirect/file/xxxx
92 * @return Status A good status contains the url to redirect to
94 function dispatchFile() {
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
) );
101 } catch ( MalformedTitleException
$e ) {
102 return Status
::newFatal( $e->getMessageObject() );
104 $file = wfFindFile( $title );
106 if ( !$file ||
!$file->exists() ) {
107 // Message: redirect-not-exists
108 return Status
::newFatal( $this->getMessagePrefix() . '-not-exists' );
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 );
116 // If a width is requested...
117 if ( $width != -1 ) {
118 $mto = $file->transform( [ 'width' => $width, 'height' => $height ] );
120 if ( $mto && !$mto->isError() ) {
121 // ... change the URL to point to a thumbnail.
122 $url = $mto->getUrl();
126 return Status
::newGood( $url );
130 * Handle Special:Redirect/revision/xxx
131 * (by redirecting to index.php?oldid=xxx)
133 * @return Status A good status contains the url to redirect to
135 function dispatchRevision() {
136 $oldid = $this->mValue
;
137 if ( !ctype_digit( $oldid ) ) {
138 // Message: redirect-not-numeric
139 return Status
::newFatal( $this->getMessagePrefix() . '-not-numeric' );
141 $oldid = (int)$oldid;
142 if ( $oldid === 0 ) {
143 // Message: redirect-not-exists
144 return Status
::newFatal( $this->getMessagePrefix() . '-not-exists' );
147 return Status
::newGood( wfAppendQuery( wfScript( 'index' ), [
153 * Handle Special:Redirect/page/xxx (by redirecting to index.php?curid=xxx)
155 * @return Status A good status contains the url to redirect to
157 function dispatchPage() {
158 $curid = $this->mValue
;
159 if ( !ctype_digit( $curid ) ) {
160 // Message: redirect-not-numeric
161 return Status
::newFatal( $this->getMessagePrefix() . '-not-numeric' );
163 $curid = (int)$curid;
164 if ( $curid === 0 ) {
165 // Message: redirect-not-exists
166 return Status
::newFatal( $this->getMessagePrefix() . '-not-exists' );
169 return Status
::newGood( wfAppendQuery( wfScript( 'index' ), [
175 * Handle Special:Redirect/logid/xxx
176 * (by redirecting to index.php?title=Special:Log&logid=xxx)
179 * @return Status A good status contains the url to redirect to
181 function dispatchLog() {
182 $logid = $this->mValue
;
183 if ( !ctype_digit( $logid ) ) {
184 // Message: redirect-not-numeric
185 return Status
::newFatal( $this->getMessagePrefix() . '-not-numeric' );
187 $logid = (int)$logid;
188 if ( $logid === 0 ) {
189 // Message: redirect-not-exists
190 return Status
::newFatal( $this->getMessagePrefix() . '-not-exists' );
192 $query = [ 'title' => 'Special:Log', 'logid' => $logid ];
193 return Status
::newGood( wfAppendQuery( wfScript( 'index' ), $query ) );
197 * Use appropriate dispatch* method to obtain a redirection URL,
198 * and either: redirect, set a 404 error code and error message,
199 * or do nothing (if $mValue wasn't set) allowing the form to be
202 * @return Status|bool True if a redirect was successfully handled.
204 function dispatch() {
205 // the various namespaces supported by Special:Redirect
206 switch ( $this->mType
) {
208 $status = $this->dispatchUser();
211 $status = $this->dispatchFile();
214 $status = $this->dispatchRevision();
217 $status = $this->dispatchPage();
220 $status = $this->dispatchLog();
226 if ( $status && $status->isGood() ) {
227 $this->getOutput()->redirect( $status->getValue() );
231 if ( !is_null( $this->mValue
) ) {
232 $this->getOutput()->setStatusCode( 404 );
240 protected function getFormFields() {
241 $mp = $this->getMessagePrefix();
243 // subpage => message
244 // Messages: redirect-user, redirect-page, redirect-revision,
245 // redirect-file, redirect-logid
246 'user' => $mp . '-user',
247 'page' => $mp . '-page',
248 'revision' => $mp . '-revision',
249 'file' => $mp . '-file',
250 'logid' => $mp . '-logid',
255 'label-message' => $mp . '-lookup', // Message: redirect-lookup
257 'default' => current( array_keys( $ns ) ),
259 foreach ( $ns as $n => $m ) {
260 $m = $this->msg( $m )->text();
261 $a['type']['options'][$m] = $n;
265 'label-message' => $mp . '-value' // Message: redirect-value
267 // set the defaults according to the parsed subpage path
268 if ( !empty( $this->mType
) ) {
269 $a['type']['default'] = $this->mType
;
271 if ( !empty( $this->mValue
) ) {
272 $a['value']['default'] = $this->mValue
;
278 public function onSubmit( array $data ) {
279 if ( !empty( $data['type'] ) && !empty( $data['value'] ) ) {
280 $this->setParameter( $data['type'] . '/' . $data['value'] );
283 /* if this returns false, will show the form */
284 return $this->dispatch();
287 public function onSuccess() {
288 /* do nothing, we redirect in $this->dispatch if successful. */
291 protected function alterForm( HTMLForm
$form ) {
292 /* display summary at top of page */
293 $this->outputHeader();
294 // tweak label on submit button
295 // Message: redirect-submit
296 $form->setSubmitTextMsg( $this->getMessagePrefix() . '-submit' );
297 /* submit form every time */
298 $form->setMethod( 'get' );
301 protected function getDisplayFormat() {
306 * Return an array of subpages that this special page will accept.
308 * @return string[] subpages
310 protected function getSubpagesForPrefixSearch() {
323 public function requiresWrite() {
330 public function requiresUnblock() {
334 protected function getGroupName() {