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