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
= $parts[0];
65 $this->mValue
= $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 // Note: This url is more temporary as can change
123 // if file is reuploaded and has different aspect ratio.
124 $url = [ $mto->getUrl(), $height === -1 ?
301 : 302 ];
128 return Status
::newGood( $url );
132 * Handle Special:Redirect/revision/xxx
133 * (by redirecting to index.php?oldid=xxx)
135 * @return Status A good status contains the url to redirect to
137 function dispatchRevision() {
138 $oldid = $this->mValue
;
139 if ( !ctype_digit( $oldid ) ) {
140 // Message: redirect-not-numeric
141 return Status
::newFatal( $this->getMessagePrefix() . '-not-numeric' );
143 $oldid = (int)$oldid;
144 if ( $oldid === 0 ) {
145 // Message: redirect-not-exists
146 return Status
::newFatal( $this->getMessagePrefix() . '-not-exists' );
149 return Status
::newGood( wfAppendQuery( wfScript( 'index' ), [
155 * Handle Special:Redirect/page/xxx (by redirecting to index.php?curid=xxx)
157 * @return Status A good status contains the url to redirect to
159 function dispatchPage() {
160 $curid = $this->mValue
;
161 if ( !ctype_digit( $curid ) ) {
162 // Message: redirect-not-numeric
163 return Status
::newFatal( $this->getMessagePrefix() . '-not-numeric' );
165 $curid = (int)$curid;
166 if ( $curid === 0 ) {
167 // Message: redirect-not-exists
168 return Status
::newFatal( $this->getMessagePrefix() . '-not-exists' );
171 return Status
::newGood( wfAppendQuery( wfScript( 'index' ), [
177 * Handle Special:Redirect/logid/xxx
178 * (by redirecting to index.php?title=Special:Log&logid=xxx)
181 * @return Status A good status contains the url to redirect to
183 function dispatchLog() {
184 $logid = $this->mValue
;
185 if ( !ctype_digit( $logid ) ) {
186 // Message: redirect-not-numeric
187 return Status
::newFatal( $this->getMessagePrefix() . '-not-numeric' );
189 $logid = (int)$logid;
190 if ( $logid === 0 ) {
191 // Message: redirect-not-exists
192 return Status
::newFatal( $this->getMessagePrefix() . '-not-exists' );
194 $query = [ 'title' => 'Special:Log', 'logid' => $logid ];
195 return Status
::newGood( wfAppendQuery( wfScript( 'index' ), $query ) );
199 * Use appropriate dispatch* method to obtain a redirection URL,
200 * and either: redirect, set a 404 error code and error message,
201 * or do nothing (if $mValue wasn't set) allowing the form to be
204 * @return Status|bool True if a redirect was successfully handled.
206 function dispatch() {
207 // the various namespaces supported by Special:Redirect
208 switch ( $this->mType
) {
210 $status = $this->dispatchUser();
213 $status = $this->dispatchFile();
216 $status = $this->dispatchRevision();
219 $status = $this->dispatchPage();
222 $status = $this->dispatchLog();
228 if ( $status && $status->isGood() ) {
229 // These urls can sometimes be linked from prominent places,
231 $value = $status->getValue();
232 if ( is_array( $value ) ) {
233 list( $url, $code ) = $value;
238 if ( $code === 301 ) {
239 $this->getOutput()->setCdnMaxage( 60 * 60 );
241 $this->getOutput()->setCdnMaxage( 10 );
243 $this->getOutput()->redirect( $url, $code );
247 if ( !is_null( $this->mValue
) ) {
248 $this->getOutput()->setStatusCode( 404 );
256 protected function getFormFields() {
257 $mp = $this->getMessagePrefix();
259 // subpage => message
260 // Messages: redirect-user, redirect-page, redirect-revision,
261 // redirect-file, redirect-logid
262 'user' => $mp . '-user',
263 'page' => $mp . '-page',
264 'revision' => $mp . '-revision',
265 'file' => $mp . '-file',
266 'logid' => $mp . '-logid',
271 'label-message' => $mp . '-lookup', // Message: redirect-lookup
273 'default' => current( array_keys( $ns ) ),
275 foreach ( $ns as $n => $m ) {
276 $m = $this->msg( $m )->text();
277 $a['type']['options'][$m] = $n;
281 'label-message' => $mp . '-value' // Message: redirect-value
283 // set the defaults according to the parsed subpage path
284 if ( !empty( $this->mType
) ) {
285 $a['type']['default'] = $this->mType
;
287 if ( !empty( $this->mValue
) ) {
288 $a['value']['default'] = $this->mValue
;
294 public function onSubmit( array $data ) {
295 if ( !empty( $data['type'] ) && !empty( $data['value'] ) ) {
296 $this->setParameter( $data['type'] . '/' . $data['value'] );
299 /* if this returns false, will show the form */
300 return $this->dispatch();
303 public function onSuccess() {
304 /* do nothing, we redirect in $this->dispatch if successful. */
307 protected function alterForm( HTMLForm
$form ) {
308 /* display summary at top of page */
309 $this->outputHeader();
310 // tweak label on submit button
311 // Message: redirect-submit
312 $form->setSubmitTextMsg( $this->getMessagePrefix() . '-submit' );
313 /* submit form every time */
314 $form->setMethod( 'get' );
317 protected function getDisplayFormat() {
322 * Return an array of subpages that this special page will accept.
324 * @return string[] subpages
326 protected function getSubpagesForPrefixSearch() {
339 public function requiresWrite() {
346 public function requiresUnblock() {
350 protected function getGroupName() {