Replacing strictEqual-assertion with a more useful QUnit.push that includes the expec...
[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 /**
521 * @param $user User
522 * @param $defaultPreferences Array
523 */
524 static function filesPreferences( $user, &$defaultPreferences ) {
525 ## Files #####################################
526 $defaultPreferences['imagesize'] = array(
527 'type' => 'select',
528 'options' => self::getImageSizes(),
529 'label-message' => 'imagemaxsize',
530 'section' => 'rendering/files',
531 );
532 $defaultPreferences['thumbsize'] = array(
533 'type' => 'select',
534 'options' => self::getThumbSizes(),
535 'label-message' => 'thumbsize',
536 'section' => 'rendering/files',
537 );
538 }
539
540 /**
541 * @param $user User
542 * @param $defaultPreferences
543 * @return void
544 */
545 static function datetimePreferences( $user, &$defaultPreferences ) {
546 global $wgLang;
547
548 ## Date and time #####################################
549 $dateOptions = self::getDateOptions();
550 if ( $dateOptions ) {
551 $defaultPreferences['date'] = array(
552 'type' => 'radio',
553 'options' => $dateOptions,
554 'label' => '&#160;',
555 'section' => 'datetime/dateformat',
556 );
557 }
558
559 // Info
560 $now = wfTimestampNow();
561 $nowlocal = Xml::element( 'span', array( 'id' => 'wpLocalTime' ),
562 $wgLang->time( $now, true ) );
563 $nowserver = $wgLang->time( $now, false ) .
564 Html::hidden( 'wpServerTime', (int)substr( $now, 8, 2 ) * 60 + (int)substr( $now, 10, 2 ) );
565
566 $defaultPreferences['nowserver'] = array(
567 'type' => 'info',
568 'raw' => 1,
569 'label-message' => 'servertime',
570 'default' => $nowserver,
571 'section' => 'datetime/timeoffset',
572 );
573
574 $defaultPreferences['nowlocal'] = array(
575 'type' => 'info',
576 'raw' => 1,
577 'label-message' => 'localtime',
578 'default' => $nowlocal,
579 'section' => 'datetime/timeoffset',
580 );
581
582 // Grab existing pref.
583 $tzOffset = $user->getOption( 'timecorrection' );
584 $tz = explode( '|', $tzOffset, 2 );
585
586 $tzSetting = $tzOffset;
587 if ( count( $tz ) > 1 && $tz[0] == 'Offset' ) {
588 $minDiff = $tz[1];
589 $tzSetting = sprintf( '%+03d:%02d', floor( $minDiff / 60 ), abs( $minDiff ) % 60 );
590 }
591
592 $defaultPreferences['timecorrection'] = array(
593 'class' => 'HTMLSelectOrOtherField',
594 'label-message' => 'timezonelegend',
595 'options' => self::getTimezoneOptions(),
596 'default' => $tzSetting,
597 'size' => 20,
598 'section' => 'datetime/timeoffset',
599 );
600 }
601
602 /**
603 * @param $user User
604 * @param $defaultPreferences Array
605 */
606 static function renderingPreferences( $user, &$defaultPreferences ) {
607 ## Page Rendering ##############################
608 global $wgAllowUserCssPrefs;
609 if ( $wgAllowUserCssPrefs ) {
610 $defaultPreferences['underline'] = array(
611 'type' => 'select',
612 'options' => array(
613 wfMsg( 'underline-never' ) => 0,
614 wfMsg( 'underline-always' ) => 1,
615 wfMsg( 'underline-default' ) => 2,
616 ),
617 'label-message' => 'tog-underline',
618 'section' => 'rendering/advancedrendering',
619 );
620 }
621
622 $stubThresholdValues = array( 50, 100, 500, 1000, 2000, 5000, 10000 );
623 $stubThresholdOptions = array( wfMsg( 'stub-threshold-disabled' ) => 0 );
624 foreach ( $stubThresholdValues as $value ) {
625 $stubThresholdOptions[wfMsg( 'size-bytes', $value )] = $value;
626 }
627
628 $defaultPreferences['stubthreshold'] = array(
629 'type' => 'selectorother',
630 'section' => 'rendering/advancedrendering',
631 'options' => $stubThresholdOptions,
632 'size' => 20,
633 'label' => wfMsg( 'stub-threshold' ), // Raw HTML message. Yay?
634 );
635
636 if ( $wgAllowUserCssPrefs ) {
637 $defaultPreferences['highlightbroken'] = array(
638 'type' => 'toggle',
639 'section' => 'rendering/advancedrendering',
640 'label' => wfMsg( 'tog-highlightbroken' ), // Raw HTML
641 );
642 $defaultPreferences['showtoc'] = array(
643 'type' => 'toggle',
644 'section' => 'rendering/advancedrendering',
645 'label-message' => 'tog-showtoc',
646 );
647 }
648 $defaultPreferences['nocache'] = array(
649 'type' => 'toggle',
650 'label-message' => 'tog-nocache',
651 'section' => 'rendering/advancedrendering',
652 );
653 $defaultPreferences['showhiddencats'] = array(
654 'type' => 'toggle',
655 'section' => 'rendering/advancedrendering',
656 'label-message' => 'tog-showhiddencats'
657 );
658 $defaultPreferences['showjumplinks'] = array(
659 'type' => 'toggle',
660 'section' => 'rendering/advancedrendering',
661 'label-message' => 'tog-showjumplinks',
662 );
663
664 if ( $wgAllowUserCssPrefs ) {
665 $defaultPreferences['justify'] = array(
666 'type' => 'toggle',
667 'section' => 'rendering/advancedrendering',
668 'label-message' => 'tog-justify',
669 );
670 }
671
672 $defaultPreferences['numberheadings'] = array(
673 'type' => 'toggle',
674 'section' => 'rendering/advancedrendering',
675 'label-message' => 'tog-numberheadings',
676 );
677 }
678
679 /**
680 * @param $user User
681 * @param $defaultPreferences Array
682 */
683 static function editingPreferences( $user, &$defaultPreferences ) {
684 global $wgUseExternalEditor, $wgAllowUserCssPrefs;
685
686 ## Editing #####################################
687 $defaultPreferences['cols'] = array(
688 'type' => 'int',
689 'label-message' => 'columns',
690 'section' => 'editing/textboxsize',
691 'min' => 4,
692 'max' => 1000,
693 );
694 $defaultPreferences['rows'] = array(
695 'type' => 'int',
696 'label-message' => 'rows',
697 'section' => 'editing/textboxsize',
698 'min' => 4,
699 'max' => 1000,
700 );
701
702 if ( $wgAllowUserCssPrefs ) {
703 $defaultPreferences['editfont'] = array(
704 'type' => 'select',
705 'section' => 'editing/advancedediting',
706 'label-message' => 'editfont-style',
707 'options' => array(
708 wfMsg( 'editfont-default' ) => 'default',
709 wfMsg( 'editfont-monospace' ) => 'monospace',
710 wfMsg( 'editfont-sansserif' ) => 'sans-serif',
711 wfMsg( 'editfont-serif' ) => 'serif',
712 )
713 );
714 }
715 $defaultPreferences['previewontop'] = array(
716 'type' => 'toggle',
717 'section' => 'editing/advancedediting',
718 'label-message' => 'tog-previewontop',
719 );
720 $defaultPreferences['previewonfirst'] = array(
721 'type' => 'toggle',
722 'section' => 'editing/advancedediting',
723 'label-message' => 'tog-previewonfirst',
724 );
725
726 if ( $wgAllowUserCssPrefs ) {
727 $defaultPreferences['editsection'] = array(
728 'type' => 'toggle',
729 'section' => 'editing/advancedediting',
730 'label-message' => 'tog-editsection',
731 );
732 }
733 $defaultPreferences['editsectiononrightclick'] = array(
734 'type' => 'toggle',
735 'section' => 'editing/advancedediting',
736 'label-message' => 'tog-editsectiononrightclick',
737 );
738 $defaultPreferences['editondblclick'] = array(
739 'type' => 'toggle',
740 'section' => 'editing/advancedediting',
741 'label-message' => 'tog-editondblclick',
742 );
743 $defaultPreferences['showtoolbar'] = array(
744 'type' => 'toggle',
745 'section' => 'editing/advancedediting',
746 'label-message' => 'tog-showtoolbar',
747 );
748 $defaultPreferences['minordefault'] = array(
749 'type' => 'toggle',
750 'section' => 'editing/advancedediting',
751 'label-message' => 'tog-minordefault',
752 );
753
754 if ( $wgUseExternalEditor ) {
755 $defaultPreferences['externaleditor'] = array(
756 'type' => 'toggle',
757 'section' => 'editing/advancedediting',
758 'label-message' => 'tog-externaleditor',
759 );
760 $defaultPreferences['externaldiff'] = array(
761 'type' => 'toggle',
762 'section' => 'editing/advancedediting',
763 'label-message' => 'tog-externaldiff',
764 );
765 }
766
767 $defaultPreferences['forceeditsummary'] = array(
768 'type' => 'toggle',
769 'section' => 'editing/advancedediting',
770 'label-message' => 'tog-forceeditsummary',
771 );
772
773
774 $defaultPreferences['uselivepreview'] = array(
775 'type' => 'toggle',
776 'section' => 'editing/advancedediting',
777 'label-message' => 'tog-uselivepreview',
778 );
779 }
780
781 /**
782 * @param $user User
783 * @param $defaultPreferences Array
784 */
785 static function rcPreferences( $user, &$defaultPreferences ) {
786 global $wgRCMaxAge, $wgUseRCPatrol, $wgLang;
787
788 ## RecentChanges #####################################
789 $defaultPreferences['rcdays'] = array(
790 'type' => 'float',
791 'label-message' => 'recentchangesdays',
792 'section' => 'rc/displayrc',
793 'min' => 1,
794 'max' => ceil( $wgRCMaxAge / ( 3600 * 24 ) ),
795 'help' => wfMsgExt(
796 'recentchangesdays-max',
797 array( 'parsemag' ),
798 $wgLang->formatNum( ceil( $wgRCMaxAge / ( 3600 * 24 ) ) )
799 )
800 );
801 $defaultPreferences['rclimit'] = array(
802 'type' => 'int',
803 'label-message' => 'recentchangescount',
804 'help-message' => 'prefs-help-recentchangescount',
805 'section' => 'rc/displayrc',
806 );
807 $defaultPreferences['usenewrc'] = array(
808 'type' => 'toggle',
809 'label-message' => 'tog-usenewrc',
810 'section' => 'rc/advancedrc',
811 );
812 $defaultPreferences['hideminor'] = array(
813 'type' => 'toggle',
814 'label-message' => 'tog-hideminor',
815 'section' => 'rc/advancedrc',
816 );
817
818 if ( $wgUseRCPatrol ) {
819 $defaultPreferences['hidepatrolled'] = array(
820 'type' => 'toggle',
821 'section' => 'rc/advancedrc',
822 'label-message' => 'tog-hidepatrolled',
823 );
824 $defaultPreferences['newpageshidepatrolled'] = array(
825 'type' => 'toggle',
826 'section' => 'rc/advancedrc',
827 'label-message' => 'tog-newpageshidepatrolled',
828 );
829 }
830
831 global $wgRCShowWatchingUsers;
832 if ( $wgRCShowWatchingUsers ) {
833 $defaultPreferences['shownumberswatching'] = array(
834 'type' => 'toggle',
835 'section' => 'rc/advancedrc',
836 'label-message' => 'tog-shownumberswatching',
837 );
838 }
839 }
840
841 /**
842 * @param $user User
843 * @param $defaultPreferences
844 */
845 static function watchlistPreferences( $user, &$defaultPreferences ) {
846 global $wgUseRCPatrol, $wgEnableAPI;
847
848 ## Watchlist #####################################
849 $defaultPreferences['watchlistdays'] = array(
850 'type' => 'float',
851 'min' => 0,
852 'max' => 7,
853 'section' => 'watchlist/displaywatchlist',
854 'help' => wfMsgHtml( 'prefs-watchlist-days-max' ),
855 'label-message' => 'prefs-watchlist-days',
856 );
857 $defaultPreferences['wllimit'] = array(
858 'type' => 'int',
859 'min' => 0,
860 'max' => 1000,
861 'label-message' => 'prefs-watchlist-edits',
862 'help' => wfMsgHtml( 'prefs-watchlist-edits-max' ),
863 'section' => 'watchlist/displaywatchlist',
864 );
865 $defaultPreferences['extendwatchlist'] = array(
866 'type' => 'toggle',
867 'section' => 'watchlist/advancedwatchlist',
868 'label-message' => 'tog-extendwatchlist',
869 );
870 $defaultPreferences['watchlisthideminor'] = array(
871 'type' => 'toggle',
872 'section' => 'watchlist/advancedwatchlist',
873 'label-message' => 'tog-watchlisthideminor',
874 );
875 $defaultPreferences['watchlisthidebots'] = array(
876 'type' => 'toggle',
877 'section' => 'watchlist/advancedwatchlist',
878 'label-message' => 'tog-watchlisthidebots',
879 );
880 $defaultPreferences['watchlisthideown'] = array(
881 'type' => 'toggle',
882 'section' => 'watchlist/advancedwatchlist',
883 'label-message' => 'tog-watchlisthideown',
884 );
885 $defaultPreferences['watchlisthideanons'] = array(
886 'type' => 'toggle',
887 'section' => 'watchlist/advancedwatchlist',
888 'label-message' => 'tog-watchlisthideanons',
889 );
890 $defaultPreferences['watchlisthideliu'] = array(
891 'type' => 'toggle',
892 'section' => 'watchlist/advancedwatchlist',
893 'label-message' => 'tog-watchlisthideliu',
894 );
895
896 if ( $wgUseRCPatrol ) {
897 $defaultPreferences['watchlisthidepatrolled'] = array(
898 'type' => 'toggle',
899 'section' => 'watchlist/advancedwatchlist',
900 'label-message' => 'tog-watchlisthidepatrolled',
901 );
902 }
903
904 if ( $wgEnableAPI ) {
905 # Some random gibberish as a proposed default
906 $hash = sha1( mt_rand() . microtime( true ) );
907
908 $defaultPreferences['watchlisttoken'] = array(
909 'type' => 'text',
910 'section' => 'watchlist/advancedwatchlist',
911 'label-message' => 'prefs-watchlist-token',
912 'help' => wfMsgHtml( 'prefs-help-watchlist-token', $hash )
913 );
914 }
915
916 $watchTypes = array(
917 'edit' => 'watchdefault',
918 'move' => 'watchmoves',
919 'delete' => 'watchdeletion'
920 );
921
922 // Kinda hacky
923 if ( $user->isAllowed( 'createpage' ) || $user->isAllowed( 'createtalk' ) ) {
924 $watchTypes['read'] = 'watchcreations';
925 }
926
927 foreach ( $watchTypes as $action => $pref ) {
928 if ( $user->isAllowed( $action ) ) {
929 $defaultPreferences[$pref] = array(
930 'type' => 'toggle',
931 'section' => 'watchlist/advancedwatchlist',
932 'label-message' => "tog-$pref",
933 );
934 }
935 }
936 }
937
938 /**
939 * @param $user User
940 * @param $defaultPreferences Array
941 */
942 static function searchPreferences( $user, &$defaultPreferences ) {
943 global $wgContLang;
944
945 ## Search #####################################
946 $defaultPreferences['searchlimit'] = array(
947 'type' => 'int',
948 'label-message' => 'resultsperpage',
949 'section' => 'searchoptions/displaysearchoptions',
950 'min' => 0,
951 );
952
953 global $wgEnableMWSuggest;
954 if ( $wgEnableMWSuggest ) {
955 $defaultPreferences['disablesuggest'] = array(
956 'type' => 'toggle',
957 'label-message' => 'mwsuggest-disable',
958 'section' => 'searchoptions/displaysearchoptions',
959 );
960 }
961
962 global $wgVectorUseSimpleSearch;
963 if ( $wgVectorUseSimpleSearch ) {
964 $defaultPreferences['vector-simplesearch'] = array(
965 'type' => 'toggle',
966 'label-message' => 'vector-simplesearch-preference',
967 'section' => 'searchoptions/displaysearchoptions'
968 );
969 }
970
971 $defaultPreferences['searcheverything'] = array(
972 'type' => 'toggle',
973 'label-message' => 'searcheverything-enable',
974 'section' => 'searchoptions/advancedsearchoptions',
975 );
976
977 $nsOptions = array();
978
979 foreach ( $wgContLang->getNamespaces() as $ns => $name ) {
980 if ( $ns < 0 ) {
981 continue;
982 }
983
984 $displayNs = str_replace( '_', ' ', $name );
985
986 if ( !$displayNs ) {
987 $displayNs = wfMsg( 'blanknamespace' );
988 }
989
990 $displayNs = htmlspecialchars( $displayNs );
991 $nsOptions[$displayNs] = $ns;
992 }
993
994 $defaultPreferences['searchnamespaces'] = array(
995 'type' => 'multiselect',
996 'label-message' => 'defaultns',
997 'options' => $nsOptions,
998 'section' => 'searchoptions/advancedsearchoptions',
999 'prefix' => 'searchNs',
1000 );
1001 }
1002
1003 /**
1004 * @param $user User
1005 * @param $defaultPreferences Array
1006 */
1007 static function miscPreferences( $user, &$defaultPreferences ) {
1008 ## Misc #####################################
1009 $defaultPreferences['diffonly'] = array(
1010 'type' => 'toggle',
1011 'section' => 'misc/diffs',
1012 'label-message' => 'tog-diffonly',
1013 );
1014 $defaultPreferences['norollbackdiff'] = array(
1015 'type' => 'toggle',
1016 'section' => 'misc/diffs',
1017 'label-message' => 'tog-norollbackdiff',
1018 );
1019
1020 // Stuff from Language::getExtraUserToggles()
1021 global $wgContLang;
1022
1023 $toggles = $wgContLang->getExtraUserToggles();
1024
1025 foreach ( $toggles as $toggle ) {
1026 $defaultPreferences[$toggle] = array(
1027 'type' => 'toggle',
1028 'section' => 'personal/i18n',
1029 'label-message' => "tog-$toggle",
1030 );
1031 }
1032 }
1033
1034 /**
1035 * @param $user User The User object
1036 * @return Array: text/links to display as key; $skinkey as value
1037 */
1038 static function generateSkinOptions( $user ) {
1039 global $wgDefaultSkin, $wgLang, $wgAllowUserCss, $wgAllowUserJs;
1040 $ret = array();
1041
1042 $mptitle = Title::newMainPage();
1043 $previewtext = wfMsgHtml( 'skin-preview' );
1044
1045 # Only show members of Skin::getSkinNames() rather than
1046 # $skinNames (skins is all skin names from Language.php)
1047 $validSkinNames = Skin::getUsableSkins();
1048
1049 # Sort by UI skin name. First though need to update validSkinNames as sometimes
1050 # the skinkey & UI skinname differ (e.g. "standard" skinkey is "Classic" in the UI).
1051 foreach ( $validSkinNames as $skinkey => &$skinname ) {
1052 $msg = wfMessage( "skinname-{$skinkey}" );
1053 if ( $msg->exists() ) {
1054 $skinname = htmlspecialchars( $msg->text() );
1055 }
1056 }
1057 asort( $validSkinNames );
1058 $sk = $user->getSkin();
1059
1060 foreach ( $validSkinNames as $skinkey => $sn ) {
1061 $linkTools = array();
1062
1063 # Mark the default skin
1064 if ( $skinkey == $wgDefaultSkin ) {
1065 $linkTools[] = wfMsgHtml( 'default' );
1066 }
1067
1068 # Create preview link
1069 $mplink = htmlspecialchars( $mptitle->getLocalURL( "useskin=$skinkey" ) );
1070 $linkTools[] = "<a target='_blank' href=\"$mplink\">$previewtext</a>";
1071
1072 # Create links to user CSS/JS pages
1073 if ( $wgAllowUserCss ) {
1074 $cssPage = Title::makeTitleSafe( NS_USER, $user->getName() . '/' . $skinkey . '.css' );
1075 $linkTools[] = $sk->link( $cssPage, wfMsgHtml( 'prefs-custom-css' ) );
1076 }
1077
1078 if ( $wgAllowUserJs ) {
1079 $jsPage = Title::makeTitleSafe( NS_USER, $user->getName() . '/' . $skinkey . '.js' );
1080 $linkTools[] = $sk->link( $jsPage, wfMsgHtml( 'prefs-custom-js' ) );
1081 }
1082
1083 $display = $sn . ' ' . wfMsg( 'parentheses', $wgLang->pipeList( $linkTools ) );
1084 $ret[$display] = $skinkey;
1085 }
1086
1087 return $ret;
1088 }
1089
1090 /**
1091 * @return array
1092 */
1093 static function getDateOptions() {
1094 global $wgLang;
1095 $dateopts = $wgLang->getDatePreferences();
1096
1097 $ret = array();
1098
1099 if ( $dateopts ) {
1100 if ( !in_array( 'default', $dateopts ) ) {
1101 $dateopts[] = 'default'; // Make sure default is always valid
1102 // Bug 19237
1103 }
1104
1105 // KLUGE: site default might not be valid for user language
1106 global $wgDefaultUserOptions;
1107 if ( !in_array( $wgDefaultUserOptions['date'], $dateopts ) ) {
1108 $wgDefaultUserOptions['date'] = 'default';
1109 }
1110
1111 $epoch = wfTimestampNow();
1112 foreach ( $dateopts as $key ) {
1113 if ( $key == 'default' ) {
1114 $formatted = wfMsgHtml( 'datedefault' );
1115 } else {
1116 $formatted = htmlspecialchars( $wgLang->timeanddate( $epoch, false, $key ) );
1117 }
1118 $ret[$formatted] = $key;
1119 }
1120 }
1121 return $ret;
1122 }
1123
1124 /**
1125 * @return array
1126 */
1127 static function getImageSizes() {
1128 global $wgImageLimits;
1129
1130 $ret = array();
1131
1132 foreach ( $wgImageLimits as $index => $limits ) {
1133 $display = "{$limits[0]}×{$limits[1]}" . wfMsg( 'unit-pixel' );
1134 $ret[$display] = $index;
1135 }
1136
1137 return $ret;
1138 }
1139
1140 /**
1141 * @return array
1142 */
1143 static function getThumbSizes() {
1144 global $wgThumbLimits;
1145
1146 $ret = array();
1147
1148 foreach ( $wgThumbLimits as $index => $size ) {
1149 $display = $size . wfMsg( 'unit-pixel' );
1150 $ret[$display] = $index;
1151 }
1152
1153 return $ret;
1154 }
1155
1156 /**
1157 * @param $signature
1158 * @param $alldata
1159 * @return bool|string
1160 */
1161 static function validateSignature( $signature, $alldata ) {
1162 global $wgParser, $wgMaxSigChars, $wgLang;
1163 if ( mb_strlen( $signature ) > $wgMaxSigChars ) {
1164 return Xml::element( 'span', array( 'class' => 'error' ),
1165 wfMsgExt( 'badsiglength', 'parsemag',
1166 $wgLang->formatNum( $wgMaxSigChars )
1167 )
1168 );
1169 } elseif ( isset( $alldata['fancysig'] ) &&
1170 $alldata['fancysig'] &&
1171 false === $wgParser->validateSig( $signature ) ) {
1172 return Xml::element( 'span', array( 'class' => 'error' ), wfMsg( 'badsig' ) );
1173 } else {
1174 return true;
1175 }
1176 }
1177
1178 /**
1179 * @param $signature string
1180 * @param $alldata array
1181 * @return string
1182 */
1183 static function cleanSignature( $signature, $alldata ) {
1184 global $wgParser;
1185 if ( isset( $alldata['fancysig'] ) && $alldata['fancysig'] ) {
1186 $signature = $wgParser->cleanSig( $signature );
1187 } else {
1188 // When no fancy sig used, make sure ~{3,5} get removed.
1189 $signature = $wgParser->cleanSigInSig( $signature );
1190 }
1191
1192 return $signature;
1193 }
1194
1195 /**
1196 * @param $email
1197 * @param $alldata
1198 * @return bool|String
1199 */
1200 static function validateEmail( $email, $alldata ) {
1201 if ( $email && !Sanitizer::validateEmail( $email ) ) {
1202 return wfMsgExt( 'invalidemailaddress', 'parseinline' );
1203 }
1204
1205 global $wgEmailConfirmToEdit;
1206 if ( $wgEmailConfirmToEdit && !$email ) {
1207 return wfMsgExt( 'noemailtitle', 'parseinline' );
1208 }
1209 return true;
1210 }
1211
1212 /**
1213 * @param $user User
1214 * @param $formClass string
1215 * @return HtmlForm
1216 */
1217 static function getFormObject( $user, $formClass = 'PreferencesForm' ) {
1218 $formDescriptor = Preferences::getPreferences( $user );
1219 $htmlForm = new $formClass( $formDescriptor, 'prefs' );
1220
1221 $htmlForm->setId( 'mw-prefs-form' );
1222 $htmlForm->setSubmitText( wfMsg( 'saveprefs' ) );
1223 # Used message keys: 'accesskey-preferences-save', 'tooltip-preferences-save'
1224 $htmlForm->setSubmitTooltip( 'preferences-save' );
1225 $htmlForm->setTitle( SpecialPage::getTitleFor( 'Preferences' ) );
1226 $htmlForm->setSubmitID( 'prefsubmit' );
1227 $htmlForm->setSubmitCallback( array( 'Preferences', 'tryFormSubmit' ) );
1228
1229 return $htmlForm;
1230 }
1231
1232 /**
1233 * @return array
1234 */
1235 static function getTimezoneOptions() {
1236 $opt = array();
1237
1238 global $wgLocalTZoffset, $wgLocaltimezone;
1239 // Check that $wgLocalTZoffset is the same as $wgLocaltimezone
1240 if ( $wgLocalTZoffset == date( 'Z' ) / 60 ) {
1241 $server_tz_msg = wfMsg( 'timezoneuseserverdefault', $wgLocaltimezone );
1242 } else {
1243 $tzstring = sprintf( '%+03d:%02d', floor( $wgLocalTZoffset / 60 ), abs( $wgLocalTZoffset ) % 60 );
1244 $server_tz_msg = wfMsg( 'timezoneuseserverdefault', $tzstring );
1245 }
1246 $opt[$server_tz_msg] = "System|$wgLocalTZoffset";
1247 $opt[wfMsg( 'timezoneuseoffset' )] = 'other';
1248 $opt[wfMsg( 'guesstimezone' )] = 'guess';
1249
1250 if ( function_exists( 'timezone_identifiers_list' ) ) {
1251 # Read timezone list
1252 $tzs = timezone_identifiers_list();
1253 sort( $tzs );
1254
1255 $tzRegions = array();
1256 $tzRegions['Africa'] = wfMsg( 'timezoneregion-africa' );
1257 $tzRegions['America'] = wfMsg( 'timezoneregion-america' );
1258 $tzRegions['Antarctica'] = wfMsg( 'timezoneregion-antarctica' );
1259 $tzRegions['Arctic'] = wfMsg( 'timezoneregion-arctic' );
1260 $tzRegions['Asia'] = wfMsg( 'timezoneregion-asia' );
1261 $tzRegions['Atlantic'] = wfMsg( 'timezoneregion-atlantic' );
1262 $tzRegions['Australia'] = wfMsg( 'timezoneregion-australia' );
1263 $tzRegions['Europe'] = wfMsg( 'timezoneregion-europe' );
1264 $tzRegions['Indian'] = wfMsg( 'timezoneregion-indian' );
1265 $tzRegions['Pacific'] = wfMsg( 'timezoneregion-pacific' );
1266 asort( $tzRegions );
1267
1268 $prefill = array_fill_keys( array_values( $tzRegions ), array() );
1269 $opt = array_merge( $opt, $prefill );
1270
1271 $now = date_create( 'now' );
1272
1273 foreach ( $tzs as $tz ) {
1274 $z = explode( '/', $tz, 2 );
1275
1276 # timezone_identifiers_list() returns a number of
1277 # backwards-compatibility entries. This filters them out of the
1278 # list presented to the user.
1279 if ( count( $z ) != 2 || !array_key_exists( $z[0], $tzRegions ) ) {
1280 continue;
1281 }
1282
1283 # Localize region
1284 $z[0] = $tzRegions[$z[0]];
1285
1286 $minDiff = floor( timezone_offset_get( timezone_open( $tz ), $now ) / 60 );
1287
1288 $display = str_replace( '_', ' ', $z[0] . '/' . $z[1] );
1289 $value = "ZoneInfo|$minDiff|$tz";
1290
1291 $opt[$z[0]][$display] = $value;
1292 }
1293 }
1294 return $opt;
1295 }
1296
1297 /**
1298 * @param $value
1299 * @param $alldata
1300 * @return int
1301 */
1302 static function filterIntval( $value, $alldata ){
1303 return intval( $value );
1304 }
1305
1306 /**
1307 * @param $tz
1308 * @param $alldata
1309 * @return string
1310 */
1311 static function filterTimezoneInput( $tz, $alldata ) {
1312 $data = explode( '|', $tz, 3 );
1313 switch ( $data[0] ) {
1314 case 'ZoneInfo':
1315 case 'System':
1316 return $tz;
1317 default:
1318 $data = explode( ':', $tz, 2 );
1319 if ( count( $data ) == 2 ) {
1320 $data[0] = intval( $data[0] );
1321 $data[1] = intval( $data[1] );
1322 $minDiff = abs( $data[0] ) * 60 + $data[1];
1323 if ( $data[0] < 0 ) $minDiff = - $minDiff;
1324 } else {
1325 $minDiff = intval( $data[0] ) * 60;
1326 }
1327
1328 # Max is +14:00 and min is -12:00, see:
1329 # http://en.wikipedia.org/wiki/Timezone
1330 $minDiff = min( $minDiff, 840 ); # 14:00
1331 $minDiff = max( $minDiff, - 720 ); # -12:00
1332 return 'Offset|' . $minDiff;
1333 }
1334 }
1335
1336 /**
1337 * @param $formData
1338 * @param $entryPoint string
1339 * @return bool|Status|string
1340 */
1341 static function tryFormSubmit( $formData, $entryPoint = 'internal' ) {
1342 global $wgUser, $wgEmailAuthentication, $wgEnableEmail;
1343
1344 $result = true;
1345
1346 // Filter input
1347 foreach ( array_keys( $formData ) as $name ) {
1348 if ( isset( self::$saveFilters[$name] ) ) {
1349 $formData[$name] =
1350 call_user_func( self::$saveFilters[$name], $formData[$name], $formData );
1351 }
1352 }
1353
1354 // Stuff that shouldn't be saved as a preference.
1355 $saveBlacklist = array(
1356 'realname',
1357 'emailaddress',
1358 );
1359
1360 if ( $wgEnableEmail ) {
1361 $newaddr = $formData['emailaddress'];
1362 $oldaddr = $wgUser->getEmail();
1363 if ( ( $newaddr != '' ) && ( $newaddr != $oldaddr ) ) {
1364 # the user has supplied a new email address on the login page
1365 # new behaviour: set this new emailaddr from login-page into user database record
1366 $wgUser->setEmail( $newaddr );
1367 # but flag as "dirty" = unauthenticated
1368 $wgUser->invalidateEmail();
1369 if ( $wgEmailAuthentication ) {
1370 # Mail a temporary password to the dirty address.
1371 # User can come back through the confirmation URL to re-enable email.
1372 $type = $oldaddr != '' ? 'changed' : 'set';
1373 $result = $wgUser->sendConfirmationMail( $type );
1374 if ( !$result->isGood() ) {
1375 return htmlspecialchars( $result->getWikiText( 'mailerror' ) );
1376 } elseif ( $entryPoint == 'ui' ) {
1377 $result = 'eauth';
1378 }
1379 }
1380 } else {
1381 $wgUser->setEmail( $newaddr );
1382 }
1383 if ( $oldaddr != $newaddr ) {
1384 wfRunHooks( 'PrefsEmailAudit', array( $wgUser, $oldaddr, $newaddr ) );
1385 }
1386 }
1387
1388 // Fortunately, the realname field is MUCH simpler
1389 global $wgHiddenPrefs;
1390 if ( !in_array( 'realname', $wgHiddenPrefs ) ) {
1391 $realName = $formData['realname'];
1392 $wgUser->setRealName( $realName );
1393 }
1394
1395 foreach ( $saveBlacklist as $b ) {
1396 unset( $formData[$b] );
1397 }
1398
1399 # If users have saved a value for a preference which has subsequently been disabled
1400 # via $wgHiddenPrefs, we don't want to destroy that setting in case the preference
1401 # is subsequently re-enabled
1402 # TODO: maintenance script to actually delete these
1403 foreach( $wgHiddenPrefs as $pref ){
1404 # If the user has not set a non-default value here, the default will be returned
1405 # and subsequently discarded
1406 $formData[$pref] = $wgUser->getOption( $pref, null, true );
1407 }
1408
1409 // Keeps old preferences from interfering due to back-compat
1410 // code, etc.
1411 $wgUser->resetOptions();
1412
1413 foreach ( $formData as $key => $value ) {
1414 $wgUser->setOption( $key, $value );
1415 }
1416
1417 $wgUser->saveSettings();
1418
1419 return $result;
1420 }
1421
1422 /**
1423 * @param $formData
1424 * @return Status
1425 */
1426 public static function tryUISubmit( $formData ) {
1427 $res = self::tryFormSubmit( $formData, 'ui' );
1428
1429 if ( $res ) {
1430 $urlOptions = array( 'success' );
1431
1432 if ( $res === 'eauth' ) {
1433 $urlOptions[] = 'eauth';
1434 }
1435
1436 $queryString = implode( '&', $urlOptions );
1437
1438 $url = SpecialPage::getTitleFor( 'Preferences' )->getFullURL( $queryString );
1439
1440 global $wgOut;
1441 $wgOut->redirect( $url );
1442 }
1443
1444 return Status::newGood();
1445 }
1446
1447 /**
1448 * @param $user User
1449 * @return array
1450 */
1451 public static function loadOldSearchNs( $user ) {
1452 $searchableNamespaces = SearchEngine::searchableNamespaces();
1453 // Back compat with old format
1454 $arr = array();
1455
1456 foreach ( $searchableNamespaces as $ns => $name ) {
1457 if ( $user->getOption( 'searchNs' . $ns ) ) {
1458 $arr[] = $ns;
1459 }
1460 }
1461
1462 return $arr;
1463 }
1464 }
1465
1466 /** Some tweaks to allow js prefs to work */
1467 class PreferencesForm extends HTMLForm {
1468
1469 /**
1470 * @param $html string
1471 * @return String
1472 */
1473 function wrapForm( $html ) {
1474 $html = Xml::tags( 'div', array( 'id' => 'preferences' ), $html );
1475
1476 return parent::wrapForm( $html );
1477 }
1478
1479 /**
1480 * @return String
1481 */
1482 function getButtons() {
1483 $html = parent::getButtons();
1484
1485 global $wgUser;
1486
1487 $sk = $wgUser->getSkin();
1488 $t = SpecialPage::getTitleFor( 'Preferences', 'reset' );
1489
1490 $html .= "\n" . $sk->link( $t, wfMsgHtml( 'restoreprefs' ) );
1491
1492 $html = Xml::tags( 'div', array( 'class' => 'mw-prefs-buttons' ), $html );
1493
1494 return $html;
1495 }
1496
1497 /**
1498 * @param $data array
1499 * @return array
1500 */
1501 function filterDataForSubmit( $data ) {
1502 // Support for separating MultiSelect preferences into multiple preferences
1503 // Due to lack of array support.
1504 foreach ( $this->mFlatFields as $fieldname => $field ) {
1505 $info = $field->mParams;
1506 if ( $field instanceof HTMLMultiSelectField ) {
1507 $options = HTMLFormField::flattenOptions( $info['options'] );
1508 $prefix = isset( $info['prefix'] ) ? $info['prefix'] : $fieldname;
1509
1510 foreach ( $options as $opt ) {
1511 $data["$prefix$opt"] = in_array( $opt, $data[$fieldname] );
1512 }
1513
1514 unset( $data[$fieldname] );
1515 }
1516 }
1517
1518 return $data;
1519 }
1520 }