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