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