]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/specials/SpecialPreferences.php
MediaWiki 1.15.4-scripts
[autoinstallsdev/mediawiki.git] / includes / specials / SpecialPreferences.php
1 <?php
2 /**
3  * Hold things related to displaying and saving user preferences.
4  * @file
5  * @ingroup SpecialPage
6  */
7
8 /**
9  * Entry point that create the "Preferences" object
10  */
11 function wfSpecialPreferences() {
12         global $wgRequest;
13
14         $form = new PreferencesForm( $wgRequest );
15         $form->execute();
16 }
17
18 /**
19  * Preferences form handling
20  * This object will show the preferences form and can save it as well.
21  * @ingroup SpecialPage
22  */
23 class PreferencesForm {
24         var $mQuickbar, $mStubs;
25         var $mRows, $mCols, $mSkin, $mMath, $mDate, $mUserEmail, $mEmailFlag, $mNick;
26         var $mUserLanguage, $mUserVariant;
27         var $mSearch, $mRecent, $mRecentDays, $mTimeZone, $mHourDiff, $mSearchLines, $mSearchChars, $mAction;
28         var $mReset, $mPosted, $mToggles, $mSearchNs, $mRealName, $mImageSize;
29         var $mUnderline, $mWatchlistEdits, $mGender;
30
31         /**
32          * Constructor
33          * Load some values
34          */
35         function __construct( &$request ) {
36                 global $wgContLang, $wgUser, $wgAllowRealName;
37
38                 $this->mQuickbar = $request->getVal( 'wpQuickbar' );
39                 $this->mStubs = $request->getVal( 'wpStubs' );
40                 $this->mRows = $request->getVal( 'wpRows' );
41                 $this->mCols = $request->getVal( 'wpCols' );
42                 $this->mSkin = Skin::normalizeKey( $request->getVal( 'wpSkin' ) );
43                 $this->mMath = $request->getVal( 'wpMath' );
44                 $this->mDate = $request->getVal( 'wpDate' );
45                 $this->mUserEmail = $request->getVal( 'wpUserEmail' );
46                 $this->mRealName = $wgAllowRealName ? $request->getVal( 'wpRealName' ) : '';
47                 $this->mEmailFlag = $request->getCheck( 'wpEmailFlag' ) ? 0 : 1;
48                 $this->mNick = $request->getVal( 'wpNick' );
49                 $this->mUserLanguage = $request->getVal( 'wpUserLanguage' );
50                 $this->mUserVariant = $request->getVal( 'wpUserVariant' );
51                 $this->mSearch = $request->getVal( 'wpSearch' );
52                 $this->mRecent = $request->getVal( 'wpRecent' );
53                 $this->mRecentDays = $request->getVal( 'wpRecentDays' );
54                 $this->mTimeZone = $request->getVal( 'wpTimeZone' );
55                 $this->mHourDiff = $request->getVal( 'wpHourDiff' );
56                 $this->mSearchLines = $request->getVal( 'wpSearchLines' );
57                 $this->mSearchChars = $request->getVal( 'wpSearchChars' );
58                 $this->mImageSize = $request->getVal( 'wpImageSize' );
59                 $this->mThumbSize = $request->getInt( 'wpThumbSize' );
60                 $this->mUnderline = $request->getInt( 'wpOpunderline' );
61                 $this->mAction = $request->getVal( 'action' );
62                 $this->mReset = $request->getCheck( 'wpReset' );
63                 $this->mRestoreprefs = $request->getCheck( 'wpRestore' );
64                 $this->mPosted = $request->wasPosted();
65                 $this->mSuccess = $request->getCheck( 'success' );
66                 $this->mWatchlistDays = $request->getVal( 'wpWatchlistDays' );
67                 $this->mWatchlistEdits = $request->getVal( 'wpWatchlistEdits' );
68                 $this->mDisableMWSuggest = $request->getCheck( 'wpDisableMWSuggest' );
69                 $this->mGender = $request->getVal( 'wpGender' );
70
71                 $this->mSaveprefs = $request->getCheck( 'wpSaveprefs' ) &&
72                         $this->mPosted &&
73                         $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
74
75                 # User toggles  (the big ugly unsorted list of checkboxes)
76                 $this->mToggles = array();
77                 if ( $this->mPosted ) {
78                         $togs = User::getToggles();
79                         foreach ( $togs as $tname ) {
80                                 $this->mToggles[$tname] = $request->getCheck( "wpOp$tname" ) ? 1 : 0;
81                         }
82                 }
83
84                 $this->mUsedToggles = array();
85
86                 # Search namespace options
87                 # Note: namespaces don't necessarily have consecutive keys
88                 $this->mSearchNs = array();
89                 if ( $this->mPosted ) {
90                         $namespaces = $wgContLang->getNamespaces();
91                         foreach ( $namespaces as $i => $namespace ) {
92                                 if ( $i >= 0 ) {
93                                         $this->mSearchNs[$i] = $request->getCheck( "wpNs$i" ) ? 1 : 0;
94                                 }
95                         }
96                 }
97
98                 # Validate language
99                 if ( !preg_match( '/^[a-z\-]*$/', $this->mUserLanguage ) ) {
100                         $this->mUserLanguage = 'nolanguage';
101                 }
102
103                 wfRunHooks( 'InitPreferencesForm', array( $this, $request ) );
104         }
105
106         function execute() {
107                 global $wgUser, $wgOut, $wgTitle;
108
109                 if ( $wgUser->isAnon() ) {
110                         $wgOut->showErrorPage( 'prefsnologin', 'prefsnologintext', array($wgTitle->getPrefixedDBkey()) );
111                         return;
112                 }
113                 if ( wfReadOnly() ) {
114                         $wgOut->readOnlyPage();
115                         return;
116                 }
117                 if ( $this->mReset ) {
118                         $this->resetPrefs();
119                         $this->mainPrefsForm( 'reset', wfMsg( 'prefsreset' ) );
120                 } else if ( $this->mSaveprefs ) {
121                         $this->savePreferences();
122                 } else if ( $this->mRestoreprefs ) {
123                         $this->restorePreferences();
124                 } else {
125                         $this->resetPrefs();
126                         $this->mainPrefsForm( '' );
127                 }
128         }
129         /**
130          * @access private
131          */
132         function validateInt( &$val, $min=0, $max=0x7fffffff ) {
133                 $val = intval($val);
134                 $val = min($val, $max);
135                 $val = max($val, $min);
136                 return $val;
137         }
138
139         /**
140          * @access private
141          */
142         function validateFloat( &$val, $min, $max=0x7fffffff ) {
143                 $val = floatval( $val );
144                 $val = min( $val, $max );
145                 $val = max( $val, $min );
146                 return( $val );
147         }
148
149         /**
150          * @access private
151          */
152         function validateIntOrNull( &$val, $min=0, $max=0x7fffffff ) {
153                 $val = trim($val);
154                 if($val === '') {
155                         return null;
156                 } else {
157                         return $this->validateInt( $val, $min, $max );
158                 }
159         }
160
161         /**
162          * @access private
163          */
164         function validateDate( $val ) {
165                 global $wgLang, $wgContLang;
166                 if ( $val !== false && (
167                         in_array( $val, (array)$wgLang->getDatePreferences() ) ||
168                         in_array( $val, (array)$wgContLang->getDatePreferences() ) ) )
169                 {
170                         return $val;
171                 } else {
172                         return $wgLang->getDefaultDateFormat();
173                 }
174         }
175
176         /**
177          * Used to validate the user inputed timezone before saving it as
178          * 'timecorrection', will return 'System' if fed bogus data.
179          * @access private
180          * @param string $tz the user input Zoneinfo timezone
181          * @param string $s  the user input offset string
182          * @return string
183          */
184         function validateTimeZone( $tz, $s ) {
185                 $data = explode( '|', $tz, 3 );
186                 switch ( $data[0] ) {
187                         case 'ZoneInfo':
188                         case 'System':
189                                 return $tz;
190                         case 'Offset':
191                         default:
192                                 $data = explode( ':', $s, 2 );
193                                 $minDiff = 0;
194                                 if( count( $data ) == 2 ) {
195                                         $data[0] = intval( $data[0] );
196                                         $data[1] = intval( $data[1] );
197                                         $minDiff = abs( $data[0] ) * 60 + $data[1];
198                                         if ( $data[0] < 0 ) $minDiff = -$minDiff;
199                                 } else {
200                                         $minDiff = intval( $data[0] ) * 60;
201                                 }
202
203                                 # Max is +14:00 and min is -12:00, see:
204                                 # http://en.wikipedia.org/wiki/Timezone
205                                 $minDiff = min( $minDiff, 840 );  # 14:00
206                                 $minDiff = max( $minDiff, -720 ); # -12:00
207                                 return 'Offset|'.$minDiff;
208                 }
209         }
210
211         function validateGender( $val ) {
212                 $valid = array( 'male', 'female', 'unknown' );
213                 if ( in_array($val, $valid) ) {
214                         return $val;
215                 } else {
216                         return User::getDefaultOption( 'gender' );
217                 }
218         }
219
220         /**
221          * @access private
222          */
223         function savePreferences() {
224                 global $wgUser, $wgOut, $wgParser;
225                 global $wgEnableUserEmail, $wgEnableEmail;
226                 global $wgEmailAuthentication, $wgRCMaxAge;
227                 global $wgAuth, $wgEmailConfirmToEdit;
228
229                 $wgUser->setRealName( $this->mRealName );
230                 $oldOptions = $wgUser->mOptions;
231
232                 if( $wgUser->getOption( 'language' ) !== $this->mUserLanguage ) {
233                         $needRedirect = true;
234                 } else {
235                         $needRedirect = false;
236                 }
237
238                 # Validate the signature and clean it up as needed
239                 global $wgMaxSigChars;
240                 if( mb_strlen( $this->mNick ) > $wgMaxSigChars ) {
241                         global $wgLang;
242                         $this->mainPrefsForm( 'error',
243                                 wfMsgExt( 'badsiglength', 'parsemag', $wgLang->formatNum( $wgMaxSigChars ) ) );
244                         return;
245                 } elseif( $this->mToggles['fancysig'] ) {
246                         if( $wgParser->validateSig( $this->mNick ) !== false ) {
247                                 $this->mNick = $wgParser->cleanSig( $this->mNick );
248                         } else {
249                                 $this->mainPrefsForm( 'error', wfMsg( 'badsig' ) );
250                                 return;
251                         }
252                 } else {
253                         // When no fancy sig used, make sure ~{3,5} get removed.
254                         $this->mNick = $wgParser->cleanSigInSig( $this->mNick );
255                 }
256
257                 $wgUser->setOption( 'language', $this->mUserLanguage );
258                 $wgUser->setOption( 'variant', $this->mUserVariant );
259                 $wgUser->setOption( 'nickname', $this->mNick );
260                 $wgUser->setOption( 'quickbar', $this->mQuickbar );
261                 global $wgAllowUserSkin;
262                 if( $wgAllowUserSkin ) {
263                         $wgUser->setOption( 'skin', $this->mSkin );
264                 }
265                 global $wgUseTeX;
266                 if( $wgUseTeX ) {
267                         $wgUser->setOption( 'math', $this->mMath );
268                 }
269                 $wgUser->setOption( 'date', $this->validateDate( $this->mDate ) );
270                 $wgUser->setOption( 'searchlimit', $this->validateIntOrNull( $this->mSearch ) );
271                 $wgUser->setOption( 'contextlines', $this->validateIntOrNull( $this->mSearchLines ) );
272                 $wgUser->setOption( 'contextchars', $this->validateIntOrNull( $this->mSearchChars ) );
273                 $wgUser->setOption( 'rclimit', $this->validateIntOrNull( $this->mRecent ) );
274                 $wgUser->setOption( 'rcdays', $this->validateInt($this->mRecentDays, 1, ceil($wgRCMaxAge / (3600*24))));
275                 $wgUser->setOption( 'wllimit', $this->validateIntOrNull( $this->mWatchlistEdits, 0, 1000 ) );
276                 $wgUser->setOption( 'rows', $this->validateInt( $this->mRows, 4, 1000 ) );
277                 $wgUser->setOption( 'cols', $this->validateInt( $this->mCols, 4, 1000 ) );
278                 $wgUser->setOption( 'stubthreshold', $this->validateIntOrNull( $this->mStubs ) );
279                 $wgUser->setOption( 'timecorrection', $this->validateTimeZone( $this->mTimeZone, $this->mHourDiff ) );
280                 $wgUser->setOption( 'imagesize', $this->mImageSize );
281                 $wgUser->setOption( 'thumbsize', $this->mThumbSize );
282                 $wgUser->setOption( 'underline', $this->validateInt($this->mUnderline, 0, 2) );
283                 $wgUser->setOption( 'watchlistdays', $this->validateFloat( $this->mWatchlistDays, 0, 7 ) );
284                 $wgUser->setOption( 'disablesuggest', $this->mDisableMWSuggest );
285                 $wgUser->setOption( 'gender', $this->validateGender( $this->mGender ) );
286
287                 # Set search namespace options
288                 foreach( $this->mSearchNs as $i => $value ) {
289                         $wgUser->setOption( "searchNs{$i}", $value );
290                 }
291
292                 if( $wgEnableEmail && $wgEnableUserEmail ) {
293                         $wgUser->setOption( 'disablemail', $this->mEmailFlag );
294                 }
295
296                 # Set user toggles
297                 foreach ( $this->mToggles as $tname => $tvalue ) {
298                         $wgUser->setOption( $tname, $tvalue );
299                 }
300
301                 $error = false;
302                 if( $wgEnableEmail ) {
303                         $newadr = $this->mUserEmail;
304                         $oldadr = $wgUser->getEmail();
305                         if( ($newadr != '') && ($newadr != $oldadr) ) {
306                                 # the user has supplied a new email address on the login page
307                                 if( $wgUser->isValidEmailAddr( $newadr ) ) {
308                                         # new behaviour: set this new emailaddr from login-page into user database record
309                                         $wgUser->setEmail( $newadr );
310                                         # but flag as "dirty" = unauthenticated
311                                         $wgUser->invalidateEmail();
312                                         if ($wgEmailAuthentication) {
313                                                 # Mail a temporary password to the dirty address.
314                                                 # User can come back through the confirmation URL to re-enable email.
315                                                 $result = $wgUser->sendConfirmationMail();
316                                                 if( WikiError::isError( $result ) ) {
317                                                         $error = wfMsg( 'mailerror', htmlspecialchars( $result->getMessage() ) );
318                                                 } else {
319                                                         $error = wfMsg( 'eauthentsent', $wgUser->getName() );
320                                                 }
321                                         }
322                                 } else {
323                                         $error = wfMsg( 'invalidemailaddress' );
324                                 }
325                         } else {
326                                 if( $wgEmailConfirmToEdit && empty( $newadr ) ) {
327                                         $this->mainPrefsForm( 'error', wfMsg( 'noemailtitle' ) );
328                                         return;
329                                 }
330                                 $wgUser->setEmail( $this->mUserEmail );
331                         }
332                         if( $oldadr != $newadr ) {
333                                 wfRunHooks( 'PrefsEmailAudit', array( $wgUser, $oldadr, $newadr ) );
334                         }
335                 }
336
337                 if( !$wgAuth->updateExternalDB( $wgUser ) ){
338                         $this->mainPrefsForm( 'error', wfMsg( 'externaldberror' ) );
339                         return;
340                 }
341
342                 $msg = '';
343                 if ( !wfRunHooks( 'SavePreferences', array( $this, $wgUser, &$msg, $oldOptions ) ) ) {
344                         $this->mainPrefsForm( 'error', $msg );
345                         return;
346                 }
347
348                 $wgUser->setCookies();
349                 $wgUser->saveSettings();
350
351                 if( $needRedirect && $error === false ) {
352                         $title = SpecialPage::getTitleFor( 'Preferences' );
353                         $wgOut->redirect( $title->getFullURL( 'success' ) );
354                         return;
355                 }
356
357                 $wgOut->parserOptions( ParserOptions::newFromUser( $wgUser ) );
358                 $this->mainPrefsForm( $error === false ? 'success' : 'error', $error);
359         }
360
361         /**
362          * @access private
363          */
364         function resetPrefs() {
365                 global $wgUser, $wgLang, $wgContLang, $wgContLanguageCode, $wgAllowRealName, $wgLocalTZoffset;
366
367                 $this->mUserEmail = $wgUser->getEmail();
368                 $this->mUserEmailAuthenticationtimestamp = $wgUser->getEmailAuthenticationtimestamp();
369                 $this->mRealName = ($wgAllowRealName) ? $wgUser->getRealName() : '';
370
371                 # language value might be blank, default to content language
372                 $this->mUserLanguage = $wgUser->getOption( 'language', $wgContLanguageCode );
373
374                 $this->mUserVariant = $wgUser->getOption( 'variant');
375                 $this->mEmailFlag = $wgUser->getOption( 'disablemail' ) == 1 ? 1 : 0;
376                 $this->mNick = $wgUser->getOption( 'nickname' );
377
378                 $this->mQuickbar = $wgUser->getOption( 'quickbar' );
379                 $this->mSkin = Skin::normalizeKey( $wgUser->getOption( 'skin' ) );
380                 $this->mMath = $wgUser->getOption( 'math' );
381                 $this->mDate = $wgUser->getDatePreference();
382                 $this->mRows = $wgUser->getOption( 'rows' );
383                 $this->mCols = $wgUser->getOption( 'cols' );
384                 $this->mStubs = $wgUser->getOption( 'stubthreshold' );
385
386                 $tz = $wgUser->getOption( 'timecorrection' );
387                 $data = explode( '|', $tz, 3 );
388                 $minDiff = null;
389                 switch ( $data[0] ) {
390                         case 'ZoneInfo':
391                                 $this->mTimeZone = $tz;
392                                 # Check if the specified TZ exists, and change to 'Offset' if 
393                                 # not.
394                                 if ( !function_exists('timezone_open') || @timezone_open( $data[2] ) === false ) {
395                                         $this->mTimeZone = 'Offset';
396                                         $minDiff = intval( $data[1] );
397                                 }
398                                 break;
399                         case '':
400                         case 'System':
401                                 $this->mTimeZone = 'System|'.$wgLocalTZoffset;
402                                 break;
403                         case 'Offset':
404                                 $this->mTimeZone = 'Offset';
405                                 $minDiff = intval( $data[1] );
406                                 break;
407                         default:
408                                 $this->mTimeZone = 'Offset';
409                                 $data = explode( ':', $tz, 2 );
410                                 if( count( $data ) == 2 ) {
411                                         $data[0] = intval( $data[0] );
412                                         $data[1] = intval( $data[1] );
413                                         $minDiff = abs( $data[0] ) * 60 + $data[1];
414                                         if ( $data[0] < 0 ) $minDiff = -$minDiff;
415                                 } else {
416                                         $minDiff = intval( $data[0] ) * 60;
417                                 }
418                                 break;
419                 }
420                 if ( is_null( $minDiff ) ) {
421                         $this->mHourDiff = '';
422                 } else {
423                         $this->mHourDiff = sprintf( '%+03d:%02d', floor($minDiff/60), abs($minDiff)%60 );
424                 }
425
426                 $this->mSearch = $wgUser->getOption( 'searchlimit' );
427                 $this->mSearchLines = $wgUser->getOption( 'contextlines' );
428                 $this->mSearchChars = $wgUser->getOption( 'contextchars' );
429                 $this->mImageSize = $wgUser->getOption( 'imagesize' );
430                 $this->mThumbSize = $wgUser->getOption( 'thumbsize' );
431                 $this->mRecent = $wgUser->getOption( 'rclimit' );
432                 $this->mRecentDays = $wgUser->getOption( 'rcdays' );
433                 $this->mWatchlistEdits = $wgUser->getOption( 'wllimit' );
434                 $this->mUnderline = $wgUser->getOption( 'underline' );
435                 $this->mWatchlistDays = $wgUser->getOption( 'watchlistdays' );
436                 $this->mDisableMWSuggest = $wgUser->getBoolOption( 'disablesuggest' );
437                 $this->mGender = $wgUser->getOption( 'gender' );
438
439                 $togs = User::getToggles();
440                 foreach ( $togs as $tname ) {
441                         $this->mToggles[$tname] = $wgUser->getOption( $tname );
442                 }
443
444                 $namespaces = $wgContLang->getNamespaces();
445                 foreach ( $namespaces as $i => $namespace ) {
446                         if ( $i >= NS_MAIN ) {
447                                 $this->mSearchNs[$i] = $wgUser->getOption( 'searchNs'.$i );
448                         }
449                 }
450
451                 wfRunHooks( 'ResetPreferences', array( $this, $wgUser ) );
452         }
453         
454         /**
455          * @access private
456          */
457         function restorePreferences() {
458                 global $wgUser, $wgOut;
459                 $wgUser->restoreOptions();
460                 $wgUser->setCookies();
461                 $wgUser->saveSettings();
462                 $title = SpecialPage::getTitleFor( 'Preferences' );
463                 $wgOut->redirect( $title->getFullURL( 'success' ) );
464         }
465
466         /**
467          * @access private
468          */
469         function namespacesCheckboxes() {
470                 global $wgContLang;
471
472                 # Determine namespace checkboxes
473                 $namespaces = $wgContLang->getNamespaces();
474                 $r1 = null;
475
476                 foreach ( $namespaces as $i => $name ) {
477                         if ($i < 0)
478                                 continue;
479                         $checked = $this->mSearchNs[$i] ? "checked='checked'" : '';
480                         $name = str_replace( '_', ' ', $namespaces[$i] );
481
482                         if ( empty($name) )
483                                 $name = wfMsg( 'blanknamespace' );
484
485                         $r1 .= "<input type='checkbox' value='1' name='wpNs$i' id='wpNs$i' {$checked}/> <label for='wpNs$i'>{$name}</label><br />\n";
486                 }
487                 return $r1;
488         }
489
490
491         function getToggle( $tname, $trailer = false, $disabled = false ) {
492                 global $wgUser, $wgLang;
493
494                 $this->mUsedToggles[$tname] = true;
495                 $ttext = $wgLang->getUserToggle( $tname );
496
497                 $checked = $wgUser->getOption( $tname ) == 1 ? ' checked="checked"' : '';
498                 $disabled = $disabled ? ' disabled="disabled"' : '';
499                 $trailer = $trailer ? $trailer : '';
500                 return "<div class='toggle'><input type='checkbox' value='1' id=\"$tname\" name=\"wpOp$tname\"$checked$disabled />" .
501                         " <span class='toggletext'><label for=\"$tname\">$ttext</label>$trailer</span></div>\n";
502         }
503
504         function getToggles( $items ) {
505                 $out = "";
506                 foreach( $items as $item ) {
507                         if( $item === false )
508                                 continue;
509                         if( is_array( $item ) ) {
510                                 list( $key, $trailer ) = $item;
511                         } else {
512                                 $key = $item;
513                                 $trailer = false;
514                         }
515                         $out .= $this->getToggle( $key, $trailer );
516                 }
517                 return $out;
518         }
519
520         function addRow($td1, $td2) {
521                 return "<tr><td class='mw-label'>$td1</td><td class='mw-input'>$td2</td></tr>";
522         }
523
524         /**
525          * Helper function for user information panel
526          * @param $td1 label for an item
527          * @param $td2 item or null
528          * @param $td3 optional help or null
529          * @return xhtml block
530          */
531         function tableRow( $td1, $td2 = null, $td3 = null ) {
532
533                 if ( is_null( $td3 ) ) {
534                         $td3 = '';
535                 } else {
536                         $td3 = Xml::tags( 'tr', null,
537                                 Xml::tags( 'td', array( 'class' => 'pref-label', 'colspan' => '2' ), $td3 )
538                         );
539                 }
540
541                 if ( is_null( $td2 ) ) {
542                         $td1 = Xml::tags( 'td', array( 'class' => 'pref-label', 'colspan' => '2' ), $td1 );
543                         $td2 = '';
544                 } else {
545                         $td1 = Xml::tags( 'td', array( 'class' => 'pref-label' ), $td1 );
546                         $td2 = Xml::tags( 'td', array( 'class' => 'pref-input' ), $td2 );
547                 }
548
549                 return Xml::tags( 'tr', null, $td1 . $td2 ). $td3 . "\n";
550
551         }
552
553         /**
554          * @access private
555          */
556         function mainPrefsForm( $status , $message = '' ) {
557                 global $wgUser, $wgOut, $wgLang, $wgContLang, $wgAuth;
558                 global $wgAllowRealName, $wgImageLimits, $wgThumbLimits;
559                 global $wgDisableLangConversion, $wgDisableTitleConversion;
560                 global $wgEnotifWatchlist, $wgEnotifUserTalk,$wgEnotifMinorEdits;
561                 global $wgRCShowWatchingUsers, $wgEnotifRevealEditorAddress;
562                 global $wgEnableEmail, $wgEnableUserEmail, $wgEmailAuthentication;
563                 global $wgContLanguageCode, $wgDefaultSkin, $wgCookieExpiration;
564                 global $wgEmailConfirmToEdit, $wgEnableMWSuggest, $wgLocalTZoffset;
565
566                 $wgOut->setPageTitle( wfMsg( 'preferences' ) );
567                 $wgOut->setArticleRelated( false );
568                 $wgOut->setRobotPolicy( 'noindex,nofollow' );
569                 $wgOut->addScriptFile( 'prefs.js' );
570
571                 $wgOut->disallowUserJs();  # Prevent hijacked user scripts from sniffing passwords etc.
572
573                 if ( $this->mSuccess || 'success' == $status ) {
574                         $wgOut->wrapWikiMsg( '<div class="successbox"><strong>$1</strong></div>', 'savedprefs' );
575                 } else  if ( 'error' == $status ) {
576                         $wgOut->addWikiText( '<div class="errorbox"><strong>' . $message  . '</strong></div>' );
577                 } else if ( '' != $status ) {
578                         $wgOut->addWikiText( $message . "\n----" );
579                 }
580
581                 $qbs = $wgLang->getQuickbarSettings();
582                 $mathopts = $wgLang->getMathNames();
583                 $dateopts = $wgLang->getDatePreferences();
584                 $togs = User::getToggles();
585
586                 $titleObj = SpecialPage::getTitleFor( 'Preferences' );
587
588                 # Pre-expire some toggles so they won't show if disabled
589                 $this->mUsedToggles[ 'shownumberswatching' ] = true;
590                 $this->mUsedToggles[ 'showupdated' ] = true;
591                 $this->mUsedToggles[ 'enotifwatchlistpages' ] = true;
592                 $this->mUsedToggles[ 'enotifusertalkpages' ] = true;
593                 $this->mUsedToggles[ 'enotifminoredits' ] = true;
594                 $this->mUsedToggles[ 'enotifrevealaddr' ] = true;
595                 $this->mUsedToggles[ 'ccmeonemails' ] = true;
596                 $this->mUsedToggles[ 'uselivepreview' ] = true;
597                 $this->mUsedToggles[ 'noconvertlink' ] = true;
598
599
600                 if ( !$this->mEmailFlag ) { $emfc = 'checked="checked"'; }
601                 else { $emfc = ''; }
602
603
604                 if ($wgEmailAuthentication && ($this->mUserEmail != '') ) {
605                         if( $wgUser->getEmailAuthenticationTimestamp() ) {
606                                 // date and time are separate parameters to facilitate localisation.
607                                 // $time is kept for backward compat reasons.
608                                 // 'emailauthenticated' is also used in SpecialConfirmemail.php
609                                 $time = $wgLang->timeAndDate( $wgUser->getEmailAuthenticationTimestamp(), true );
610                                 $d = $wgLang->date( $wgUser->getEmailAuthenticationTimestamp(), true );
611                                 $t = $wgLang->time( $wgUser->getEmailAuthenticationTimestamp(), true );
612                                 $emailauthenticated = wfMsg('emailauthenticated', $time, $d, $t ).'<br />';
613                                 $disableEmailPrefs = false;
614                         } else {
615                                 $disableEmailPrefs = true;
616                                 $skin = $wgUser->getSkin();
617                                 $emailauthenticated = wfMsg('emailnotauthenticated').'<br />' .
618                                         $skin->makeKnownLinkObj( SpecialPage::getTitleFor( 'Confirmemail' ),
619                                                 wfMsg( 'emailconfirmlink' ) ) . '<br />';
620                         }
621                 } else {
622                         $emailauthenticated = '';
623                         $disableEmailPrefs = false;
624                 }
625
626                 if ($this->mUserEmail == '') {
627                         $emailauthenticated = wfMsg( 'noemailprefs' ) . '<br />';
628                 }
629
630                 $ps = $this->namespacesCheckboxes();
631
632                 $enotifwatchlistpages = ($wgEnotifWatchlist) ? $this->getToggle( 'enotifwatchlistpages', false, $disableEmailPrefs ) : '';
633                 $enotifusertalkpages = ($wgEnotifUserTalk) ? $this->getToggle( 'enotifusertalkpages', false, $disableEmailPrefs ) : '';
634                 $enotifminoredits = ($wgEnotifWatchlist && $wgEnotifMinorEdits) ? $this->getToggle( 'enotifminoredits', false, $disableEmailPrefs ) : '';
635                 $enotifrevealaddr = (($wgEnotifWatchlist || $wgEnotifUserTalk) && $wgEnotifRevealEditorAddress) ? $this->getToggle( 'enotifrevealaddr', false, $disableEmailPrefs ) : '';
636
637                 # </FIXME>
638
639                 $wgOut->addHTML(
640                         Xml::openElement( 'form', array(
641                                 'action' => $titleObj->getLocalUrl(),
642                                 'method' => 'post',
643                                 'id'     => 'mw-preferences-form',
644                         ) ) .
645                         Xml::openElement( 'div', array( 'id' => 'preferences' ) )
646                 );
647
648                 # User data
649
650                 $wgOut->addHTML(
651                         Xml::fieldset( wfMsg('prefs-personal') ) .
652                         Xml::openElement( 'table' ) .
653                         $this->tableRow( Xml::element( 'h2', null, wfMsg( 'prefs-personal' ) ) )
654                 );
655
656                 # Get groups to which the user belongs
657                 $userEffectiveGroups = $wgUser->getEffectiveGroups();
658                 $userEffectiveGroupsArray = array();
659                 foreach( $userEffectiveGroups as $ueg ) {
660                         if( $ueg == '*' ) {
661                                 // Skip the default * group, seems useless here
662                                 continue;
663                         }
664                         $userEffectiveGroupsArray[] = User::makeGroupLinkHTML( $ueg );
665                 }
666                 asort( $userEffectiveGroupsArray );
667
668                 $sk = $wgUser->getSkin();
669                 $toolLinks = array();
670                 $toolLinks[] = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'ListGroupRights' ), wfMsg( 'listgrouprights' ) );
671                 # At the moment one tool link only but be prepared for the future...
672                 # FIXME: Add a link to Special:Userrights for users who are allowed to use it.
673                 # $wgUser->isAllowed( 'userrights' ) seems to strict in some cases
674
675                 $userInformationHtml =
676                         $this->tableRow( wfMsgHtml( 'username' ), htmlspecialchars( $wgUser->getName() ) ) .
677                         $this->tableRow( wfMsgHtml( 'uid' ), htmlspecialchars( $wgUser->getId() ) ) .
678
679                         $this->tableRow(
680                                 wfMsgExt( 'prefs-memberingroups', array( 'parseinline' ), count( $userEffectiveGroupsArray ) ),
681                                 $wgLang->commaList( $userEffectiveGroupsArray ) .
682                                 '<br />(' . $wgLang->pipeList( $toolLinks ) . ')'
683                         ) .
684
685                         $this->tableRow(
686                                 wfMsgHtml( 'prefs-edits' ),
687                                 $wgLang->formatNum( $wgUser->getEditCount() )
688                         );
689
690                 if( wfRunHooks( 'PreferencesUserInformationPanel', array( $this, &$userInformationHtml ) ) ) {
691                         $wgOut->addHTML( $userInformationHtml );
692                 }
693
694                 if ( $wgAllowRealName ) {
695                         $wgOut->addHTML(
696                                 $this->tableRow(
697                                         Xml::label( wfMsg('yourrealname'), 'wpRealName' ),
698                                         Xml::input( 'wpRealName', 25, $this->mRealName, array( 'id' => 'wpRealName' ) ),
699                                         Xml::tags('div', array( 'class' => 'prefsectiontip' ),
700                                                 wfMsgExt( 'prefs-help-realname', 'parseinline' )
701                                         )
702                                 )
703                         );
704                 }
705                 if ( $wgEnableEmail ) {
706                         $wgOut->addHTML(
707                                 $this->tableRow(
708                                         Xml::label( wfMsg('youremail'), 'wpUserEmail' ),
709                                         Xml::input( 'wpUserEmail', 25, $this->mUserEmail, array( 'id' => 'wpUserEmail' ) ),
710                                         Xml::tags('div', array( 'class' => 'prefsectiontip' ),
711                                                 wfMsgExt( $wgEmailConfirmToEdit ? 'prefs-help-email-required' : 'prefs-help-email', 'parseinline' )
712                                         )
713                                 )
714                         );
715                 }
716
717                 global $wgParser, $wgMaxSigChars;
718                 if( mb_strlen( $this->mNick ) > $wgMaxSigChars ) {
719                         $invalidSig = $this->tableRow(
720                                 '&nbsp;',
721                                 Xml::element( 'span', array( 'class' => 'error' ),
722                                         wfMsgExt( 'badsiglength', 'parsemag', $wgLang->formatNum( $wgMaxSigChars ) ) )
723                         );
724                 } elseif( !empty( $this->mToggles['fancysig'] ) &&
725                         false === $wgParser->validateSig( $this->mNick ) ) {
726                         $invalidSig = $this->tableRow(
727                                 '&nbsp;',
728                                 Xml::element( 'span', array( 'class' => 'error' ), wfMsg( 'badsig' ) )
729                         );
730                 } else {
731                         $invalidSig = '';
732                 }
733
734                 $wgOut->addHTML(
735                         $this->tableRow(
736                                 Xml::label( wfMsg( 'yournick' ), 'wpNick' ),
737                                 Xml::input( 'wpNick', 25, $this->mNick,
738                                         array(
739                                                 'id' => 'wpNick',
740                                                 // Note: $wgMaxSigChars is enforced in Unicode characters,
741                                                 // both on the backend and now in the browser.
742                                                 // Badly-behaved requests may still try to submit
743                                                 // an overlong string, however.
744                                                 'maxlength' => $wgMaxSigChars ) )
745                         ) .
746                         $invalidSig .
747                         $this->tableRow( '&nbsp;', $this->getToggle( 'fancysig' ) )
748                 );
749
750                 $gender = new XMLSelect( 'wpGender', 'wpGender', $this->mGender );
751                 $gender->addOption( wfMsg( 'gender-unknown' ), 'unknown' );
752                 $gender->addOption( wfMsg( 'gender-male' ), 'male' );
753                 $gender->addOption( wfMsg( 'gender-female' ), 'female' );
754
755                 $wgOut->addHTML(
756                         $this->tableRow(
757                                 Xml::label( wfMsg( 'yourgender' ), 'wpGender' ),
758                                 $gender->getHTML(),
759                                 Xml::tags( 'div', array( 'class' => 'prefsectiontip' ),
760                                         wfMsgExt( 'prefs-help-gender', 'parseinline' )
761                                 )
762                         )
763                 );
764
765                 list( $lsLabel, $lsSelect) = Xml::languageSelector( $this->mUserLanguage, false );
766                 $wgOut->addHTML(
767                         $this->tableRow( $lsLabel, $lsSelect )
768                 );
769
770                 /* see if there are multiple language variants to choose from*/
771                 if(!$wgDisableLangConversion) {
772                         $variants = $wgContLang->getVariants();
773                         $variantArray = array();
774
775                         $languages = Language::getLanguageNames( true );
776                         foreach($variants as $v) {
777                                 $v = str_replace( '_', '-', strtolower($v));
778                                 if( array_key_exists( $v, $languages ) ) {
779                                         // If it doesn't have a name, we'll pretend it doesn't exist
780                                         $variantArray[$v] = $languages[$v];
781                                 }
782                         }
783
784                         $options = "\n";
785                         foreach( $variantArray as $code => $name ) {
786                                 $selected = ($code == $this->mUserVariant);
787                                 $options .= Xml::option( "$code - $name", $code, $selected ) . "\n";
788                         }
789
790                         if(count($variantArray) > 1) {
791                                 $wgOut->addHTML(
792                                         $this->tableRow(
793                                                 Xml::label( wfMsg( 'yourvariant' ), 'wpUserVariant' ),
794                                                 Xml::tags( 'select',
795                                                         array( 'name' => 'wpUserVariant', 'id' => 'wpUserVariant' ),
796                                                         $options
797                                                 )
798                                         )
799                                 );
800                         }
801
802                         if(count($variantArray) > 1 && !$wgDisableLangConversion && !$wgDisableTitleConversion) {
803                                 $wgOut->addHTML(
804                                         Xml::tags( 'tr', null,
805                                                 Xml::tags( 'td', array( 'colspan' => '2' ),
806                                                         $this->getToggle( "noconvertlink" )
807                                                 )
808                                         )
809                                 );
810                         }
811                 }
812
813                 # Password
814                 if( $wgAuth->allowPasswordChange() ) {
815                         $link = $wgUser->getSkin()->link( SpecialPage::getTitleFor( 'ResetPass' ), wfMsgHtml( 'prefs-resetpass' ),
816                                 array() , array( 'returnto' => SpecialPage::getTitleFor( 'Preferences' )->getPrefixedText() ) );
817                         $wgOut->addHTML(
818                                 $this->tableRow( Xml::element( 'h2', null, wfMsg( 'changepassword' ) ) ) .
819                                 $this->tableRow( '<ul><li>' . $link . '</li></ul>' ) );
820                 }
821
822                 # <FIXME>
823                 # Enotif
824                 if ( $wgEnableEmail ) {
825
826                         $moreEmail = '';
827                         if ($wgEnableUserEmail) {
828                                 // fixme -- the "allowemail" pseudotoggle is a hacked-together
829                                 // inversion for the "disableemail" preference.
830                                 $emf = wfMsg( 'allowemail' );
831                                 $disabled = $disableEmailPrefs ? ' disabled="disabled"' : '';
832                                 $moreEmail =
833                                         "<input type='checkbox' $emfc $disabled value='1' name='wpEmailFlag' id='wpEmailFlag' /> <label for='wpEmailFlag'>$emf</label>" .
834                                         $this->getToggle( 'ccmeonemails', '', $disableEmailPrefs );
835                         }
836
837
838                         $wgOut->addHTML(
839                                 $this->tableRow( Xml::element( 'h2', null, wfMsg( 'email' ) ) ) .
840                                 $this->tableRow(
841                                         $emailauthenticated.
842                                         $enotifrevealaddr.
843                                         $enotifwatchlistpages.
844                                         $enotifusertalkpages.
845                                         $enotifminoredits.
846                                         $moreEmail
847                                 )
848                         );
849                 }
850                 # </FIXME>
851
852                 $wgOut->addHTML(
853                         Xml::closeElement( 'table' ) .
854                         Xml::closeElement( 'fieldset' )
855                 );
856
857
858                 # Quickbar
859                 #
860                 if ($this->mSkin == 'cologneblue' || $this->mSkin == 'standard') {
861                         $wgOut->addHTML( "<fieldset>\n<legend>" . wfMsg( 'qbsettings' ) . "</legend>\n" );
862                         for ( $i = 0; $i < count( $qbs ); ++$i ) {
863                                 if ( $i == $this->mQuickbar ) { $checked = ' checked="checked"'; }
864                                 else { $checked = ""; }
865                                 $wgOut->addHTML( "<div><label><input type='radio' name='wpQuickbar' value=\"$i\"$checked />{$qbs[$i]}</label></div>\n" );
866                         }
867                         $wgOut->addHTML( "</fieldset>\n\n" );
868                 } else {
869                         # Need to output a hidden option even if the relevant skin is not in use,
870                         # otherwise the preference will get reset to 0 on submit
871                         $wgOut->addHTML( Xml::hidden( 'wpQuickbar', $this->mQuickbar ) );
872                 }
873
874                 # Skin
875                 #
876                 global $wgAllowUserSkin;
877                 if( $wgAllowUserSkin ) {
878                         $wgOut->addHTML( "<fieldset>\n<legend>\n" . wfMsg( 'skin' ) . "</legend>\n" );
879                         $mptitle = Title::newMainPage();
880                         $previewtext = wfMsg( 'skin-preview' );
881                         # Only show members of Skin::getSkinNames() rather than
882                         # $skinNames (skins is all skin names from Language.php)
883                         $validSkinNames = Skin::getUsableSkins();
884                         # Sort by UI skin name. First though need to update validSkinNames as sometimes
885                         # the skinkey & UI skinname differ (e.g. "standard" skinkey is "Classic" in the UI).
886                         foreach ( $validSkinNames as $skinkey => &$skinname ) {
887                                 $msgName = "skinname-{$skinkey}";
888                                 $localisedSkinName = wfMsg( $msgName );
889                                 if ( !wfEmptyMsg( $msgName, $localisedSkinName ) )  {
890                                         $skinname = $localisedSkinName;
891                                 }
892                         }
893                         asort($validSkinNames);
894                         foreach( $validSkinNames as $skinkey => $sn ) {
895                                 $checked = $skinkey == $this->mSkin ? ' checked="checked"' : '';
896                                 $mplink = htmlspecialchars( $mptitle->getLocalURL( "useskin=$skinkey" ) );
897                                 $previewlink = "(<a target='_blank' href=\"$mplink\">$previewtext</a>)";
898                                 $extraLinks = '';
899                                 global $wgAllowUserCss, $wgAllowUserJs;
900                                 if( $wgAllowUserCss ) {
901                                         $cssPage = Title::makeTitleSafe( NS_USER, $wgUser->getName().'/'.$skinkey.'.css' );
902                                         $customCSS = $sk->makeLinkObj( $cssPage, wfMsgExt('prefs-custom-css', array() ) );
903                                         $extraLinks .= " ($customCSS)";
904                                 }
905                                 if( $wgAllowUserJs ) {
906                                         $jsPage = Title::makeTitleSafe( NS_USER, $wgUser->getName().'/'.$skinkey.'.js' );
907                                         $customJS = $sk->makeLinkObj( $jsPage, wfMsgHtml('prefs-custom-js') );
908                                         $extraLinks .= " ($customJS)";
909                                 }
910                                 if( $skinkey == $wgDefaultSkin )
911                                         $sn .= ' (' . wfMsg( 'default' ) . ')';
912                                 $wgOut->addHTML( "<input type='radio' name='wpSkin' id=\"wpSkin$skinkey\" value=\"$skinkey\"$checked /> 
913                                         <label for=\"wpSkin$skinkey\">{$sn}</label> $previewlink{$extraLinks}<br />\n" );
914                         }
915                         $wgOut->addHTML( "</fieldset>\n\n" );
916                 }
917
918                 # Math
919                 #
920                 global $wgUseTeX;
921                 if( $wgUseTeX ) {
922                         $wgOut->addHTML( "<fieldset>\n<legend>" . wfMsg('math') . '</legend>' );
923                         foreach ( $mathopts as $k => $v ) {
924                                 $checked = ($k == $this->mMath);
925                                 $wgOut->addHTML(
926                                         Xml::openElement( 'div' ) .
927                                         Xml::radioLabel( wfMsg( $v ), 'wpMath', $k, "mw-sp-math-$k", $checked ) .
928                                         Xml::closeElement( 'div' ) . "\n"
929                                 );
930                         }
931                         $wgOut->addHTML( "</fieldset>\n\n" );
932                 }
933
934                 # Files
935                 #
936                 $imageLimitOptions = null;
937                 foreach ( $wgImageLimits as $index => $limits ) {
938                         $selected = ($index == $this->mImageSize);
939                         $imageLimitOptions .= Xml::option( "{$limits[0]}×{$limits[1]}" .
940                                 wfMsg('unit-pixel'), $index, $selected );
941                 }
942
943                 $imageThumbOptions = null;
944                 foreach ( $wgThumbLimits as $index => $size ) {
945                         $selected = ($index == $this->mThumbSize);
946                         $imageThumbOptions .= Xml::option($size . wfMsg('unit-pixel'), $index,
947                                 $selected);
948                 }
949
950                 $imageSizeId = 'wpImageSize';
951                 $thumbSizeId = 'wpThumbSize';
952                 $wgOut->addHTML(
953                         Xml::fieldset( wfMsg( 'files' ) ) . "\n" .
954                         Xml::openElement( 'table' ) .
955                                 '<tr>
956                                         <td class="mw-label">' .
957                                                 Xml::label( wfMsg( 'imagemaxsize' ), $imageSizeId ) .
958                                         '</td>
959                                         <td class="mw-input">' .
960                                                 Xml::openElement( 'select', array( 'name' => $imageSizeId, 'id' => $imageSizeId ) ) .
961                                                 $imageLimitOptions .
962                                                 Xml::closeElement( 'select' ) .
963                                         '</td>
964                                 </tr><tr>
965                                         <td class="mw-label">' .
966                                                 Xml::label( wfMsg( 'thumbsize' ), $thumbSizeId ) .
967                                         '</td>
968                                         <td class="mw-input">' .
969                                                 Xml::openElement( 'select', array( 'name' => $thumbSizeId, 'id' => $thumbSizeId ) ) .
970                                                 $imageThumbOptions .
971                                                 Xml::closeElement( 'select' ) .
972                                         '</td>
973                                 </tr>' .
974                         Xml::closeElement( 'table' ) .
975                         Xml::closeElement( 'fieldset' )
976                 );
977
978                 # Date format
979                 #
980                 # Date/Time
981                 #
982
983                 $wgOut->addHTML(
984                         Xml::openElement( 'fieldset' ) .
985                         Xml::element( 'legend', null, wfMsg( 'datetime' ) ) . "\n"
986                 );
987
988                 if ($dateopts) {
989                         $wgOut->addHTML(
990                                 Xml::openElement( 'fieldset' ) .
991                                 Xml::element( 'legend', null, wfMsg( 'dateformat' ) ) . "\n"
992                         );
993                         $idCnt = 0;
994                         $epoch = '20010115161234'; # Wikipedia day
995                         foreach( $dateopts as $key ) {
996                                 if( $key == 'default' ) {
997                                         $formatted = wfMsg( 'datedefault' );
998                                 } else {
999                                         $formatted = $wgLang->timeanddate( $epoch, false, $key );
1000                                 }
1001                                 $wgOut->addHTML(
1002                                         Xml::tags( 'div', null,
1003                                                 Xml::radioLabel( $formatted, 'wpDate', $key, "wpDate$idCnt", $key == $this->mDate )
1004                                         ) . "\n"
1005                                 );
1006                                 $idCnt++;
1007                         }
1008                         $wgOut->addHTML( Xml::closeElement( 'fieldset' ) . "\n" );
1009                 }
1010
1011                 $nowlocal = Xml::openElement( 'span', array( 'id' => 'wpLocalTime' ) ) .
1012                         $wgLang->time( $now = wfTimestampNow(), true ) .
1013                         Xml::closeElement( 'span' );
1014                 $nowserver = $wgLang->time( $now, false ) .
1015                         Xml::hidden( 'wpServerTime', substr( $now, 8, 2 ) * 60 + substr( $now, 10, 2 ) );
1016
1017                 $wgOut->addHTML(
1018                         Xml::openElement( 'fieldset' ) .
1019                         Xml::element( 'legend', null, wfMsg( 'timezonelegend' ) ) .
1020                         Xml::openElement( 'table' ) .
1021                         $this->addRow( wfMsg( 'servertime' ), $nowserver ) .
1022                         $this->addRow( wfMsg( 'localtime' ), $nowlocal )
1023                 );
1024                 $opt = Xml::openElement( 'select', array(
1025                         'name' => 'wpTimeZone',
1026                         'id' => 'wpTimeZone',
1027                         'onchange' => 'javascript:updateTimezoneSelection(false)' ) );
1028                 $opt .= Xml::option( wfMsg( 'timezoneuseserverdefault' ), "System|$wgLocalTZoffset", $this->mTimeZone === "System|$wgLocalTZoffset" );
1029                 $opt .= Xml::option( wfMsg( 'timezoneuseoffset' ), 'Offset', $this->mTimeZone === 'Offset' );
1030
1031                 if ( function_exists( 'timezone_identifiers_list' ) ) {
1032                         # Read timezone list
1033                         $tzs = timezone_identifiers_list();
1034                         sort( $tzs );
1035
1036                         # Precache localized region names
1037                         $tzRegions = array();
1038                         $tzRegions['Africa'] = wfMsg( 'timezoneregion-africa' );
1039                         $tzRegions['America'] = wfMsg( 'timezoneregion-america' );
1040                         $tzRegions['Antarctica'] = wfMsg( 'timezoneregion-antarctica' );
1041                         $tzRegions['Arctic'] = wfMsg( 'timezoneregion-arctic' );
1042                         $tzRegions['Asia'] = wfMsg( 'timezoneregion-asia' );
1043                         $tzRegions['Atlantic'] = wfMsg( 'timezoneregion-atlantic' );
1044                         $tzRegions['Australia'] = wfMsg( 'timezoneregion-australia' );
1045                         $tzRegions['Europe'] = wfMsg( 'timezoneregion-europe' );
1046                         $tzRegions['Indian'] = wfMsg( 'timezoneregion-indian' );
1047                         $tzRegions['Pacific'] = wfMsg( 'timezoneregion-pacific' );
1048                         asort( $tzRegions );
1049
1050                         $selZone = explode( '|', $this->mTimeZone, 3 );
1051                         $selZone = ( $selZone[0] == 'ZoneInfo' ) ? $selZone[2] : null;
1052                         $now = date_create( 'now' );
1053                         $optgroup = '';
1054
1055                         foreach ( $tzs as $tz ) {
1056                                 $z = explode( '/', $tz, 2 );
1057
1058                                 # timezone_identifiers_list() returns a number of
1059                                 # backwards-compatibility entries. This filters them out of the 
1060                                 # list presented to the user.
1061                                 if ( count( $z ) != 2 || !array_key_exists( $z[0], $tzRegions ) )
1062                                         continue;
1063
1064                                 # Localize region
1065                                 $z[0] = $tzRegions[$z[0]];
1066
1067                                 # Create region groups
1068                                 if ( $optgroup != $z[0] ) {
1069                                         if ( $optgroup !== '' ) {
1070                                                 $opt .= Xml::closeElement( 'optgroup' );
1071                                         }
1072                                         $optgroup = $z[0];
1073                                         $opt .= Xml::openElement( 'optgroup', array( 'label' => $z[0] ) ) . "\n";
1074                                 }
1075
1076                                 $minDiff = floor( timezone_offset_get( timezone_open( $tz ), $now ) / 60 );
1077                                 $opt .= Xml::option( str_replace( '_', ' ', $z[0] . '/' . $z[1] ), "ZoneInfo|$minDiff|$tz", $selZone === $tz, array( 'label' => $z[1] ) ) . "\n";
1078                         }
1079                         if ( $optgroup !== '' ) $opt .= Xml::closeElement( 'optgroup' );
1080                 }
1081                 $opt .= Xml::closeElement( 'select' );
1082                 $wgOut->addHTML(
1083                         $this->addRow(
1084                                 Xml::label( wfMsg( 'timezoneselect' ), 'wpTimeZone' ),
1085                                 $opt )
1086                 );
1087                 $wgOut->addHTML(
1088                         $this->addRow(
1089                                 Xml::label( wfMsg( 'timezoneoffset' ), 'wpHourDiff'  ),
1090                                 Xml::input( 'wpHourDiff', 6, $this->mHourDiff, array(
1091                                         'id' => 'wpHourDiff',
1092                                         'onfocus' => 'javascript:updateTimezoneSelection(true)',
1093                                         'onblur' => 'javascript:updateTimezoneSelection(false)' ) ) ) .
1094                         "<tr>
1095                                 <td></td>
1096                                 <td class='mw-submit'>" .
1097                                         Xml::element( 'input',
1098                                                 array( 'type' => 'button',
1099                                                         'value' => wfMsg( 'guesstimezone' ),
1100                                                         'onclick' => 'javascript:guessTimezone()',
1101                                                         'id' => 'guesstimezonebutton',
1102                                                         'style' => 'display:none;' ) ) .
1103                                 "</td>
1104                         </tr>" .
1105                         Xml::closeElement( 'table' ) .
1106                         Xml::tags( 'div', array( 'class' => 'prefsectiontip' ), wfMsgExt( 'timezonetext', 'parseinline' ) ).
1107                         Xml::closeElement( 'fieldset' ) .
1108                         Xml::closeElement( 'fieldset' ) . "\n\n"
1109                 );
1110
1111                 # Editing
1112                 #
1113                 global $wgLivePreview;
1114                 $wgOut->addHTML(
1115                         Xml::fieldset( wfMsg( 'textboxsize' ) ) .
1116                         wfMsgHTML( 'prefs-edit-boxsize' ) . ' ' .
1117                         Xml::inputLabel( wfMsg( 'rows' ), 'wpRows', 'wpRows', 3, $this->mRows ) . ' ' .
1118                         Xml::inputLabel( wfMsg( 'columns' ), 'wpCols', 'wpCols', 3, $this->mCols ) .
1119                         $this->getToggles( array(
1120                                 'editsection',
1121                                 'editsectiononrightclick',
1122                                 'editondblclick',
1123                                 'editwidth',
1124                                 'showtoolbar',
1125                                 'previewonfirst',
1126                                 'previewontop',
1127                                 'minordefault',
1128                                 'externaleditor',
1129                                 'externaldiff',
1130                                 $wgLivePreview ? 'uselivepreview' : false,
1131                                 'forceeditsummary',
1132                         ) )
1133                 );
1134
1135                 $wgOut->addHTML( Xml::closeElement( 'fieldset' ) );
1136
1137                 # Recent changes
1138                 global $wgRCMaxAge, $wgUseRCPatrol;
1139                 $wgOut->addHTML(
1140                         Xml::fieldset( wfMsg( 'prefs-rc' ) ) .
1141                         Xml::openElement( 'table' ) .
1142                                 '<tr>
1143                                         <td class="mw-label">' .
1144                                                 Xml::label( wfMsg( 'recentchangesdays' ), 'wpRecentDays' ) .
1145                                         '</td>
1146                                         <td class="mw-input">' .
1147                                                 Xml::input( 'wpRecentDays', 3, $this->mRecentDays, array( 'id' => 'wpRecentDays' ) ) . ' ' .
1148                                                 wfMsgExt( 'recentchangesdays-max', 'parsemag',
1149                                                         $wgLang->formatNum( ceil( $wgRCMaxAge / ( 3600 * 24 ) ) ) ) .
1150                                         '</td>
1151                                 </tr><tr>
1152                                         <td class="mw-label">' .
1153                                                 Xml::label( wfMsg( 'recentchangescount' ), 'wpRecent' ) .
1154                                         '</td>
1155                                         <td class="mw-input">' .
1156                                                 Xml::input( 'wpRecent', 3, $this->mRecent, array( 'id' => 'wpRecent' ) ) .
1157                                         '</td>
1158                                 </tr>' .
1159                         Xml::closeElement( 'table' ) .
1160                         '<br />'
1161                 );
1162
1163                 $toggles[] = 'hideminor';
1164                 if( $wgUseRCPatrol ) {
1165                         $toggles[] = 'hidepatrolled';
1166                         $toggles[] = 'newpageshidepatrolled';
1167                 }
1168                 if( $wgRCShowWatchingUsers ) $toggles[] = 'shownumberswatching';
1169                 $toggles[] = 'usenewrc';
1170
1171                 $wgOut->addHTML(
1172                         $this->getToggles( $toggles ) .
1173                         Xml::closeElement( 'fieldset' )
1174                 );
1175
1176                 # Watchlist
1177                 $watchlistToggles = array( 'watchlisthideminor', 'watchlisthidebots', 'watchlisthideown',
1178                         'watchlisthideanons', 'watchlisthideliu' );
1179                 if( $wgUseRCPatrol ) $watchlistToggles[] = 'watchlisthidepatrolled';
1180
1181                 $wgOut->addHTML( 
1182                         Xml::fieldset( wfMsg( 'prefs-watchlist' ) ) .
1183                         Xml::inputLabel( wfMsg( 'prefs-watchlist-days' ), 'wpWatchlistDays', 'wpWatchlistDays', 3, $this->mWatchlistDays ) . ' ' .
1184                         wfMsgHTML( 'prefs-watchlist-days-max' ) .
1185                         '<br /><br />' .
1186                         $this->getToggle( 'extendwatchlist' ) .
1187                         Xml::inputLabel( wfMsg( 'prefs-watchlist-edits' ), 'wpWatchlistEdits', 'wpWatchlistEdits', 3, $this->mWatchlistEdits ) . ' ' .
1188                         wfMsgHTML( 'prefs-watchlist-edits-max' ) .
1189                         '<br /><br />' .
1190                         $this->getToggles( $watchlistToggles )
1191                 );
1192
1193                 if( $wgUser->isAllowed( 'createpage' ) || $wgUser->isAllowed( 'createtalk' ) ) {
1194                         $wgOut->addHTML( $this->getToggle( 'watchcreations' ) );
1195                 }
1196
1197                 foreach( array( 'edit' => 'watchdefault', 'move' => 'watchmoves', 'delete' => 'watchdeletion' ) as $action => $toggle ) {
1198                         if( $wgUser->isAllowed( $action ) )
1199                                 $wgOut->addHTML( $this->getToggle( $toggle ) );
1200                 }
1201                 $this->mUsedToggles['watchcreations'] = true;
1202                 $this->mUsedToggles['watchdefault'] = true;
1203                 $this->mUsedToggles['watchmoves'] = true;
1204                 $this->mUsedToggles['watchdeletion'] = true;
1205
1206                 $wgOut->addHTML( Xml::closeElement( 'fieldset' ) );
1207
1208                 # Search
1209                 $mwsuggest = $wgEnableMWSuggest ?
1210                         $this->addRow(
1211                                 Xml::label( wfMsg( 'mwsuggest-disable' ), 'wpDisableMWSuggest' ),
1212                                 Xml::check( 'wpDisableMWSuggest', $this->mDisableMWSuggest, array( 'id' => 'wpDisableMWSuggest' ) )
1213                         ) : '';
1214                 $wgOut->addHTML(
1215                         // Elements for the search tab itself
1216                         Xml::openElement( 'fieldset' ) .
1217                         Xml::element( 'legend', null, wfMsg( 'searchresultshead' ) ) .
1218                         // Elements for the search options in the search tab
1219                         Xml::openElement( 'fieldset' ) .
1220                         Xml::element( 'legend', null, wfMsg( 'prefs-searchoptions' ) ) .
1221                         Xml::openElement( 'table' ) .
1222                         $this->addRow(
1223                                 Xml::label( wfMsg( 'resultsperpage' ), 'wpSearch' ),
1224                                 Xml::input( 'wpSearch', 4, $this->mSearch, array( 'id' => 'wpSearch' ) )
1225                         ) .
1226                         $this->addRow(
1227                                 Xml::label( wfMsg( 'contextlines' ), 'wpSearchLines' ),
1228                                 Xml::input( 'wpSearchLines', 4, $this->mSearchLines, array( 'id' => 'wpSearchLines' ) )
1229                         ) .
1230                         $this->addRow(
1231                                 Xml::label( wfMsg( 'contextchars' ), 'wpSearchChars' ),
1232                                 Xml::input( 'wpSearchChars', 4, $this->mSearchChars, array( 'id' => 'wpSearchChars' ) )
1233                         ) .
1234                         $mwsuggest .
1235                         Xml::closeElement( 'table' ) .
1236                         Xml::closeElement( 'fieldset' ) .
1237                         // Elements for the namespace options in the search tab
1238                         Xml::openElement( 'fieldset' ) .
1239                         Xml::element( 'legend', null, wfMsg( 'prefs-namespaces' ) ) .
1240                         wfMsgExt( 'defaultns', array( 'parse' ) ) .
1241                         $ps .
1242                         Xml::closeElement( 'fieldset' ) .
1243                         // End of the search tab
1244                         Xml::closeElement( 'fieldset' )
1245                 );
1246
1247                 # Misc
1248                 #
1249                 $uopt = $wgUser->getOption( 'underline' );
1250                 $wgOut->addHTML(
1251                         Xml::fieldset( wfMsg( 'prefs-misc' ) ) .
1252                         Xml::openElement( 'table' ) .
1253                                 '<tr>
1254                                         <td class="mw-label">' .
1255                                                 // Xml::label() cannot be used because 'stub-threshold' contains plain HTML
1256                                                 Xml::tags( 'label', array( 'for' => 'wpStubs' ), wfMsg( 'stub-threshold' ) ) .
1257                                         '</td>
1258                                         <td class="mw-input">' .
1259                                                 Xml::input( 'wpStubs', 6, $this->mStubs, array( 'id' => 'wpStubs' ) ) .
1260                                         '</td>
1261                                 </tr><tr>
1262                                         <td class="mw-label">' .
1263                                                 Xml::label( wfMsg( 'tog-underline' ), 'wpOpunderline' ) .
1264                                         '</td>
1265                                         <td class="mw-input">' .
1266                                                 Xml::openElement( 'select', array( 'id' => 'wpOpunderline', 'name' => 'wpOpunderline' ) ) .
1267                                                 Xml::option( wfMsg ( 'underline-never' ), '0', $uopt == 0 ) .
1268                                                 Xml::option( wfMsg ( 'underline-always' ), '1', $uopt == 1 ) .
1269                                                 Xml::option( wfMsg ( 'underline-default' ), '2', $uopt == 2 ) .
1270                                                 Xml::closeElement( 'select' ) .
1271                                         '</td>
1272                                 </tr>' .
1273                         Xml::closeElement( 'table' )
1274                 );
1275
1276                 # And now the rest = Misc.
1277                 foreach ( $togs as $tname ) {
1278                         if( !array_key_exists( $tname, $this->mUsedToggles ) ) {
1279                                 if( $tname == 'norollbackdiff' && $wgUser->isAllowed( 'rollback' ) )
1280                                         $wgOut->addHTML( $this->getToggle( $tname ) );
1281                                 else
1282                                         $wgOut->addHTML( $this->getToggle( $tname ) );
1283                         }
1284                 }
1285
1286                 $wgOut->addHTML( '</fieldset>' );
1287
1288                 wfRunHooks( 'RenderPreferencesForm', array( $this, $wgOut ) );
1289
1290                 $token = htmlspecialchars( $wgUser->editToken() );
1291                 $skin = $wgUser->getSkin();
1292                 $rtl = $wgContLang->isRTL() ? 'left' : 'right';
1293                 $wgOut->addHTML( "
1294         <table id='prefsubmit' cellpadding='0' width='100%' style='background:none;'><tr>
1295                 <td><input type='submit' name='wpSaveprefs' class='btnSavePrefs' value=\"" . wfMsgHtml( 'saveprefs' ) . 
1296                         '"'.$skin->tooltipAndAccesskey('save')." />
1297                 <input type='submit' name='wpReset' value=\"" . wfMsgHtml( 'resetprefs' ) . "\" /></td>
1298                 <td align='$rtl'><input type='submit' name='wpRestore' value=\"" . wfMsgHtml( 'restoreprefs' ) . "\" /></td>
1299         </tr></table>
1300
1301         <input type='hidden' name='wpEditToken' value=\"{$token}\" />
1302         </div></form>\n" );
1303
1304                 $wgOut->addHTML( Xml::tags( 'div', array( 'class' => "prefcache" ),
1305                         wfMsgExt( 'clearyourcache', 'parseinline' ) )
1306                 );
1307         }
1308 }