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
::fetchLanguageNames( null, 'mw' );
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)";
358 $defaultPreferences['emailaddress'] = array(
361 'default' => $emailAddress,
362 'label-message' => 'youremail',
363 'section' => 'personal/email',
364 # 'cssclass' chosen below
367 $disableEmailPrefs = false;
369 $emailauthenticationclass = 'mw-email-not-authenticated';
370 if ( $wgEmailAuthentication ) {
371 if ( $user->getEmail() ) {
372 if ( $user->getEmailAuthenticationTimestamp() ) {
373 // date and time are separate parameters to facilitate localisation.
374 // $time is kept for backward compat reasons.
375 // 'emailauthenticated' is also used in SpecialConfirmemail.php
376 $displayUser = $context->getUser();
377 $emailTimestamp = $user->getEmailAuthenticationTimestamp();
378 $time = $lang->userTimeAndDate( $emailTimestamp, $displayUser );
379 $d = $lang->userDate( $emailTimestamp, $displayUser );
380 $t = $lang->userTime( $emailTimestamp, $displayUser );
381 $emailauthenticated = $context->msg( 'emailauthenticated',
382 $time, $d, $t )->parse() . '<br />';
383 $disableEmailPrefs = false;
384 $emailauthenticationclass = 'mw-email-authenticated';
386 $disableEmailPrefs = true;
387 $emailauthenticated = $context->msg( 'emailnotauthenticated' )->parse() . '<br />' .
389 SpecialPage
::getTitleFor( 'Confirmemail' ),
390 $context->msg( 'emailconfirmlink' )->escaped()
392 $emailauthenticationclass="mw-email-not-authenticated";
395 $disableEmailPrefs = true;
396 $emailauthenticated = $context->msg( 'noemailprefs' )->escaped();
397 $emailauthenticationclass = 'mw-email-none';
400 $defaultPreferences['emailauthentication'] = array(
403 'section' => 'personal/email',
404 'label-message' => 'prefs-emailconfirm-label',
405 'default' => $emailauthenticated,
406 # Apply the same CSS class used on the input to the message:
407 'cssclass' => $emailauthenticationclass,
410 $defaultPreferences['emailaddress']['cssclass'] = $emailauthenticationclass;
412 if ( $wgEnableUserEmail && $user->isAllowed( 'sendemail' ) ) {
413 $defaultPreferences['disablemail'] = array(
416 'section' => 'personal/email',
417 'label-message' => 'allowemail',
418 'disabled' => $disableEmailPrefs,
420 $defaultPreferences['ccmeonemails'] = array(
422 'section' => 'personal/email',
423 'label-message' => 'tog-ccmeonemails',
424 'disabled' => $disableEmailPrefs,
428 if ( $wgEnotifWatchlist ) {
429 $defaultPreferences['enotifwatchlistpages'] = array(
431 'section' => 'personal/email',
432 'label-message' => 'tog-enotifwatchlistpages',
433 'disabled' => $disableEmailPrefs,
436 if ( $wgEnotifUserTalk ) {
437 $defaultPreferences['enotifusertalkpages'] = array(
439 'section' => 'personal/email',
440 'label-message' => 'tog-enotifusertalkpages',
441 'disabled' => $disableEmailPrefs,
444 if ( $wgEnotifUserTalk ||
$wgEnotifWatchlist ) {
445 $defaultPreferences['enotifminoredits'] = array(
447 'section' => 'personal/email',
448 'label-message' => 'tog-enotifminoredits',
449 'disabled' => $disableEmailPrefs,
452 if ( $wgEnotifRevealEditorAddress ) {
453 $defaultPreferences['enotifrevealaddr'] = array(
455 'section' => 'personal/email',
456 'label-message' => 'tog-enotifrevealaddr',
457 'disabled' => $disableEmailPrefs,
466 * @param $context IContextSource
467 * @param $defaultPreferences
470 static function skinPreferences( $user, IContextSource
$context, &$defaultPreferences ) {
471 ## Skin #####################################
472 global $wgAllowUserCss, $wgAllowUserJs;
474 $defaultPreferences['skin'] = array(
476 'options' => self
::generateSkinOptions( $user, $context ),
478 'section' => 'rendering/skin',
481 # Create links to user CSS/JS pages for all skins
482 # This code is basically copied from generateSkinOptions(). It'd
483 # be nice to somehow merge this back in there to avoid redundancy.
484 if ( $wgAllowUserCss ||
$wgAllowUserJs ) {
485 $linkTools = array();
487 if ( $wgAllowUserCss ) {
488 $cssPage = Title
::makeTitleSafe( NS_USER
, $user->getName() . '/common.css' );
489 $linkTools[] = Linker
::link( $cssPage, $context->msg( 'prefs-custom-css' )->escaped() );
492 if ( $wgAllowUserJs ) {
493 $jsPage = Title
::makeTitleSafe( NS_USER
, $user->getName() . '/common.js' );
494 $linkTools[] = Linker
::link( $jsPage, $context->msg( 'prefs-custom-js' )->escaped() );
497 $defaultPreferences['commoncssjs'] = array(
500 'default' => $context->getLanguage()->pipeList( $linkTools ),
501 'label-message' => 'prefs-common-css-js',
502 'section' => 'rendering/skin',
506 $selectedSkin = $user->getOption( 'skin' );
507 if ( in_array( $selectedSkin, array( 'cologneblue', 'standard' ) ) ) {
508 $settings = array_flip( $context->getLanguage()->getQuickbarSettings() );
510 $defaultPreferences['quickbar'] = array(
512 'options' => $settings,
513 'section' => 'rendering/skin',
514 'label-message' => 'qbsettings',
521 * @param $context IContextSource
522 * @param $defaultPreferences Array
524 static function filesPreferences( $user, IContextSource
$context, &$defaultPreferences ) {
525 ## Files #####################################
526 $defaultPreferences['imagesize'] = array(
528 'options' => self
::getImageSizes( $context ),
529 'label-message' => 'imagemaxsize',
530 'section' => 'rendering/files',
532 $defaultPreferences['thumbsize'] = array(
534 'options' => self
::getThumbSizes( $context ),
535 'label-message' => 'thumbsize',
536 'section' => 'rendering/files',
542 * @param $context IContextSource
543 * @param $defaultPreferences
546 static function datetimePreferences( $user, IContextSource
$context, &$defaultPreferences ) {
547 ## Date and time #####################################
548 $dateOptions = self
::getDateOptions( $context );
549 if ( $dateOptions ) {
550 $defaultPreferences['date'] = array(
552 'options' => $dateOptions,
554 'section' => 'datetime/dateformat',
559 $now = wfTimestampNow();
560 $lang = $context->getLanguage();
561 $nowlocal = Xml
::element( 'span', array( 'id' => 'wpLocalTime' ),
562 $lang->time( $now, true ) );
563 $nowserver = $lang->time( $now, false ) .
564 Html
::hidden( 'wpServerTime', (int)substr( $now, 8, 2 ) * 60 +
(int)substr( $now, 10, 2 ) );
566 $defaultPreferences['nowserver'] = array(
569 'label-message' => 'servertime',
570 'default' => $nowserver,
571 'section' => 'datetime/timeoffset',
574 $defaultPreferences['nowlocal'] = array(
577 'label-message' => 'localtime',
578 'default' => $nowlocal,
579 'section' => 'datetime/timeoffset',
582 // Grab existing pref.
583 $tzOffset = $user->getOption( 'timecorrection' );
584 $tz = explode( '|', $tzOffset, 3 );
586 $tzOptions = self
::getTimezoneOptions( $context );
588 $tzSetting = $tzOffset;
589 if ( count( $tz ) > 1 && $tz[0] == 'Offset' ) {
591 $tzSetting = sprintf( '%+03d:%02d', floor( $minDiff / 60 ), abs( $minDiff ) %
60 );
592 } elseif ( count( $tz ) > 1 && $tz[0] == 'ZoneInfo' &&
593 !in_array( $tzOffset, HTMLFormField
::flattenOptions( $tzOptions ) ) )
595 # Timezone offset can vary with DST
596 $userTZ = timezone_open( $tz[2] );
597 if ( $userTZ !== false ) {
598 $minDiff = floor( timezone_offset_get( $userTZ, date_create( 'now' ) ) / 60 );
599 $tzSetting = "ZoneInfo|$minDiff|{$tz[2]}";
603 $defaultPreferences['timecorrection'] = array(
604 'class' => 'HTMLSelectOrOtherField',
605 'label-message' => 'timezonelegend',
606 'options' => $tzOptions,
607 'default' => $tzSetting,
609 'section' => 'datetime/timeoffset',
615 * @param $context IContextSource
616 * @param $defaultPreferences Array
618 static function renderingPreferences( $user, IContextSource
$context, &$defaultPreferences ) {
619 ## Page Rendering ##############################
620 global $wgAllowUserCssPrefs;
621 if ( $wgAllowUserCssPrefs ) {
622 $defaultPreferences['underline'] = array(
625 $context->msg( 'underline-never' )->text() => 0,
626 $context->msg( 'underline-always' )->text() => 1,
627 $context->msg( 'underline-default' )->text() => 2,
629 'label-message' => 'tog-underline',
630 'section' => 'rendering/advancedrendering',
634 $stubThresholdValues = array( 50, 100, 500, 1000, 2000, 5000, 10000 );
635 $stubThresholdOptions = array( $context->msg( 'stub-threshold-disabled' )->text() => 0 );
636 foreach ( $stubThresholdValues as $value ) {
637 $stubThresholdOptions[$context->msg( 'size-bytes', $value )->text()] = $value;
640 $defaultPreferences['stubthreshold'] = array(
641 'type' => 'selectorother',
642 'section' => 'rendering/advancedrendering',
643 'options' => $stubThresholdOptions,
645 'label' => $context->msg( 'stub-threshold' )->text(), // Raw HTML message. Yay?
648 if ( $wgAllowUserCssPrefs ) {
649 $defaultPreferences['showtoc'] = array(
651 'section' => 'rendering/advancedrendering',
652 'label-message' => 'tog-showtoc',
655 $defaultPreferences['nocache'] = array(
657 'label-message' => 'tog-nocache',
658 'section' => 'rendering/advancedrendering',
660 $defaultPreferences['showhiddencats'] = array(
662 'section' => 'rendering/advancedrendering',
663 'label-message' => 'tog-showhiddencats'
665 $defaultPreferences['showjumplinks'] = array(
667 'section' => 'rendering/advancedrendering',
668 'label-message' => 'tog-showjumplinks',
671 if ( $wgAllowUserCssPrefs ) {
672 $defaultPreferences['justify'] = array(
674 'section' => 'rendering/advancedrendering',
675 'label-message' => 'tog-justify',
679 $defaultPreferences['numberheadings'] = array(
681 'section' => 'rendering/advancedrendering',
682 'label-message' => 'tog-numberheadings',
688 * @param $context IContextSource
689 * @param $defaultPreferences Array
691 static function editingPreferences( $user, IContextSource
$context, &$defaultPreferences ) {
692 global $wgUseExternalEditor, $wgAllowUserCssPrefs;
694 ## Editing #####################################
695 $defaultPreferences['cols'] = array(
697 'label-message' => 'columns',
698 'section' => 'editing/textboxsize',
702 $defaultPreferences['rows'] = array(
704 'label-message' => 'rows',
705 'section' => 'editing/textboxsize',
710 if ( $wgAllowUserCssPrefs ) {
711 $defaultPreferences['editfont'] = array(
713 'section' => 'editing/advancedediting',
714 'label-message' => 'editfont-style',
716 $context->msg( 'editfont-default' )->text() => 'default',
717 $context->msg( 'editfont-monospace' )->text() => 'monospace',
718 $context->msg( 'editfont-sansserif' )->text() => 'sans-serif',
719 $context->msg( 'editfont-serif' )->text() => 'serif',
723 $defaultPreferences['previewontop'] = array(
725 'section' => 'editing/advancedediting',
726 'label-message' => 'tog-previewontop',
728 $defaultPreferences['previewonfirst'] = array(
730 'section' => 'editing/advancedediting',
731 'label-message' => 'tog-previewonfirst',
734 if ( $wgAllowUserCssPrefs ) {
735 $defaultPreferences['editsection'] = array(
737 'section' => 'editing/advancedediting',
738 'label-message' => 'tog-editsection',
741 $defaultPreferences['editsectiononrightclick'] = array(
743 'section' => 'editing/advancedediting',
744 'label-message' => 'tog-editsectiononrightclick',
746 $defaultPreferences['editondblclick'] = array(
748 'section' => 'editing/advancedediting',
749 'label-message' => 'tog-editondblclick',
751 $defaultPreferences['showtoolbar'] = array(
753 'section' => 'editing/advancedediting',
754 'label-message' => 'tog-showtoolbar',
757 if ( $user->isAllowed( 'minoredit' ) ) {
758 $defaultPreferences['minordefault'] = array(
760 'section' => 'editing/advancedediting',
761 'label-message' => 'tog-minordefault',
765 if ( $wgUseExternalEditor ) {
766 $defaultPreferences['externaleditor'] = array(
768 'section' => 'editing/advancedediting',
769 'label-message' => 'tog-externaleditor',
771 $defaultPreferences['externaldiff'] = array(
773 'section' => 'editing/advancedediting',
774 'label-message' => 'tog-externaldiff',
778 $defaultPreferences['forceeditsummary'] = array(
780 'section' => 'editing/advancedediting',
781 'label-message' => 'tog-forceeditsummary',
785 $defaultPreferences['uselivepreview'] = array(
787 'section' => 'editing/advancedediting',
788 'label-message' => 'tog-uselivepreview',
794 * @param $context IContextSource
795 * @param $defaultPreferences Array
797 static function rcPreferences( $user, IContextSource
$context, &$defaultPreferences ) {
798 global $wgRCMaxAge, $wgRCShowWatchingUsers;
800 ## RecentChanges #####################################
801 $defaultPreferences['rcdays'] = array(
803 'label-message' => 'recentchangesdays',
804 'section' => 'rc/displayrc',
806 'max' => ceil( $wgRCMaxAge / ( 3600 * 24 ) ),
807 'help' => $context->msg( 'recentchangesdays-max' )->numParams(
808 ceil( $wgRCMaxAge / ( 3600 * 24 ) ) )->text()
810 $defaultPreferences['rclimit'] = array(
812 'label-message' => 'recentchangescount',
813 'help-message' => 'prefs-help-recentchangescount',
814 'section' => 'rc/displayrc',
816 $defaultPreferences['usenewrc'] = array(
818 'label-message' => 'tog-usenewrc',
819 'section' => 'rc/advancedrc',
821 $defaultPreferences['hideminor'] = array(
823 'label-message' => 'tog-hideminor',
824 'section' => 'rc/advancedrc',
827 if ( $user->useRCPatrol() ) {
828 $defaultPreferences['hidepatrolled'] = array(
830 'section' => 'rc/advancedrc',
831 'label-message' => 'tog-hidepatrolled',
833 $defaultPreferences['newpageshidepatrolled'] = array(
835 'section' => 'rc/advancedrc',
836 'label-message' => 'tog-newpageshidepatrolled',
840 if ( $wgRCShowWatchingUsers ) {
841 $defaultPreferences['shownumberswatching'] = array(
843 'section' => 'rc/advancedrc',
844 'label-message' => 'tog-shownumberswatching',
851 * @param $context IContextSource
852 * @param $defaultPreferences
854 static function watchlistPreferences( $user, IContextSource
$context, &$defaultPreferences ) {
855 global $wgUseRCPatrol, $wgEnableAPI, $wgRCMaxAge;
857 $watchlistdaysMax = ceil( $wgRCMaxAge / ( 3600 * 24 ) );
859 ## Watchlist #####################################
860 $defaultPreferences['watchlistdays'] = array(
863 'max' => $watchlistdaysMax,
864 'section' => 'watchlist/displaywatchlist',
865 'help' => $context->msg( 'prefs-watchlist-days-max' )->numParams(
866 $watchlistdaysMax )->text(),
867 'label-message' => 'prefs-watchlist-days',
869 $defaultPreferences['wllimit'] = array(
873 'label-message' => 'prefs-watchlist-edits',
874 'help' => $context->msg( 'prefs-watchlist-edits-max' )->escaped(),
875 'section' => 'watchlist/displaywatchlist',
877 $defaultPreferences['extendwatchlist'] = array(
879 'section' => 'watchlist/advancedwatchlist',
880 'label-message' => 'tog-extendwatchlist',
882 $defaultPreferences['watchlisthideminor'] = array(
884 'section' => 'watchlist/advancedwatchlist',
885 'label-message' => 'tog-watchlisthideminor',
887 $defaultPreferences['watchlisthidebots'] = array(
889 'section' => 'watchlist/advancedwatchlist',
890 'label-message' => 'tog-watchlisthidebots',
892 $defaultPreferences['watchlisthideown'] = array(
894 'section' => 'watchlist/advancedwatchlist',
895 'label-message' => 'tog-watchlisthideown',
897 $defaultPreferences['watchlisthideanons'] = array(
899 'section' => 'watchlist/advancedwatchlist',
900 'label-message' => 'tog-watchlisthideanons',
902 $defaultPreferences['watchlisthideliu'] = array(
904 'section' => 'watchlist/advancedwatchlist',
905 'label-message' => 'tog-watchlisthideliu',
908 if ( $wgUseRCPatrol ) {
909 $defaultPreferences['watchlisthidepatrolled'] = array(
911 'section' => 'watchlist/advancedwatchlist',
912 'label-message' => 'tog-watchlisthidepatrolled',
916 if ( $wgEnableAPI ) {
917 # Some random gibberish as a proposed default
918 $hash = sha1( mt_rand() . microtime( true ) );
920 $defaultPreferences['watchlisttoken'] = array(
922 'section' => 'watchlist/advancedwatchlist',
923 'label-message' => 'prefs-watchlist-token',
924 'help' => $context->msg( 'prefs-help-watchlist-token', $hash )->escaped()
929 'edit' => 'watchdefault',
930 'move' => 'watchmoves',
931 'delete' => 'watchdeletion'
935 if ( $user->isAllowed( 'createpage' ) ||
$user->isAllowed( 'createtalk' ) ) {
936 $watchTypes['read'] = 'watchcreations';
939 foreach ( $watchTypes as $action => $pref ) {
940 if ( $user->isAllowed( $action ) ) {
941 $defaultPreferences[$pref] = array(
943 'section' => 'watchlist/advancedwatchlist',
944 'label-message' => "tog-$pref",
952 * @param $context IContextSource
953 * @param $defaultPreferences Array
955 static function searchPreferences( $user, IContextSource
$context, &$defaultPreferences ) {
956 global $wgContLang, $wgEnableMWSuggest, $wgVectorUseSimpleSearch;
958 ## Search #####################################
959 $defaultPreferences['searchlimit'] = array(
961 'label-message' => 'resultsperpage',
962 'section' => 'searchoptions/displaysearchoptions',
966 if ( $wgEnableMWSuggest ) {
967 $defaultPreferences['disablesuggest'] = array(
969 'label-message' => 'mwsuggest-disable',
970 'section' => 'searchoptions/displaysearchoptions',
974 if ( $wgVectorUseSimpleSearch ) {
975 $defaultPreferences['vector-simplesearch'] = array(
977 'label-message' => 'vector-simplesearch-preference',
978 'section' => 'searchoptions/displaysearchoptions'
982 $defaultPreferences['searcheverything'] = array(
984 'label-message' => 'searcheverything-enable',
985 'section' => 'searchoptions/advancedsearchoptions',
988 $nsOptions = array();
990 foreach ( $wgContLang->getNamespaces() as $ns => $name ) {
995 $displayNs = str_replace( '_', ' ', $name );
998 $displayNs = $context->msg( 'blanknamespace' )->text();
1001 $displayNs = htmlspecialchars( $displayNs );
1002 $nsOptions[$displayNs] = $ns;
1005 $defaultPreferences['searchnamespaces'] = array(
1006 'type' => 'multiselect',
1007 'label-message' => 'defaultns',
1008 'options' => $nsOptions,
1009 'section' => 'searchoptions/advancedsearchoptions',
1010 'prefix' => 'searchNs',
1016 * @param $context IContextSource
1017 * @param $defaultPreferences Array
1019 static function miscPreferences( $user, IContextSource
$context, &$defaultPreferences ) {
1022 ## Misc #####################################
1023 $defaultPreferences['diffonly'] = array(
1025 'section' => 'misc/diffs',
1026 'label-message' => 'tog-diffonly',
1028 $defaultPreferences['norollbackdiff'] = array(
1030 'section' => 'misc/diffs',
1031 'label-message' => 'tog-norollbackdiff',
1034 // Stuff from Language::getExtraUserToggles()
1035 $toggles = $wgContLang->getExtraUserToggles();
1037 foreach ( $toggles as $toggle ) {
1038 $defaultPreferences[$toggle] = array(
1040 'section' => 'personal/i18n',
1041 'label-message' => "tog-$toggle",
1047 * @param $user User The User object
1048 * @param $context IContextSource
1049 * @return Array: text/links to display as key; $skinkey as value
1051 static function generateSkinOptions( $user, IContextSource
$context ) {
1052 global $wgDefaultSkin, $wgAllowUserCss, $wgAllowUserJs;
1055 $mptitle = Title
::newMainPage();
1056 $previewtext = $context->msg( 'skin-preview' )->text();
1058 # Only show members of Skin::getSkinNames() rather than
1059 # $skinNames (skins is all skin names from Language.php)
1060 $validSkinNames = Skin
::getUsableSkins();
1062 # Sort by UI skin name. First though need to update validSkinNames as sometimes
1063 # the skinkey & UI skinname differ (e.g. "standard" skinkey is "Classic" in the UI).
1064 foreach ( $validSkinNames as $skinkey => &$skinname ) {
1065 $msg = $context->msg( "skinname-{$skinkey}" );
1066 if ( $msg->exists() ) {
1067 $skinname = htmlspecialchars( $msg->text() );
1070 asort( $validSkinNames );
1072 foreach ( $validSkinNames as $skinkey => $sn ) {
1073 $linkTools = array();
1075 # Mark the default skin
1076 if ( $skinkey == $wgDefaultSkin ) {
1077 $linkTools[] = $context->msg( 'default' )->escaped();
1080 # Create preview link
1081 $mplink = htmlspecialchars( $mptitle->getLocalURL( "useskin=$skinkey" ) );
1082 $linkTools[] = "<a target='_blank' href=\"$mplink\">$previewtext</a>";
1084 # Create links to user CSS/JS pages
1085 if ( $wgAllowUserCss ) {
1086 $cssPage = Title
::makeTitleSafe( NS_USER
, $user->getName() . '/' . $skinkey . '.css' );
1087 $linkTools[] = Linker
::link( $cssPage, $context->msg( 'prefs-custom-css' )->escaped() );
1090 if ( $wgAllowUserJs ) {
1091 $jsPage = Title
::makeTitleSafe( NS_USER
, $user->getName() . '/' . $skinkey . '.js' );
1092 $linkTools[] = Linker
::link( $jsPage, $context->msg( 'prefs-custom-js' )->escaped() );
1095 $display = $sn . ' ' . $context->msg( 'parentheses', $context->getLanguage()->pipeList( $linkTools ) )->text();
1096 $ret[$display] = $skinkey;
1103 * @param $context IContextSource
1106 static function getDateOptions( IContextSource
$context ) {
1107 $lang = $context->getLanguage();
1108 $dateopts = $lang->getDatePreferences();
1113 if ( !in_array( 'default', $dateopts ) ) {
1114 $dateopts[] = 'default'; // Make sure default is always valid
1118 // KLUGE: site default might not be valid for user language
1119 global $wgDefaultUserOptions;
1120 if ( !in_array( $wgDefaultUserOptions['date'], $dateopts ) ) {
1121 $wgDefaultUserOptions['date'] = 'default';
1124 $epoch = wfTimestampNow();
1125 foreach ( $dateopts as $key ) {
1126 if ( $key == 'default' ) {
1127 $formatted = $context->msg( 'datedefault' )->escaped();
1129 $formatted = htmlspecialchars( $lang->timeanddate( $epoch, false, $key ) );
1131 $ret[$formatted] = $key;
1138 * @param $context IContextSource
1141 static function getImageSizes( IContextSource
$context ) {
1142 global $wgImageLimits;
1145 $pixels = $context->msg( 'unit-pixel' )->text();
1147 foreach ( $wgImageLimits as $index => $limits ) {
1148 $display = "{$limits[0]}×{$limits[1]}" . $pixels;
1149 $ret[$display] = $index;
1156 * @param $context IContextSource
1159 static function getThumbSizes( IContextSource
$context ) {
1160 global $wgThumbLimits;
1163 $pixels = $context->msg( 'unit-pixel' )->text();
1165 foreach ( $wgThumbLimits as $index => $size ) {
1166 $display = $size . $pixels;
1167 $ret[$display] = $index;
1174 * @param $signature string
1175 * @param $alldata array
1176 * @param $form HTMLForm
1177 * @return bool|string
1179 static function validateSignature( $signature, $alldata, $form ) {
1180 global $wgParser, $wgMaxSigChars;
1181 if ( mb_strlen( $signature ) > $wgMaxSigChars ) {
1182 return Xml
::element( 'span', array( 'class' => 'error' ),
1183 $form->msg( 'badsiglength' )->numParams( $wgMaxSigChars )->text() );
1184 } elseif ( isset( $alldata['fancysig'] ) &&
1185 $alldata['fancysig'] &&
1186 false === $wgParser->validateSig( $signature ) ) {
1187 return Xml
::element( 'span', array( 'class' => 'error' ), $form->msg( 'badsig' )->text() );
1194 * @param $signature string
1195 * @param $alldata array
1196 * @param $form HTMLForm
1199 static function cleanSignature( $signature, $alldata, $form ) {
1200 if ( isset( $alldata['fancysig'] ) && $alldata['fancysig'] ) {
1202 $signature = $wgParser->cleanSig( $signature );
1204 // When no fancy sig used, make sure ~{3,5} get removed.
1205 $signature = Parser
::cleanSigInSig( $signature );
1213 * @param $context IContextSource
1214 * @param $formClass string
1215 * @param $remove Array: array of items to remove
1218 static function getFormObject( $user, IContextSource
$context, $formClass = 'PreferencesForm', array $remove = array() ) {
1219 $formDescriptor = Preferences
::getPreferences( $user, $context );
1220 if ( count( $remove ) ) {
1221 $removeKeys = array_flip( $remove );
1222 $formDescriptor = array_diff_key( $formDescriptor, $removeKeys );
1226 * @var $htmlForm PreferencesForm
1228 $htmlForm = new $formClass( $formDescriptor, $context, 'prefs' );
1230 $htmlForm->setModifiedUser( $user );
1231 $htmlForm->setId( 'mw-prefs-form' );
1232 $htmlForm->setSubmitText( $context->msg( 'saveprefs' )->text() );
1233 # Used message keys: 'accesskey-preferences-save', 'tooltip-preferences-save'
1234 $htmlForm->setSubmitTooltip( 'preferences-save' );
1235 $htmlForm->setSubmitID( 'prefsubmit' );
1236 $htmlForm->setSubmitCallback( array( 'Preferences', 'tryFormSubmit' ) );
1244 static function getTimezoneOptions( IContextSource
$context ) {
1247 global $wgLocalTZoffset, $wgLocaltimezone;
1248 // Check that $wgLocalTZoffset is the same as $wgLocaltimezone
1249 if ( $wgLocalTZoffset == date( 'Z' ) / 60 ) {
1250 $server_tz_msg = $context->msg( 'timezoneuseserverdefault', $wgLocaltimezone )->text();
1252 $tzstring = sprintf( '%+03d:%02d', floor( $wgLocalTZoffset / 60 ), abs( $wgLocalTZoffset ) %
60 );
1253 $server_tz_msg = $context->msg( 'timezoneuseserverdefault', $tzstring )->text();
1255 $opt[$server_tz_msg] = "System|$wgLocalTZoffset";
1256 $opt[$context->msg( 'timezoneuseoffset' )->text()] = 'other';
1257 $opt[$context->msg( 'guesstimezone' )->text()] = 'guess';
1259 if ( function_exists( 'timezone_identifiers_list' ) ) {
1260 # Read timezone list
1261 $tzs = timezone_identifiers_list();
1264 $tzRegions = array();
1265 $tzRegions['Africa'] = $context->msg( 'timezoneregion-africa' )->text();
1266 $tzRegions['America'] = $context->msg( 'timezoneregion-america' )->text();
1267 $tzRegions['Antarctica'] = $context->msg( 'timezoneregion-antarctica' )->text();
1268 $tzRegions['Arctic'] = $context->msg( 'timezoneregion-arctic' )->text();
1269 $tzRegions['Asia'] = $context->msg( 'timezoneregion-asia' )->text();
1270 $tzRegions['Atlantic'] = $context->msg( 'timezoneregion-atlantic' )->text();
1271 $tzRegions['Australia'] = $context->msg( 'timezoneregion-australia' )->text();
1272 $tzRegions['Europe'] = $context->msg( 'timezoneregion-europe' )->text();
1273 $tzRegions['Indian'] = $context->msg( 'timezoneregion-indian' )->text();
1274 $tzRegions['Pacific'] = $context->msg( 'timezoneregion-pacific' )->text();
1275 asort( $tzRegions );
1277 $prefill = array_fill_keys( array_values( $tzRegions ), array() );
1278 $opt = array_merge( $opt, $prefill );
1280 $now = date_create( 'now' );
1282 foreach ( $tzs as $tz ) {
1283 $z = explode( '/', $tz, 2 );
1285 # timezone_identifiers_list() returns a number of
1286 # backwards-compatibility entries. This filters them out of the
1287 # list presented to the user.
1288 if ( count( $z ) != 2 ||
!array_key_exists( $z[0], $tzRegions ) ) {
1293 $z[0] = $tzRegions[$z[0]];
1295 $minDiff = floor( timezone_offset_get( timezone_open( $tz ), $now ) / 60 );
1297 $display = str_replace( '_', ' ', $z[0] . '/' . $z[1] );
1298 $value = "ZoneInfo|$minDiff|$tz";
1300 $opt[$z[0]][$display] = $value;
1311 static function filterIntval( $value, $alldata ){
1312 return intval( $value );
1320 static function filterTimezoneInput( $tz, $alldata ) {
1321 $data = explode( '|', $tz, 3 );
1322 switch ( $data[0] ) {
1327 $data = explode( ':', $tz, 2 );
1328 if ( count( $data ) == 2 ) {
1329 $data[0] = intval( $data[0] );
1330 $data[1] = intval( $data[1] );
1331 $minDiff = abs( $data[0] ) * 60 +
$data[1];
1332 if ( $data[0] < 0 ) $minDiff = - $minDiff;
1334 $minDiff = intval( $data[0] ) * 60;
1337 # Max is +14:00 and min is -12:00, see:
1338 # http://en.wikipedia.org/wiki/Timezone
1339 $minDiff = min( $minDiff, 840 ); # 14:00
1340 $minDiff = max( $minDiff, - 720 ); # -12:00
1341 return 'Offset|' . $minDiff;
1347 * @param $form PreferencesForm
1348 * @param $entryPoint string
1349 * @return bool|Status|string
1351 static function tryFormSubmit( $formData, $form, $entryPoint = 'internal' ) {
1352 global $wgHiddenPrefs;
1354 $user = $form->getModifiedUser();
1358 foreach ( array_keys( $formData ) as $name ) {
1359 if ( isset( self
::$saveFilters[$name] ) ) {
1361 call_user_func( self
::$saveFilters[$name], $formData[$name], $formData );
1365 // Stuff that shouldn't be saved as a preference.
1366 $saveBlacklist = array(
1371 // Fortunately, the realname field is MUCH simpler
1372 if ( !in_array( 'realname', $wgHiddenPrefs ) ) {
1373 $realName = $formData['realname'];
1374 $user->setRealName( $realName );
1377 foreach ( $saveBlacklist as $b ) {
1378 unset( $formData[$b] );
1381 # If users have saved a value for a preference which has subsequently been disabled
1382 # via $wgHiddenPrefs, we don't want to destroy that setting in case the preference
1383 # is subsequently re-enabled
1384 # TODO: maintenance script to actually delete these
1385 foreach( $wgHiddenPrefs as $pref ){
1386 # If the user has not set a non-default value here, the default will be returned
1387 # and subsequently discarded
1388 $formData[$pref] = $user->getOption( $pref, null, true );
1391 // Keeps old preferences from interfering due to back-compat
1393 $user->resetOptions();
1395 foreach ( $formData as $key => $value ) {
1396 $user->setOption( $key, $value );
1399 $user->saveSettings();
1406 * @param $form PreferencesForm
1409 public static function tryUISubmit( $formData, $form ) {
1410 $res = self
::tryFormSubmit( $formData, $form, 'ui' );
1413 $urlOptions = array( 'success' => 1 );
1415 if ( $res === 'eauth' ) {
1416 $urlOptions['eauth'] = 1;
1419 $urlOptions +
= $form->getExtraSuccessRedirectParameters();
1421 $url = $form->getTitle()->getFullURL( $urlOptions );
1423 $form->getContext()->getOutput()->redirect( $url );
1426 return Status
::newGood();
1430 * Try to set a user's email address.
1431 * This does *not* try to validate the address.
1432 * Caller is responsible for checking $wgAuth.
1434 * @param $newaddr string New email address
1435 * @return Array (true on success or Status on failure, info string)
1437 public static function trySetUserEmail( User
$user, $newaddr ) {
1438 global $wgEnableEmail, $wgEmailAuthentication;
1441 if ( $wgEnableEmail ) {
1442 $oldaddr = $user->getEmail();
1443 if ( ( $newaddr != '' ) && ( $newaddr != $oldaddr ) ) {
1444 # The user has supplied a new email address on the login page
1445 # new behaviour: set this new emailaddr from login-page into user database record
1446 $user->setEmail( $newaddr );
1447 if ( $wgEmailAuthentication ) {
1448 # Mail a temporary password to the dirty address.
1449 # User can come back through the confirmation URL to re-enable email.
1450 $type = $oldaddr != '' ?
'changed' : 'set';
1451 $result = $user->sendConfirmationMail( $type );
1452 if ( !$result->isGood() ) {
1453 return array( $result, 'mailerror' );
1457 } elseif ( $newaddr != $oldaddr ) { // if the address is the same, don't change it
1458 $user->setEmail( $newaddr );
1460 if ( $oldaddr != $newaddr ) {
1461 wfRunHooks( 'PrefsEmailAudit', array( $user, $oldaddr, $newaddr ) );
1465 return array( true, $info );
1469 * @deprecated in 1.19; will be removed in 1.20.
1473 public static function loadOldSearchNs( $user ) {
1474 wfDeprecated( __METHOD__
, '1.19' );
1476 $searchableNamespaces = SearchEngine
::searchableNamespaces();
1477 // Back compat with old format
1480 foreach ( $searchableNamespaces as $ns => $name ) {
1481 if ( $user->getOption( 'searchNs' . $ns ) ) {
1490 /** Some tweaks to allow js prefs to work */
1491 class PreferencesForm
extends HTMLForm
{
1492 // Override default value from HTMLForm
1493 protected $mSubSectionBeforeFields = false;
1495 private $modifiedUser;
1500 public function setModifiedUser( $user ) {
1501 $this->modifiedUser
= $user;
1507 public function getModifiedUser() {
1508 if ( $this->modifiedUser
=== null ) {
1509 return $this->getUser();
1511 return $this->modifiedUser
;
1516 * Get extra parameters for the query string when redirecting after
1521 public function getExtraSuccessRedirectParameters() {
1526 * @param $html string
1529 function wrapForm( $html ) {
1530 $html = Xml
::tags( 'div', array( 'id' => 'preferences' ), $html );
1532 return parent
::wrapForm( $html );
1538 function getButtons() {
1539 $html = parent
::getButtons();
1541 $t = SpecialPage
::getTitleFor( 'Preferences', 'reset' );
1543 $html .= "\n" . Linker
::link( $t, $this->msg( 'restoreprefs' )->escaped() );
1545 $html = Xml
::tags( 'div', array( 'class' => 'mw-prefs-buttons' ), $html );
1551 * @param $data array
1554 function filterDataForSubmit( $data ) {
1555 // Support for separating MultiSelect preferences into multiple preferences
1556 // Due to lack of array support.
1557 foreach ( $this->mFlatFields
as $fieldname => $field ) {
1558 $info = $field->mParams
;
1559 if ( $field instanceof HTMLMultiSelectField
) {
1560 $options = HTMLFormField
::flattenOptions( $info['options'] );
1561 $prefix = isset( $info['prefix'] ) ?
$info['prefix'] : $fieldname;
1563 foreach ( $options as $opt ) {
1564 $data["$prefix$opt"] = in_array( $opt, $data[$fieldname] );
1567 unset( $data[$fieldname] );
1575 * Get the whole body of the form.
1578 function getBody() {
1579 return $this->displaySection( $this->mFieldTree
, '', 'mw-prefsection-' );
1583 * Get the <legend> for a given section key. Normally this is the
1584 * prefs-$key message but we'll allow extensions to override it.
1585 * @param $key string
1588 function getLegend( $key ) {
1589 $legend = parent
::getLegend( $key );
1590 wfRunHooks( 'PreferencesGetLegend', array( $this, $key, &$legend ) );