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 'rows' => array( 'Preferences', 'filterIntval' ),
33 'rclimit' => array( 'Preferences', 'filterIntval' ),
34 'wllimit' => array( 'Preferences', 'filterIntval' ),
35 'searchlimit' => array( 'Preferences', 'filterIntval' ),
41 * @param $context IContextSource
44 static function getPreferences( $user, IContextSource
$context ) {
45 if ( self
::$defaultPreferences ) {
46 return self
::$defaultPreferences;
49 $defaultPreferences = array();
51 self
::profilePreferences( $user, $context, $defaultPreferences );
52 self
::skinPreferences( $user, $context, $defaultPreferences );
53 self
::filesPreferences( $user, $context, $defaultPreferences );
54 self
::datetimePreferences( $user, $context, $defaultPreferences );
55 self
::renderingPreferences( $user, $context, $defaultPreferences );
56 self
::editingPreferences( $user, $context, $defaultPreferences );
57 self
::rcPreferences( $user, $context, $defaultPreferences );
58 self
::watchlistPreferences( $user, $context, $defaultPreferences );
59 self
::searchPreferences( $user, $context, $defaultPreferences );
60 self
::miscPreferences( $user, $context, $defaultPreferences );
62 wfRunHooks( 'GetPreferences', array( $user, &$defaultPreferences ) );
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] );
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]
81 // If it validates, set it as the default
82 if ( isset( $info['default'] ) ) {
83 // Already set, no problem
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;
91 throw new MWException( "Global default '$globalDefault' is invalid for field $name" );
95 self
::$defaultPreferences = $defaultPreferences;
97 return $defaultPreferences;
101 * Pull option from a user account. Handles stuff like array-type preferences.
106 * @return array|String
108 static function getOptionFromUser( $name, $info, $user ) {
109 $val = $user->getOption( $name );
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;
118 foreach ( $options as $value ) {
119 if ( $user->getOption( "$prefix$value" ) ) {
130 * @param $context IContextSource
131 * @param $defaultPreferences
134 static function profilePreferences( $user, IContextSource
$context, &$defaultPreferences ) {
135 global $wgAuth, $wgContLang, $wgParser, $wgCookieExpiration, $wgLanguageCode,
136 $wgDisableTitleConversion, $wgDisableLangConversion, $wgMaxSigChars,
137 $wgEnableEmail, $wgEmailConfirmToEdit, $wgEnableUserEmail, $wgEmailAuthentication,
138 $wgEnotifWatchlist, $wgEnotifUserTalk, $wgEnotifRevealEditorAddress;
140 ## User info #####################################
142 $defaultPreferences['username'] = array(
144 'label-message' => 'username',
145 'default' => $user->getName(),
146 'section' => 'personal/info',
149 $defaultPreferences['userid'] = array(
151 'label-message' => 'uid',
152 'default' => $user->getId(),
153 'section' => 'personal/info',
156 # Get groups to which the user belongs
157 $userEffectiveGroups = $user->getEffectiveGroups();
158 $userGroups = $userMembers = array();
159 foreach ( $userEffectiveGroups as $ueg ) {
161 // Skip the default * group, seems useless here
164 $groupName = User
::getGroupName( $ueg );
165 $userGroups[] = User
::makeGroupLinkHTML( $ueg, $groupName );
167 $memberName = User
::getGroupMember( $ueg, $user->getName() );
168 $userMembers[] = User
::makeGroupLinkHTML( $ueg, $memberName );
170 asort( $userGroups );
171 asort( $userMembers );
173 $lang = $context->getLanguage();
175 $defaultPreferences['usergroups'] = array(
177 'label' => $context->msg( 'prefs-memberingroups' )->numParams(
178 count( $userGroups ) )->parse(),
179 'default' => $context->msg( 'prefs-memberingroups-type',
180 $lang->commaList( $userGroups ),
181 $lang->commaList( $userMembers )
184 'section' => 'personal/info',
187 $defaultPreferences['editcount'] = array(
189 'label-message' => 'prefs-edits',
190 'default' => $lang->formatNum( $user->getEditCount() ),
191 'section' => 'personal/info',
194 if ( $user->getRegistration() ) {
195 $displayUser = $context->getUser();
196 $userRegistration = $user->getRegistration();
197 $defaultPreferences['registrationdate'] = array(
199 'label-message' => 'prefs-registration',
200 'default' => $context->msg(
201 'prefs-registration-date-time',
202 $lang->userTimeAndDate( $userRegistration, $displayUser ),
203 $lang->userDate( $userRegistration, $displayUser ),
204 $lang->userTime( $userRegistration, $displayUser )
206 'section' => 'personal/info',
210 // Actually changeable stuff
211 $defaultPreferences['realname'] = array(
212 'type' => $wgAuth->allowPropChange( 'realname' ) ?
'text' : 'info',
213 'default' => $user->getRealName(),
214 'section' => 'personal/info',
215 'label-message' => 'yourrealname',
216 'help-message' => 'prefs-help-realname',
219 $defaultPreferences['gender'] = array(
221 'section' => 'personal/info',
223 $context->msg( 'gender-male' )->text() => 'male',
224 $context->msg( 'gender-female' )->text() => 'female',
225 $context->msg( 'gender-unknown' )->text() => 'unknown',
227 'label-message' => 'yourgender',
228 'help-message' => 'prefs-help-gender',
231 if ( $wgAuth->allowPasswordChange() ) {
232 $link = Linker
::link( SpecialPage
::getTitleFor( 'ChangePassword' ),
233 $context->msg( 'prefs-resetpass' )->escaped(), array(),
234 array( 'returnto' => SpecialPage
::getTitleFor( 'Preferences' ) ) );
236 $defaultPreferences['password'] = array(
240 'label-message' => 'yourpassword',
241 'section' => 'personal/info',
244 if ( $wgCookieExpiration > 0 ) {
245 $defaultPreferences['rememberpassword'] = array(
247 'label' => $context->msg( 'tog-rememberpassword' )->numParams(
248 ceil( $wgCookieExpiration / ( 3600 * 24 ) ) )->text(),
249 'section' => 'personal/info',
254 $languages = Language
::getLanguageNames( false );
255 if ( !array_key_exists( $wgLanguageCode, $languages ) ) {
256 $languages[$wgLanguageCode] = $wgLanguageCode;
261 foreach ( $languages as $code => $name ) {
262 $display = wfBCP47( $code ) . ' - ' . $name;
263 $options[$display] = $code;
265 $defaultPreferences['language'] = array(
267 'section' => 'personal/i18n',
268 'options' => $options,
269 'label-message' => 'yourlanguage',
272 /* see if there are multiple language variants to choose from*/
273 $variantArray = array();
274 if ( !$wgDisableLangConversion ) {
275 $variants = $wgContLang->getVariants();
277 foreach ( $variants as $v ) {
278 $v = str_replace( '_', '-', strtolower( $v ) );
279 $variantArray[$v] = $wgContLang->getVariantname( $v, false );
283 foreach ( $variantArray as $code => $name ) {
284 $display = wfBCP47( $code ) . ' - ' . $name;
285 $options[$display] = $code;
288 if ( count( $variantArray ) > 1 ) {
289 $defaultPreferences['variant'] = array(
290 'label-message' => 'yourvariant',
292 'options' => $options,
293 'section' => 'personal/i18n',
294 'help-message' => 'prefs-help-variant',
299 if ( count( $variantArray ) > 1 && !$wgDisableLangConversion && !$wgDisableTitleConversion ) {
300 $defaultPreferences['noconvertlink'] =
303 'section' => 'personal/i18n',
304 'label-message' => 'tog-noconvertlink',
308 // show a preview of the old signature first
309 $oldsigWikiText = $wgParser->preSaveTransform( "~~~", $context->getTitle(), $user, ParserOptions
::newFromContext( $context ) );
310 $oldsigHTML = $context->getOutput()->parseInline( $oldsigWikiText, true, true );
311 $defaultPreferences['oldsig'] = array(
314 'label-message' => 'tog-oldsig',
315 'default' => $oldsigHTML,
316 'section' => 'personal/signature',
318 $defaultPreferences['nickname'] = array(
319 'type' => $wgAuth->allowPropChange( 'nickname' ) ?
'text' : 'info',
320 'maxlength' => $wgMaxSigChars,
321 'label-message' => 'yournick',
322 'validation-callback' => array( 'Preferences', 'validateSignature' ),
323 'section' => 'personal/signature',
324 'filter-callback' => array( 'Preferences', 'cleanSignature' ),
326 $defaultPreferences['fancysig'] = array(
328 'label-message' => 'tog-fancysig',
329 'help-message' => 'prefs-help-signature', // show general help about signature at the bottom of the section
330 'section' => 'personal/signature'
335 if ( $wgEnableEmail ) {
336 $helpMessages[] = $wgEmailConfirmToEdit
337 ?
'prefs-help-email-required'
338 : 'prefs-help-email' ;
340 if( $wgEnableUserEmail ) {
341 // additional messages when users can send email to each other
342 $helpMessages[] = 'prefs-help-email-others';
345 $link = Linker
::link(
346 SpecialPage
::getTitleFor( 'ChangeEmail' ),
347 $context->msg( $user->getEmail() ?
'prefs-changeemail' : 'prefs-setemail' )->escaped(),
349 array( 'returnto' => SpecialPage
::getTitleFor( 'Preferences' ) ) );
351 $emailAddress = $user->getEmail() ?
htmlspecialchars( $user->getEmail() ) : '';
352 if ( $wgAuth->allowPropChange( 'emailaddress' ) ) {
353 $emailAddress .= $emailAddress == '' ?
$link : " ($link)";
356 $defaultPreferences['emailaddress'] = array(
359 'default' => $emailAddress,
360 'label-message' => 'youremail',
361 'section' => 'personal/email',
364 $disableEmailPrefs = false;
366 if ( $wgEmailAuthentication ) {
367 if ( $user->getEmail() ) {
368 if ( $user->getEmailAuthenticationTimestamp() ) {
369 // date and time are separate parameters to facilitate localisation.
370 // $time is kept for backward compat reasons.
371 // 'emailauthenticated' is also used in SpecialConfirmemail.php
372 $displayUser = $context->getUser();
373 $emailTimestamp = $user->getEmailAuthenticationTimestamp();
374 $time = $lang->userTimeAndDate( $emailTimestamp, $displayUser );
375 $d = $lang->userDate( $emailTimestamp, $displayUser );
376 $t = $lang->userTime( $emailTimestamp, $displayUser );
377 $emailauthenticated = $context->msg( 'emailauthenticated',
378 $time, $d, $t )->parse() . '<br />';
379 $disableEmailPrefs = false;
381 $disableEmailPrefs = true;
382 $emailauthenticated = $context->msg( 'emailnotauthenticated' )->parse() . '<br />' .
384 SpecialPage
::getTitleFor( 'Confirmemail' ),
385 $context->msg( 'emailconfirmlink' )->escaped()
389 $disableEmailPrefs = true;
390 $emailauthenticated = $context->msg( 'noemailprefs' )->escaped();
393 $defaultPreferences['emailauthentication'] = array(
396 'section' => 'personal/email',
397 'label-message' => 'prefs-emailconfirm-label',
398 'default' => $emailauthenticated,
403 if ( $wgEnableUserEmail && $user->isAllowed( 'sendemail' ) ) {
404 $defaultPreferences['disablemail'] = array(
407 'section' => 'personal/email',
408 'label-message' => 'allowemail',
409 'disabled' => $disableEmailPrefs,
411 $defaultPreferences['ccmeonemails'] = array(
413 'section' => 'personal/email',
414 'label-message' => 'tog-ccmeonemails',
415 'disabled' => $disableEmailPrefs,
419 if ( $wgEnotifWatchlist ) {
420 $defaultPreferences['enotifwatchlistpages'] = array(
422 'section' => 'personal/email',
423 'label-message' => 'tog-enotifwatchlistpages',
424 'disabled' => $disableEmailPrefs,
427 if ( $wgEnotifUserTalk ) {
428 $defaultPreferences['enotifusertalkpages'] = array(
430 'section' => 'personal/email',
431 'label-message' => 'tog-enotifusertalkpages',
432 'disabled' => $disableEmailPrefs,
435 if ( $wgEnotifUserTalk ||
$wgEnotifWatchlist ) {
436 $defaultPreferences['enotifminoredits'] = array(
438 'section' => 'personal/email',
439 'label-message' => 'tog-enotifminoredits',
440 'disabled' => $disableEmailPrefs,
443 if ( $wgEnotifRevealEditorAddress ) {
444 $defaultPreferences['enotifrevealaddr'] = array(
446 'section' => 'personal/email',
447 'label-message' => 'tog-enotifrevealaddr',
448 'disabled' => $disableEmailPrefs,
457 * @param $context IContextSource
458 * @param $defaultPreferences
461 static function skinPreferences( $user, IContextSource
$context, &$defaultPreferences ) {
462 ## Skin #####################################
463 global $wgAllowUserCss, $wgAllowUserJs;
465 $defaultPreferences['skin'] = array(
467 'options' => self
::generateSkinOptions( $user, $context ),
469 'section' => 'rendering/skin',
472 # Create links to user CSS/JS pages for all skins
473 # This code is basically copied from generateSkinOptions(). It'd
474 # be nice to somehow merge this back in there to avoid redundancy.
475 if ( $wgAllowUserCss ||
$wgAllowUserJs ) {
476 $linkTools = array();
478 if ( $wgAllowUserCss ) {
479 $cssPage = Title
::makeTitleSafe( NS_USER
, $user->getName() . '/common.css' );
480 $linkTools[] = Linker
::link( $cssPage, $context->msg( 'prefs-custom-css' )->escaped() );
483 if ( $wgAllowUserJs ) {
484 $jsPage = Title
::makeTitleSafe( NS_USER
, $user->getName() . '/common.js' );
485 $linkTools[] = Linker
::link( $jsPage, $context->msg( 'prefs-custom-js' )->escaped() );
488 $defaultPreferences['commoncssjs'] = array(
491 'default' => $context->getLanguage()->pipeList( $linkTools ),
492 'label-message' => 'prefs-common-css-js',
493 'section' => 'rendering/skin',
497 $selectedSkin = $user->getOption( 'skin' );
498 if ( in_array( $selectedSkin, array( 'cologneblue', 'standard' ) ) ) {
499 $settings = array_flip( $context->getLanguage()->getQuickbarSettings() );
501 $defaultPreferences['quickbar'] = array(
503 'options' => $settings,
504 'section' => 'rendering/skin',
505 'label-message' => 'qbsettings',
512 * @param $context IContextSource
513 * @param $defaultPreferences Array
515 static function filesPreferences( $user, IContextSource
$context, &$defaultPreferences ) {
516 ## Files #####################################
517 $defaultPreferences['imagesize'] = array(
519 'options' => self
::getImageSizes( $context ),
520 'label-message' => 'imagemaxsize',
521 'section' => 'rendering/files',
523 $defaultPreferences['thumbsize'] = array(
525 'options' => self
::getThumbSizes( $context ),
526 'label-message' => 'thumbsize',
527 'section' => 'rendering/files',
533 * @param $context IContextSource
534 * @param $defaultPreferences
537 static function datetimePreferences( $user, IContextSource
$context, &$defaultPreferences ) {
538 ## Date and time #####################################
539 $dateOptions = self
::getDateOptions( $context );
540 if ( $dateOptions ) {
541 $defaultPreferences['date'] = array(
543 'options' => $dateOptions,
545 'section' => 'datetime/dateformat',
550 $now = wfTimestampNow();
551 $lang = $context->getLanguage();
552 $nowlocal = Xml
::element( 'span', array( 'id' => 'wpLocalTime' ),
553 $lang->time( $now, true ) );
554 $nowserver = $lang->time( $now, false ) .
555 Html
::hidden( 'wpServerTime', (int)substr( $now, 8, 2 ) * 60 +
(int)substr( $now, 10, 2 ) );
557 $defaultPreferences['nowserver'] = array(
560 'label-message' => 'servertime',
561 'default' => $nowserver,
562 'section' => 'datetime/timeoffset',
565 $defaultPreferences['nowlocal'] = array(
568 'label-message' => 'localtime',
569 'default' => $nowlocal,
570 'section' => 'datetime/timeoffset',
573 // Grab existing pref.
574 $tzOffset = $user->getOption( 'timecorrection' );
575 $tz = explode( '|', $tzOffset, 3 );
577 $tzOptions = self
::getTimezoneOptions( $context );
579 $tzSetting = $tzOffset;
580 if ( count( $tz ) > 1 && $tz[0] == 'Offset' ) {
582 $tzSetting = sprintf( '%+03d:%02d', floor( $minDiff / 60 ), abs( $minDiff ) %
60 );
583 } elseif ( count( $tz ) > 1 && $tz[0] == 'ZoneInfo' &&
584 !in_array( $tzOffset, HTMLFormField
::flattenOptions( $tzOptions ) ) )
586 # Timezone offset can vary with DST
587 $userTZ = timezone_open( $tz[2] );
588 if ( $userTZ !== false ) {
589 $minDiff = floor( timezone_offset_get( $userTZ, date_create( 'now' ) ) / 60 );
590 $tzSetting = "ZoneInfo|$minDiff|{$tz[2]}";
594 $defaultPreferences['timecorrection'] = array(
595 'class' => 'HTMLSelectOrOtherField',
596 'label-message' => 'timezonelegend',
597 'options' => $tzOptions,
598 'default' => $tzSetting,
600 'section' => 'datetime/timeoffset',
606 * @param $context IContextSource
607 * @param $defaultPreferences Array
609 static function renderingPreferences( $user, IContextSource
$context, &$defaultPreferences ) {
610 ## Page Rendering ##############################
611 global $wgAllowUserCssPrefs;
612 if ( $wgAllowUserCssPrefs ) {
613 $defaultPreferences['underline'] = array(
616 $context->msg( 'underline-never' )->text() => 0,
617 $context->msg( 'underline-always' )->text() => 1,
618 $context->msg( 'underline-default' )->text() => 2,
620 'label-message' => 'tog-underline',
621 'section' => 'rendering/advancedrendering',
625 $stubThresholdValues = array( 50, 100, 500, 1000, 2000, 5000, 10000 );
626 $stubThresholdOptions = array( $context->msg( 'stub-threshold-disabled' )->text() => 0 );
627 foreach ( $stubThresholdValues as $value ) {
628 $stubThresholdOptions[$context->msg( 'size-bytes', $value )->text()] = $value;
631 $defaultPreferences['stubthreshold'] = array(
632 'type' => 'selectorother',
633 'section' => 'rendering/advancedrendering',
634 'options' => $stubThresholdOptions,
636 'label' => $context->msg( 'stub-threshold' )->text(), // Raw HTML message. Yay?
639 if ( $wgAllowUserCssPrefs ) {
640 $defaultPreferences['highlightbroken'] = array(
642 'section' => 'rendering/advancedrendering',
643 'label' => $context->msg( 'tog-highlightbroken' )->text(), // Raw HTML
645 $defaultPreferences['showtoc'] = array(
647 'section' => 'rendering/advancedrendering',
648 'label-message' => 'tog-showtoc',
651 $defaultPreferences['nocache'] = array(
653 'label-message' => 'tog-nocache',
654 'section' => 'rendering/advancedrendering',
656 $defaultPreferences['showhiddencats'] = array(
658 'section' => 'rendering/advancedrendering',
659 'label-message' => 'tog-showhiddencats'
661 $defaultPreferences['showjumplinks'] = array(
663 'section' => 'rendering/advancedrendering',
664 'label-message' => 'tog-showjumplinks',
667 if ( $wgAllowUserCssPrefs ) {
668 $defaultPreferences['justify'] = array(
670 'section' => 'rendering/advancedrendering',
671 'label-message' => 'tog-justify',
675 $defaultPreferences['numberheadings'] = array(
677 'section' => 'rendering/advancedrendering',
678 'label-message' => 'tog-numberheadings',
684 * @param $context IContextSource
685 * @param $defaultPreferences Array
687 static function editingPreferences( $user, IContextSource
$context, &$defaultPreferences ) {
688 global $wgUseExternalEditor, $wgAllowUserCssPrefs;
690 ## Editing #####################################
692 $defaultPreferences['rows'] = array(
694 'label-message' => 'rows',
695 'section' => 'editing/textboxsize',
700 if ( $wgAllowUserCssPrefs ) {
701 $defaultPreferences['editfont'] = array(
703 'section' => 'editing/advancedediting',
704 'label-message' => 'editfont-style',
706 $context->msg( 'editfont-default' )->text() => 'default',
707 $context->msg( 'editfont-monospace' )->text() => 'monospace',
708 $context->msg( 'editfont-sansserif' )->text() => 'sans-serif',
709 $context->msg( 'editfont-serif' )->text() => 'serif',
713 $defaultPreferences['previewontop'] = array(
715 'section' => 'editing/advancedediting',
716 'label-message' => 'tog-previewontop',
718 $defaultPreferences['previewonfirst'] = array(
720 'section' => 'editing/advancedediting',
721 'label-message' => 'tog-previewonfirst',
724 if ( $wgAllowUserCssPrefs ) {
725 $defaultPreferences['editsection'] = array(
727 'section' => 'editing/advancedediting',
728 'label-message' => 'tog-editsection',
731 $defaultPreferences['editsectiononrightclick'] = array(
733 'section' => 'editing/advancedediting',
734 'label-message' => 'tog-editsectiononrightclick',
736 $defaultPreferences['editondblclick'] = array(
738 'section' => 'editing/advancedediting',
739 'label-message' => 'tog-editondblclick',
741 $defaultPreferences['showtoolbar'] = array(
743 'section' => 'editing/advancedediting',
744 'label-message' => 'tog-showtoolbar',
747 if ( $user->isAllowed( 'minoredit' ) ) {
748 $defaultPreferences['minordefault'] = array(
750 'section' => 'editing/advancedediting',
751 'label-message' => 'tog-minordefault',
755 if ( $wgUseExternalEditor ) {
756 $defaultPreferences['externaleditor'] = array(
758 'section' => 'editing/advancedediting',
759 'label-message' => 'tog-externaleditor',
761 $defaultPreferences['externaldiff'] = array(
763 'section' => 'editing/advancedediting',
764 'label-message' => 'tog-externaldiff',
768 $defaultPreferences['forceeditsummary'] = array(
770 'section' => 'editing/advancedediting',
771 'label-message' => 'tog-forceeditsummary',
775 $defaultPreferences['uselivepreview'] = array(
777 'section' => 'editing/advancedediting',
778 'label-message' => 'tog-uselivepreview',
784 * @param $context IContextSource
785 * @param $defaultPreferences Array
787 static function rcPreferences( $user, IContextSource
$context, &$defaultPreferences ) {
788 global $wgRCMaxAge, $wgRCShowWatchingUsers;
790 ## RecentChanges #####################################
791 $defaultPreferences['rcdays'] = array(
793 'label-message' => 'recentchangesdays',
794 'section' => 'rc/displayrc',
796 'max' => ceil( $wgRCMaxAge / ( 3600 * 24 ) ),
797 'help' => $context->msg( 'recentchangesdays-max' )->numParams(
798 ceil( $wgRCMaxAge / ( 3600 * 24 ) ) )->text()
800 $defaultPreferences['rclimit'] = array(
802 'label-message' => 'recentchangescount',
803 'help-message' => 'prefs-help-recentchangescount',
804 'section' => 'rc/displayrc',
806 $defaultPreferences['usenewrc'] = array(
808 'label-message' => 'tog-usenewrc',
809 'section' => 'rc/advancedrc',
811 $defaultPreferences['hideminor'] = array(
813 'label-message' => 'tog-hideminor',
814 'section' => 'rc/advancedrc',
817 if ( $user->useRCPatrol() ) {
818 $defaultPreferences['hidepatrolled'] = array(
820 'section' => 'rc/advancedrc',
821 'label-message' => 'tog-hidepatrolled',
823 $defaultPreferences['newpageshidepatrolled'] = array(
825 'section' => 'rc/advancedrc',
826 'label-message' => 'tog-newpageshidepatrolled',
830 if ( $wgRCShowWatchingUsers ) {
831 $defaultPreferences['shownumberswatching'] = array(
833 'section' => 'rc/advancedrc',
834 'label-message' => 'tog-shownumberswatching',
841 * @param $context IContextSource
842 * @param $defaultPreferences
844 static function watchlistPreferences( $user, IContextSource
$context, &$defaultPreferences ) {
845 global $wgUseRCPatrol, $wgEnableAPI, $wgRCMaxAge;
847 $watchlistdaysMax = ceil( $wgRCMaxAge / ( 3600 * 24 ) );
849 ## Watchlist #####################################
850 $defaultPreferences['watchlistdays'] = array(
853 'max' => $watchlistdaysMax,
854 'section' => 'watchlist/displaywatchlist',
855 'help' => $context->msg( 'prefs-watchlist-days-max' )->numParams(
856 $watchlistdaysMax )->text(),
857 'label-message' => 'prefs-watchlist-days',
859 $defaultPreferences['wllimit'] = array(
863 'label-message' => 'prefs-watchlist-edits',
864 'help' => $context->msg( 'prefs-watchlist-edits-max' )->escaped(),
865 'section' => 'watchlist/displaywatchlist',
867 $defaultPreferences['extendwatchlist'] = array(
869 'section' => 'watchlist/advancedwatchlist',
870 'label-message' => 'tog-extendwatchlist',
872 $defaultPreferences['watchlisthideminor'] = array(
874 'section' => 'watchlist/advancedwatchlist',
875 'label-message' => 'tog-watchlisthideminor',
877 $defaultPreferences['watchlisthidebots'] = array(
879 'section' => 'watchlist/advancedwatchlist',
880 'label-message' => 'tog-watchlisthidebots',
882 $defaultPreferences['watchlisthideown'] = array(
884 'section' => 'watchlist/advancedwatchlist',
885 'label-message' => 'tog-watchlisthideown',
887 $defaultPreferences['watchlisthideanons'] = array(
889 'section' => 'watchlist/advancedwatchlist',
890 'label-message' => 'tog-watchlisthideanons',
892 $defaultPreferences['watchlisthideliu'] = array(
894 'section' => 'watchlist/advancedwatchlist',
895 'label-message' => 'tog-watchlisthideliu',
898 if ( $wgUseRCPatrol ) {
899 $defaultPreferences['watchlisthidepatrolled'] = array(
901 'section' => 'watchlist/advancedwatchlist',
902 'label-message' => 'tog-watchlisthidepatrolled',
906 if ( $wgEnableAPI ) {
907 # Some random gibberish as a proposed default
908 $hash = sha1( mt_rand() . microtime( true ) );
910 $defaultPreferences['watchlisttoken'] = array(
912 'section' => 'watchlist/advancedwatchlist',
913 'label-message' => 'prefs-watchlist-token',
914 'help' => $context->msg( 'prefs-help-watchlist-token', $hash )->escaped()
919 'edit' => 'watchdefault',
920 'move' => 'watchmoves',
921 'delete' => 'watchdeletion'
925 if ( $user->isAllowed( 'createpage' ) ||
$user->isAllowed( 'createtalk' ) ) {
926 $watchTypes['read'] = 'watchcreations';
929 foreach ( $watchTypes as $action => $pref ) {
930 if ( $user->isAllowed( $action ) ) {
931 $defaultPreferences[$pref] = array(
933 'section' => 'watchlist/advancedwatchlist',
934 'label-message' => "tog-$pref",
942 * @param $context IContextSource
943 * @param $defaultPreferences Array
945 static function searchPreferences( $user, IContextSource
$context, &$defaultPreferences ) {
946 global $wgContLang, $wgEnableMWSuggest, $wgVectorUseSimpleSearch;
948 ## Search #####################################
949 $defaultPreferences['searchlimit'] = array(
951 'label-message' => 'resultsperpage',
952 'section' => 'searchoptions/displaysearchoptions',
956 if ( $wgEnableMWSuggest ) {
957 $defaultPreferences['disablesuggest'] = array(
959 'label-message' => 'mwsuggest-disable',
960 'section' => 'searchoptions/displaysearchoptions',
964 if ( $wgVectorUseSimpleSearch ) {
965 $defaultPreferences['vector-simplesearch'] = array(
967 'label-message' => 'vector-simplesearch-preference',
968 'section' => 'searchoptions/displaysearchoptions'
972 $defaultPreferences['searcheverything'] = array(
974 'label-message' => 'searcheverything-enable',
975 'section' => 'searchoptions/advancedsearchoptions',
978 $nsOptions = array();
980 foreach ( $wgContLang->getNamespaces() as $ns => $name ) {
985 $displayNs = str_replace( '_', ' ', $name );
988 $displayNs = $context->msg( 'blanknamespace' )->text();
991 $displayNs = htmlspecialchars( $displayNs );
992 $nsOptions[$displayNs] = $ns;
995 $defaultPreferences['searchnamespaces'] = array(
996 'type' => 'multiselect',
997 'label-message' => 'defaultns',
998 'options' => $nsOptions,
999 'section' => 'searchoptions/advancedsearchoptions',
1000 'prefix' => 'searchNs',
1006 * @param $context IContextSource
1007 * @param $defaultPreferences Array
1009 static function miscPreferences( $user, IContextSource
$context, &$defaultPreferences ) {
1012 ## Misc #####################################
1013 $defaultPreferences['diffonly'] = array(
1015 'section' => 'misc/diffs',
1016 'label-message' => 'tog-diffonly',
1018 $defaultPreferences['norollbackdiff'] = array(
1020 'section' => 'misc/diffs',
1021 'label-message' => 'tog-norollbackdiff',
1024 // Stuff from Language::getExtraUserToggles()
1025 $toggles = $wgContLang->getExtraUserToggles();
1027 foreach ( $toggles as $toggle ) {
1028 $defaultPreferences[$toggle] = array(
1030 'section' => 'personal/i18n',
1031 'label-message' => "tog-$toggle",
1037 * @param $user User The User object
1038 * @param $context IContextSource
1039 * @return Array: text/links to display as key; $skinkey as value
1041 static function generateSkinOptions( $user, IContextSource
$context ) {
1042 global $wgDefaultSkin, $wgAllowUserCss, $wgAllowUserJs;
1045 $mptitle = Title
::newMainPage();
1046 $previewtext = $context->msg( 'skin-preview' )->text();
1048 # Only show members of Skin::getSkinNames() rather than
1049 # $skinNames (skins is all skin names from Language.php)
1050 $validSkinNames = Skin
::getUsableSkins();
1052 # Sort by UI skin name. First though need to update validSkinNames as sometimes
1053 # the skinkey & UI skinname differ (e.g. "standard" skinkey is "Classic" in the UI).
1054 foreach ( $validSkinNames as $skinkey => &$skinname ) {
1055 $msg = $context->msg( "skinname-{$skinkey}" );
1056 if ( $msg->exists() ) {
1057 $skinname = htmlspecialchars( $msg->text() );
1060 asort( $validSkinNames );
1062 foreach ( $validSkinNames as $skinkey => $sn ) {
1063 $linkTools = array();
1065 # Mark the default skin
1066 if ( $skinkey == $wgDefaultSkin ) {
1067 $linkTools[] = $context->msg( 'default' )->escaped();
1070 # Create preview link
1071 $mplink = htmlspecialchars( $mptitle->getLocalURL( "useskin=$skinkey" ) );
1072 $linkTools[] = "<a target='_blank' href=\"$mplink\">$previewtext</a>";
1074 # Create links to user CSS/JS pages
1075 if ( $wgAllowUserCss ) {
1076 $cssPage = Title
::makeTitleSafe( NS_USER
, $user->getName() . '/' . $skinkey . '.css' );
1077 $linkTools[] = Linker
::link( $cssPage, $context->msg( 'prefs-custom-css' )->escaped() );
1080 if ( $wgAllowUserJs ) {
1081 $jsPage = Title
::makeTitleSafe( NS_USER
, $user->getName() . '/' . $skinkey . '.js' );
1082 $linkTools[] = Linker
::link( $jsPage, $context->msg( 'prefs-custom-js' )->escaped() );
1085 $display = $sn . ' ' . $context->msg( 'parentheses', $context->getLanguage()->pipeList( $linkTools ) )->text();
1086 $ret[$display] = $skinkey;
1093 * @param $context IContextSource
1096 static function getDateOptions( IContextSource
$context ) {
1097 $lang = $context->getLanguage();
1098 $dateopts = $lang->getDatePreferences();
1103 if ( !in_array( 'default', $dateopts ) ) {
1104 $dateopts[] = 'default'; // Make sure default is always valid
1108 // KLUGE: site default might not be valid for user language
1109 global $wgDefaultUserOptions;
1110 if ( !in_array( $wgDefaultUserOptions['date'], $dateopts ) ) {
1111 $wgDefaultUserOptions['date'] = 'default';
1114 $epoch = wfTimestampNow();
1115 foreach ( $dateopts as $key ) {
1116 if ( $key == 'default' ) {
1117 $formatted = $context->msg( 'datedefault' )->escaped();
1119 $formatted = htmlspecialchars( $lang->timeanddate( $epoch, false, $key ) );
1121 $ret[$formatted] = $key;
1128 * @param $context IContextSource
1131 static function getImageSizes( IContextSource
$context ) {
1132 global $wgImageLimits;
1135 $pixels = $context->msg( 'unit-pixel' )->text();
1137 foreach ( $wgImageLimits as $index => $limits ) {
1138 $display = "{$limits[0]}×{$limits[1]}" . $pixels;
1139 $ret[$display] = $index;
1146 * @param $context IContextSource
1149 static function getThumbSizes( IContextSource
$context ) {
1150 global $wgThumbLimits;
1153 $pixels = $context->msg( 'unit-pixel' )->text();
1155 foreach ( $wgThumbLimits as $index => $size ) {
1156 $display = $size . $pixels;
1157 $ret[$display] = $index;
1164 * @param $signature string
1165 * @param $alldata array
1166 * @param $form HTMLForm
1167 * @return bool|string
1169 static function validateSignature( $signature, $alldata, $form ) {
1170 global $wgParser, $wgMaxSigChars;
1171 if ( mb_strlen( $signature ) > $wgMaxSigChars ) {
1172 return Xml
::element( 'span', array( 'class' => 'error' ),
1173 $form->msg( 'badsiglength' )->numParams( $wgMaxSigChars )->text() );
1174 } elseif ( isset( $alldata['fancysig'] ) &&
1175 $alldata['fancysig'] &&
1176 false === $wgParser->validateSig( $signature ) ) {
1177 return Xml
::element( 'span', array( 'class' => 'error' ), $form->msg( 'badsig' )->text() );
1184 * @param $signature string
1185 * @param $alldata array
1186 * @param $form HTMLForm
1189 static function cleanSignature( $signature, $alldata, $form ) {
1190 if ( isset( $alldata['fancysig'] ) && $alldata['fancysig'] ) {
1192 $signature = $wgParser->cleanSig( $signature );
1194 // When no fancy sig used, make sure ~{3,5} get removed.
1195 $signature = Parser
::cleanSigInSig( $signature );
1203 * @param $context IContextSource
1204 * @param $formClass string
1205 * @param $remove Array: array of items to remove
1208 static function getFormObject( $user, IContextSource
$context, $formClass = 'PreferencesForm', array $remove = array() ) {
1209 $formDescriptor = Preferences
::getPreferences( $user, $context );
1210 if ( count( $remove ) ) {
1211 $removeKeys = array_flip( $remove );
1212 $formDescriptor = array_diff_key( $formDescriptor, $removeKeys );
1214 $htmlForm = new $formClass( $formDescriptor, $context, 'prefs' );
1216 $htmlForm->setModifiedUser( $user );
1217 $htmlForm->setId( 'mw-prefs-form' );
1218 $htmlForm->setSubmitText( $context->msg( 'saveprefs' )->text() );
1219 # Used message keys: 'accesskey-preferences-save', 'tooltip-preferences-save'
1220 $htmlForm->setSubmitTooltip( 'preferences-save' );
1221 $htmlForm->setSubmitID( 'prefsubmit' );
1222 $htmlForm->setSubmitCallback( array( 'Preferences', 'tryFormSubmit' ) );
1230 static function getTimezoneOptions( IContextSource
$context ) {
1233 global $wgLocalTZoffset, $wgLocaltimezone;
1234 // Check that $wgLocalTZoffset is the same as $wgLocaltimezone
1235 if ( $wgLocalTZoffset == date( 'Z' ) / 60 ) {
1236 $server_tz_msg = $context->msg( 'timezoneuseserverdefault', $wgLocaltimezone )->text();
1238 $tzstring = sprintf( '%+03d:%02d', floor( $wgLocalTZoffset / 60 ), abs( $wgLocalTZoffset ) %
60 );
1239 $server_tz_msg = $context->msg( 'timezoneuseserverdefault', $tzstring )->text();
1241 $opt[$server_tz_msg] = "System|$wgLocalTZoffset";
1242 $opt[$context->msg( 'timezoneuseoffset' )->text()] = 'other';
1243 $opt[$context->msg( 'guesstimezone' )->text()] = 'guess';
1245 if ( function_exists( 'timezone_identifiers_list' ) ) {
1246 # Read timezone list
1247 $tzs = timezone_identifiers_list();
1250 $tzRegions = array();
1251 $tzRegions['Africa'] = $context->msg( 'timezoneregion-africa' )->text();
1252 $tzRegions['America'] = $context->msg( 'timezoneregion-america' )->text();
1253 $tzRegions['Antarctica'] = $context->msg( 'timezoneregion-antarctica' )->text();
1254 $tzRegions['Arctic'] = $context->msg( 'timezoneregion-arctic' )->text();
1255 $tzRegions['Asia'] = $context->msg( 'timezoneregion-asia' )->text();
1256 $tzRegions['Atlantic'] = $context->msg( 'timezoneregion-atlantic' )->text();
1257 $tzRegions['Australia'] = $context->msg( 'timezoneregion-australia' )->text();
1258 $tzRegions['Europe'] = $context->msg( 'timezoneregion-europe' )->text();
1259 $tzRegions['Indian'] = $context->msg( 'timezoneregion-indian' )->text();
1260 $tzRegions['Pacific'] = $context->msg( 'timezoneregion-pacific' )->text();
1261 asort( $tzRegions );
1263 $prefill = array_fill_keys( array_values( $tzRegions ), array() );
1264 $opt = array_merge( $opt, $prefill );
1266 $now = date_create( 'now' );
1268 foreach ( $tzs as $tz ) {
1269 $z = explode( '/', $tz, 2 );
1271 # timezone_identifiers_list() returns a number of
1272 # backwards-compatibility entries. This filters them out of the
1273 # list presented to the user.
1274 if ( count( $z ) != 2 ||
!array_key_exists( $z[0], $tzRegions ) ) {
1279 $z[0] = $tzRegions[$z[0]];
1281 $minDiff = floor( timezone_offset_get( timezone_open( $tz ), $now ) / 60 );
1283 $display = str_replace( '_', ' ', $z[0] . '/' . $z[1] );
1284 $value = "ZoneInfo|$minDiff|$tz";
1286 $opt[$z[0]][$display] = $value;
1297 static function filterIntval( $value, $alldata ){
1298 return intval( $value );
1306 static function filterTimezoneInput( $tz, $alldata ) {
1307 $data = explode( '|', $tz, 3 );
1308 switch ( $data[0] ) {
1313 $data = explode( ':', $tz, 2 );
1314 if ( count( $data ) == 2 ) {
1315 $data[0] = intval( $data[0] );
1316 $data[1] = intval( $data[1] );
1317 $minDiff = abs( $data[0] ) * 60 +
$data[1];
1318 if ( $data[0] < 0 ) $minDiff = - $minDiff;
1320 $minDiff = intval( $data[0] ) * 60;
1323 # Max is +14:00 and min is -12:00, see:
1324 # http://en.wikipedia.org/wiki/Timezone
1325 $minDiff = min( $minDiff, 840 ); # 14:00
1326 $minDiff = max( $minDiff, - 720 ); # -12:00
1327 return 'Offset|' . $minDiff;
1333 * @param $form HTMLForm
1334 * @param $entryPoint string
1335 * @return bool|Status|string
1337 static function tryFormSubmit( $formData, $form, $entryPoint = 'internal' ) {
1338 global $wgHiddenPrefs;
1340 $user = $form->getModifiedUser();
1344 foreach ( array_keys( $formData ) as $name ) {
1345 if ( isset( self
::$saveFilters[$name] ) ) {
1347 call_user_func( self
::$saveFilters[$name], $formData[$name], $formData );
1351 // Stuff that shouldn't be saved as a preference.
1352 $saveBlacklist = array(
1357 // Fortunately, the realname field is MUCH simpler
1358 if ( !in_array( 'realname', $wgHiddenPrefs ) ) {
1359 $realName = $formData['realname'];
1360 $user->setRealName( $realName );
1363 foreach ( $saveBlacklist as $b ) {
1364 unset( $formData[$b] );
1367 # If users have saved a value for a preference which has subsequently been disabled
1368 # via $wgHiddenPrefs, we don't want to destroy that setting in case the preference
1369 # is subsequently re-enabled
1370 # TODO: maintenance script to actually delete these
1371 foreach( $wgHiddenPrefs as $pref ){
1372 # If the user has not set a non-default value here, the default will be returned
1373 # and subsequently discarded
1374 $formData[$pref] = $user->getOption( $pref, null, true );
1377 // Keeps old preferences from interfering due to back-compat
1379 $user->resetOptions();
1381 foreach ( $formData as $key => $value ) {
1382 $user->setOption( $key, $value );
1385 $user->saveSettings();
1394 public static function tryUISubmit( $formData, $form ) {
1395 $res = self
::tryFormSubmit( $formData, $form, 'ui' );
1398 $urlOptions = array( 'success' => 1 );
1400 if ( $res === 'eauth' ) {
1401 $urlOptions['eauth'] = 1;
1404 $urlOptions +
= $form->getExtraSuccessRedirectParameters();
1406 $url = $form->getTitle()->getFullURL( $urlOptions );
1408 $form->getContext()->getOutput()->redirect( $url );
1411 return Status
::newGood();
1415 * Try to set a user's email address.
1416 * This does *not* try to validate the address.
1417 * Caller is responsible for checking $wgAuth.
1419 * @param $newaddr string New email address
1420 * @return Array (true on success or Status on failure, info string)
1422 public static function trySetUserEmail( User
$user, $newaddr ) {
1423 global $wgEnableEmail, $wgEmailAuthentication;
1426 if ( $wgEnableEmail ) {
1427 $oldaddr = $user->getEmail();
1428 if ( ( $newaddr != '' ) && ( $newaddr != $oldaddr ) ) {
1429 # The user has supplied a new email address on the login page
1430 # new behaviour: set this new emailaddr from login-page into user database record
1431 $user->setEmail( $newaddr );
1432 if ( $wgEmailAuthentication ) {
1433 # Mail a temporary password to the dirty address.
1434 # User can come back through the confirmation URL to re-enable email.
1435 $type = $oldaddr != '' ?
'changed' : 'set';
1436 $result = $user->sendConfirmationMail( $type );
1437 if ( !$result->isGood() ) {
1438 return array( $result, 'mailerror' );
1442 } elseif ( $newaddr != $oldaddr ) { // if the address is the same, don't change it
1443 $user->setEmail( $newaddr );
1445 if ( $oldaddr != $newaddr ) {
1446 wfRunHooks( 'PrefsEmailAudit', array( $user, $oldaddr, $newaddr ) );
1450 return array( true, $info );
1457 public static function loadOldSearchNs( $user ) {
1458 $searchableNamespaces = SearchEngine
::searchableNamespaces();
1459 // Back compat with old format
1462 foreach ( $searchableNamespaces as $ns => $name ) {
1463 if ( $user->getOption( 'searchNs' . $ns ) ) {
1472 /** Some tweaks to allow js prefs to work */
1473 class PreferencesForm
extends HTMLForm
{
1474 // Override default value from HTMLForm
1475 protected $mSubSectionBeforeFields = false;
1477 private $modifiedUser;
1479 public function setModifiedUser( $user ) {
1480 $this->modifiedUser
= $user;
1483 public function getModifiedUser() {
1484 if ( $this->modifiedUser
=== null ) {
1485 return $this->getUser();
1487 return $this->modifiedUser
;
1492 * Get extra parameters for the query string when redirecting after
1497 public function getExtraSuccessRedirectParameters() {
1502 * @param $html string
1505 function wrapForm( $html ) {
1506 $html = Xml
::tags( 'div', array( 'id' => 'preferences' ), $html );
1508 return parent
::wrapForm( $html );
1514 function getButtons() {
1515 $html = parent
::getButtons();
1517 $t = SpecialPage
::getTitleFor( 'Preferences', 'reset' );
1519 $html .= "\n" . Linker
::link( $t, $this->msg( 'restoreprefs' )->escaped() );
1521 $html = Xml
::tags( 'div', array( 'class' => 'mw-prefs-buttons' ), $html );
1527 * @param $data array
1530 function filterDataForSubmit( $data ) {
1531 // Support for separating MultiSelect preferences into multiple preferences
1532 // Due to lack of array support.
1533 foreach ( $this->mFlatFields
as $fieldname => $field ) {
1534 $info = $field->mParams
;
1535 if ( $field instanceof HTMLMultiSelectField
) {
1536 $options = HTMLFormField
::flattenOptions( $info['options'] );
1537 $prefix = isset( $info['prefix'] ) ?
$info['prefix'] : $fieldname;
1539 foreach ( $options as $opt ) {
1540 $data["$prefix$opt"] = in_array( $opt, $data[$fieldname] );
1543 unset( $data[$fieldname] );
1550 * Get the whole body of the form.
1552 function getBody() {
1553 return $this->displaySection( $this->mFieldTree
, '', 'mw-prefsection-' );
1557 * Get the <legend> for a given section key. Normally this is the
1558 * prefs-$key message but we'll allow extensions to override it.
1560 function getLegend( $key ) {
1561 $legend = parent
::getLegend( $key );
1562 wfRunHooks( 'PreferencesGetLegend', array( $this, $key, &$legend ) );