fix some spacing
[lhc/web/wiklou.git] / includes / Preferences.php
1 <?php
2 /**
3 * Form to edit user perferences.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * We're now using the HTMLForm object with some customisation to generate the
25 * Preferences form. This object handles generic submission, CSRF protection,
26 * layout and other logic in a reusable manner. We subclass it as a PreferencesForm
27 * to make some minor customisations.
28 *
29 * In order to generate the form, the HTMLForm object needs an array structure
30 * detailing the form fields available, and that's what this class is for. Each
31 * element of the array is a basic property-list, including the type of field,
32 * the label it is to be given in the form, callbacks for validation and
33 * 'filtering', and other pertinent information. Note that the 'default' field
34 * is named for generic forms, and does not represent the preference's default
35 * (which is stored in $wgDefaultUserOptions), but the default for the form
36 * field, which should be whatever the user has set for that preference. There
37 * is no need to override it unless you have some special storage logic (for
38 * instance, those not presently stored as options, but which are best set from
39 * the user preferences view).
40 *
41 * Field types are implemented as subclasses of the generic HTMLFormField
42 * object, and typically implement at least getInputHTML, which generates the
43 * HTML for the input field to be placed in the table.
44 *
45 * Once fields have been retrieved and validated, submission logic is handed
46 * over to the tryUISubmit static method of this class.
47 */
48 class Preferences {
49 static $defaultPreferences = null;
50 static $saveFilters = array(
51 'timecorrection' => array( 'Preferences', 'filterTimezoneInput' ),
52 'cols' => array( 'Preferences', 'filterIntval' ),
53 'rows' => array( 'Preferences', 'filterIntval' ),
54 'rclimit' => array( 'Preferences', 'filterIntval' ),
55 'wllimit' => array( 'Preferences', 'filterIntval' ),
56 'searchlimit' => array( 'Preferences', 'filterIntval' ),
57 );
58
59 /**
60 * @throws MWException
61 * @param $user User
62 * @param $context IContextSource
63 * @return array|null
64 */
65 static function getPreferences( $user, IContextSource $context ) {
66 if ( self::$defaultPreferences ) {
67 return self::$defaultPreferences;
68 }
69
70 $defaultPreferences = array();
71
72 self::profilePreferences( $user, $context, $defaultPreferences );
73 self::skinPreferences( $user, $context, $defaultPreferences );
74 self::filesPreferences( $user, $context, $defaultPreferences );
75 self::datetimePreferences( $user, $context, $defaultPreferences );
76 self::renderingPreferences( $user, $context, $defaultPreferences );
77 self::editingPreferences( $user, $context, $defaultPreferences );
78 self::rcPreferences( $user, $context, $defaultPreferences );
79 self::watchlistPreferences( $user, $context, $defaultPreferences );
80 self::searchPreferences( $user, $context, $defaultPreferences );
81 self::miscPreferences( $user, $context, $defaultPreferences );
82
83 wfRunHooks( 'GetPreferences', array( $user, &$defaultPreferences ) );
84
85 ## Remove preferences that wikis don't want to use
86 global $wgHiddenPrefs;
87 foreach ( $wgHiddenPrefs as $pref ) {
88 if ( isset( $defaultPreferences[$pref] ) ) {
89 unset( $defaultPreferences[$pref] );
90 }
91 }
92
93 ## Prod in defaults from the user
94 foreach ( $defaultPreferences as $name => &$info ) {
95 $prefFromUser = self::getOptionFromUser( $name, $info, $user );
96 $field = HTMLForm::loadInputFromParameters( $name, $info ); // For validation
97 $defaultOptions = User::getDefaultOptions();
98 $globalDefault = isset( $defaultOptions[$name] )
99 ? $defaultOptions[$name]
100 : null;
101
102 // If it validates, set it as the default
103 if ( isset( $info['default'] ) ) {
104 // Already set, no problem
105 continue;
106 } elseif ( !is_null( $prefFromUser ) && // Make sure we're not just pulling nothing
107 $field->validate( $prefFromUser, $user->getOptions() ) === true ) {
108 $info['default'] = $prefFromUser;
109 } elseif ( $field->validate( $globalDefault, $user->getOptions() ) === true ) {
110 $info['default'] = $globalDefault;
111 } else {
112 throw new MWException( "Global default '$globalDefault' is invalid for field $name" );
113 }
114 }
115
116 self::$defaultPreferences = $defaultPreferences;
117
118 return $defaultPreferences;
119 }
120
121 /**
122 * Pull option from a user account. Handles stuff like array-type preferences.
123 *
124 * @param $name
125 * @param $info
126 * @param $user User
127 * @return array|String
128 */
129 static function getOptionFromUser( $name, $info, $user ) {
130 $val = $user->getOption( $name );
131
132 // Handling for multiselect preferences
133 if ( ( isset( $info['type'] ) && $info['type'] == 'multiselect' ) ||
134 ( isset( $info['class'] ) && $info['class'] == 'HTMLMultiSelectField' ) ) {
135 $options = HTMLFormField::flattenOptions( $info['options'] );
136 $prefix = isset( $info['prefix'] ) ? $info['prefix'] : $name;
137 $val = array();
138
139 foreach ( $options as $value ) {
140 if ( $user->getOption( "$prefix$value" ) ) {
141 $val[] = $value;
142 }
143 }
144 }
145
146 // Handling for checkmatrix preferences
147 if ( ( isset( $info['type'] ) && $info['type'] == 'checkmatrix' ) ||
148 ( isset( $info['class'] ) && $info['class'] == 'HTMLCheckMatrix' ) ) {
149 $columns = HTMLFormField::flattenOptions( $info['columns'] );
150 $rows = HTMLFormField::flattenOptions( $info['rows'] );
151 $prefix = isset( $info['prefix'] ) ? $info['prefix'] : $name;
152 $val = array();
153
154 foreach ( $columns as $column ) {
155 foreach ( $rows as $row ) {
156 if ( $user->getOption( "$prefix-$column-$row" ) ) {
157 $val[] = "$column-$row";
158 }
159 }
160 }
161 }
162
163 return $val;
164 }
165
166 /**
167 * @param $user User
168 * @param $context IContextSource
169 * @param $defaultPreferences
170 * @return void
171 */
172 static function profilePreferences( $user, IContextSource $context, &$defaultPreferences ) {
173 global $wgAuth, $wgContLang, $wgParser, $wgCookieExpiration, $wgLanguageCode,
174 $wgDisableTitleConversion, $wgDisableLangConversion, $wgMaxSigChars,
175 $wgEnableEmail, $wgEmailConfirmToEdit, $wgEnableUserEmail, $wgEmailAuthentication,
176 $wgEnotifWatchlist, $wgEnotifUserTalk, $wgEnotifRevealEditorAddress;
177
178 // retrieving user name for GENDER and misc.
179 $userName = $user->getName();
180
181 ## User info #####################################
182 // Information panel
183 $defaultPreferences['username'] = array(
184 'type' => 'info',
185 'label-message' => array( 'username', $userName ),
186 'default' => $userName,
187 'section' => 'personal/info',
188 );
189
190 $defaultPreferences['userid'] = array(
191 'type' => 'info',
192 'label-message' => array( 'uid', $userName ),
193 'default' => $user->getId(),
194 'section' => 'personal/info',
195 );
196
197 # Get groups to which the user belongs
198 $userEffectiveGroups = $user->getEffectiveGroups();
199 $userGroups = $userMembers = array();
200 foreach ( $userEffectiveGroups as $ueg ) {
201 if ( $ueg == '*' ) {
202 // Skip the default * group, seems useless here
203 continue;
204 }
205 $groupName = User::getGroupName( $ueg );
206 $userGroups[] = User::makeGroupLinkHTML( $ueg, $groupName );
207
208 $memberName = User::getGroupMember( $ueg, $userName );
209 $userMembers[] = User::makeGroupLinkHTML( $ueg, $memberName );
210 }
211 asort( $userGroups );
212 asort( $userMembers );
213
214 $lang = $context->getLanguage();
215
216 $defaultPreferences['usergroups'] = array(
217 'type' => 'info',
218 'label' => $context->msg( 'prefs-memberingroups' )->numParams(
219 count( $userGroups ) )->params( $userName )->parse(),
220 'default' => $context->msg( 'prefs-memberingroups-type',
221 $lang->commaList( $userGroups ),
222 $lang->commaList( $userMembers )
223 )->plain(),
224 'raw' => true,
225 'section' => 'personal/info',
226 );
227
228 $defaultPreferences['editcount'] = array(
229 'type' => 'info',
230 'label-message' => 'prefs-edits',
231 'default' => $lang->formatNum( $user->getEditCount() ),
232 'section' => 'personal/info',
233 );
234
235 if ( $user->getRegistration() ) {
236 $displayUser = $context->getUser();
237 $userRegistration = $user->getRegistration();
238 $defaultPreferences['registrationdate'] = array(
239 'type' => 'info',
240 'label-message' => 'prefs-registration',
241 'default' => $context->msg(
242 'prefs-registration-date-time',
243 $lang->userTimeAndDate( $userRegistration, $displayUser ),
244 $lang->userDate( $userRegistration, $displayUser ),
245 $lang->userTime( $userRegistration, $displayUser )
246 )->parse(),
247 'section' => 'personal/info',
248 );
249 }
250
251 // Actually changeable stuff
252 $defaultPreferences['realname'] = array(
253 'type' => $wgAuth->allowPropChange( 'realname' ) ? 'text' : 'info',
254 'default' => $user->getRealName(),
255 'section' => 'personal/info',
256 'label-message' => 'yourrealname',
257 'help-message' => 'prefs-help-realname',
258 );
259
260 $defaultPreferences['gender'] = array(
261 'type' => 'select',
262 'section' => 'personal/info',
263 'options' => array(
264 $context->msg( 'gender-male' )->text() => 'male',
265 $context->msg( 'gender-female' )->text() => 'female',
266 $context->msg( 'gender-unknown' )->text() => 'unknown',
267 ),
268 'label-message' => 'yourgender',
269 'help-message' => 'prefs-help-gender',
270 );
271
272 if ( $wgAuth->allowPasswordChange() ) {
273 $link = Linker::link( SpecialPage::getTitleFor( 'ChangePassword' ),
274 $context->msg( 'prefs-resetpass' )->escaped(), array(),
275 array( 'returnto' => SpecialPage::getTitleFor( 'Preferences' )->getPrefixedText() ) );
276
277 $defaultPreferences['password'] = array(
278 'type' => 'info',
279 'raw' => true,
280 'default' => $link,
281 'label-message' => 'yourpassword',
282 'section' => 'personal/info',
283 );
284 }
285 if ( $wgCookieExpiration > 0 ) {
286 $defaultPreferences['rememberpassword'] = array(
287 'type' => 'toggle',
288 'label' => $context->msg( 'tog-rememberpassword' )->numParams(
289 ceil( $wgCookieExpiration / ( 3600 * 24 ) ) )->text(),
290 'section' => 'personal/info',
291 );
292 }
293
294 // Language
295 $languages = Language::fetchLanguageNames( null, 'mw' );
296 if ( !array_key_exists( $wgLanguageCode, $languages ) ) {
297 $languages[$wgLanguageCode] = $wgLanguageCode;
298 }
299 ksort( $languages );
300
301 $options = array();
302 foreach ( $languages as $code => $name ) {
303 $display = wfBCP47( $code ) . ' - ' . $name;
304 $options[$display] = $code;
305 }
306 $defaultPreferences['language'] = array(
307 'type' => 'select',
308 'section' => 'personal/i18n',
309 'options' => $options,
310 'label-message' => 'yourlanguage',
311 );
312
313 /* see if there are multiple language variants to choose from*/
314 $variantArray = array();
315 if ( !$wgDisableLangConversion ) {
316 $variants = $wgContLang->getVariants();
317
318 foreach ( $variants as $v ) {
319 $v = str_replace( '_', '-', strtolower( $v ) );
320 $variantArray[$v] = $wgContLang->getVariantname( $v, false );
321 }
322
323 $options = array();
324 foreach ( $variantArray as $code => $name ) {
325 $display = wfBCP47( $code ) . ' - ' . $name;
326 $options[$display] = $code;
327 }
328
329 if ( count( $variantArray ) > 1 ) {
330 $defaultPreferences['variant'] = array(
331 'label-message' => 'yourvariant',
332 'type' => 'select',
333 'options' => $options,
334 'section' => 'personal/i18n',
335 'help-message' => 'prefs-help-variant',
336 );
337 }
338 }
339
340 if ( count( $variantArray ) > 1 && !$wgDisableLangConversion && !$wgDisableTitleConversion ) {
341 $defaultPreferences['noconvertlink'] =
342 array(
343 'type' => 'toggle',
344 'section' => 'personal/i18n',
345 'label-message' => 'tog-noconvertlink',
346 );
347 }
348
349 // show a preview of the old signature first
350 $oldsigWikiText = $wgParser->preSaveTransform( "~~~", $context->getTitle(), $user, ParserOptions::newFromContext( $context ) );
351 $oldsigHTML = $context->getOutput()->parseInline( $oldsigWikiText, true, true );
352 $defaultPreferences['oldsig'] = array(
353 'type' => 'info',
354 'raw' => true,
355 'label-message' => 'tog-oldsig',
356 'default' => $oldsigHTML,
357 'section' => 'personal/signature',
358 );
359 $defaultPreferences['nickname'] = array(
360 'type' => $wgAuth->allowPropChange( 'nickname' ) ? 'text' : 'info',
361 'maxlength' => $wgMaxSigChars,
362 'label-message' => 'yournick',
363 'validation-callback' => array( 'Preferences', 'validateSignature' ),
364 'section' => 'personal/signature',
365 'filter-callback' => array( 'Preferences', 'cleanSignature' ),
366 );
367 $defaultPreferences['fancysig'] = array(
368 'type' => 'toggle',
369 'label-message' => 'tog-fancysig',
370 'help-message' => 'prefs-help-signature', // show general help about signature at the bottom of the section
371 'section' => 'personal/signature'
372 );
373
374 ## Email stuff
375
376 if ( $wgEnableEmail ) {
377 $helpMessages[] = $wgEmailConfirmToEdit
378 ? 'prefs-help-email-required'
379 : 'prefs-help-email';
380
381 if( $wgEnableUserEmail ) {
382 // additional messages when users can send email to each other
383 $helpMessages[] = 'prefs-help-email-others';
384 }
385
386 $link = Linker::link(
387 SpecialPage::getTitleFor( 'ChangeEmail' ),
388 $context->msg( $user->getEmail() ? 'prefs-changeemail' : 'prefs-setemail' )->escaped(),
389 array(),
390 array( 'returnto' => SpecialPage::getTitleFor( 'Preferences' )->getPrefixedText() ) );
391
392 $emailAddress = $user->getEmail() ? htmlspecialchars( $user->getEmail() ) : '';
393 if ( $wgAuth->allowPropChange( 'emailaddress' ) ) {
394 $emailAddress .= $emailAddress == '' ? $link : (
395 $context->msg( 'word-separator' )->plain()
396 . $context->msg( 'parentheses' )->rawParams( $link )->plain()
397 );
398 }
399
400 $defaultPreferences['emailaddress'] = array(
401 'type' => 'info',
402 'raw' => true,
403 'default' => $emailAddress,
404 'label-message' => 'youremail',
405 'section' => 'personal/email',
406 'help-messages' => $helpMessages,
407 # 'cssclass' chosen below
408 );
409
410 $disableEmailPrefs = false;
411
412 $emailauthenticationclass = 'mw-email-not-authenticated';
413 if ( $wgEmailAuthentication ) {
414 if ( $user->getEmail() ) {
415 if ( $user->getEmailAuthenticationTimestamp() ) {
416 // date and time are separate parameters to facilitate localisation.
417 // $time is kept for backward compat reasons.
418 // 'emailauthenticated' is also used in SpecialConfirmemail.php
419 $displayUser = $context->getUser();
420 $emailTimestamp = $user->getEmailAuthenticationTimestamp();
421 $time = $lang->userTimeAndDate( $emailTimestamp, $displayUser );
422 $d = $lang->userDate( $emailTimestamp, $displayUser );
423 $t = $lang->userTime( $emailTimestamp, $displayUser );
424 $emailauthenticated = $context->msg( 'emailauthenticated',
425 $time, $d, $t )->parse() . '<br />';
426 $disableEmailPrefs = false;
427 $emailauthenticationclass = 'mw-email-authenticated';
428 } else {
429 $disableEmailPrefs = true;
430 $emailauthenticated = $context->msg( 'emailnotauthenticated' )->parse() . '<br />' .
431 Linker::linkKnown(
432 SpecialPage::getTitleFor( 'Confirmemail' ),
433 $context->msg( 'emailconfirmlink' )->escaped()
434 ) . '<br />';
435 $emailauthenticationclass="mw-email-not-authenticated";
436 }
437 } else {
438 $disableEmailPrefs = true;
439 $emailauthenticated = $context->msg( 'noemailprefs' )->escaped();
440 $emailauthenticationclass = 'mw-email-none';
441 }
442
443 $defaultPreferences['emailauthentication'] = array(
444 'type' => 'info',
445 'raw' => true,
446 'section' => 'personal/email',
447 'label-message' => 'prefs-emailconfirm-label',
448 'default' => $emailauthenticated,
449 # Apply the same CSS class used on the input to the message:
450 'cssclass' => $emailauthenticationclass,
451 );
452 }
453 $defaultPreferences['emailaddress']['cssclass'] = $emailauthenticationclass;
454
455 if ( $wgEnableUserEmail && $user->isAllowed( 'sendemail' ) ) {
456 $defaultPreferences['disablemail'] = array(
457 'type' => 'toggle',
458 'invert' => true,
459 'section' => 'personal/email',
460 'label-message' => 'allowemail',
461 'disabled' => $disableEmailPrefs,
462 );
463 $defaultPreferences['ccmeonemails'] = array(
464 'type' => 'toggle',
465 'section' => 'personal/email',
466 'label-message' => 'tog-ccmeonemails',
467 'disabled' => $disableEmailPrefs,
468 );
469 }
470
471 if ( $wgEnotifWatchlist ) {
472 $defaultPreferences['enotifwatchlistpages'] = array(
473 'type' => 'toggle',
474 'section' => 'personal/email',
475 'label-message' => 'tog-enotifwatchlistpages',
476 'disabled' => $disableEmailPrefs,
477 );
478 }
479 if ( $wgEnotifUserTalk ) {
480 $defaultPreferences['enotifusertalkpages'] = array(
481 'type' => 'toggle',
482 'section' => 'personal/email',
483 'label-message' => 'tog-enotifusertalkpages',
484 'disabled' => $disableEmailPrefs,
485 );
486 }
487 if ( $wgEnotifUserTalk || $wgEnotifWatchlist ) {
488 $defaultPreferences['enotifminoredits'] = array(
489 'type' => 'toggle',
490 'section' => 'personal/email',
491 'label-message' => 'tog-enotifminoredits',
492 'disabled' => $disableEmailPrefs,
493 );
494
495 if ( $wgEnotifRevealEditorAddress ) {
496 $defaultPreferences['enotifrevealaddr'] = array(
497 'type' => 'toggle',
498 'section' => 'personal/email',
499 'label-message' => 'tog-enotifrevealaddr',
500 'disabled' => $disableEmailPrefs,
501 );
502 }
503 }
504 }
505 }
506
507 /**
508 * @param $user User
509 * @param $context IContextSource
510 * @param $defaultPreferences
511 * @return void
512 */
513 static function skinPreferences( $user, IContextSource $context, &$defaultPreferences ) {
514 ## Skin #####################################
515 global $wgAllowUserCss, $wgAllowUserJs;
516
517 $defaultPreferences['skin'] = array(
518 'type' => 'radio',
519 'options' => self::generateSkinOptions( $user, $context ),
520 'label' => '&#160;',
521 'section' => 'rendering/skin',
522 );
523
524 # Create links to user CSS/JS pages for all skins
525 # This code is basically copied from generateSkinOptions(). It'd
526 # be nice to somehow merge this back in there to avoid redundancy.
527 if ( $wgAllowUserCss || $wgAllowUserJs ) {
528 $linkTools = array();
529 $userName = $user->getName();
530
531 if ( $wgAllowUserCss ) {
532 $cssPage = Title::makeTitleSafe( NS_USER, $userName . '/common.css' );
533 $linkTools[] = Linker::link( $cssPage, $context->msg( 'prefs-custom-css' )->escaped() );
534 }
535
536 if ( $wgAllowUserJs ) {
537 $jsPage = Title::makeTitleSafe( NS_USER, $userName . '/common.js' );
538 $linkTools[] = Linker::link( $jsPage, $context->msg( 'prefs-custom-js' )->escaped() );
539 }
540
541 $defaultPreferences['commoncssjs'] = array(
542 'type' => 'info',
543 'raw' => true,
544 'default' => $context->getLanguage()->pipeList( $linkTools ),
545 'label-message' => 'prefs-common-css-js',
546 'section' => 'rendering/skin',
547 );
548 }
549
550 $selectedSkin = $user->getOption( 'skin' );
551 if ( in_array( $selectedSkin, array( 'cologneblue', 'standard' ) ) ) {
552 $settings = array_flip( $context->getLanguage()->getQuickbarSettings() );
553
554 $defaultPreferences['quickbar'] = array(
555 'type' => 'radio',
556 'options' => $settings,
557 'section' => 'rendering/skin',
558 'label-message' => 'qbsettings',
559 );
560 }
561 }
562
563 /**
564 * @param $user User
565 * @param $context IContextSource
566 * @param $defaultPreferences Array
567 */
568 static function filesPreferences( $user, IContextSource $context, &$defaultPreferences ) {
569 ## Files #####################################
570 $defaultPreferences['imagesize'] = array(
571 'type' => 'select',
572 'options' => self::getImageSizes( $context ),
573 'label-message' => 'imagemaxsize',
574 'section' => 'rendering/files',
575 );
576 $defaultPreferences['thumbsize'] = array(
577 'type' => 'select',
578 'options' => self::getThumbSizes( $context ),
579 'label-message' => 'thumbsize',
580 'section' => 'rendering/files',
581 );
582 }
583
584 /**
585 * @param $user User
586 * @param $context IContextSource
587 * @param $defaultPreferences
588 * @return void
589 */
590 static function datetimePreferences( $user, IContextSource $context, &$defaultPreferences ) {
591 ## Date and time #####################################
592 $dateOptions = self::getDateOptions( $context );
593 if ( $dateOptions ) {
594 $defaultPreferences['date'] = array(
595 'type' => 'radio',
596 'options' => $dateOptions,
597 'label' => '&#160;',
598 'section' => 'datetime/dateformat',
599 );
600 }
601
602 // Info
603 $now = wfTimestampNow();
604 $lang = $context->getLanguage();
605 $nowlocal = Xml::element( 'span', array( 'id' => 'wpLocalTime' ),
606 $lang->time( $now, true ) );
607 $nowserver = $lang->time( $now, false ) .
608 Html::hidden( 'wpServerTime', (int)substr( $now, 8, 2 ) * 60 + (int)substr( $now, 10, 2 ) );
609
610 $defaultPreferences['nowserver'] = array(
611 'type' => 'info',
612 'raw' => 1,
613 'label-message' => 'servertime',
614 'default' => $nowserver,
615 'section' => 'datetime/timeoffset',
616 );
617
618 $defaultPreferences['nowlocal'] = array(
619 'type' => 'info',
620 'raw' => 1,
621 'label-message' => 'localtime',
622 'default' => $nowlocal,
623 'section' => 'datetime/timeoffset',
624 );
625
626 // Grab existing pref.
627 $tzOffset = $user->getOption( 'timecorrection' );
628 $tz = explode( '|', $tzOffset, 3 );
629
630 $tzOptions = self::getTimezoneOptions( $context );
631
632 $tzSetting = $tzOffset;
633 if ( count( $tz ) > 1 && $tz[0] == 'Offset' ) {
634 $minDiff = $tz[1];
635 $tzSetting = sprintf( '%+03d:%02d', floor( $minDiff / 60 ), abs( $minDiff ) % 60 );
636 } elseif ( count( $tz ) > 1 && $tz[0] == 'ZoneInfo' &&
637 !in_array( $tzOffset, HTMLFormField::flattenOptions( $tzOptions ) ) )
638 {
639 # Timezone offset can vary with DST
640 $userTZ = timezone_open( $tz[2] );
641 if ( $userTZ !== false ) {
642 $minDiff = floor( timezone_offset_get( $userTZ, date_create( 'now' ) ) / 60 );
643 $tzSetting = "ZoneInfo|$minDiff|{$tz[2]}";
644 }
645 }
646
647 $defaultPreferences['timecorrection'] = array(
648 'class' => 'HTMLSelectOrOtherField',
649 'label-message' => 'timezonelegend',
650 'options' => $tzOptions,
651 'default' => $tzSetting,
652 'size' => 20,
653 'section' => 'datetime/timeoffset',
654 );
655 }
656
657 /**
658 * @param $user User
659 * @param $context IContextSource
660 * @param $defaultPreferences Array
661 */
662 static function renderingPreferences( $user, IContextSource $context, &$defaultPreferences ) {
663 ## Page Rendering ##############################
664 global $wgAllowUserCssPrefs;
665 if ( $wgAllowUserCssPrefs ) {
666 $defaultPreferences['underline'] = array(
667 'type' => 'select',
668 'options' => array(
669 $context->msg( 'underline-never' )->text() => 0,
670 $context->msg( 'underline-always' )->text() => 1,
671 $context->msg( 'underline-default' )->text() => 2,
672 ),
673 'label-message' => 'tog-underline',
674 'section' => 'rendering/advancedrendering',
675 );
676 }
677
678 $stubThresholdValues = array( 50, 100, 500, 1000, 2000, 5000, 10000 );
679 $stubThresholdOptions = array( $context->msg( 'stub-threshold-disabled' )->text() => 0 );
680 foreach ( $stubThresholdValues as $value ) {
681 $stubThresholdOptions[$context->msg( 'size-bytes', $value )->text()] = $value;
682 }
683
684 $defaultPreferences['stubthreshold'] = array(
685 'type' => 'selectorother',
686 'section' => 'rendering/advancedrendering',
687 'options' => $stubThresholdOptions,
688 'size' => 20,
689 'label' => $context->msg( 'stub-threshold' )->text(), // Raw HTML message. Yay?
690 );
691
692 if ( $wgAllowUserCssPrefs ) {
693 $defaultPreferences['showtoc'] = array(
694 'type' => 'toggle',
695 'section' => 'rendering/advancedrendering',
696 'label-message' => 'tog-showtoc',
697 );
698 }
699 $defaultPreferences['nocache'] = array(
700 'type' => 'toggle',
701 'label-message' => 'tog-nocache',
702 'section' => 'rendering/advancedrendering',
703 );
704 $defaultPreferences['showhiddencats'] = array(
705 'type' => 'toggle',
706 'section' => 'rendering/advancedrendering',
707 'label-message' => 'tog-showhiddencats'
708 );
709 $defaultPreferences['showjumplinks'] = array(
710 'type' => 'toggle',
711 'section' => 'rendering/advancedrendering',
712 'label-message' => 'tog-showjumplinks',
713 );
714
715 if ( $wgAllowUserCssPrefs ) {
716 $defaultPreferences['justify'] = array(
717 'type' => 'toggle',
718 'section' => 'rendering/advancedrendering',
719 'label-message' => 'tog-justify',
720 );
721 }
722
723 $defaultPreferences['numberheadings'] = array(
724 'type' => 'toggle',
725 'section' => 'rendering/advancedrendering',
726 'label-message' => 'tog-numberheadings',
727 );
728 }
729
730 /**
731 * @param $user User
732 * @param $context IContextSource
733 * @param $defaultPreferences Array
734 */
735 static function editingPreferences( $user, IContextSource $context, &$defaultPreferences ) {
736 global $wgUseExternalEditor, $wgAllowUserCssPrefs;
737
738 ## Editing #####################################
739 $defaultPreferences['cols'] = array(
740 'type' => 'int',
741 'label-message' => 'columns',
742 'section' => 'editing/textboxsize',
743 'min' => 4,
744 'max' => 1000,
745 );
746 $defaultPreferences['rows'] = array(
747 'type' => 'int',
748 'label-message' => 'rows',
749 'section' => 'editing/textboxsize',
750 'min' => 4,
751 'max' => 1000,
752 );
753
754 if ( $wgAllowUserCssPrefs ) {
755 $defaultPreferences['editfont'] = array(
756 'type' => 'select',
757 'section' => 'editing/advancedediting',
758 'label-message' => 'editfont-style',
759 'options' => array(
760 $context->msg( 'editfont-default' )->text() => 'default',
761 $context->msg( 'editfont-monospace' )->text() => 'monospace',
762 $context->msg( 'editfont-sansserif' )->text() => 'sans-serif',
763 $context->msg( 'editfont-serif' )->text() => 'serif',
764 )
765 );
766 }
767 $defaultPreferences['previewontop'] = array(
768 'type' => 'toggle',
769 'section' => 'editing/advancedediting',
770 'label-message' => 'tog-previewontop',
771 );
772 $defaultPreferences['previewonfirst'] = array(
773 'type' => 'toggle',
774 'section' => 'editing/advancedediting',
775 'label-message' => 'tog-previewonfirst',
776 );
777
778 if ( $wgAllowUserCssPrefs ) {
779 $defaultPreferences['editsection'] = array(
780 'type' => 'toggle',
781 'section' => 'editing/advancedediting',
782 'label-message' => 'tog-editsection',
783 );
784 }
785 $defaultPreferences['editsectiononrightclick'] = array(
786 'type' => 'toggle',
787 'section' => 'editing/advancedediting',
788 'label-message' => 'tog-editsectiononrightclick',
789 );
790 $defaultPreferences['editondblclick'] = array(
791 'type' => 'toggle',
792 'section' => 'editing/advancedediting',
793 'label-message' => 'tog-editondblclick',
794 );
795 $defaultPreferences['showtoolbar'] = array(
796 'type' => 'toggle',
797 'section' => 'editing/advancedediting',
798 'label-message' => 'tog-showtoolbar',
799 );
800
801 if ( $user->isAllowed( 'minoredit' ) ) {
802 $defaultPreferences['minordefault'] = array(
803 'type' => 'toggle',
804 'section' => 'editing/advancedediting',
805 'label-message' => 'tog-minordefault',
806 );
807 }
808
809 if ( $wgUseExternalEditor ) {
810 $defaultPreferences['externaleditor'] = array(
811 'type' => 'toggle',
812 'section' => 'editing/advancedediting',
813 'label-message' => 'tog-externaleditor',
814 );
815 $defaultPreferences['externaldiff'] = array(
816 'type' => 'toggle',
817 'section' => 'editing/advancedediting',
818 'label-message' => 'tog-externaldiff',
819 );
820 }
821
822 $defaultPreferences['forceeditsummary'] = array(
823 'type' => 'toggle',
824 'section' => 'editing/advancedediting',
825 'label-message' => 'tog-forceeditsummary',
826 );
827
828 $defaultPreferences['uselivepreview'] = array(
829 'type' => 'toggle',
830 'section' => 'editing/advancedediting',
831 'label-message' => 'tog-uselivepreview',
832 );
833 }
834
835 /**
836 * @param $user User
837 * @param $context IContextSource
838 * @param $defaultPreferences Array
839 */
840 static function rcPreferences( $user, IContextSource $context, &$defaultPreferences ) {
841 global $wgRCMaxAge, $wgRCShowWatchingUsers;
842
843 ## RecentChanges #####################################
844 $defaultPreferences['rcdays'] = array(
845 'type' => 'float',
846 'label-message' => 'recentchangesdays',
847 'section' => 'rc/displayrc',
848 'min' => 1,
849 'max' => ceil( $wgRCMaxAge / ( 3600 * 24 ) ),
850 'help' => $context->msg( 'recentchangesdays-max' )->numParams(
851 ceil( $wgRCMaxAge / ( 3600 * 24 ) ) )->text()
852 );
853 $defaultPreferences['rclimit'] = array(
854 'type' => 'int',
855 'label-message' => 'recentchangescount',
856 'help-message' => 'prefs-help-recentchangescount',
857 'section' => 'rc/displayrc',
858 );
859 $defaultPreferences['usenewrc'] = array(
860 'type' => 'toggle',
861 'label-message' => 'tog-usenewrc',
862 'section' => 'rc/advancedrc',
863 );
864 $defaultPreferences['hideminor'] = array(
865 'type' => 'toggle',
866 'label-message' => 'tog-hideminor',
867 'section' => 'rc/advancedrc',
868 );
869
870 if ( $user->useRCPatrol() ) {
871 $defaultPreferences['hidepatrolled'] = array(
872 'type' => 'toggle',
873 'section' => 'rc/advancedrc',
874 'label-message' => 'tog-hidepatrolled',
875 );
876 $defaultPreferences['newpageshidepatrolled'] = array(
877 'type' => 'toggle',
878 'section' => 'rc/advancedrc',
879 'label-message' => 'tog-newpageshidepatrolled',
880 );
881 }
882
883 if ( $wgRCShowWatchingUsers ) {
884 $defaultPreferences['shownumberswatching'] = array(
885 'type' => 'toggle',
886 'section' => 'rc/advancedrc',
887 'label-message' => 'tog-shownumberswatching',
888 );
889 }
890 }
891
892 /**
893 * @param $user User
894 * @param $context IContextSource
895 * @param $defaultPreferences
896 */
897 static function watchlistPreferences( $user, IContextSource $context, &$defaultPreferences ) {
898 global $wgUseRCPatrol, $wgEnableAPI, $wgRCMaxAge;
899
900 $watchlistdaysMax = ceil( $wgRCMaxAge / ( 3600 * 24 ) );
901
902 ## Watchlist #####################################
903 $defaultPreferences['watchlistdays'] = array(
904 'type' => 'float',
905 'min' => 0,
906 'max' => $watchlistdaysMax,
907 'section' => 'watchlist/displaywatchlist',
908 'help' => $context->msg( 'prefs-watchlist-days-max' )->numParams(
909 $watchlistdaysMax )->text(),
910 'label-message' => 'prefs-watchlist-days',
911 );
912 $defaultPreferences['wllimit'] = array(
913 'type' => 'int',
914 'min' => 0,
915 'max' => 1000,
916 'label-message' => 'prefs-watchlist-edits',
917 'help' => $context->msg( 'prefs-watchlist-edits-max' )->escaped(),
918 'section' => 'watchlist/displaywatchlist',
919 );
920 $defaultPreferences['extendwatchlist'] = array(
921 'type' => 'toggle',
922 'section' => 'watchlist/advancedwatchlist',
923 'label-message' => 'tog-extendwatchlist',
924 );
925 $defaultPreferences['watchlisthideminor'] = array(
926 'type' => 'toggle',
927 'section' => 'watchlist/advancedwatchlist',
928 'label-message' => 'tog-watchlisthideminor',
929 );
930 $defaultPreferences['watchlisthidebots'] = array(
931 'type' => 'toggle',
932 'section' => 'watchlist/advancedwatchlist',
933 'label-message' => 'tog-watchlisthidebots',
934 );
935 $defaultPreferences['watchlisthideown'] = array(
936 'type' => 'toggle',
937 'section' => 'watchlist/advancedwatchlist',
938 'label-message' => 'tog-watchlisthideown',
939 );
940 $defaultPreferences['watchlisthideanons'] = array(
941 'type' => 'toggle',
942 'section' => 'watchlist/advancedwatchlist',
943 'label-message' => 'tog-watchlisthideanons',
944 );
945 $defaultPreferences['watchlisthideliu'] = array(
946 'type' => 'toggle',
947 'section' => 'watchlist/advancedwatchlist',
948 'label-message' => 'tog-watchlisthideliu',
949 );
950
951 if ( $wgUseRCPatrol ) {
952 $defaultPreferences['watchlisthidepatrolled'] = array(
953 'type' => 'toggle',
954 'section' => 'watchlist/advancedwatchlist',
955 'label-message' => 'tog-watchlisthidepatrolled',
956 );
957 }
958
959 if ( $wgEnableAPI ) {
960 # Some random gibberish as a proposed default
961 // @todo Fixme: this should use CryptRand but we may not want to read urandom on every view
962 $hash = sha1( mt_rand() . microtime( true ) );
963
964 $defaultPreferences['watchlisttoken'] = array(
965 'type' => 'text',
966 'section' => 'watchlist/advancedwatchlist',
967 'label-message' => 'prefs-watchlist-token',
968 'help' => $context->msg( 'prefs-help-watchlist-token', $hash )->escaped()
969 );
970 }
971
972 $watchTypes = array(
973 'edit' => 'watchdefault',
974 'move' => 'watchmoves',
975 'delete' => 'watchdeletion'
976 );
977
978 // Kinda hacky
979 if ( $user->isAllowed( 'createpage' ) || $user->isAllowed( 'createtalk' ) ) {
980 $watchTypes['read'] = 'watchcreations';
981 }
982
983 foreach ( $watchTypes as $action => $pref ) {
984 if ( $user->isAllowed( $action ) ) {
985 $defaultPreferences[$pref] = array(
986 'type' => 'toggle',
987 'section' => 'watchlist/advancedwatchlist',
988 'label-message' => "tog-$pref",
989 );
990 }
991 }
992 }
993
994 /**
995 * @param $user User
996 * @param $context IContextSource
997 * @param $defaultPreferences Array
998 */
999 static function searchPreferences( $user, IContextSource $context, &$defaultPreferences ) {
1000 global $wgContLang, $wgVectorUseSimpleSearch;
1001
1002 ## Search #####################################
1003 $defaultPreferences['searchlimit'] = array(
1004 'type' => 'int',
1005 'label-message' => 'resultsperpage',
1006 'section' => 'searchoptions/displaysearchoptions',
1007 'min' => 0,
1008 );
1009
1010 if ( $wgVectorUseSimpleSearch ) {
1011 $defaultPreferences['vector-simplesearch'] = array(
1012 'type' => 'toggle',
1013 'label-message' => 'vector-simplesearch-preference',
1014 'section' => 'searchoptions/displaysearchoptions',
1015 );
1016 }
1017
1018 $defaultPreferences['disablesuggest'] = array(
1019 'type' => 'toggle',
1020 'label-message' => 'mwsuggest-disable',
1021 'section' => 'searchoptions/displaysearchoptions',
1022 );
1023
1024 $defaultPreferences['searcheverything'] = array(
1025 'type' => 'toggle',
1026 'label-message' => 'searcheverything-enable',
1027 'section' => 'searchoptions/advancedsearchoptions',
1028 );
1029
1030 $nsOptions = $wgContLang->getFormattedNamespaces();
1031 $nsOptions[0] = $context->msg( 'blanknamespace' )->text();
1032 foreach ( $nsOptions as $ns => $name ) {
1033 if ( $ns < 0 )
1034 unset( $nsOptions[$ns] );
1035 }
1036
1037 $defaultPreferences['searchnamespaces'] = array(
1038 'type' => 'multiselect',
1039 'label-message' => 'defaultns',
1040 'options' => array_flip( $nsOptions ),
1041 'section' => 'searchoptions/advancedsearchoptions',
1042 'prefix' => 'searchNs',
1043 );
1044 }
1045
1046 /**
1047 * @param $user User
1048 * @param $context IContextSource
1049 * @param $defaultPreferences Array
1050 */
1051 static function miscPreferences( $user, IContextSource $context, &$defaultPreferences ) {
1052 global $wgContLang;
1053
1054 ## Misc #####################################
1055 $defaultPreferences['diffonly'] = array(
1056 'type' => 'toggle',
1057 'section' => 'misc/diffs',
1058 'label-message' => 'tog-diffonly',
1059 );
1060 $defaultPreferences['norollbackdiff'] = array(
1061 'type' => 'toggle',
1062 'section' => 'misc/diffs',
1063 'label-message' => 'tog-norollbackdiff',
1064 );
1065
1066 // Stuff from Language::getExtraUserToggles()
1067 $toggles = $wgContLang->getExtraUserToggles();
1068
1069 foreach ( $toggles as $toggle ) {
1070 $defaultPreferences[$toggle] = array(
1071 'type' => 'toggle',
1072 'section' => 'personal/i18n',
1073 'label-message' => "tog-$toggle",
1074 );
1075 }
1076 }
1077
1078 /**
1079 * @param $user User The User object
1080 * @param $context IContextSource
1081 * @return Array: text/links to display as key; $skinkey as value
1082 */
1083 static function generateSkinOptions( $user, IContextSource $context ) {
1084 global $wgDefaultSkin, $wgAllowUserCss, $wgAllowUserJs;
1085 $ret = array();
1086
1087 $mptitle = Title::newMainPage();
1088 $previewtext = $context->msg( 'skin-preview' )->text();
1089
1090 # Only show members of Skin::getSkinNames() rather than
1091 # $skinNames (skins is all skin names from Language.php)
1092 $validSkinNames = Skin::getUsableSkins();
1093
1094 # Sort by UI skin name. First though need to update validSkinNames as sometimes
1095 # the skinkey & UI skinname differ (e.g. "standard" skinkey is "Classic" in the UI).
1096 foreach ( $validSkinNames as $skinkey => &$skinname ) {
1097 $msg = $context->msg( "skinname-{$skinkey}" );
1098 if ( $msg->exists() ) {
1099 $skinname = htmlspecialchars( $msg->text() );
1100 }
1101 }
1102 asort( $validSkinNames );
1103
1104 foreach ( $validSkinNames as $skinkey => $sn ) {
1105 $linkTools = array();
1106
1107 # Mark the default skin
1108 if ( $skinkey == $wgDefaultSkin ) {
1109 $linkTools[] = $context->msg( 'default' )->escaped();
1110 }
1111
1112 # Create preview link
1113 $mplink = htmlspecialchars( $mptitle->getLocalURL( "useskin=$skinkey" ) );
1114 $linkTools[] = "<a target='_blank' href=\"$mplink\">$previewtext</a>";
1115
1116 # Create links to user CSS/JS pages
1117 if ( $wgAllowUserCss ) {
1118 $cssPage = Title::makeTitleSafe( NS_USER, $user->getName() . '/' . $skinkey . '.css' );
1119 $linkTools[] = Linker::link( $cssPage, $context->msg( 'prefs-custom-css' )->escaped() );
1120 }
1121
1122 if ( $wgAllowUserJs ) {
1123 $jsPage = Title::makeTitleSafe( NS_USER, $user->getName() . '/' . $skinkey . '.js' );
1124 $linkTools[] = Linker::link( $jsPage, $context->msg( 'prefs-custom-js' )->escaped() );
1125 }
1126
1127 $display = $sn . ' ' . $context->msg( 'parentheses', $context->getLanguage()->pipeList( $linkTools ) )->text();
1128 $ret[$display] = $skinkey;
1129 }
1130
1131 return $ret;
1132 }
1133
1134 /**
1135 * @param $context IContextSource
1136 * @return array
1137 */
1138 static function getDateOptions( IContextSource $context ) {
1139 $lang = $context->getLanguage();
1140 $dateopts = $lang->getDatePreferences();
1141
1142 $ret = array();
1143
1144 if ( $dateopts ) {
1145 if ( !in_array( 'default', $dateopts ) ) {
1146 $dateopts[] = 'default'; // Make sure default is always valid
1147 // Bug 19237
1148 }
1149
1150 // KLUGE: site default might not be valid for user language
1151 global $wgDefaultUserOptions;
1152 if ( !in_array( $wgDefaultUserOptions['date'], $dateopts ) ) {
1153 $wgDefaultUserOptions['date'] = 'default';
1154 }
1155
1156 $epoch = wfTimestampNow();
1157 foreach ( $dateopts as $key ) {
1158 if ( $key == 'default' ) {
1159 $formatted = $context->msg( 'datedefault' )->escaped();
1160 } else {
1161 $formatted = htmlspecialchars( $lang->timeanddate( $epoch, false, $key ) );
1162 }
1163 $ret[$formatted] = $key;
1164 }
1165 }
1166 return $ret;
1167 }
1168
1169 /**
1170 * @param $context IContextSource
1171 * @return array
1172 */
1173 static function getImageSizes( IContextSource $context ) {
1174 global $wgImageLimits;
1175
1176 $ret = array();
1177 $pixels = $context->msg( 'unit-pixel' )->text();
1178
1179 foreach ( $wgImageLimits as $index => $limits ) {
1180 $display = "{$limits[0]}×{$limits[1]}" . $pixels;
1181 $ret[$display] = $index;
1182 }
1183
1184 return $ret;
1185 }
1186
1187 /**
1188 * @param $context IContextSource
1189 * @return array
1190 */
1191 static function getThumbSizes( IContextSource $context ) {
1192 global $wgThumbLimits;
1193
1194 $ret = array();
1195 $pixels = $context->msg( 'unit-pixel' )->text();
1196
1197 foreach ( $wgThumbLimits as $index => $size ) {
1198 $display = $size . $pixels;
1199 $ret[$display] = $index;
1200 }
1201
1202 return $ret;
1203 }
1204
1205 /**
1206 * @param $signature string
1207 * @param $alldata array
1208 * @param $form HTMLForm
1209 * @return bool|string
1210 */
1211 static function validateSignature( $signature, $alldata, $form ) {
1212 global $wgParser, $wgMaxSigChars;
1213 if ( mb_strlen( $signature ) > $wgMaxSigChars ) {
1214 return Xml::element( 'span', array( 'class' => 'error' ),
1215 $form->msg( 'badsiglength' )->numParams( $wgMaxSigChars )->text() );
1216 } elseif ( isset( $alldata['fancysig'] ) &&
1217 $alldata['fancysig'] &&
1218 false === $wgParser->validateSig( $signature ) ) {
1219 return Xml::element( 'span', array( 'class' => 'error' ), $form->msg( 'badsig' )->text() );
1220 } else {
1221 return true;
1222 }
1223 }
1224
1225 /**
1226 * @param $signature string
1227 * @param $alldata array
1228 * @param $form HTMLForm
1229 * @return string
1230 */
1231 static function cleanSignature( $signature, $alldata, $form ) {
1232 if ( isset( $alldata['fancysig'] ) && $alldata['fancysig'] ) {
1233 global $wgParser;
1234 $signature = $wgParser->cleanSig( $signature );
1235 } else {
1236 // When no fancy sig used, make sure ~{3,5} get removed.
1237 $signature = Parser::cleanSigInSig( $signature );
1238 }
1239
1240 return $signature;
1241 }
1242
1243 /**
1244 * @param $user User
1245 * @param $context IContextSource
1246 * @param $formClass string
1247 * @param $remove Array: array of items to remove
1248 * @return HtmlForm
1249 */
1250 static function getFormObject( $user, IContextSource $context, $formClass = 'PreferencesForm', array $remove = array() ) {
1251 $formDescriptor = Preferences::getPreferences( $user, $context );
1252 if ( count( $remove ) ) {
1253 $removeKeys = array_flip( $remove );
1254 $formDescriptor = array_diff_key( $formDescriptor, $removeKeys );
1255 }
1256
1257 // Remove type=api preferences. They are not intended for rendering in the form.
1258 foreach ( $formDescriptor as $name => $info ) {
1259 if ( isset( $info['type'] ) && $info['type'] === 'api' ) {
1260 unset( $formDescriptor[$name] );
1261 }
1262 }
1263
1264 /**
1265 * @var $htmlForm PreferencesForm
1266 */
1267 $htmlForm = new $formClass( $formDescriptor, $context, 'prefs' );
1268
1269 $htmlForm->setModifiedUser( $user );
1270 $htmlForm->setId( 'mw-prefs-form' );
1271 $htmlForm->setSubmitText( $context->msg( 'saveprefs' )->text() );
1272 # Used message keys: 'accesskey-preferences-save', 'tooltip-preferences-save'
1273 $htmlForm->setSubmitTooltip( 'preferences-save' );
1274 $htmlForm->setSubmitID( 'prefsubmit' );
1275 $htmlForm->setSubmitCallback( array( 'Preferences', 'tryFormSubmit' ) );
1276
1277 return $htmlForm;
1278 }
1279
1280 /**
1281 * @return array
1282 */
1283 static function getTimezoneOptions( IContextSource $context ) {
1284 $opt = array();
1285
1286 global $wgLocalTZoffset, $wgLocaltimezone;
1287 // Check that $wgLocalTZoffset is the same as $wgLocaltimezone
1288 if ( $wgLocalTZoffset == date( 'Z' ) / 60 ) {
1289 $server_tz_msg = $context->msg( 'timezoneuseserverdefault', $wgLocaltimezone )->text();
1290 } else {
1291 $tzstring = sprintf( '%+03d:%02d', floor( $wgLocalTZoffset / 60 ), abs( $wgLocalTZoffset ) % 60 );
1292 $server_tz_msg = $context->msg( 'timezoneuseserverdefault', $tzstring )->text();
1293 }
1294 $opt[$server_tz_msg] = "System|$wgLocalTZoffset";
1295 $opt[$context->msg( 'timezoneuseoffset' )->text()] = 'other';
1296 $opt[$context->msg( 'guesstimezone' )->text()] = 'guess';
1297
1298 if ( function_exists( 'timezone_identifiers_list' ) ) {
1299 # Read timezone list
1300 $tzs = timezone_identifiers_list();
1301 sort( $tzs );
1302
1303 $tzRegions = array();
1304 $tzRegions['Africa'] = $context->msg( 'timezoneregion-africa' )->text();
1305 $tzRegions['America'] = $context->msg( 'timezoneregion-america' )->text();
1306 $tzRegions['Antarctica'] = $context->msg( 'timezoneregion-antarctica' )->text();
1307 $tzRegions['Arctic'] = $context->msg( 'timezoneregion-arctic' )->text();
1308 $tzRegions['Asia'] = $context->msg( 'timezoneregion-asia' )->text();
1309 $tzRegions['Atlantic'] = $context->msg( 'timezoneregion-atlantic' )->text();
1310 $tzRegions['Australia'] = $context->msg( 'timezoneregion-australia' )->text();
1311 $tzRegions['Europe'] = $context->msg( 'timezoneregion-europe' )->text();
1312 $tzRegions['Indian'] = $context->msg( 'timezoneregion-indian' )->text();
1313 $tzRegions['Pacific'] = $context->msg( 'timezoneregion-pacific' )->text();
1314 asort( $tzRegions );
1315
1316 $prefill = array_fill_keys( array_values( $tzRegions ), array() );
1317 $opt = array_merge( $opt, $prefill );
1318
1319 $now = date_create( 'now' );
1320
1321 foreach ( $tzs as $tz ) {
1322 $z = explode( '/', $tz, 2 );
1323
1324 # timezone_identifiers_list() returns a number of
1325 # backwards-compatibility entries. This filters them out of the
1326 # list presented to the user.
1327 if ( count( $z ) != 2 || !array_key_exists( $z[0], $tzRegions ) ) {
1328 continue;
1329 }
1330
1331 # Localize region
1332 $z[0] = $tzRegions[$z[0]];
1333
1334 $minDiff = floor( timezone_offset_get( timezone_open( $tz ), $now ) / 60 );
1335
1336 $display = str_replace( '_', ' ', $z[0] . '/' . $z[1] );
1337 $value = "ZoneInfo|$minDiff|$tz";
1338
1339 $opt[$z[0]][$display] = $value;
1340 }
1341 }
1342 return $opt;
1343 }
1344
1345 /**
1346 * @param $value
1347 * @param $alldata
1348 * @return int
1349 */
1350 static function filterIntval( $value, $alldata ) {
1351 return intval( $value );
1352 }
1353
1354 /**
1355 * @param $tz
1356 * @param $alldata
1357 * @return string
1358 */
1359 static function filterTimezoneInput( $tz, $alldata ) {
1360 $data = explode( '|', $tz, 3 );
1361 switch ( $data[0] ) {
1362 case 'ZoneInfo':
1363 case 'System':
1364 return $tz;
1365 default:
1366 $data = explode( ':', $tz, 2 );
1367 if ( count( $data ) == 2 ) {
1368 $data[0] = intval( $data[0] );
1369 $data[1] = intval( $data[1] );
1370 $minDiff = abs( $data[0] ) * 60 + $data[1];
1371 if ( $data[0] < 0 ) $minDiff = - $minDiff;
1372 } else {
1373 $minDiff = intval( $data[0] ) * 60;
1374 }
1375
1376 # Max is +14:00 and min is -12:00, see:
1377 # http://en.wikipedia.org/wiki/Timezone
1378 $minDiff = min( $minDiff, 840 ); # 14:00
1379 $minDiff = max( $minDiff, - 720 ); # -12:00
1380 return 'Offset|' . $minDiff;
1381 }
1382 }
1383
1384 /**
1385 * @param $formData
1386 * @param $form PreferencesForm
1387 * @param $entryPoint string
1388 * @return bool|Status|string
1389 */
1390 static function tryFormSubmit( $formData, $form, $entryPoint = 'internal' ) {
1391 global $wgHiddenPrefs, $wgAuth;
1392
1393 $user = $form->getModifiedUser();
1394 $result = true;
1395
1396 // Filter input
1397 foreach ( array_keys( $formData ) as $name ) {
1398 if ( isset( self::$saveFilters[$name] ) ) {
1399 $formData[$name] =
1400 call_user_func( self::$saveFilters[$name], $formData[$name], $formData );
1401 }
1402 }
1403
1404 // Stuff that shouldn't be saved as a preference.
1405 $saveBlacklist = array(
1406 'realname',
1407 'emailaddress',
1408 );
1409
1410 // Fortunately, the realname field is MUCH simpler
1411 if ( !in_array( 'realname', $wgHiddenPrefs ) ) {
1412 $realName = $formData['realname'];
1413 $user->setRealName( $realName );
1414 }
1415
1416 foreach ( $saveBlacklist as $b ) {
1417 unset( $formData[$b] );
1418 }
1419
1420 # If users have saved a value for a preference which has subsequently been disabled
1421 # via $wgHiddenPrefs, we don't want to destroy that setting in case the preference
1422 # is subsequently re-enabled
1423 # TODO: maintenance script to actually delete these
1424 foreach( $wgHiddenPrefs as $pref ) {
1425 # If the user has not set a non-default value here, the default will be returned
1426 # and subsequently discarded
1427 $formData[$pref] = $user->getOption( $pref, null, true );
1428 }
1429
1430 // Keep old preferences from interfering due to back-compat code, etc.
1431 $user->resetOptions( 'unused', $form->getContext() );
1432
1433 foreach ( $formData as $key => $value ) {
1434 $user->setOption( $key, $value );
1435 }
1436
1437 $user->saveSettings();
1438
1439 $wgAuth->updateExternalDB( $user );
1440
1441 return $result;
1442 }
1443
1444 /**
1445 * @param $formData
1446 * @param $form PreferencesForm
1447 * @return Status
1448 */
1449 public static function tryUISubmit( $formData, $form ) {
1450 $res = self::tryFormSubmit( $formData, $form, 'ui' );
1451
1452 if ( $res ) {
1453 $urlOptions = array( 'success' => 1 );
1454
1455 if ( $res === 'eauth' ) {
1456 $urlOptions['eauth'] = 1;
1457 }
1458
1459 $urlOptions += $form->getExtraSuccessRedirectParameters();
1460
1461 $url = $form->getTitle()->getFullURL( $urlOptions );
1462
1463 $form->getContext()->getOutput()->redirect( $url );
1464 }
1465
1466 return Status::newGood();
1467 }
1468
1469 /**
1470 * Try to set a user's email address.
1471 * This does *not* try to validate the address.
1472 * Caller is responsible for checking $wgAuth.
1473 *
1474 * @deprecated in 1.20; use User::setEmailWithConfirmation() instead.
1475 * @param $user User
1476 * @param $newaddr string New email address
1477 * @return Array (true on success or Status on failure, info string)
1478 */
1479 public static function trySetUserEmail( User $user, $newaddr ) {
1480 wfDeprecated( __METHOD__, '1.20' );
1481
1482 $result = $user->setEmailWithConfirmation( $newaddr );
1483 if ( $result->isGood() ) {
1484 return array( true, $result->value );
1485 } else {
1486 return array( $result, 'mailerror' );
1487 }
1488 }
1489
1490 /**
1491 * @deprecated in 1.19; will be removed in 1.20.
1492 * @param $user User
1493 * @return array
1494 */
1495 public static function loadOldSearchNs( $user ) {
1496 wfDeprecated( __METHOD__, '1.19' );
1497
1498 $searchableNamespaces = SearchEngine::searchableNamespaces();
1499 // Back compat with old format
1500 $arr = array();
1501
1502 foreach ( $searchableNamespaces as $ns => $name ) {
1503 if ( $user->getOption( 'searchNs' . $ns ) ) {
1504 $arr[] = $ns;
1505 }
1506 }
1507
1508 return $arr;
1509 }
1510 }
1511
1512 /** Some tweaks to allow js prefs to work */
1513 class PreferencesForm extends HTMLForm {
1514 // Override default value from HTMLForm
1515 protected $mSubSectionBeforeFields = false;
1516
1517 private $modifiedUser;
1518
1519 /**
1520 * @param $user User
1521 */
1522 public function setModifiedUser( $user ) {
1523 $this->modifiedUser = $user;
1524 }
1525
1526 /**
1527 * @return User
1528 */
1529 public function getModifiedUser() {
1530 if ( $this->modifiedUser === null ) {
1531 return $this->getUser();
1532 } else {
1533 return $this->modifiedUser;
1534 }
1535 }
1536
1537 /**
1538 * Get extra parameters for the query string when redirecting after
1539 * successful save.
1540 *
1541 * @return array()
1542 */
1543 public function getExtraSuccessRedirectParameters() {
1544 return array();
1545 }
1546
1547 /**
1548 * @param $html string
1549 * @return String
1550 */
1551 function wrapForm( $html ) {
1552 $html = Xml::tags( 'div', array( 'id' => 'preferences' ), $html );
1553
1554 return parent::wrapForm( $html );
1555 }
1556
1557 /**
1558 * @return String
1559 */
1560 function getButtons() {
1561 $html = parent::getButtons();
1562
1563 $t = SpecialPage::getTitleFor( 'Preferences', 'reset' );
1564
1565 $html .= "\n" . Linker::link( $t, $this->msg( 'restoreprefs' )->escaped() );
1566
1567 $html = Xml::tags( 'div', array( 'class' => 'mw-prefs-buttons' ), $html );
1568
1569 return $html;
1570 }
1571
1572 /**
1573 * @param $data array
1574 * @return array
1575 */
1576 function filterDataForSubmit( $data ) {
1577 // Support for separating multi-option preferences into multiple preferences
1578 // Due to lack of array support.
1579 foreach ( $this->mFlatFields as $fieldname => $field ) {
1580 $info = $field->mParams;
1581
1582 if ( $field instanceof HTMLMultiSelectField ) {
1583 $options = HTMLFormField::flattenOptions( $info['options'] );
1584 $prefix = isset( $info['prefix'] ) ? $info['prefix'] : $fieldname;
1585
1586 foreach ( $options as $opt ) {
1587 $data["$prefix$opt"] = in_array( $opt, $data[$fieldname] );
1588 }
1589
1590 unset( $data[$fieldname] );
1591
1592 } elseif ( $field instanceof HTMLCheckMatrix ) {
1593 $columns = HTMLFormField::flattenOptions( $info['columns'] );
1594 $rows = HTMLFormField::flattenOptions( $info['rows'] );
1595 $prefix = isset( $info['prefix'] ) ? $info['prefix'] : $fieldname;
1596 foreach ( $columns as $column ) {
1597 foreach ( $rows as $row ) {
1598 // Make sure option hasn't been removed
1599 if ( !isset( $info['remove-options'] )
1600 || !in_array( "$column-$row", $info['remove-options'] ) )
1601 {
1602 $data["$prefix-$column-$row"] = in_array( "$column-$row", $data[$fieldname] );
1603 }
1604 }
1605 }
1606
1607 unset( $data[$fieldname] );
1608 }
1609 }
1610
1611 return $data;
1612 }
1613
1614 /**
1615 * Get the whole body of the form.
1616 * @return string
1617 */
1618 function getBody() {
1619 return $this->displaySection( $this->mFieldTree, '', 'mw-prefsection-' );
1620 }
1621
1622 /**
1623 * Get the "<legend>" for a given section key. Normally this is the
1624 * prefs-$key message but we'll allow extensions to override it.
1625 * @param $key string
1626 * @return string
1627 */
1628 function getLegend( $key ) {
1629 $legend = parent::getLegend( $key );
1630 wfRunHooks( 'PreferencesGetLegend', array( $this, $key, &$legend ) );
1631 return $legend;
1632 }
1633 }