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.
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).
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.
24 * Once fields have been retrieved and validated, submission logic is handed
25 * over to the tryUISubmit static method of this class.
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' ),
42 * @param $context IContextSource
45 static function getPreferences( $user, IContextSource
$context ) {
46 if ( self
::$defaultPreferences ) {
47 return self
::$defaultPreferences;
50 $defaultPreferences = array();
52 self
::profilePreferences( $user, $context, $defaultPreferences );
53 self
::skinPreferences( $user, $context, $defaultPreferences );
54 self
::filesPreferences( $user, $context, $defaultPreferences );
55 self
::datetimePreferences( $user, $context, $defaultPreferences );
56 self
::renderingPreferences( $user, $context, $defaultPreferences );
57 self
::editingPreferences( $user, $context, $defaultPreferences );
58 self
::rcPreferences( $user, $context, $defaultPreferences );
59 self
::watchlistPreferences( $user, $context, $defaultPreferences );
60 self
::searchPreferences( $user, $context, $defaultPreferences );
61 self
::miscPreferences( $user, $context, $defaultPreferences );
63 wfRunHooks( 'GetPreferences', array( $user, &$defaultPreferences ) );
65 ## Remove preferences that wikis don't want to use
66 global $wgHiddenPrefs;
67 foreach ( $wgHiddenPrefs as $pref ) {
68 if ( isset( $defaultPreferences[$pref] ) ) {
69 unset( $defaultPreferences[$pref] );
73 ## Prod in defaults from the user
74 foreach ( $defaultPreferences as $name => &$info ) {
75 $prefFromUser = self
::getOptionFromUser( $name, $info, $user );
76 $field = HTMLForm
::loadInputFromParameters( $name, $info ); // For validation
77 $defaultOptions = User
::getDefaultOptions();
78 $globalDefault = isset( $defaultOptions[$name] )
79 ?
$defaultOptions[$name]
82 // If it validates, set it as the default
83 if ( isset( $info['default'] ) ) {
84 // Already set, no problem
86 } elseif ( !is_null( $prefFromUser ) && // Make sure we're not just pulling nothing
87 $field->validate( $prefFromUser, $user->getOptions() ) === true ) {
88 $info['default'] = $prefFromUser;
89 } elseif ( $field->validate( $globalDefault, $user->getOptions() ) === true ) {
90 $info['default'] = $globalDefault;
92 throw new MWException( "Global default '$globalDefault' is invalid for field $name" );
96 self
::$defaultPreferences = $defaultPreferences;
98 return $defaultPreferences;
102 * Pull option from a user account. Handles stuff like array-type preferences.
107 * @return array|String
109 static function getOptionFromUser( $name, $info, $user ) {
110 $val = $user->getOption( $name );
112 // Handling for array-type preferences
113 if ( ( isset( $info['type'] ) && $info['type'] == 'multiselect' ) ||
114 ( isset( $info['class'] ) && $info['class'] == 'HTMLMultiSelectField' ) ) {
115 $options = HTMLFormField
::flattenOptions( $info['options'] );
116 $prefix = isset( $info['prefix'] ) ?
$info['prefix'] : $name;
119 foreach ( $options as $value ) {
120 if ( $user->getOption( "$prefix$value" ) ) {
131 * @param $context IContextSource
132 * @param $defaultPreferences
135 static function profilePreferences( $user, IContextSource
$context, &$defaultPreferences ) {
136 global $wgAuth, $wgContLang, $wgParser, $wgCookieExpiration, $wgLanguageCode,
137 $wgDisableTitleConversion, $wgDisableLangConversion, $wgMaxSigChars,
138 $wgEnableEmail, $wgEmailConfirmToEdit, $wgEnableUserEmail, $wgEmailAuthentication,
139 $wgEnotifWatchlist, $wgEnotifUserTalk, $wgEnotifRevealEditorAddress;
141 ## User info #####################################
143 $defaultPreferences['username'] = array(
145 'label-message' => 'username',
146 'default' => $user->getName(),
147 'section' => 'personal/info',
150 $defaultPreferences['userid'] = array(
152 'label-message' => 'uid',
153 'default' => $user->getId(),
154 'section' => 'personal/info',
157 # Get groups to which the user belongs
158 $userEffectiveGroups = $user->getEffectiveGroups();
159 $userGroups = $userMembers = array();
160 foreach ( $userEffectiveGroups as $ueg ) {
162 // Skip the default * group, seems useless here
165 $groupName = User
::getGroupName( $ueg );
166 $userGroups[] = User
::makeGroupLinkHTML( $ueg, $groupName );
168 $memberName = User
::getGroupMember( $ueg, $user->getName() );
169 $userMembers[] = User
::makeGroupLinkHTML( $ueg, $memberName );
171 asort( $userGroups );
172 asort( $userMembers );
174 $lang = $context->getLanguage();
176 $defaultPreferences['usergroups'] = array(
178 'label' => $context->msg( 'prefs-memberingroups' )->numParams(
179 count( $userGroups ) )->parse(),
180 'default' => $context->msg( 'prefs-memberingroups-type',
181 $lang->commaList( $userGroups ),
182 $lang->commaList( $userMembers )
185 'section' => 'personal/info',
188 $defaultPreferences['editcount'] = array(
190 'label-message' => 'prefs-edits',
191 'default' => $lang->formatNum( $user->getEditCount() ),
192 'section' => 'personal/info',
195 if ( $user->getRegistration() ) {
196 $displayUser = $context->getUser();
197 $userRegistration = $user->getRegistration();
198 $defaultPreferences['registrationdate'] = array(
200 'label-message' => 'prefs-registration',
201 'default' => $context->msg(
202 'prefs-registration-date-time',
203 $lang->userTimeAndDate( $userRegistration, $displayUser ),
204 $lang->userDate( $userRegistration, $displayUser ),
205 $lang->userTime( $userRegistration, $displayUser )
207 'section' => 'personal/info',
211 // Actually changeable stuff
212 $defaultPreferences['realname'] = array(
213 'type' => $wgAuth->allowPropChange( 'realname' ) ?
'text' : 'info',
214 'default' => $user->getRealName(),
215 'section' => 'personal/info',
216 'label-message' => 'yourrealname',
217 'help-message' => 'prefs-help-realname',
220 $defaultPreferences['gender'] = array(
222 'section' => 'personal/info',
224 $context->msg( 'gender-male' )->text() => 'male',
225 $context->msg( 'gender-female' )->text() => 'female',
226 $context->msg( 'gender-unknown' )->text() => 'unknown',
228 'label-message' => 'yourgender',
229 'help-message' => 'prefs-help-gender',
232 if ( $wgAuth->allowPasswordChange() ) {
233 $link = Linker
::link( SpecialPage
::getTitleFor( 'ChangePassword' ),
234 $context->msg( 'prefs-resetpass' )->escaped(), array(),
235 array( 'returnto' => SpecialPage
::getTitleFor( 'Preferences' ) ) );
237 $defaultPreferences['password'] = array(
241 'label-message' => 'yourpassword',
242 'section' => 'personal/info',
245 if ( $wgCookieExpiration > 0 ) {
246 $defaultPreferences['rememberpassword'] = array(
248 'label' => $context->msg( 'tog-rememberpassword' )->numParams(
249 ceil( $wgCookieExpiration / ( 3600 * 24 ) ) )->text(),
250 'section' => 'personal/info',
255 $languages = Language
::getLanguageNames( false );
256 if ( !array_key_exists( $wgLanguageCode, $languages ) ) {
257 $languages[$wgLanguageCode] = $wgLanguageCode;
262 foreach ( $languages as $code => $name ) {
263 $display = wfBCP47( $code ) . ' - ' . $name;
264 $options[$display] = $code;
266 $defaultPreferences['language'] = array(
268 'section' => 'personal/i18n',
269 'options' => $options,
270 'label-message' => 'yourlanguage',
273 /* see if there are multiple language variants to choose from*/
274 $variantArray = array();
275 if ( !$wgDisableLangConversion ) {
276 $variants = $wgContLang->getVariants();
278 foreach ( $variants as $v ) {
279 $v = str_replace( '_', '-', strtolower( $v ) );
280 $variantArray[$v] = $wgContLang->getVariantname( $v, false );
284 foreach ( $variantArray as $code => $name ) {
285 $display = wfBCP47( $code ) . ' - ' . $name;
286 $options[$display] = $code;
289 if ( count( $variantArray ) > 1 ) {
290 $defaultPreferences['variant'] = array(
291 'label-message' => 'yourvariant',
293 'options' => $options,
294 'section' => 'personal/i18n',
295 'help-message' => 'prefs-help-variant',
300 if ( count( $variantArray ) > 1 && !$wgDisableLangConversion && !$wgDisableTitleConversion ) {
301 $defaultPreferences['noconvertlink'] =
304 'section' => 'personal/i18n',
305 'label-message' => 'tog-noconvertlink',
309 // show a preview of the old signature first
310 $oldsigWikiText = $wgParser->preSaveTransform( "~~~", $context->getTitle(), $user, ParserOptions
::newFromContext( $context ) );
311 $oldsigHTML = $context->getOutput()->parseInline( $oldsigWikiText, true, true );
312 $defaultPreferences['oldsig'] = array(
315 'label-message' => 'tog-oldsig',
316 'default' => $oldsigHTML,
317 'section' => 'personal/signature',
319 $defaultPreferences['nickname'] = array(
320 'type' => $wgAuth->allowPropChange( 'nickname' ) ?
'text' : 'info',
321 'maxlength' => $wgMaxSigChars,
322 'label-message' => 'yournick',
323 'validation-callback' => array( 'Preferences', 'validateSignature' ),
324 'section' => 'personal/signature',
325 'filter-callback' => array( 'Preferences', 'cleanSignature' ),
327 $defaultPreferences['fancysig'] = array(
329 'label-message' => 'tog-fancysig',
330 'help-message' => 'prefs-help-signature', // show general help about signature at the bottom of the section
331 'section' => 'personal/signature'
336 if ( $wgEnableEmail ) {
337 $helpMessages[] = $wgEmailConfirmToEdit
338 ?
'prefs-help-email-required'
339 : 'prefs-help-email' ;
341 if( $wgEnableUserEmail ) {
342 // additional messages when users can send email to each other
343 $helpMessages[] = 'prefs-help-email-others';
346 $link = Linker
::link(
347 SpecialPage
::getTitleFor( 'ChangeEmail' ),
348 $context->msg( $user->getEmail() ?
'prefs-changeemail' : 'prefs-setemail' )->escaped(),
350 array( 'returnto' => SpecialPage
::getTitleFor( 'Preferences' ) ) );
352 $emailAddress = $user->getEmail() ?
htmlspecialchars( $user->getEmail() ) : '';
353 if ( $wgAuth->allowPropChange( 'emailaddress' ) ) {
354 $emailAddress .= $emailAddress == '' ?
$link : " ($link)";
357 $defaultPreferences['emailaddress'] = array(
360 'default' => $emailAddress,
361 'label-message' => 'youremail',
362 'section' => 'personal/email',
365 $disableEmailPrefs = false;
367 if ( $wgEmailAuthentication ) {
368 if ( $user->getEmail() ) {
369 if ( $user->getEmailAuthenticationTimestamp() ) {
370 // date and time are separate parameters to facilitate localisation.
371 // $time is kept for backward compat reasons.
372 // 'emailauthenticated' is also used in SpecialConfirmemail.php
373 $displayUser = $context->getUser();
374 $emailTimestamp = $user->getEmailAuthenticationTimestamp();
375 $time = $lang->userTimeAndDate( $emailTimestamp, $displayUser );
376 $d = $lang->userDate( $emailTimestamp, $displayUser );
377 $t = $lang->userTime( $emailTimestamp, $displayUser );
378 $emailauthenticated = $context->msg( 'emailauthenticated',
379 $time, $d, $t )->parse() . '<br />';
380 $disableEmailPrefs = false;
382 $disableEmailPrefs = true;
383 $emailauthenticated = $context->msg( 'emailnotauthenticated' )->parse() . '<br />' .
385 SpecialPage
::getTitleFor( 'Confirmemail' ),
386 $context->msg( 'emailconfirmlink' )->escaped()
390 $disableEmailPrefs = true;
391 $emailauthenticated = $context->msg( 'noemailprefs' )->escaped();
394 $defaultPreferences['emailauthentication'] = array(
397 'section' => 'personal/email',
398 'label-message' => 'prefs-emailconfirm-label',
399 'default' => $emailauthenticated,
404 if ( $wgEnableUserEmail && $user->isAllowed( 'sendemail' ) ) {
405 $defaultPreferences['disablemail'] = array(
408 'section' => 'personal/email',
409 'label-message' => 'allowemail',
410 'disabled' => $disableEmailPrefs,
412 $defaultPreferences['ccmeonemails'] = array(
414 'section' => 'personal/email',
415 'label-message' => 'tog-ccmeonemails',
416 'disabled' => $disableEmailPrefs,
420 if ( $wgEnotifWatchlist ) {
421 $defaultPreferences['enotifwatchlistpages'] = array(
423 'section' => 'personal/email',
424 'label-message' => 'tog-enotifwatchlistpages',
425 'disabled' => $disableEmailPrefs,
428 if ( $wgEnotifUserTalk ) {
429 $defaultPreferences['enotifusertalkpages'] = array(
431 'section' => 'personal/email',
432 'label-message' => 'tog-enotifusertalkpages',
433 'disabled' => $disableEmailPrefs,
436 if ( $wgEnotifUserTalk ||
$wgEnotifWatchlist ) {
437 $defaultPreferences['enotifminoredits'] = array(
439 'section' => 'personal/email',
440 'label-message' => 'tog-enotifminoredits',
441 'disabled' => $disableEmailPrefs,
444 if ( $wgEnotifRevealEditorAddress ) {
445 $defaultPreferences['enotifrevealaddr'] = array(
447 'section' => 'personal/email',
448 'label-message' => 'tog-enotifrevealaddr',
449 'disabled' => $disableEmailPrefs,
458 * @param $context IContextSource
459 * @param $defaultPreferences
462 static function skinPreferences( $user, IContextSource
$context, &$defaultPreferences ) {
463 ## Skin #####################################
464 global $wgAllowUserCss, $wgAllowUserJs;
466 $defaultPreferences['skin'] = array(
468 'options' => self
::generateSkinOptions( $user, $context ),
470 'section' => 'rendering/skin',
473 # Create links to user CSS/JS pages for all skins
474 # This code is basically copied from generateSkinOptions(). It'd
475 # be nice to somehow merge this back in there to avoid redundancy.
476 if ( $wgAllowUserCss ||
$wgAllowUserJs ) {
477 $linkTools = array();
479 if ( $wgAllowUserCss ) {
480 $cssPage = Title
::makeTitleSafe( NS_USER
, $user->getName() . '/common.css' );
481 $linkTools[] = Linker
::link( $cssPage, $context->msg( 'prefs-custom-css' )->escaped() );
484 if ( $wgAllowUserJs ) {
485 $jsPage = Title
::makeTitleSafe( NS_USER
, $user->getName() . '/common.js' );
486 $linkTools[] = Linker
::link( $jsPage, $context->msg( 'prefs-custom-js' )->escaped() );
489 $defaultPreferences['commoncssjs'] = array(
492 'default' => $context->getLanguage()->pipeList( $linkTools ),
493 'label-message' => 'prefs-common-css-js',
494 'section' => 'rendering/skin',
498 $selectedSkin = $user->getOption( 'skin' );
499 if ( in_array( $selectedSkin, array( 'cologneblue', 'standard' ) ) ) {
500 $settings = array_flip( $context->getLanguage()->getQuickbarSettings() );
502 $defaultPreferences['quickbar'] = array(
504 'options' => $settings,
505 'section' => 'rendering/skin',
506 'label-message' => 'qbsettings',
513 * @param $context IContextSource
514 * @param $defaultPreferences Array
516 static function filesPreferences( $user, IContextSource
$context, &$defaultPreferences ) {
517 ## Files #####################################
518 $defaultPreferences['imagesize'] = array(
520 'options' => self
::getImageSizes( $context ),
521 'label-message' => 'imagemaxsize',
522 'section' => 'rendering/files',
524 $defaultPreferences['thumbsize'] = array(
526 'options' => self
::getThumbSizes( $context ),
527 'label-message' => 'thumbsize',
528 'section' => 'rendering/files',
534 * @param $context IContextSource
535 * @param $defaultPreferences
538 static function datetimePreferences( $user, IContextSource
$context, &$defaultPreferences ) {
539 ## Date and time #####################################
540 $dateOptions = self
::getDateOptions( $context );
541 if ( $dateOptions ) {
542 $defaultPreferences['date'] = array(
544 'options' => $dateOptions,
546 'section' => 'datetime/dateformat',
551 $now = wfTimestampNow();
552 $lang = $context->getLanguage();
553 $nowlocal = Xml
::element( 'span', array( 'id' => 'wpLocalTime' ),
554 $lang->time( $now, true ) );
555 $nowserver = $lang->time( $now, false ) .
556 Html
::hidden( 'wpServerTime', (int)substr( $now, 8, 2 ) * 60 +
(int)substr( $now, 10, 2 ) );
558 $defaultPreferences['nowserver'] = array(
561 'label-message' => 'servertime',
562 'default' => $nowserver,
563 'section' => 'datetime/timeoffset',
566 $defaultPreferences['nowlocal'] = array(
569 'label-message' => 'localtime',
570 'default' => $nowlocal,
571 'section' => 'datetime/timeoffset',
574 // Grab existing pref.
575 $tzOffset = $user->getOption( 'timecorrection' );
576 $tz = explode( '|', $tzOffset, 3 );
578 $tzOptions = self
::getTimezoneOptions( $context );
580 $tzSetting = $tzOffset;
581 if ( count( $tz ) > 1 && $tz[0] == 'Offset' ) {
583 $tzSetting = sprintf( '%+03d:%02d', floor( $minDiff / 60 ), abs( $minDiff ) %
60 );
584 } elseif ( count( $tz ) > 1 && $tz[0] == 'ZoneInfo' &&
585 !in_array( $tzOffset, HTMLFormField
::flattenOptions( $tzOptions ) ) )
587 # Timezone offset can vary with DST
588 $userTZ = timezone_open( $tz[2] );
589 if ( $userTZ !== false ) {
590 $minDiff = floor( timezone_offset_get( $userTZ, date_create( 'now' ) ) / 60 );
591 $tzSetting = "ZoneInfo|$minDiff|{$tz[2]}";
595 $defaultPreferences['timecorrection'] = array(
596 'class' => 'HTMLSelectOrOtherField',
597 'label-message' => 'timezonelegend',
598 'options' => $tzOptions,
599 'default' => $tzSetting,
601 'section' => 'datetime/timeoffset',
607 * @param $context IContextSource
608 * @param $defaultPreferences Array
610 static function renderingPreferences( $user, IContextSource
$context, &$defaultPreferences ) {
611 ## Page Rendering ##############################
612 global $wgAllowUserCssPrefs;
613 if ( $wgAllowUserCssPrefs ) {
614 $defaultPreferences['underline'] = array(
617 $context->msg( 'underline-never' )->text() => 0,
618 $context->msg( 'underline-always' )->text() => 1,
619 $context->msg( 'underline-default' )->text() => 2,
621 'label-message' => 'tog-underline',
622 'section' => 'rendering/advancedrendering',
626 $stubThresholdValues = array( 50, 100, 500, 1000, 2000, 5000, 10000 );
627 $stubThresholdOptions = array( $context->msg( 'stub-threshold-disabled' )->text() => 0 );
628 foreach ( $stubThresholdValues as $value ) {
629 $stubThresholdOptions[$context->msg( 'size-bytes', $value )->text()] = $value;
632 $defaultPreferences['stubthreshold'] = array(
633 'type' => 'selectorother',
634 'section' => 'rendering/advancedrendering',
635 'options' => $stubThresholdOptions,
637 'label' => $context->msg( 'stub-threshold' )->text(), // Raw HTML message. Yay?
640 if ( $wgAllowUserCssPrefs ) {
641 $defaultPreferences['showtoc'] = array(
643 'section' => 'rendering/advancedrendering',
644 'label-message' => 'tog-showtoc',
647 $defaultPreferences['nocache'] = array(
649 'label-message' => 'tog-nocache',
650 'section' => 'rendering/advancedrendering',
652 $defaultPreferences['showhiddencats'] = array(
654 'section' => 'rendering/advancedrendering',
655 'label-message' => 'tog-showhiddencats'
657 $defaultPreferences['showjumplinks'] = array(
659 'section' => 'rendering/advancedrendering',
660 'label-message' => 'tog-showjumplinks',
663 if ( $wgAllowUserCssPrefs ) {
664 $defaultPreferences['justify'] = array(
666 'section' => 'rendering/advancedrendering',
667 'label-message' => 'tog-justify',
671 $defaultPreferences['numberheadings'] = array(
673 'section' => 'rendering/advancedrendering',
674 'label-message' => 'tog-numberheadings',
680 * @param $context IContextSource
681 * @param $defaultPreferences Array
683 static function editingPreferences( $user, IContextSource
$context, &$defaultPreferences ) {
684 global $wgUseExternalEditor, $wgAllowUserCssPrefs;
686 ## Editing #####################################
687 $defaultPreferences['cols'] = array(
689 'label-message' => 'columns',
690 'section' => 'editing/textboxsize',
694 $defaultPreferences['rows'] = array(
696 'label-message' => 'rows',
697 'section' => 'editing/textboxsize',
702 if ( $wgAllowUserCssPrefs ) {
703 $defaultPreferences['editfont'] = array(
705 'section' => 'editing/advancedediting',
706 'label-message' => 'editfont-style',
708 $context->msg( 'editfont-default' )->text() => 'default',
709 $context->msg( 'editfont-monospace' )->text() => 'monospace',
710 $context->msg( 'editfont-sansserif' )->text() => 'sans-serif',
711 $context->msg( 'editfont-serif' )->text() => 'serif',
715 $defaultPreferences['previewontop'] = array(
717 'section' => 'editing/advancedediting',
718 'label-message' => 'tog-previewontop',
720 $defaultPreferences['previewonfirst'] = array(
722 'section' => 'editing/advancedediting',
723 'label-message' => 'tog-previewonfirst',
726 if ( $wgAllowUserCssPrefs ) {
727 $defaultPreferences['editsection'] = array(
729 'section' => 'editing/advancedediting',
730 'label-message' => 'tog-editsection',
733 $defaultPreferences['editsectiononrightclick'] = array(
735 'section' => 'editing/advancedediting',
736 'label-message' => 'tog-editsectiononrightclick',
738 $defaultPreferences['editondblclick'] = array(
740 'section' => 'editing/advancedediting',
741 'label-message' => 'tog-editondblclick',
743 $defaultPreferences['showtoolbar'] = array(
745 'section' => 'editing/advancedediting',
746 'label-message' => 'tog-showtoolbar',
749 if ( $user->isAllowed( 'minoredit' ) ) {
750 $defaultPreferences['minordefault'] = array(
752 'section' => 'editing/advancedediting',
753 'label-message' => 'tog-minordefault',
757 if ( $wgUseExternalEditor ) {
758 $defaultPreferences['externaleditor'] = array(
760 'section' => 'editing/advancedediting',
761 'label-message' => 'tog-externaleditor',
763 $defaultPreferences['externaldiff'] = array(
765 'section' => 'editing/advancedediting',
766 'label-message' => 'tog-externaldiff',
770 $defaultPreferences['forceeditsummary'] = array(
772 'section' => 'editing/advancedediting',
773 'label-message' => 'tog-forceeditsummary',
777 $defaultPreferences['uselivepreview'] = array(
779 'section' => 'editing/advancedediting',
780 'label-message' => 'tog-uselivepreview',
786 * @param $context IContextSource
787 * @param $defaultPreferences Array
789 static function rcPreferences( $user, IContextSource
$context, &$defaultPreferences ) {
790 global $wgRCMaxAge, $wgRCShowWatchingUsers;
792 ## RecentChanges #####################################
793 $defaultPreferences['rcdays'] = array(
795 'label-message' => 'recentchangesdays',
796 'section' => 'rc/displayrc',
798 'max' => ceil( $wgRCMaxAge / ( 3600 * 24 ) ),
799 'help' => $context->msg( 'recentchangesdays-max' )->numParams(
800 ceil( $wgRCMaxAge / ( 3600 * 24 ) ) )->text()
802 $defaultPreferences['rclimit'] = array(
804 'label-message' => 'recentchangescount',
805 'help-message' => 'prefs-help-recentchangescount',
806 'section' => 'rc/displayrc',
808 $defaultPreferences['usenewrc'] = array(
810 'label-message' => 'tog-usenewrc',
811 'section' => 'rc/advancedrc',
813 $defaultPreferences['hideminor'] = array(
815 'label-message' => 'tog-hideminor',
816 'section' => 'rc/advancedrc',
819 if ( $user->useRCPatrol() ) {
820 $defaultPreferences['hidepatrolled'] = array(
822 'section' => 'rc/advancedrc',
823 'label-message' => 'tog-hidepatrolled',
825 $defaultPreferences['newpageshidepatrolled'] = array(
827 'section' => 'rc/advancedrc',
828 'label-message' => 'tog-newpageshidepatrolled',
832 if ( $wgRCShowWatchingUsers ) {
833 $defaultPreferences['shownumberswatching'] = array(
835 'section' => 'rc/advancedrc',
836 'label-message' => 'tog-shownumberswatching',
843 * @param $context IContextSource
844 * @param $defaultPreferences
846 static function watchlistPreferences( $user, IContextSource
$context, &$defaultPreferences ) {
847 global $wgUseRCPatrol, $wgEnableAPI, $wgRCMaxAge;
849 $watchlistdaysMax = ceil( $wgRCMaxAge / ( 3600 * 24 ) );
851 ## Watchlist #####################################
852 $defaultPreferences['watchlistdays'] = array(
855 'max' => $watchlistdaysMax,
856 'section' => 'watchlist/displaywatchlist',
857 'help' => $context->msg( 'prefs-watchlist-days-max' )->numParams(
858 $watchlistdaysMax )->text(),
859 'label-message' => 'prefs-watchlist-days',
861 $defaultPreferences['wllimit'] = array(
865 'label-message' => 'prefs-watchlist-edits',
866 'help' => $context->msg( 'prefs-watchlist-edits-max' )->escaped(),
867 'section' => 'watchlist/displaywatchlist',
869 $defaultPreferences['extendwatchlist'] = array(
871 'section' => 'watchlist/advancedwatchlist',
872 'label-message' => 'tog-extendwatchlist',
874 $defaultPreferences['watchlisthideminor'] = array(
876 'section' => 'watchlist/advancedwatchlist',
877 'label-message' => 'tog-watchlisthideminor',
879 $defaultPreferences['watchlisthidebots'] = array(
881 'section' => 'watchlist/advancedwatchlist',
882 'label-message' => 'tog-watchlisthidebots',
884 $defaultPreferences['watchlisthideown'] = array(
886 'section' => 'watchlist/advancedwatchlist',
887 'label-message' => 'tog-watchlisthideown',
889 $defaultPreferences['watchlisthideanons'] = array(
891 'section' => 'watchlist/advancedwatchlist',
892 'label-message' => 'tog-watchlisthideanons',
894 $defaultPreferences['watchlisthideliu'] = array(
896 'section' => 'watchlist/advancedwatchlist',
897 'label-message' => 'tog-watchlisthideliu',
900 if ( $wgUseRCPatrol ) {
901 $defaultPreferences['watchlisthidepatrolled'] = array(
903 'section' => 'watchlist/advancedwatchlist',
904 'label-message' => 'tog-watchlisthidepatrolled',
908 if ( $wgEnableAPI ) {
909 # Some random gibberish as a proposed default
910 $hash = sha1( mt_rand() . microtime( true ) );
912 $defaultPreferences['watchlisttoken'] = array(
914 'section' => 'watchlist/advancedwatchlist',
915 'label-message' => 'prefs-watchlist-token',
916 'help' => $context->msg( 'prefs-help-watchlist-token', $hash )->escaped()
921 'edit' => 'watchdefault',
922 'move' => 'watchmoves',
923 'delete' => 'watchdeletion'
927 if ( $user->isAllowed( 'createpage' ) ||
$user->isAllowed( 'createtalk' ) ) {
928 $watchTypes['read'] = 'watchcreations';
931 foreach ( $watchTypes as $action => $pref ) {
932 if ( $user->isAllowed( $action ) ) {
933 $defaultPreferences[$pref] = array(
935 'section' => 'watchlist/advancedwatchlist',
936 'label-message' => "tog-$pref",
944 * @param $context IContextSource
945 * @param $defaultPreferences Array
947 static function searchPreferences( $user, IContextSource
$context, &$defaultPreferences ) {
948 global $wgContLang, $wgEnableMWSuggest, $wgVectorUseSimpleSearch;
950 ## Search #####################################
951 $defaultPreferences['searchlimit'] = array(
953 'label-message' => 'resultsperpage',
954 'section' => 'searchoptions/displaysearchoptions',
958 if ( $wgEnableMWSuggest ) {
959 $defaultPreferences['disablesuggest'] = array(
961 'label-message' => 'mwsuggest-disable',
962 'section' => 'searchoptions/displaysearchoptions',
966 if ( $wgVectorUseSimpleSearch ) {
967 $defaultPreferences['vector-simplesearch'] = array(
969 'label-message' => 'vector-simplesearch-preference',
970 'section' => 'searchoptions/displaysearchoptions'
974 $defaultPreferences['searcheverything'] = array(
976 'label-message' => 'searcheverything-enable',
977 'section' => 'searchoptions/advancedsearchoptions',
980 $nsOptions = array();
982 foreach ( $wgContLang->getNamespaces() as $ns => $name ) {
987 $displayNs = str_replace( '_', ' ', $name );
990 $displayNs = $context->msg( 'blanknamespace' )->text();
993 $displayNs = htmlspecialchars( $displayNs );
994 $nsOptions[$displayNs] = $ns;
997 $defaultPreferences['searchnamespaces'] = array(
998 'type' => 'multiselect',
999 'label-message' => 'defaultns',
1000 'options' => $nsOptions,
1001 'section' => 'searchoptions/advancedsearchoptions',
1002 'prefix' => 'searchNs',
1008 * @param $context IContextSource
1009 * @param $defaultPreferences Array
1011 static function miscPreferences( $user, IContextSource
$context, &$defaultPreferences ) {
1014 ## Misc #####################################
1015 $defaultPreferences['diffonly'] = array(
1017 'section' => 'misc/diffs',
1018 'label-message' => 'tog-diffonly',
1020 $defaultPreferences['norollbackdiff'] = array(
1022 'section' => 'misc/diffs',
1023 'label-message' => 'tog-norollbackdiff',
1026 // Stuff from Language::getExtraUserToggles()
1027 $toggles = $wgContLang->getExtraUserToggles();
1029 foreach ( $toggles as $toggle ) {
1030 $defaultPreferences[$toggle] = array(
1032 'section' => 'personal/i18n',
1033 'label-message' => "tog-$toggle",
1039 * @param $user User The User object
1040 * @param $context IContextSource
1041 * @return Array: text/links to display as key; $skinkey as value
1043 static function generateSkinOptions( $user, IContextSource
$context ) {
1044 global $wgDefaultSkin, $wgAllowUserCss, $wgAllowUserJs;
1047 $mptitle = Title
::newMainPage();
1048 $previewtext = $context->msg( 'skin-preview' )->text();
1050 # Only show members of Skin::getSkinNames() rather than
1051 # $skinNames (skins is all skin names from Language.php)
1052 $validSkinNames = Skin
::getUsableSkins();
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 = $context->msg( "skinname-{$skinkey}" );
1058 if ( $msg->exists() ) {
1059 $skinname = htmlspecialchars( $msg->text() );
1062 asort( $validSkinNames );
1064 foreach ( $validSkinNames as $skinkey => $sn ) {
1065 $linkTools = array();
1067 # Mark the default skin
1068 if ( $skinkey == $wgDefaultSkin ) {
1069 $linkTools[] = $context->msg( 'default' )->escaped();
1072 # Create preview link
1073 $mplink = htmlspecialchars( $mptitle->getLocalURL( "useskin=$skinkey" ) );
1074 $linkTools[] = "<a target='_blank' href=\"$mplink\">$previewtext</a>";
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, $context->msg( 'prefs-custom-css' )->escaped() );
1082 if ( $wgAllowUserJs ) {
1083 $jsPage = Title
::makeTitleSafe( NS_USER
, $user->getName() . '/' . $skinkey . '.js' );
1084 $linkTools[] = Linker
::link( $jsPage, $context->msg( 'prefs-custom-js' )->escaped() );
1087 $display = $sn . ' ' . $context->msg( 'parentheses', $context->getLanguage()->pipeList( $linkTools ) )->text();
1088 $ret[$display] = $skinkey;
1095 * @param $context IContextSource
1098 static function getDateOptions( IContextSource
$context ) {
1099 $lang = $context->getLanguage();
1100 $dateopts = $lang->getDatePreferences();
1105 if ( !in_array( 'default', $dateopts ) ) {
1106 $dateopts[] = 'default'; // Make sure default is always valid
1110 // KLUGE: site default might not be valid for user language
1111 global $wgDefaultUserOptions;
1112 if ( !in_array( $wgDefaultUserOptions['date'], $dateopts ) ) {
1113 $wgDefaultUserOptions['date'] = 'default';
1116 $epoch = wfTimestampNow();
1117 foreach ( $dateopts as $key ) {
1118 if ( $key == 'default' ) {
1119 $formatted = $context->msg( 'datedefault' )->escaped();
1121 $formatted = htmlspecialchars( $lang->timeanddate( $epoch, false, $key ) );
1123 $ret[$formatted] = $key;
1130 * @param $context IContextSource
1133 static function getImageSizes( IContextSource
$context ) {
1134 global $wgImageLimits;
1137 $pixels = $context->msg( 'unit-pixel' )->text();
1139 foreach ( $wgImageLimits as $index => $limits ) {
1140 $display = "{$limits[0]}×{$limits[1]}" . $pixels;
1141 $ret[$display] = $index;
1148 * @param $context IContextSource
1151 static function getThumbSizes( IContextSource
$context ) {
1152 global $wgThumbLimits;
1155 $pixels = $context->msg( 'unit-pixel' )->text();
1157 foreach ( $wgThumbLimits as $index => $size ) {
1158 $display = $size . $pixels;
1159 $ret[$display] = $index;
1166 * @param $signature string
1167 * @param $alldata array
1168 * @param $form HTMLForm
1169 * @return bool|string
1171 static function validateSignature( $signature, $alldata, $form ) {
1172 global $wgParser, $wgMaxSigChars;
1173 if ( mb_strlen( $signature ) > $wgMaxSigChars ) {
1174 return Xml
::element( 'span', array( 'class' => 'error' ),
1175 $form->msg( 'badsiglength' )->numParams( $wgMaxSigChars )->text() );
1176 } elseif ( isset( $alldata['fancysig'] ) &&
1177 $alldata['fancysig'] &&
1178 false === $wgParser->validateSig( $signature ) ) {
1179 return Xml
::element( 'span', array( 'class' => 'error' ), $form->msg( 'badsig' )->text() );
1186 * @param $signature string
1187 * @param $alldata array
1188 * @param $form HTMLForm
1191 static function cleanSignature( $signature, $alldata, $form ) {
1192 if ( isset( $alldata['fancysig'] ) && $alldata['fancysig'] ) {
1194 $signature = $wgParser->cleanSig( $signature );
1196 // When no fancy sig used, make sure ~{3,5} get removed.
1197 $signature = Parser
::cleanSigInSig( $signature );
1205 * @param $context IContextSource
1206 * @param $formClass string
1207 * @param $remove Array: array of items to remove
1210 static function getFormObject( $user, IContextSource
$context, $formClass = 'PreferencesForm', array $remove = array() ) {
1211 $formDescriptor = Preferences
::getPreferences( $user, $context );
1212 if ( count( $remove ) ) {
1213 $removeKeys = array_flip( $remove );
1214 $formDescriptor = array_diff_key( $formDescriptor, $removeKeys );
1218 * @var $htmlForm PreferencesForm
1220 $htmlForm = new $formClass( $formDescriptor, $context, 'prefs' );
1222 $htmlForm->setModifiedUser( $user );
1223 $htmlForm->setId( 'mw-prefs-form' );
1224 $htmlForm->setSubmitText( $context->msg( 'saveprefs' )->text() );
1225 # Used message keys: 'accesskey-preferences-save', 'tooltip-preferences-save'
1226 $htmlForm->setSubmitTooltip( 'preferences-save' );
1227 $htmlForm->setSubmitID( 'prefsubmit' );
1228 $htmlForm->setSubmitCallback( array( 'Preferences', 'tryFormSubmit' ) );
1236 static function getTimezoneOptions( IContextSource
$context ) {
1239 global $wgLocalTZoffset, $wgLocaltimezone;
1240 // Check that $wgLocalTZoffset is the same as $wgLocaltimezone
1241 if ( $wgLocalTZoffset == date( 'Z' ) / 60 ) {
1242 $server_tz_msg = $context->msg( 'timezoneuseserverdefault', $wgLocaltimezone )->text();
1244 $tzstring = sprintf( '%+03d:%02d', floor( $wgLocalTZoffset / 60 ), abs( $wgLocalTZoffset ) %
60 );
1245 $server_tz_msg = $context->msg( 'timezoneuseserverdefault', $tzstring )->text();
1247 $opt[$server_tz_msg] = "System|$wgLocalTZoffset";
1248 $opt[$context->msg( 'timezoneuseoffset' )->text()] = 'other';
1249 $opt[$context->msg( 'guesstimezone' )->text()] = 'guess';
1251 if ( function_exists( 'timezone_identifiers_list' ) ) {
1252 # Read timezone list
1253 $tzs = timezone_identifiers_list();
1256 $tzRegions = array();
1257 $tzRegions['Africa'] = $context->msg( 'timezoneregion-africa' )->text();
1258 $tzRegions['America'] = $context->msg( 'timezoneregion-america' )->text();
1259 $tzRegions['Antarctica'] = $context->msg( 'timezoneregion-antarctica' )->text();
1260 $tzRegions['Arctic'] = $context->msg( 'timezoneregion-arctic' )->text();
1261 $tzRegions['Asia'] = $context->msg( 'timezoneregion-asia' )->text();
1262 $tzRegions['Atlantic'] = $context->msg( 'timezoneregion-atlantic' )->text();
1263 $tzRegions['Australia'] = $context->msg( 'timezoneregion-australia' )->text();
1264 $tzRegions['Europe'] = $context->msg( 'timezoneregion-europe' )->text();
1265 $tzRegions['Indian'] = $context->msg( 'timezoneregion-indian' )->text();
1266 $tzRegions['Pacific'] = $context->msg( 'timezoneregion-pacific' )->text();
1267 asort( $tzRegions );
1269 $prefill = array_fill_keys( array_values( $tzRegions ), array() );
1270 $opt = array_merge( $opt, $prefill );
1272 $now = date_create( 'now' );
1274 foreach ( $tzs as $tz ) {
1275 $z = explode( '/', $tz, 2 );
1277 # timezone_identifiers_list() returns a number of
1278 # backwards-compatibility entries. This filters them out of the
1279 # list presented to the user.
1280 if ( count( $z ) != 2 ||
!array_key_exists( $z[0], $tzRegions ) ) {
1285 $z[0] = $tzRegions[$z[0]];
1287 $minDiff = floor( timezone_offset_get( timezone_open( $tz ), $now ) / 60 );
1289 $display = str_replace( '_', ' ', $z[0] . '/' . $z[1] );
1290 $value = "ZoneInfo|$minDiff|$tz";
1292 $opt[$z[0]][$display] = $value;
1303 static function filterIntval( $value, $alldata ){
1304 return intval( $value );
1312 static function filterTimezoneInput( $tz, $alldata ) {
1313 $data = explode( '|', $tz, 3 );
1314 switch ( $data[0] ) {
1319 $data = explode( ':', $tz, 2 );
1320 if ( count( $data ) == 2 ) {
1321 $data[0] = intval( $data[0] );
1322 $data[1] = intval( $data[1] );
1323 $minDiff = abs( $data[0] ) * 60 +
$data[1];
1324 if ( $data[0] < 0 ) $minDiff = - $minDiff;
1326 $minDiff = intval( $data[0] ) * 60;
1329 # Max is +14:00 and min is -12:00, see:
1330 # http://en.wikipedia.org/wiki/Timezone
1331 $minDiff = min( $minDiff, 840 ); # 14:00
1332 $minDiff = max( $minDiff, - 720 ); # -12:00
1333 return 'Offset|' . $minDiff;
1339 * @param $form PreferencesForm
1340 * @param $entryPoint string
1341 * @return bool|Status|string
1343 static function tryFormSubmit( $formData, $form, $entryPoint = 'internal' ) {
1344 global $wgHiddenPrefs;
1346 $user = $form->getModifiedUser();
1350 foreach ( array_keys( $formData ) as $name ) {
1351 if ( isset( self
::$saveFilters[$name] ) ) {
1353 call_user_func( self
::$saveFilters[$name], $formData[$name], $formData );
1357 // Stuff that shouldn't be saved as a preference.
1358 $saveBlacklist = array(
1363 // Fortunately, the realname field is MUCH simpler
1364 if ( !in_array( 'realname', $wgHiddenPrefs ) ) {
1365 $realName = $formData['realname'];
1366 $user->setRealName( $realName );
1369 foreach ( $saveBlacklist as $b ) {
1370 unset( $formData[$b] );
1373 # If users have saved a value for a preference which has subsequently been disabled
1374 # via $wgHiddenPrefs, we don't want to destroy that setting in case the preference
1375 # is subsequently re-enabled
1376 # TODO: maintenance script to actually delete these
1377 foreach( $wgHiddenPrefs as $pref ){
1378 # If the user has not set a non-default value here, the default will be returned
1379 # and subsequently discarded
1380 $formData[$pref] = $user->getOption( $pref, null, true );
1383 // Keeps old preferences from interfering due to back-compat
1385 $user->resetOptions();
1387 foreach ( $formData as $key => $value ) {
1388 $user->setOption( $key, $value );
1391 $user->saveSettings();
1398 * @param $form PreferencesForm
1401 public static function tryUISubmit( $formData, $form ) {
1402 $res = self
::tryFormSubmit( $formData, $form, 'ui' );
1405 $urlOptions = array( 'success' => 1 );
1407 if ( $res === 'eauth' ) {
1408 $urlOptions['eauth'] = 1;
1411 $urlOptions +
= $form->getExtraSuccessRedirectParameters();
1413 $url = $form->getTitle()->getFullURL( $urlOptions );
1415 $form->getContext()->getOutput()->redirect( $url );
1418 return Status
::newGood();
1422 * Try to set a user's email address.
1423 * This does *not* try to validate the address.
1424 * Caller is responsible for checking $wgAuth.
1426 * @param $newaddr string New email address
1427 * @return Array (true on success or Status on failure, info string)
1429 public static function trySetUserEmail( User
$user, $newaddr ) {
1430 global $wgEnableEmail, $wgEmailAuthentication;
1433 if ( $wgEnableEmail ) {
1434 $oldaddr = $user->getEmail();
1435 if ( ( $newaddr != '' ) && ( $newaddr != $oldaddr ) ) {
1436 # The user has supplied a new email address on the login page
1437 # new behaviour: set this new emailaddr from login-page into user database record
1438 $user->setEmail( $newaddr );
1439 if ( $wgEmailAuthentication ) {
1440 # Mail a temporary password to the dirty address.
1441 # User can come back through the confirmation URL to re-enable email.
1442 $type = $oldaddr != '' ?
'changed' : 'set';
1443 $result = $user->sendConfirmationMail( $type );
1444 if ( !$result->isGood() ) {
1445 return array( $result, 'mailerror' );
1449 } elseif ( $newaddr != $oldaddr ) { // if the address is the same, don't change it
1450 $user->setEmail( $newaddr );
1452 if ( $oldaddr != $newaddr ) {
1453 wfRunHooks( 'PrefsEmailAudit', array( $user, $oldaddr, $newaddr ) );
1457 return array( true, $info );
1461 * @deprecated in 1.19; will be removed in 1.20.
1465 public static function loadOldSearchNs( $user ) {
1466 wfDeprecated( __METHOD__
, '1.19' );
1468 $searchableNamespaces = SearchEngine
::searchableNamespaces();
1469 // Back compat with old format
1472 foreach ( $searchableNamespaces as $ns => $name ) {
1473 if ( $user->getOption( 'searchNs' . $ns ) ) {
1482 /** Some tweaks to allow js prefs to work */
1483 class PreferencesForm
extends HTMLForm
{
1484 // Override default value from HTMLForm
1485 protected $mSubSectionBeforeFields = false;
1487 private $modifiedUser;
1492 public function setModifiedUser( $user ) {
1493 $this->modifiedUser
= $user;
1499 public function getModifiedUser() {
1500 if ( $this->modifiedUser
=== null ) {
1501 return $this->getUser();
1503 return $this->modifiedUser
;
1508 * Get extra parameters for the query string when redirecting after
1513 public function getExtraSuccessRedirectParameters() {
1518 * @param $html string
1521 function wrapForm( $html ) {
1522 $html = Xml
::tags( 'div', array( 'id' => 'preferences' ), $html );
1524 return parent
::wrapForm( $html );
1530 function getButtons() {
1531 $html = parent
::getButtons();
1533 $t = SpecialPage
::getTitleFor( 'Preferences', 'reset' );
1535 $html .= "\n" . Linker
::link( $t, $this->msg( 'restoreprefs' )->escaped() );
1537 $html = Xml
::tags( 'div', array( 'class' => 'mw-prefs-buttons' ), $html );
1543 * @param $data array
1546 function filterDataForSubmit( $data ) {
1547 // Support for separating MultiSelect preferences into multiple preferences
1548 // Due to lack of array support.
1549 foreach ( $this->mFlatFields
as $fieldname => $field ) {
1550 $info = $field->mParams
;
1551 if ( $field instanceof HTMLMultiSelectField
) {
1552 $options = HTMLFormField
::flattenOptions( $info['options'] );
1553 $prefix = isset( $info['prefix'] ) ?
$info['prefix'] : $fieldname;
1555 foreach ( $options as $opt ) {
1556 $data["$prefix$opt"] = in_array( $opt, $data[$fieldname] );
1559 unset( $data[$fieldname] );
1567 * Get the whole body of the form.
1570 function getBody() {
1571 return $this->displaySection( $this->mFieldTree
, '', 'mw-prefsection-' );
1575 * Get the <legend> for a given section key. Normally this is the
1576 * prefs-$key message but we'll allow extensions to override it.
1577 * @param $key string
1580 function getLegend( $key ) {
1581 $legend = parent
::getLegend( $key );
1582 wfRunHooks( 'PreferencesGetLegend', array( $this, $key, &$legend ) );