BUG#76 For categories, don't use the Category:-prefix for the sortkey.
[lhc/web/wiklou.git] / includes / SpecialPreferences.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 * @subpackage SpecialPage
6 */
7
8 /**
9 *
10 */
11 function wfSpecialPreferences() {
12 global $wgRequest;
13
14 $form = new PreferencesForm( $wgRequest );
15 $form->execute();
16 }
17
18 /**
19 *
20 * @package MediaWiki
21 * @subpackage SpecialPage
22 */
23 class PreferencesForm {
24 var $mQuickbar, $mOldpass, $mNewpass, $mRetypePass, $mStubs;
25 var $mRows, $mCols, $mSkin, $mMath, $mDate, $mUserEmail, $mEmailFlag, $mNick;
26 var $mSearch, $mRecent, $mHourDiff, $mSearchLines, $mSearchChars, $mAction;
27 var $mReset, $mPosted, $mToggles, $mSearchNs, $mRealName;
28
29 function PreferencesForm( &$request ) {
30 global $wgLang, $wgAllowRealName;
31
32 $this->mQuickbar = $request->getVal( 'wpQuickbar' );
33 $this->mOldpass = $request->getVal( 'wpOldpass' );
34 $this->mNewpass = $request->getVal( 'wpNewpass' );
35 $this->mRetypePass =$request->getVal( 'wpRetypePass' );
36 $this->mStubs = $request->getVal( 'wpStubs' );
37 $this->mRows = $request->getVal( 'wpRows' );
38 $this->mCols = $request->getVal( 'wpCols' );
39 $this->mSkin = $request->getVal( 'wpSkin' );
40 $this->mMath = $request->getVal( 'wpMath' );
41 $this->mDate = $request->getVal( 'wpDate' );
42 $this->mUserEmail = $request->getVal( 'wpUserEmail' );
43 $this->mRealName = ($wgAllowRealName) ? $request->getVal( 'wpRealName' ) : '';
44 $this->mEmailFlag = $request->getCheck( 'wpEmailFlag' ) ? 1 : 0;
45 $this->mNick = $request->getVal( 'wpNick' );
46 $this->mSearch = $request->getVal( 'wpSearch' );
47 $this->mRecent = $request->getVal( 'wpRecent' );
48 $this->mHourDiff = $request->getVal( 'wpHourDiff' );
49 $this->mSearchLines = $request->getVal( 'wpSearchLines' );
50 $this->mSearchChars = $request->getVal( 'wpSearchChars' );
51 $this->mAction = $request->getVal( 'action' );
52 $this->mReset = $request->getCheck( 'wpReset' );
53 $this->mPosted = $request->wasPosted();
54 $this->mSaveprefs = $request->getCheck( 'wpSaveprefs' ) && $this->mPosted;
55
56 # User toggles (the big ugly unsorted list of checkboxes)
57 $this->mToggles = array();
58 if ( $this->mPosted ) {
59 $togs = $wgLang->getUserToggles();
60 foreach ( $togs as $tname ) {
61 $this->mToggles[$tname] = $request->getCheck( "wpOp$tname" ) ? 1 : 0;
62 }
63 }
64
65 $this->mUsedToggles = array();
66
67 # Search namespace options
68 # Note: namespaces don't necessarily have consecutive keys
69 $this->mSearchNs = array();
70 if ( $this->mPosted ) {
71 $namespaces = $wgLang->getNamespaces();
72 foreach ( $namespaces as $i => $namespace ) {
73 if ( $i >= 0 ) {
74 $this->mSearchNs[$i] = $request->getCheck( "wpNs$i" ) ? 1 : 0;
75 }
76 }
77 }
78 }
79
80 function execute() {
81 global $wgUser, $wgOut, $wgUseDynamicDates;
82
83 if ( 0 == $wgUser->getID() ) {
84 $wgOut->errorpage( "prefsnologin", "prefsnologintext" );
85 return;
86 }
87 if ( wfReadOnly() ) {
88 $wgOut->readOnlyPage();
89 return;
90 }
91 if ( $this->mReset ) {
92 $this->resetPrefs();
93 $this->mainPrefsForm( wfMsg( "prefsreset" ) );
94 } else if ( $this->mSaveprefs ) {
95 $this->savePreferences();
96 } else {
97 $this->resetPrefs();
98 $this->mainPrefsForm( "" );
99 }
100 }
101
102 /* private */ function validateInt( &$val, $min=0, $max=0x7fffffff ) {
103 $val = intval($val);
104 $val = min($val, $max);
105 $val = max($val, $min);
106 return $val;
107 }
108
109 /* private */ function validateIntOrNull( &$val, $min=0, $max=0x7fffffff ) {
110 $val = trim($val);
111 if($val === "") {
112 return $val;
113 } else {
114 return $this->validateInt( $val, $min, $max );
115 }
116 }
117
118 /* private */ function validateTimeZone( $s )
119 {
120
121 if ( $s !== "" ) {
122 if ( strpos( $s, ":" ) ) {
123 # HH:MM
124 $array = explode( ":" , $s );
125 $hour = intval( $array[0] );
126 $minute = intval( $array[1] );
127 } else {
128 $minute = intval( $s * 60 );
129 $hour = intval( $minute / 60 );
130 $minute = abs( $minute ) % 60;
131 }
132 $hour = min( $hour, 15 );
133 $hour = max( $hour, -15 );
134 $minute = min( $minute, 59 );
135 $minute = max( $minute, 0 );
136 $s = sprintf( "%02d:%02d", $hour, $minute );
137 }
138 return $s;
139 }
140
141 /* private */ function savePreferences()
142 {
143 global $wgUser, $wgLang, $wgDeferredUpdateList, $wgOut;
144
145 if ( "" != $this->mNewpass ) {
146 if ( $this->mNewpass != $this->mRetypePass ) {
147 $this->mainPrefsForm( wfMsg( "badretype" ) );
148 return;
149 }
150
151 if (!$wgUser->checkPassword( $this->mOldpass )) {
152 $this->mainPrefsForm( wfMsg( "wrongpassword" ) );
153 return;
154 }
155 $wgUser->setPassword( $this->mNewpass );
156 }
157 $wgUser->setEmail( $this->mUserEmail );
158 $wgUser->setRealName( $this->mRealName );
159 $wgUser->setOption( "nickname", $this->mNick );
160 $wgUser->setOption( "quickbar", $this->mQuickbar );
161 $wgUser->setOption( "skin", $this->mSkin );
162 $wgUser->setOption( "math", $this->mMath );
163 $wgUser->setOption( "date", $this->mDate );
164 $wgUser->setOption( "searchlimit", $this->validateIntOrNull( $this->mSearch ) );
165 $wgUser->setOption( "contextlines", $this->validateIntOrNull( $this->mSearchLines ) );
166 $wgUser->setOption( "contextchars", $this->validateIntOrNull( $this->mSearchChars ) );
167 $wgUser->setOption( "rclimit", $this->validateIntOrNull( $this->mRecent ) );
168 $wgUser->setOption( "rows", $this->validateInt( $this->mRows, 4, 1000 ) );
169 $wgUser->setOption( "cols", $this->validateInt( $this->mCols, 4, 1000 ) );
170 $wgUser->setOption( "stubthreshold", $this->validateIntOrNull( $this->mStubs ) );
171 $wgUser->setOption( "timecorrection", $this->validateTimeZone( $this->mHourDiff, -12, 14 ) );
172
173 # Set search namespace options
174 foreach( $this->mSearchNs as $i => $value ) {
175 $wgUser->setOption( "searchNs{$i}", $value );
176 }
177
178 $wgUser->setOption( "disablemail", $this->mEmailFlag );
179
180 # Set user toggles
181 foreach ( $this->mToggles as $tname => $tvalue ) {
182 $wgUser->setOption( $tname, $tvalue );
183 }
184 $wgUser->setCookies();
185 $up = new UserUpdate();
186 array_push( $wgDeferredUpdateList, $up );
187 $wgOut->setParserOptions( ParserOptions::newFromUser( $wgUser ) );
188 $po = ParserOptions::newFromUser( $wgUser );
189 $this->mainPrefsForm( wfMsg( "savedprefs" ) );
190 }
191
192 /* private */ function resetPrefs()
193 {
194 global $wgUser, $wgLang, $wgAllowRealName;
195
196 $this->mOldpass = $this->mNewpass = $this->mRetypePass = "";
197 $this->mUserEmail = $wgUser->getEmail();
198 $this->mRealName = ($wgAllowRealName) ? $wgUser->getRealName() : '';
199 if ( 1 == $wgUser->getOption( "disablemail" ) ) { $this->mEmailFlag = 1; }
200 else { $this->mEmailFlag = 0; }
201 $this->mNick = $wgUser->getOption( "nickname" );
202
203 $this->mQuickbar = $wgUser->getOption( "quickbar" );
204 $this->mSkin = $wgUser->getOption( "skin" );
205 $this->mMath = $wgUser->getOption( "math" );
206 $this->mDate = $wgUser->getOption( "date" );
207 $this->mRows = $wgUser->getOption( "rows" );
208 $this->mCols = $wgUser->getOption( "cols" );
209 $this->mStubs = $wgUser->getOption( "stubthreshold" );
210 $this->mHourDiff = $wgUser->getOption( "timecorrection" );
211 $this->mSearch = $wgUser->getOption( "searchlimit" );
212 $this->mSearchLines = $wgUser->getOption( "contextlines" );
213 $this->mSearchChars = $wgUser->getOption( "contextchars" );
214 $this->mRecent = $wgUser->getOption( "rclimit" );
215
216 $togs = $wgLang->getUserToggles();
217 foreach ( $togs as $tname ) {
218 $ttext = wfMsg("tog-".$tname);
219 $this->mToggles[$tname] = $wgUser->getOption( $tname );
220 }
221
222 $namespaces = $wgLang->getNamespaces();
223 foreach ( $namespaces as $i => $namespace ) {
224 if ( $i >= 0 ) {
225 $this->mSearchNs[$i] = $wgUser->getOption( "searchNs$i" );
226 }
227 }
228 }
229
230 /* private */ function namespacesCheckboxes()
231 {
232 global $wgLang, $wgUser;
233
234 # Determine namespace checkboxes
235 $namespaces = $wgLang->getNamespaces();
236 $r1 = "";
237
238 foreach ( $namespaces as $i => $name ) {
239 # Skip special or anything similar
240 if ( $i >= 0 ) {
241 $checked = "";
242 if ( $this->mSearchNs[$i] ) {
243 $checked = ' checked="checked"';
244 }
245 $name = str_replace( "_", " ", $namespaces[$i] );
246 if ( "" == $name ) {
247 $name = wfMsg( "blanknamespace" );
248 }
249
250 if ( 0 != $i ) {
251 $r1 .= " ";
252 }
253 $r1 .= "<label><input type='checkbox' value=\"1\" name=\"" .
254 "wpNs$i\"{$checked} />{$name}</label>\n";
255 }
256 }
257
258 return $r1;
259 }
260
261
262 function getToggle( $tname ) {
263 global $wgUser, $wgLang;
264
265 $this->mUsedToggles[$tname] = true;
266 $ttext = $wgLang->getUserToggle( $tname );
267
268 if ( 1 == $wgUser->getOption( $tname ) ) {
269 $checked = ' checked="checked"';
270 } else {
271 $checked = "";
272 }
273 return "<div><input type='checkbox' value=\"1\" "
274 . "id=\"$tname\" name=\"wpOp$tname\"$checked /><label for=\"$tname\">$ttext</label></div>\n";
275 }
276
277 /* private */ function mainPrefsForm( $err )
278 {
279 global $wgUser, $wgOut, $wgLang, $wgUseDynamicDates, $wgValidSkinNames;
280 global $wgAllowRealName;
281
282 $wgOut->setPageTitle( wfMsg( "preferences" ) );
283 $wgOut->setArticleRelated( false );
284 $wgOut->setRobotpolicy( "noindex,nofollow" );
285
286 if ( "" != $err ) {
287 $wgOut->addHTML( "<p class='error'>" . htmlspecialchars( $err ) . "</p>\n" );
288 }
289 $uname = $wgUser->getName();
290 $uid = $wgUser->getID();
291
292 $wgOut->addWikiText( wfMsg( "prefslogintext", $uname, $uid ) );
293 $wgOut->addWikiText( wfMsg('clearyourcache'));
294
295 $qbs = $wgLang->getQuickbarSettings();
296 $skinNames = $wgLang->getSkinNames();
297 $mathopts = $wgLang->getMathNames();
298 $dateopts = $wgLang->getDateFormats();
299 $togs = $wgLang->getUserToggles();
300
301 $titleObj = Title::makeTitle( NS_SPECIAL, "Preferences" );
302 $action = $titleObj->escapeLocalURL();
303
304 $qb = wfMsg( "qbsettings" );
305 $cp = wfMsg( "changepassword" );
306 $sk = wfMsg( "skin" );
307 $math = wfMsg( "math" );
308 $dateFormat = wfMsg("dateformat");
309 $opw = wfMsg( "oldpassword" );
310 $npw = wfMsg( "newpassword" );
311 $rpw = wfMsg( "retypenew" );
312 $svp = wfMsg( "saveprefs" );
313 $rsp = wfMsg( "resetprefs" );
314 $tbs = wfMsg( "textboxsize" );
315 $tbr = wfMsg( "rows" );
316 $tbc = wfMsg( "columns" );
317 $ltz = wfMsg( "localtime" );
318 $timezone = wfMsg( "timezonelegend" );
319 $tzt = wfMsg( "timezonetext" );
320 $tzo = wfMsg( "timezoneoffset" );
321 $tzGuess = wfMsg( "guesstimezone" );
322 $tzServerTime = wfMsg( "servertime" );
323 $yem = wfMsg( "youremail" );
324 $yrn = ($wgAllowRealName) ? wfMsg( "yourrealname" ) : '';
325 $emf = wfMsg( "emailflag" );
326 $ynn = wfMsg( "yournick" );
327 $stt = wfMsg ( "stubthreshold" ) ;
328 $srh = wfMsg( "searchresultshead" );
329 $rpp = wfMsg( "resultsperpage" );
330 $scl = wfMsg( "contextlines" );
331 $scc = wfMsg( "contextchars" );
332 $rcc = wfMsg( "recentchangescount" );
333 $dsn = wfMsg( "defaultns" );
334
335 $wgOut->addHTML( "<form id=\"preferences\" name=\"preferences\" action=\"$action\"
336 method=\"post\">" );
337
338 # First section: identity
339 # Email, etc.
340 #
341 $this->mUserEmail = htmlspecialchars( $this->mUserEmail );
342 $this->mRealName = htmlspecialchars( $this->mRealName );
343 $this->mNick = htmlspecialchars( $this->mNick );
344 if ( $this->mEmailFlag ) { $emfc = 'checked="checked"'; }
345 else { $emfc = ""; }
346
347 $ps = $this->namespacesCheckboxes();
348
349 $wgOut->addHTML( "<fieldset>
350 <legend>".wfMsg('prefs-personal')."</legend>");
351 if ($wgAllowRealName) {
352 $wgOut->addHTML("<div><label>$yrn: <input type='text' name=\"wpRealName\" value=\"{$this->mRealName}\" size='20' /></label></div>");
353 }
354 $wgOut->addHTML("
355 <div><label>$yem: <input type='text' name=\"wpUserEmail\" value=\"{$this->mUserEmail}\" size='20' /></label></div>
356 <div><label><input type='checkbox' $emfc value=\"1\" name=\"wpEmailFlag\" /> $emf</label></div>
357 <div><label>$ynn: <input type='text' name=\"wpNick\" value=\"{$this->mNick}\" size='12' /></label></div>\n" );
358
359 # Fields for changing password
360 #
361 $this->mOldpass = htmlspecialchars( $this->mOldpass );
362 $this->mNewpass = htmlspecialchars( $this->mNewpass );
363 $this->mRetypePass = htmlspecialchars( $this->mRetypePass );
364
365 $wgOut->addHTML( "<fieldset>
366 <legend>$cp</legend>
367 <div><label>$opw: <input type='password' name=\"wpOldpass\" value=\"{$this->mOldpass}\" size='20' /></label></div>
368 <div><label>$npw: <input type='password' name=\"wpNewpass\" value=\"{$this->mNewpass}\" size='20' /></label></div>
369 <div><label>$rpw: <input type='password' name=\"wpRetypePass\" value=\"{$this->mRetypePass}\" size='20' /></label></div>
370 " . $this->getToggle( "rememberpassword" ) . "
371 </fieldset>
372 <div class='prefsectiontip'>".wfMsg('prefs-help-userdata')."</div>\n</fieldset>\n" );
373
374
375 # Quickbar setting
376 #
377 $wgOut->addHtml( "<fieldset>\n<legend>$qb</legend>\n" );
378 for ( $i = 0; $i < count( $qbs ); ++$i ) {
379 if ( $i == $this->mQuickbar ) { $checked = ' checked="checked"'; }
380 else { $checked = ""; }
381 $wgOut->addHTML( "<div><label><input type='radio' name=\"wpQuickbar\"
382 value=\"$i\"$checked /> {$qbs[$i]}</label></div>\n" );
383 }
384 $wgOut->addHtml('<div class="prefsectiontip">'.wfMsg('qbsettingsnote').'</div>');
385 $wgOut->addHtml( "</fieldset>\n\n" );
386
387 # Skin setting
388 #
389 $wgOut->addHTML( "<fieldset>\n<legend>$sk</legend>\n" );
390 # Only show members of $wgValidSkinNames rather than
391 # $skinNames (skins is all skin names from Language.php)
392 foreach ($wgValidSkinNames as $skinkey => $skinname ) {
393 if ( $skinkey == $this->mSkin ) {
394 $checked = ' checked="checked"';
395 } else {
396 $checked = "";
397 }
398 if ( isset( $skinNames[$skinkey] ) ) {
399 $sn = $skinNames[$skinkey];
400 } else {
401 $sn = $skinname;
402 }
403 $wgOut->addHTML( "<div><label><input type='radio' name=\"wpSkin\"
404 value=\"$skinkey\"$checked /> {$sn}</label></div>\n" );
405 }
406 $wgOut->addHTML( "</fieldset>\n\n" );
407
408 # Math setting
409 #
410 $wgOut->addHTML( "<fieldset>\n<legend>$math</legend>\n" );
411 for ( $i = 0; $i < count( $mathopts ); ++$i ) {
412 if ( $i == $this->mMath ) { $checked = ' checked="checked"'; }
413 else { $checked = ""; }
414 $wgOut->addHTML( "<div><label><input type='radio' name=\"wpMath\"
415 value=\"$i\"$checked /> ".wfMsg($mathopts[$i])."</label></div>\n" );
416 }
417 $wgOut->addHTML( "</fieldset>\n\n" );
418
419 # Date format
420 #
421 if ( $wgUseDynamicDates ) {
422 $wgOut->addHTML( "<fieldset>\n<legend>$dateFormat</legend>\n" );
423 for ( $i = 0; $i < count( $dateopts ); ++$i) {
424 if ( $i == $this->mDate ) {
425 $checked = ' checked="checked"';
426 } else {
427 $checked = "";
428 }
429 $wgOut->addHTML( "<div><label><input type='radio' name=\"wpDate\" ".
430 "value=\"$i\"$checked /> {$dateopts[$i]}</label></div>\n" );
431 }
432 $wgOut->addHTML( "</fieldset>\n\n");
433 }
434
435 # Textbox rows, cols
436 #
437 $nowlocal = $wgLang->time( $now = wfTimestampNow(), true );
438 $nowserver = $wgLang->time( $now, false );
439 $wgOut->addHTML( "<fieldset>
440 <legend>$tbs</legend>\n
441 <div>
442 <label>$tbr: <input type='text' name=\"wpRows\" value=\"{$this->mRows}\" size='6' /></label>
443 <label>$tbc: <input type='text' name=\"wpCols\" value=\"{$this->mCols}\" size='6' /></label>
444 </div> " .
445 $this->getToggle( "editwidth" ) .
446 $this->getToggle( "showtoolbar" ) .
447 $this->getToggle( "previewontop" ) .
448 $this->getToggle( "watchdefault" ) .
449 $this->getToggle( "minordefault" ) . "
450 </fieldset>
451
452 <fieldset>
453 <legend>$timezone</legend>
454 <div><b>$tzServerTime:</b> $nowserver</div>
455 <div><b>$ltz:</b> $nowlocal</div>
456 <div><label>$tzo*: <input type='text' name=\"wpHourDiff\" value=\"{$this->mHourDiff}\" size='6' /></label></div>
457 <div><input type=\"button\" value=\"$tzGuess\" onClick=\"javascript:guessTimezone()\" id=\"guesstimezonebutton\" style=\"display:none\" /></div>
458 <div class='prefsectiontip'>* {$tzt}</div>
459 </fieldset>\n\n" );
460
461 $wgOut->addHTML( "
462 <fieldset><legend>".wfMsg('prefs-rc')."</legend>
463 <div><label>$rcc: <input type='text' name=\"wpRecent\" value=\"$this->mRecent\" size='6' /></label></div>
464 " . $this->getToggle( "hideminor" ) .
465 $this->getToggle( "usenewrc" ) . "
466 <div><label>$stt: <input type='text' name=\"wpStubs\" value=\"$this->mStubs\" size='6' /></label></div>
467 </fieldset>
468
469 <fieldset>
470 <legend>$srh</legend>
471 <div><label>$rpp: <input type='text' name=\"wpSearch\" value=\"$this->mSearch\" size='6' /></label></div>
472 <div><label>$scl: <input type='text' name=\"wpSearchLines\" value=\"$this->mSearchLines\" size='6' /></label></div>
473 <div><label>$scc: <input type='text' name=\"wpSearchChars\" value=\"$this->mSearchChars\" size='6' /></label></div>
474
475 <fieldset>
476 <legend>$dsn</legend>
477 $ps
478 </fieldset>
479 </fieldset>
480 " );
481
482 # Various checkbox options
483 #
484 $wgOut->addHTML("<fieldset><legend>".wfMsg('prefs-misc')."</legend>");
485 foreach ( $togs as $tname ) {
486 if( !array_key_exists( $tname, $this->mUsedToggles ) ) {
487 $wgOut->addHTML( $this->getToggle( $tname ) );
488 }
489 }
490 $wgOut->addHTML( "</fieldset>\n\n" );
491
492 $wgOut->addHTML( "
493 <div id='prefsubmit'>
494 <div>
495 <input type='submit' name=\"wpSaveprefs\" value=\"$svp\" accesskey=\"".
496 wfMsg('accesskey-save')."\" title=\"[alt-".wfMsg('accesskey-save')."]\" />
497 <input type='submit' name=\"wpReset\" value=\"$rsp\" />
498 </div>
499
500 </div>
501
502 </form>\n" );
503 }
504 }
505 ?>