800604876f47531f718fca98303222c380d3411d
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 string|null Url to redirect to, or null if $mValue is invalid.
73 function dispatchUser() {
74 if ( !ctype_digit( $this->mValue
) ) {
77 $user = User
::newFromId( (int)$this->mValue
);
78 $username = $user->getName(); // load User as side-effect
79 if ( $user->isAnon() ) {
82 if ( $user->isHidden() && !MediaWikiServices
::getInstance()->getPermissionManager()
83 ->userHasRight( $this->getUser(), 'hideuser' )
85 throw new PermissionsError( null, [ 'badaccess-group0' ] );
87 $userpage = Title
::makeTitle( NS_USER
, $username );
89 return $userpage->getFullURL( '', false, PROTO_CURRENT
);
93 * Handle Special:Redirect/file/xxxx
95 * @return string|null Url to redirect to, or null if $mValue is not found.
97 function dispatchFile() {
98 $title = Title
::makeTitleSafe( NS_FILE
, $this->mValue
);
100 if ( !$title instanceof Title
) {
103 $file = wfFindFile( $title );
105 if ( !$file ||
!$file->exists() ) {
108 // Default behavior: Use the direct link to the file.
109 $url = $file->getUrl();
110 $request = $this->getRequest();
111 $width = $request->getInt( 'width', -1 );
112 $height = $request->getInt( 'height', -1 );
114 // If a width is requested...
115 if ( $width != -1 ) {
116 $mto = $file->transform( [ 'width' => $width, 'height' => $height ] );
118 if ( $mto && !$mto->isError() ) {
119 // ... change the URL to point to a thumbnail.
120 $url = $mto->getUrl();
128 * Handle Special:Redirect/revision/xxx
129 * (by redirecting to index.php?oldid=xxx)
131 * @return string|null Url to redirect to, or null if $mValue is invalid.
133 function dispatchRevision() {
134 $oldid = $this->mValue
;
135 if ( !ctype_digit( $oldid ) ) {
138 $oldid = (int)$oldid;
139 if ( $oldid === 0 ) {
143 return wfAppendQuery( wfScript( 'index' ), [
149 * Handle Special:Redirect/page/xxx (by redirecting to index.php?curid=xxx)
151 * @return string|null Url to redirect to, or null if $mValue is invalid.
153 function dispatchPage() {
154 $curid = $this->mValue
;
155 if ( !ctype_digit( $curid ) ) {
158 $curid = (int)$curid;
159 if ( $curid === 0 ) {
163 return wfAppendQuery( wfScript( 'index' ), [
169 * Handle Special:Redirect/logid/xxx
170 * (by redirecting to index.php?title=Special:Log&logid=xxx)
173 * @return string|null Url to redirect to, or null if $mValue is invalid.
175 function dispatchLog() {
176 $logid = $this->mValue
;
177 if ( !ctype_digit( $logid ) ) {
180 $logid = (int)$logid;
181 if ( $logid === 0 ) {
184 $query = [ 'title' => 'Special:Log', 'logid' => $logid ];
185 return wfAppendQuery( wfScript( 'index' ), $query );
189 * Use appropriate dispatch* method to obtain a redirection URL,
190 * and either: redirect, set a 404 error code and error message,
191 * or do nothing (if $mValue wasn't set) allowing the form to be
194 * @return bool True if a redirect was successfully handled.
196 function dispatch() {
197 // the various namespaces supported by Special:Redirect
198 switch ( $this->mType
) {
200 $url = $this->dispatchUser();
203 $url = $this->dispatchFile();
206 $url = $this->dispatchRevision();
209 $url = $this->dispatchPage();
212 $url = $this->dispatchLog();
219 $this->getOutput()->redirect( $url );
223 if ( !is_null( $this->mValue
) ) {
224 $this->getOutput()->setStatusCode( 404 );
225 // Message: redirect-not-exists
226 $msg = $this->getMessagePrefix() . '-not-exists';
228 return Status
::newFatal( $msg );
234 protected function getFormFields() {
235 $mp = $this->getMessagePrefix();
237 // subpage => message
238 // Messages: redirect-user, redirect-page, redirect-revision,
239 // redirect-file, redirect-logid
240 'user' => $mp . '-user',
241 'page' => $mp . '-page',
242 'revision' => $mp . '-revision',
243 'file' => $mp . '-file',
244 'logid' => $mp . '-logid',
249 'label-message' => $mp . '-lookup', // Message: redirect-lookup
251 'default' => current( array_keys( $ns ) ),
253 foreach ( $ns as $n => $m ) {
254 $m = $this->msg( $m )->text();
255 $a['type']['options'][$m] = $n;
259 'label-message' => $mp . '-value' // Message: redirect-value
261 // set the defaults according to the parsed subpage path
262 if ( !empty( $this->mType
) ) {
263 $a['type']['default'] = $this->mType
;
265 if ( !empty( $this->mValue
) ) {
266 $a['value']['default'] = $this->mValue
;
272 public function onSubmit( array $data ) {
273 if ( !empty( $data['type'] ) && !empty( $data['value'] ) ) {
274 $this->setParameter( $data['type'] . '/' . $data['value'] );
277 /* if this returns false, will show the form */
278 return $this->dispatch();
281 public function onSuccess() {
282 /* do nothing, we redirect in $this->dispatch if successful. */
285 protected function alterForm( HTMLForm
$form ) {
286 /* display summary at top of page */
287 $this->outputHeader();
288 // tweak label on submit button
289 // Message: redirect-submit
290 $form->setSubmitTextMsg( $this->getMessagePrefix() . '-submit' );
291 /* submit form every time */
292 $form->setMethod( 'get' );
295 protected function getDisplayFormat() {
300 * Return an array of subpages that this special page will accept.
302 * @return string[] subpages
304 protected function getSubpagesForPrefixSearch() {
317 public function requiresWrite() {
324 public function requiresUnblock() {
328 protected function getGroupName() {