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