Merge "Add licensing for extensions to Special:Version"
[lhc/web/wiklou.git] / includes / specials / SpecialRedirect.php
1 <?php
2 /**
3 * Implements Special:Redirect
4 *
5 * @section LICENSE
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.
10 *
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.
15 *
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
20 *
21 * @file
22 * @ingroup SpecialPage
23 */
24
25 /**
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.
28 *
29 * @ingroup SpecialPage
30 * @since 1.22
31 */
32 class SpecialRedirect extends FormSpecialPage {
33
34 /**
35 * The type of the redirect (user/file/revision)
36 *
37 * @var string $mType
38 * @example 'user'
39 */
40 protected $mType;
41
42 /**
43 * The identifier/value for the redirect (which id, which file)
44 *
45 * @var string $mValue
46 * @example '42'
47 */
48 protected $mValue;
49
50 function __construct() {
51 parent::__construct( 'Redirect' );
52 $this->mType = null;
53 $this->mValue = null;
54 }
55
56 /**
57 * Set $mType and $mValue based on parsed value of $subpage.
58 */
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;
64 }
65
66 /**
67 * Handle Special:Redirect/user/xxxx (by redirecting to User:YYYY)
68 *
69 * @return string|null url to redirect to, or null if $mValue is invalid.
70 */
71 function dispatchUser() {
72 if ( !ctype_digit( $this->mValue ) ) {
73 return null;
74 }
75 $user = User::newFromId( (int)$this->mValue );
76 $username = $user->getName(); // load User as side-effect
77 if ( $user->isAnon() ) {
78 return null;
79 }
80 $userpage = Title::makeTitle( NS_USER, $username );
81 return $userpage->getFullURL( '', false, PROTO_CURRENT );
82 }
83
84 /**
85 * Handle Special:Redirect/file/xxxx
86 *
87 * @return string|null url to redirect to, or null if $mValue is not found.
88 */
89 function dispatchFile() {
90 $title = Title::makeTitleSafe( NS_FILE, $this->mValue );
91
92 if ( ! $title instanceof Title ) {
93 return null;
94 }
95 $file = wfFindFile( $title );
96
97 if ( !$file || !$file->exists() ) {
98 return null;
99 }
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 );
105
106 // If a width is requested...
107 if ( $width != -1 ) {
108 $mto = $file->transform( array( 'width' => $width, 'height' => $height ) );
109 // ... and we can
110 if ( $mto && !$mto->isError() ) {
111 // ... change the URL to point to a thumbnail.
112 $url = $mto->getURL();
113 }
114 }
115 return $url;
116 }
117
118 /**
119 * Handle Special:Redirect/revision/xxx
120 * (by redirecting to index.php?oldid=xxx)
121 *
122 * @return string|null url to redirect to, or null if $mValue is invalid.
123 */
124 function dispatchRevision() {
125 $oldid = $this->mValue;
126 if ( !ctype_digit( $oldid ) ) {
127 return null;
128 }
129 $oldid = (int)$oldid;
130 if ( $oldid === 0 ) {
131 return null;
132 }
133 return wfAppendQuery( wfScript( 'index' ), array(
134 'oldid' => $oldid
135 ) );
136 }
137
138 /**
139 * Handle Special:Redirect/page/xxx (by redirecting to index.php?curid=xxx)
140 *
141 * @return string|null url to redirect to, or null if $mValue is invalid.
142 */
143 function dispatchPage() {
144 $curid = $this->mValue;
145 if ( !ctype_digit( $curid ) ) {
146 return null;
147 }
148 $curid = (int)$curid;
149 if ( $curid === 0 ) {
150 return null;
151 }
152 return wfAppendQuery( wfScript( 'index' ), array(
153 'curid' => $curid
154 ) );
155 }
156
157 /**
158 * Use appropriate dispatch* method to obtain a redirection URL,
159 * and either: redirect, set a 404 error code and error message,
160 * or do nothing (if $mValue wasn't set) allowing the form to be
161 * displayed.
162 *
163 * @return bool true if a redirect was successfully handled.
164 */
165 function dispatch() {
166 // the various namespaces supported by Special:Redirect
167 switch ( $this->mType ) {
168 case 'user':
169 $url = $this->dispatchUser();
170 break;
171 case 'file':
172 $url = $this->dispatchFile();
173 break;
174 case 'revision':
175 $url = $this->dispatchRevision();
176 break;
177 case 'page':
178 $url = $this->dispatchPage();
179 break;
180 default:
181 $this->getOutput()->setStatusCode( 404 );
182 $url = null;
183 break;
184 }
185 if ( $url ) {
186 $this->getOutput()->redirect( $url );
187 return true;
188 }
189 if ( !is_null( $this->mValue ) ) {
190 $this->getOutput()->setStatusCode( 404 );
191 // Message: redirect-not-exists
192 $msg = $this->getMessagePrefix() . '-not-exists';
193 return Status::newFatal( $msg );
194 }
195 return false;
196 }
197
198 protected function getFormFields() {
199 $mp = $this->getMessagePrefix();
200 $ns = array(
201 // subpage => message
202 // Messages: redirect-user, redirect-page, redirect-revision,
203 // redirect-file
204 'user' => $mp . '-user',
205 'page' => $mp . '-page',
206 'revision' => $mp . '-revision',
207 'file' => $mp . '-file',
208 );
209 $a = array();
210 $a['type'] = array(
211 'type' => 'select',
212 'label-message' => $mp . '-lookup', // Message: redirect-lookup
213 'options' => array(),
214 'default' => current( array_keys( $ns ) ),
215 );
216 foreach ( $ns as $n => $m ) {
217 $m = $this->msg( $m )->text();
218 $a['type']['options'][$m] = $n;
219 }
220 $a['value'] = array(
221 'type' => 'text',
222 'label-message' => $mp . '-value' // Message: redirect-value
223 );
224 // set the defaults according to the parsed subpage path
225 if ( !empty( $this->mType ) ) {
226 $a['type']['default'] = $this->mType;
227 }
228 if ( !empty( $this->mValue ) ) {
229 $a['value']['default'] = $this->mValue;
230 }
231 return $a;
232 }
233
234 public function onSubmit( array $data ) {
235 if ( !empty( $data['type'] ) && !empty( $data['value'] ) ) {
236 $this->setParameter( $data['type'] . '/' . $data['value'] );
237 }
238 /* if this returns false, will show the form */
239 return $this->dispatch();
240 }
241
242 public function onSuccess() {
243 /* do nothing, we redirect in $this->dispatch if successful. */
244 }
245
246 protected function alterForm( HTMLForm $form ) {
247 /* display summary at top of page */
248 $this->outputHeader();
249 // tweak label on submit button
250 // Message: redirect-submit
251 $form->setSubmitTextMsg( $this->getMessagePrefix() . '-submit' );
252 /* submit form every time */
253 $form->setMethod( 'get' );
254 }
255
256 protected function getGroupName() {
257 return 'redirects';
258 }
259 }