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->mOptions
) === true ) {
88 $info['default'] = $prefFromUser;
89 } elseif ( $field->validate( $globalDefault, $user->mOptions
) === 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['highlightbroken'] = array(
643 'section' => 'rendering/advancedrendering',
644 'label' => $context->msg( 'tog-highlightbroken' )->text(), // Raw HTML
646 $defaultPreferences['showtoc'] = array(
648 'section' => 'rendering/advancedrendering',
649 'label-message' => 'tog-showtoc',
652 $defaultPreferences['nocache'] = array(
654 'label-message' => 'tog-nocache',
655 'section' => 'rendering/advancedrendering',
657 $defaultPreferences['showhiddencats'] = array(
659 'section' => 'rendering/advancedrendering',
660 'label-message' => 'tog-showhiddencats'
662 $defaultPreferences['showjumplinks'] = array(
664 'section' => 'rendering/advancedrendering',
665 'label-message' => 'tog-showjumplinks',
668 if ( $wgAllowUserCssPrefs ) {
669 $defaultPreferences['justify'] = array(
671 'section' => 'rendering/advancedrendering',
672 'label-message' => 'tog-justify',
676 $defaultPreferences['numberheadings'] = array(
678 'section' => 'rendering/advancedrendering',
679 'label-message' => 'tog-numberheadings',
685 * @param $context IContextSource
686 * @param $defaultPreferences Array
688 static function editingPreferences( $user, IContextSource
$context, &$defaultPreferences ) {
689 global $wgUseExternalEditor, $wgAllowUserCssPrefs;
691 ## Editing #####################################
692 $defaultPreferences['cols'] = array(
694 'label-message' => 'columns',
695 'section' => 'editing/textboxsize',
699 $defaultPreferences['rows'] = array(
701 'label-message' => 'rows',
702 'section' => 'editing/textboxsize',
707 if ( $wgAllowUserCssPrefs ) {
708 $defaultPreferences['editfont'] = array(
710 'section' => 'editing/advancedediting',
711 'label-message' => 'editfont-style',
713 $context->msg( 'editfont-default' )->text() => 'default',
714 $context->msg( 'editfont-monospace' )->text() => 'monospace',
715 $context->msg( 'editfont-sansserif' )->text() => 'sans-serif',
716 $context->msg( 'editfont-serif' )->text() => 'serif',
720 $defaultPreferences['previewontop'] = array(
722 'section' => 'editing/advancedediting',
723 'label-message' => 'tog-previewontop',
725 $defaultPreferences['previewonfirst'] = array(
727 'section' => 'editing/advancedediting',
728 'label-message' => 'tog-previewonfirst',
731 if ( $wgAllowUserCssPrefs ) {
732 $defaultPreferences['editsection'] = array(
734 'section' => 'editing/advancedediting',
735 'label-message' => 'tog-editsection',
738 $defaultPreferences['editsectiononrightclick'] = array(
740 'section' => 'editing/advancedediting',
741 'label-message' => 'tog-editsectiononrightclick',
743 $defaultPreferences['editondblclick'] = array(
745 'section' => 'editing/advancedediting',
746 'label-message' => 'tog-editondblclick',
748 $defaultPreferences['showtoolbar'] = array(
750 'section' => 'editing/advancedediting',
751 'label-message' => 'tog-showtoolbar',
754 if ( $user->isAllowed( 'minoredit' ) ) {
755 $defaultPreferences['minordefault'] = array(
757 'section' => 'editing/advancedediting',
758 'label-message' => 'tog-minordefault',
762 if ( $wgUseExternalEditor ) {
763 $defaultPreferences['externaleditor'] = array(
765 'section' => 'editing/advancedediting',
766 'label-message' => 'tog-externaleditor',
768 $defaultPreferences['externaldiff'] = array(
770 'section' => 'editing/advancedediting',
771 'label-message' => 'tog-externaldiff',
775 $defaultPreferences['forceeditsummary'] = array(
777 'section' => 'editing/advancedediting',
778 'label-message' => 'tog-forceeditsummary',
782 $defaultPreferences['uselivepreview'] = array(
784 'section' => 'editing/advancedediting',
785 'label-message' => 'tog-uselivepreview',
791 * @param $context IContextSource
792 * @param $defaultPreferences Array
794 static function rcPreferences( $user, IContextSource
$context, &$defaultPreferences ) {
795 global $wgRCMaxAge, $wgRCShowWatchingUsers;
797 ## RecentChanges #####################################
798 $defaultPreferences['rcdays'] = array(
800 'label-message' => 'recentchangesdays',
801 'section' => 'rc/displayrc',
803 'max' => ceil( $wgRCMaxAge / ( 3600 * 24 ) ),
804 'help' => $context->msg( 'recentchangesdays-max' )->numParams(
805 ceil( $wgRCMaxAge / ( 3600 * 24 ) ) )->text()
807 $defaultPreferences['rclimit'] = array(
809 'label-message' => 'recentchangescount',
810 'help-message' => 'prefs-help-recentchangescount',
811 'section' => 'rc/displayrc',
813 $defaultPreferences['usenewrc'] = array(
815 'label-message' => 'tog-usenewrc',
816 'section' => 'rc/advancedrc',
818 $defaultPreferences['hideminor'] = array(
820 'label-message' => 'tog-hideminor',
821 'section' => 'rc/advancedrc',
824 if ( $user->useRCPatrol() ) {
825 $defaultPreferences['hidepatrolled'] = array(
827 'section' => 'rc/advancedrc',
828 'label-message' => 'tog-hidepatrolled',
830 $defaultPreferences['newpageshidepatrolled'] = array(
832 'section' => 'rc/advancedrc',
833 'label-message' => 'tog-newpageshidepatrolled',
837 if ( $wgRCShowWatchingUsers ) {
838 $defaultPreferences['shownumberswatching'] = array(
840 'section' => 'rc/advancedrc',
841 'label-message' => 'tog-shownumberswatching',
848 * @param $context IContextSource
849 * @param $defaultPreferences
851 static function watchlistPreferences( $user, IContextSource
$context, &$defaultPreferences ) {
852 global $wgUseRCPatrol, $wgEnableAPI, $wgRCMaxAge;
854 $watchlistdaysMax = ceil( $wgRCMaxAge / ( 3600 * 24 ) );
856 ## Watchlist #####################################
857 $defaultPreferences['watchlistdays'] = array(
860 'max' => $watchlistdaysMax,
861 'section' => 'watchlist/displaywatchlist',
862 'help' => $context->msg( 'prefs-watchlist-days-max' )->numParams(
863 $watchlistdaysMax )->text(),
864 'label-message' => 'prefs-watchlist-days',
866 $defaultPreferences['wllimit'] = array(
870 'label-message' => 'prefs-watchlist-edits',
871 'help' => $context->msg( 'prefs-watchlist-edits-max' )->escaped(),
872 'section' => 'watchlist/displaywatchlist',
874 $defaultPreferences['extendwatchlist'] = array(
876 'section' => 'watchlist/advancedwatchlist',
877 'label-message' => 'tog-extendwatchlist',
879 $defaultPreferences['watchlisthideminor'] = array(
881 'section' => 'watchlist/advancedwatchlist',
882 'label-message' => 'tog-watchlisthideminor',
884 $defaultPreferences['watchlisthidebots'] = array(
886 'section' => 'watchlist/advancedwatchlist',
887 'label-message' => 'tog-watchlisthidebots',
889 $defaultPreferences['watchlisthideown'] = array(
891 'section' => 'watchlist/advancedwatchlist',
892 'label-message' => 'tog-watchlisthideown',
894 $defaultPreferences['watchlisthideanons'] = array(
896 'section' => 'watchlist/advancedwatchlist',
897 'label-message' => 'tog-watchlisthideanons',
899 $defaultPreferences['watchlisthideliu'] = array(
901 'section' => 'watchlist/advancedwatchlist',
902 'label-message' => 'tog-watchlisthideliu',
905 if ( $wgUseRCPatrol ) {
906 $defaultPreferences['watchlisthidepatrolled'] = array(
908 'section' => 'watchlist/advancedwatchlist',
909 'label-message' => 'tog-watchlisthidepatrolled',
913 if ( $wgEnableAPI ) {
914 # Some random gibberish as a proposed default
915 $hash = sha1( mt_rand() . microtime( true ) );
917 $defaultPreferences['watchlisttoken'] = array(
919 'section' => 'watchlist/advancedwatchlist',
920 'label-message' => 'prefs-watchlist-token',
921 'help' => $context->msg( 'prefs-help-watchlist-token', $hash )->escaped()
926 'edit' => 'watchdefault',
927 'move' => 'watchmoves',
928 'delete' => 'watchdeletion'
932 if ( $user->isAllowed( 'createpage' ) ||
$user->isAllowed( 'createtalk' ) ) {
933 $watchTypes['read'] = 'watchcreations';
936 foreach ( $watchTypes as $action => $pref ) {
937 if ( $user->isAllowed( $action ) ) {
938 $defaultPreferences[$pref] = array(
940 'section' => 'watchlist/advancedwatchlist',
941 'label-message' => "tog-$pref",
949 * @param $context IContextSource
950 * @param $defaultPreferences Array
952 static function searchPreferences( $user, IContextSource
$context, &$defaultPreferences ) {
953 global $wgContLang, $wgEnableMWSuggest, $wgVectorUseSimpleSearch;
955 ## Search #####################################
956 $defaultPreferences['searchlimit'] = array(
958 'label-message' => 'resultsperpage',
959 'section' => 'searchoptions/displaysearchoptions',
963 if ( $wgEnableMWSuggest ) {
964 $defaultPreferences['disablesuggest'] = array(
966 'label-message' => 'mwsuggest-disable',
967 'section' => 'searchoptions/displaysearchoptions',
971 if ( $wgVectorUseSimpleSearch ) {
972 $defaultPreferences['vector-simplesearch'] = array(
974 'label-message' => 'vector-simplesearch-preference',
975 'section' => 'searchoptions/displaysearchoptions'
979 $defaultPreferences['searcheverything'] = array(
981 'label-message' => 'searcheverything-enable',
982 'section' => 'searchoptions/advancedsearchoptions',
985 $nsOptions = array();
987 foreach ( $wgContLang->getNamespaces() as $ns => $name ) {
992 $displayNs = str_replace( '_', ' ', $name );
995 $displayNs = $context->msg( 'blanknamespace' )->text();
998 $displayNs = htmlspecialchars( $displayNs );
999 $nsOptions[$displayNs] = $ns;
1002 $defaultPreferences['searchnamespaces'] = array(
1003 'type' => 'multiselect',
1004 'label-message' => 'defaultns',
1005 'options' => $nsOptions,
1006 'section' => 'searchoptions/advancedsearchoptions',
1007 'prefix' => 'searchNs',
1013 * @param $context IContextSource
1014 * @param $defaultPreferences Array
1016 static function miscPreferences( $user, IContextSource
$context, &$defaultPreferences ) {
1019 ## Misc #####################################
1020 $defaultPreferences['diffonly'] = array(
1022 'section' => 'misc/diffs',
1023 'label-message' => 'tog-diffonly',
1025 $defaultPreferences['norollbackdiff'] = array(
1027 'section' => 'misc/diffs',
1028 'label-message' => 'tog-norollbackdiff',
1031 // Stuff from Language::getExtraUserToggles()
1032 $toggles = $wgContLang->getExtraUserToggles();
1034 foreach ( $toggles as $toggle ) {
1035 $defaultPreferences[$toggle] = array(
1037 'section' => 'personal/i18n',
1038 'label-message' => "tog-$toggle",
1044 * @param $user User The User object
1045 * @param $context IContextSource
1046 * @return Array: text/links to display as key; $skinkey as value
1048 static function generateSkinOptions( $user, IContextSource
$context ) {
1049 global $wgDefaultSkin, $wgAllowUserCss, $wgAllowUserJs;
1052 $mptitle = Title
::newMainPage();
1053 $previewtext = $context->msg( 'skin-preview' )->text();
1055 # Only show members of Skin::getSkinNames() rather than
1056 # $skinNames (skins is all skin names from Language.php)
1057 $validSkinNames = Skin
::getUsableSkins();
1059 # Sort by UI skin name. First though need to update validSkinNames as sometimes
1060 # the skinkey & UI skinname differ (e.g. "standard" skinkey is "Classic" in the UI).
1061 foreach ( $validSkinNames as $skinkey => &$skinname ) {
1062 $msg = $context->msg( "skinname-{$skinkey}" );
1063 if ( $msg->exists() ) {
1064 $skinname = htmlspecialchars( $msg->text() );
1067 asort( $validSkinNames );
1069 foreach ( $validSkinNames as $skinkey => $sn ) {
1070 $linkTools = array();
1072 # Mark the default skin
1073 if ( $skinkey == $wgDefaultSkin ) {
1074 $linkTools[] = $context->msg( 'default' )->escaped();
1077 # Create preview link
1078 $mplink = htmlspecialchars( $mptitle->getLocalURL( "useskin=$skinkey" ) );
1079 $linkTools[] = "<a target='_blank' href=\"$mplink\">$previewtext</a>";
1081 # Create links to user CSS/JS pages
1082 if ( $wgAllowUserCss ) {
1083 $cssPage = Title
::makeTitleSafe( NS_USER
, $user->getName() . '/' . $skinkey . '.css' );
1084 $linkTools[] = Linker
::link( $cssPage, $context->msg( 'prefs-custom-css' )->escaped() );
1087 if ( $wgAllowUserJs ) {
1088 $jsPage = Title
::makeTitleSafe( NS_USER
, $user->getName() . '/' . $skinkey . '.js' );
1089 $linkTools[] = Linker
::link( $jsPage, $context->msg( 'prefs-custom-js' )->escaped() );
1092 $display = $sn . ' ' . $context->msg( 'parentheses', $context->getLanguage()->pipeList( $linkTools ) )->text();
1093 $ret[$display] = $skinkey;
1100 * @param $context IContextSource
1103 static function getDateOptions( IContextSource
$context ) {
1104 $lang = $context->getLanguage();
1105 $dateopts = $lang->getDatePreferences();
1110 if ( !in_array( 'default', $dateopts ) ) {
1111 $dateopts[] = 'default'; // Make sure default is always valid
1115 // KLUGE: site default might not be valid for user language
1116 global $wgDefaultUserOptions;
1117 if ( !in_array( $wgDefaultUserOptions['date'], $dateopts ) ) {
1118 $wgDefaultUserOptions['date'] = 'default';
1121 $epoch = wfTimestampNow();
1122 foreach ( $dateopts as $key ) {
1123 if ( $key == 'default' ) {
1124 $formatted = $context->msg( 'datedefault' )->escaped();
1126 $formatted = htmlspecialchars( $lang->timeanddate( $epoch, false, $key ) );
1128 $ret[$formatted] = $key;
1135 * @param $context IContextSource
1138 static function getImageSizes( IContextSource
$context ) {
1139 global $wgImageLimits;
1142 $pixels = $context->msg( 'unit-pixel' )->text();
1144 foreach ( $wgImageLimits as $index => $limits ) {
1145 $display = "{$limits[0]}×{$limits[1]}" . $pixels;
1146 $ret[$display] = $index;
1153 * @param $context IContextSource
1156 static function getThumbSizes( IContextSource
$context ) {
1157 global $wgThumbLimits;
1160 $pixels = $context->msg( 'unit-pixel' )->text();
1162 foreach ( $wgThumbLimits as $index => $size ) {
1163 $display = $size . $pixels;
1164 $ret[$display] = $index;
1171 * @param $signature string
1172 * @param $alldata array
1173 * @param $form HTMLForm
1174 * @return bool|string
1176 static function validateSignature( $signature, $alldata, $form ) {
1177 global $wgParser, $wgMaxSigChars;
1178 if ( mb_strlen( $signature ) > $wgMaxSigChars ) {
1179 return Xml
::element( 'span', array( 'class' => 'error' ),
1180 $form->msg( 'badsiglength' )->numParams( $wgMaxSigChars )->text() );
1181 } elseif ( isset( $alldata['fancysig'] ) &&
1182 $alldata['fancysig'] &&
1183 false === $wgParser->validateSig( $signature ) ) {
1184 return Xml
::element( 'span', array( 'class' => 'error' ), $form->msg( 'badsig' )->text() );
1191 * @param $signature string
1192 * @param $alldata array
1193 * @param $form HTMLForm
1196 static function cleanSignature( $signature, $alldata, $form ) {
1197 if ( isset( $alldata['fancysig'] ) && $alldata['fancysig'] ) {
1199 $signature = $wgParser->cleanSig( $signature );
1201 // When no fancy sig used, make sure ~{3,5} get removed.
1202 $signature = Parser
::cleanSigInSig( $signature );
1210 * @param $context IContextSource
1211 * @param $formClass string
1212 * @param $remove Array: array of items to remove
1215 static function getFormObject( $user, IContextSource
$context, $formClass = 'PreferencesForm', array $remove = array() ) {
1216 $formDescriptor = Preferences
::getPreferences( $user, $context );
1217 if ( count( $remove ) ) {
1218 $removeKeys = array_flip( $remove );
1219 $formDescriptor = array_diff_key( $formDescriptor, $removeKeys );
1223 * @var $htmlForm PreferencesForm
1225 $htmlForm = new $formClass( $formDescriptor, $context, 'prefs' );
1227 $htmlForm->setModifiedUser( $user );
1228 $htmlForm->setId( 'mw-prefs-form' );
1229 $htmlForm->setSubmitText( $context->msg( 'saveprefs' )->text() );
1230 # Used message keys: 'accesskey-preferences-save', 'tooltip-preferences-save'
1231 $htmlForm->setSubmitTooltip( 'preferences-save' );
1232 $htmlForm->setSubmitID( 'prefsubmit' );
1233 $htmlForm->setSubmitCallback( array( 'Preferences', 'tryFormSubmit' ) );
1241 static function getTimezoneOptions( IContextSource
$context ) {
1244 global $wgLocalTZoffset, $wgLocaltimezone;
1245 // Check that $wgLocalTZoffset is the same as $wgLocaltimezone
1246 if ( $wgLocalTZoffset == date( 'Z' ) / 60 ) {
1247 $server_tz_msg = $context->msg( 'timezoneuseserverdefault', $wgLocaltimezone )->text();
1249 $tzstring = sprintf( '%+03d:%02d', floor( $wgLocalTZoffset / 60 ), abs( $wgLocalTZoffset ) %
60 );
1250 $server_tz_msg = $context->msg( 'timezoneuseserverdefault', $tzstring )->text();
1252 $opt[$server_tz_msg] = "System|$wgLocalTZoffset";
1253 $opt[$context->msg( 'timezoneuseoffset' )->text()] = 'other';
1254 $opt[$context->msg( 'guesstimezone' )->text()] = 'guess';
1256 if ( function_exists( 'timezone_identifiers_list' ) ) {
1257 # Read timezone list
1258 $tzs = timezone_identifiers_list();
1261 $tzRegions = array();
1262 $tzRegions['Africa'] = $context->msg( 'timezoneregion-africa' )->text();
1263 $tzRegions['America'] = $context->msg( 'timezoneregion-america' )->text();
1264 $tzRegions['Antarctica'] = $context->msg( 'timezoneregion-antarctica' )->text();
1265 $tzRegions['Arctic'] = $context->msg( 'timezoneregion-arctic' )->text();
1266 $tzRegions['Asia'] = $context->msg( 'timezoneregion-asia' )->text();
1267 $tzRegions['Atlantic'] = $context->msg( 'timezoneregion-atlantic' )->text();
1268 $tzRegions['Australia'] = $context->msg( 'timezoneregion-australia' )->text();
1269 $tzRegions['Europe'] = $context->msg( 'timezoneregion-europe' )->text();
1270 $tzRegions['Indian'] = $context->msg( 'timezoneregion-indian' )->text();
1271 $tzRegions['Pacific'] = $context->msg( 'timezoneregion-pacific' )->text();
1272 asort( $tzRegions );
1274 $prefill = array_fill_keys( array_values( $tzRegions ), array() );
1275 $opt = array_merge( $opt, $prefill );
1277 $now = date_create( 'now' );
1279 foreach ( $tzs as $tz ) {
1280 $z = explode( '/', $tz, 2 );
1282 # timezone_identifiers_list() returns a number of
1283 # backwards-compatibility entries. This filters them out of the
1284 # list presented to the user.
1285 if ( count( $z ) != 2 ||
!array_key_exists( $z[0], $tzRegions ) ) {
1290 $z[0] = $tzRegions[$z[0]];
1292 $minDiff = floor( timezone_offset_get( timezone_open( $tz ), $now ) / 60 );
1294 $display = str_replace( '_', ' ', $z[0] . '/' . $z[1] );
1295 $value = "ZoneInfo|$minDiff|$tz";
1297 $opt[$z[0]][$display] = $value;
1308 static function filterIntval( $value, $alldata ){
1309 return intval( $value );
1317 static function filterTimezoneInput( $tz, $alldata ) {
1318 $data = explode( '|', $tz, 3 );
1319 switch ( $data[0] ) {
1324 $data = explode( ':', $tz, 2 );
1325 if ( count( $data ) == 2 ) {
1326 $data[0] = intval( $data[0] );
1327 $data[1] = intval( $data[1] );
1328 $minDiff = abs( $data[0] ) * 60 +
$data[1];
1329 if ( $data[0] < 0 ) $minDiff = - $minDiff;
1331 $minDiff = intval( $data[0] ) * 60;
1334 # Max is +14:00 and min is -12:00, see:
1335 # http://en.wikipedia.org/wiki/Timezone
1336 $minDiff = min( $minDiff, 840 ); # 14:00
1337 $minDiff = max( $minDiff, - 720 ); # -12:00
1338 return 'Offset|' . $minDiff;
1344 * @param $form PreferencesForm
1345 * @param $entryPoint string
1346 * @return bool|Status|string
1348 static function tryFormSubmit( $formData, $form, $entryPoint = 'internal' ) {
1349 global $wgHiddenPrefs;
1351 $user = $form->getModifiedUser();
1355 foreach ( array_keys( $formData ) as $name ) {
1356 if ( isset( self
::$saveFilters[$name] ) ) {
1358 call_user_func( self
::$saveFilters[$name], $formData[$name], $formData );
1362 // Stuff that shouldn't be saved as a preference.
1363 $saveBlacklist = array(
1368 // Fortunately, the realname field is MUCH simpler
1369 if ( !in_array( 'realname', $wgHiddenPrefs ) ) {
1370 $realName = $formData['realname'];
1371 $user->setRealName( $realName );
1374 foreach ( $saveBlacklist as $b ) {
1375 unset( $formData[$b] );
1378 # If users have saved a value for a preference which has subsequently been disabled
1379 # via $wgHiddenPrefs, we don't want to destroy that setting in case the preference
1380 # is subsequently re-enabled
1381 # TODO: maintenance script to actually delete these
1382 foreach( $wgHiddenPrefs as $pref ){
1383 # If the user has not set a non-default value here, the default will be returned
1384 # and subsequently discarded
1385 $formData[$pref] = $user->getOption( $pref, null, true );
1388 // Keeps old preferences from interfering due to back-compat
1390 $user->resetOptions();
1392 foreach ( $formData as $key => $value ) {
1393 $user->setOption( $key, $value );
1396 $user->saveSettings();
1403 * @param $form PreferencesForm
1406 public static function tryUISubmit( $formData, $form ) {
1407 $res = self
::tryFormSubmit( $formData, $form, 'ui' );
1410 $urlOptions = array( 'success' => 1 );
1412 if ( $res === 'eauth' ) {
1413 $urlOptions['eauth'] = 1;
1416 $urlOptions +
= $form->getExtraSuccessRedirectParameters();
1418 $url = $form->getTitle()->getFullURL( $urlOptions );
1420 $form->getContext()->getOutput()->redirect( $url );
1423 return Status
::newGood();
1427 * Try to set a user's email address.
1428 * This does *not* try to validate the address.
1429 * Caller is responsible for checking $wgAuth.
1431 * @param $newaddr string New email address
1432 * @return Array (true on success or Status on failure, info string)
1434 public static function trySetUserEmail( User
$user, $newaddr ) {
1435 global $wgEnableEmail, $wgEmailAuthentication;
1438 if ( $wgEnableEmail ) {
1439 $oldaddr = $user->getEmail();
1440 if ( ( $newaddr != '' ) && ( $newaddr != $oldaddr ) ) {
1441 # The user has supplied a new email address on the login page
1442 # new behaviour: set this new emailaddr from login-page into user database record
1443 $user->setEmail( $newaddr );
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' );
1454 } elseif ( $newaddr != $oldaddr ) { // if the address is the same, don't change it
1455 $user->setEmail( $newaddr );
1457 if ( $oldaddr != $newaddr ) {
1458 wfRunHooks( 'PrefsEmailAudit', array( $user, $oldaddr, $newaddr ) );
1462 return array( true, $info );
1466 * @deprecated in 1.19; will be removed in 1.20.
1470 public static function loadOldSearchNs( $user ) {
1471 wfDeprecated( __METHOD__
, '1.19' );
1473 $searchableNamespaces = SearchEngine
::searchableNamespaces();
1474 // Back compat with old format
1477 foreach ( $searchableNamespaces as $ns => $name ) {
1478 if ( $user->getOption( 'searchNs' . $ns ) ) {
1487 /** Some tweaks to allow js prefs to work */
1488 class PreferencesForm
extends HTMLForm
{
1489 // Override default value from HTMLForm
1490 protected $mSubSectionBeforeFields = false;
1492 private $modifiedUser;
1497 public function setModifiedUser( $user ) {
1498 $this->modifiedUser
= $user;
1504 public function getModifiedUser() {
1505 if ( $this->modifiedUser
=== null ) {
1506 return $this->getUser();
1508 return $this->modifiedUser
;
1513 * Get extra parameters for the query string when redirecting after
1518 public function getExtraSuccessRedirectParameters() {
1523 * @param $html string
1526 function wrapForm( $html ) {
1527 $html = Xml
::tags( 'div', array( 'id' => 'preferences' ), $html );
1529 return parent
::wrapForm( $html );
1535 function getButtons() {
1536 $html = parent
::getButtons();
1538 $t = SpecialPage
::getTitleFor( 'Preferences', 'reset' );
1540 $html .= "\n" . Linker
::link( $t, $this->msg( 'restoreprefs' )->escaped() );
1542 $html = Xml
::tags( 'div', array( 'class' => 'mw-prefs-buttons' ), $html );
1548 * @param $data array
1551 function filterDataForSubmit( $data ) {
1552 // Support for separating MultiSelect preferences into multiple preferences
1553 // Due to lack of array support.
1554 foreach ( $this->mFlatFields
as $fieldname => $field ) {
1555 $info = $field->mParams
;
1556 if ( $field instanceof HTMLMultiSelectField
) {
1557 $options = HTMLFormField
::flattenOptions( $info['options'] );
1558 $prefix = isset( $info['prefix'] ) ?
$info['prefix'] : $fieldname;
1560 foreach ( $options as $opt ) {
1561 $data["$prefix$opt"] = in_array( $opt, $data[$fieldname] );
1564 unset( $data[$fieldname] );
1572 * Get the whole body of the form.
1575 function getBody() {
1576 return $this->displaySection( $this->mFieldTree
, '', 'mw-prefsection-' );
1580 * Get the <legend> for a given section key. Normally this is the
1581 * prefs-$key message but we'll allow extensions to override it.
1582 * @param $key string
1585 function getLegend( $key ) {
1586 $legend = parent
::getLegend( $key );
1587 wfRunHooks( 'PreferencesGetLegend', array( $this, $key, &$legend ) );