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