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