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 $url = $mto->getUrl();
131 return Status
::newGood( $url );
135 * Handle Special:Redirect/revision/xxx
136 * (by redirecting to index.php?oldid=xxx)
138 * @return Status A good status contains the url to redirect to
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' );
146 $oldid = (int)$oldid;
147 if ( $oldid === 0 ) {
148 // Message: redirect-not-exists
149 return Status
::newFatal( $this->getMessagePrefix() . '-not-exists' );
152 return Status
::newGood( wfAppendQuery( wfScript( 'index' ), [
158 * Handle Special:Redirect/page/xxx (by redirecting to index.php?curid=xxx)
160 * @return Status A good status contains the url to redirect to
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' );
168 $curid = (int)$curid;
169 if ( $curid === 0 ) {
170 // Message: redirect-not-exists
171 return Status
::newFatal( $this->getMessagePrefix() . '-not-exists' );
174 return Status
::newGood( wfAppendQuery( wfScript( 'index' ), [
180 * Handle Special:Redirect/logid/xxx
181 * (by redirecting to index.php?title=Special:Log&logid=xxx)
184 * @return Status A good status contains the url to redirect to
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' );
192 $logid = (int)$logid;
193 if ( $logid === 0 ) {
194 // Message: redirect-not-exists
195 return Status
::newFatal( $this->getMessagePrefix() . '-not-exists' );
197 $query = [ 'title' => 'Special:Log', 'logid' => $logid ];
198 return Status
::newGood( wfAppendQuery( wfScript( 'index' ), $query ) );
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
207 * @return Status|bool True if a redirect was successfully handled.
209 function dispatch() {
210 // the various namespaces supported by Special:Redirect
211 switch ( $this->mType
) {
213 $status = $this->dispatchUser();
216 $status = $this->dispatchFile();
219 $status = $this->dispatchRevision();
222 $status = $this->dispatchPage();
225 $status = $this->dispatchLog();
231 if ( $status && $status->isGood() ) {
232 $this->getOutput()->redirect( $status->getValue() );
236 if ( !is_null( $this->mValue
) ) {
237 $this->getOutput()->setStatusCode( 404 );
245 protected function getFormFields() {
246 $mp = $this->getMessagePrefix();
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',
260 'label-message' => $mp . '-lookup', // Message: redirect-lookup
262 'default' => current( array_keys( $ns ) ),
264 foreach ( $ns as $n => $m ) {
265 $m = $this->msg( $m )->text();
266 $a['type']['options'][$m] = $n;
270 'label-message' => $mp . '-value' // Message: redirect-value
272 // set the defaults according to the parsed subpage path
273 if ( !empty( $this->mType
) ) {
274 $a['type']['default'] = $this->mType
;
276 if ( !empty( $this->mValue
) ) {
277 $a['value']['default'] = $this->mValue
;
283 public function onSubmit( array $data ) {
284 if ( !empty( $data['type'] ) && !empty( $data['value'] ) ) {
285 $this->setParameter( $data['type'] . '/' . $data['value'] );
288 /* if this returns false, will show the form */
289 return $this->dispatch();
292 public function onSuccess() {
293 /* do nothing, we redirect in $this->dispatch if successful. */
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' );
306 protected function getDisplayFormat() {
311 * Return an array of subpages that this special page will accept.
313 * @return string[] subpages
315 protected function getSubpagesForPrefixSearch() {
328 public function requiresWrite() {
335 public function requiresUnblock() {
339 protected function getGroupName() {