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
133 static function profilePreferences( $user, IContextSource
$context, &$defaultPreferences ) {
134 global $wgAuth, $wgContLang, $wgParser, $wgCookieExpiration, $wgLanguageCode,
135 $wgDisableTitleConversion, $wgDisableLangConversion, $wgMaxSigChars,
136 $wgEnableEmail, $wgEmailConfirmToEdit, $wgEnableUserEmail, $wgEmailAuthentication,
137 $wgEnotifWatchlist, $wgEnotifUserTalk, $wgEnotifRevealEditorAddress;
139 ## User info #####################################
141 $defaultPreferences['username'] = array(
143 'label-message' => 'username',
144 'default' => $user->getName(),
145 'section' => 'personal/info',
148 $defaultPreferences['userid'] = array(
150 'label-message' => 'uid',
151 'default' => $user->getId(),
152 'section' => 'personal/info',
155 # Get groups to which the user belongs
156 $userEffectiveGroups = $user->getEffectiveGroups();
157 $userGroups = $userMembers = array();
158 foreach ( $userEffectiveGroups as $ueg ) {
160 // Skip the default * group, seems useless here
163 $groupName = User
::getGroupName( $ueg );
164 $userGroups[] = User
::makeGroupLinkHTML( $ueg, $groupName );
166 $memberName = User
::getGroupMember( $ueg, $user->getName() );
167 $userMembers[] = User
::makeGroupLinkHTML( $ueg, $memberName );
169 asort( $userGroups );
170 asort( $userMembers );
172 $lang = $context->getLanguage();
174 $defaultPreferences['usergroups'] = array(
176 'label' => $context->msg( 'prefs-memberingroups' )->numParams(
177 count( $userGroups ) )->parse(),
178 'default' => $context->msg( 'prefs-memberingroups-type',
179 $lang->commaList( $userGroups ),
180 $lang->commaList( $userMembers )
183 'section' => 'personal/info',
186 $defaultPreferences['editcount'] = array(
188 'label-message' => 'prefs-edits',
189 'default' => $lang->formatNum( $user->getEditCount() ),
190 'section' => 'personal/info',
193 if ( $user->getRegistration() ) {
194 $displayUser = $context->getUser();
195 $userRegistration = $user->getRegistration();
196 $defaultPreferences['registrationdate'] = array(
198 'label-message' => 'prefs-registration',
199 'default' => $context->msg(
200 'prefs-registration-date-time',
201 $lang->userTimeAndDate( $userRegistration, $displayUser ),
202 $lang->userDate( $userRegistration, $displayUser ),
203 $lang->userTime( $userRegistration, $displayUser )
205 'section' => 'personal/info',
209 // Actually changeable stuff
210 $defaultPreferences['realname'] = array(
211 'type' => $wgAuth->allowPropChange( 'realname' ) ?
'text' : 'info',
212 'default' => $user->getRealName(),
213 'section' => 'personal/info',
214 'label-message' => 'yourrealname',
215 'help-message' => 'prefs-help-realname',
218 $defaultPreferences['gender'] = array(
220 'section' => 'personal/info',
222 $context->msg( 'gender-male' )->text() => 'male',
223 $context->msg( 'gender-female' )->text() => 'female',
224 $context->msg( 'gender-unknown' )->text() => 'unknown',
226 'label-message' => 'yourgender',
227 'help-message' => 'prefs-help-gender',
230 if ( $wgAuth->allowPasswordChange() ) {
231 $link = Linker
::link( SpecialPage
::getTitleFor( 'ChangePassword' ),
232 $context->msg( 'prefs-resetpass' )->escaped(), array(),
233 array( 'returnto' => SpecialPage
::getTitleFor( 'Preferences' ) ) );
235 $defaultPreferences['password'] = array(
239 'label-message' => 'yourpassword',
240 'section' => 'personal/info',
243 if ( $wgCookieExpiration > 0 ) {
244 $defaultPreferences['rememberpassword'] = array(
246 'label' => $context->msg( 'tog-rememberpassword' )->numParams(
247 ceil( $wgCookieExpiration / ( 3600 * 24 ) ) )->text(),
248 'section' => 'personal/info',
253 $languages = Language
::getLanguageNames( false );
254 if ( !array_key_exists( $wgLanguageCode, $languages ) ) {
255 $languages[$wgLanguageCode] = $wgLanguageCode;
260 foreach ( $languages as $code => $name ) {
261 $display = wfBCP47( $code ) . ' - ' . $name;
262 $options[$display] = $code;
264 $defaultPreferences['language'] = array(
266 'section' => 'personal/i18n',
267 'options' => $options,
268 'label-message' => 'yourlanguage',
271 /* see if there are multiple language variants to choose from*/
272 $variantArray = array();
273 if ( !$wgDisableLangConversion ) {
274 $variants = $wgContLang->getVariants();
276 foreach ( $variants as $v ) {
277 $v = str_replace( '_', '-', strtolower( $v ) );
278 $variantArray[$v] = $wgContLang->getVariantname( $v, false );
282 foreach ( $variantArray as $code => $name ) {
283 $display = wfBCP47( $code ) . ' - ' . $name;
284 $options[$display] = $code;
287 if ( count( $variantArray ) > 1 ) {
288 $defaultPreferences['variant'] = array(
289 'label-message' => 'yourvariant',
291 'options' => $options,
292 'section' => 'personal/i18n',
293 'help-message' => 'prefs-help-variant',
298 if ( count( $variantArray ) > 1 && !$wgDisableLangConversion && !$wgDisableTitleConversion ) {
299 $defaultPreferences['noconvertlink'] =
302 'section' => 'personal/i18n',
303 'label-message' => 'tog-noconvertlink',
307 // show a preview of the old signature first
308 $oldsigWikiText = $wgParser->preSaveTransform( "~~~", $context->getTitle(), $user, ParserOptions
::newFromContext( $context ) );
309 $oldsigHTML = $context->getOutput()->parseInline( $oldsigWikiText, true, true );
310 $defaultPreferences['oldsig'] = array(
313 'label-message' => 'tog-oldsig',
314 'default' => $oldsigHTML,
315 'section' => 'personal/signature',
317 $defaultPreferences['nickname'] = array(
318 'type' => $wgAuth->allowPropChange( 'nickname' ) ?
'text' : 'info',
319 'maxlength' => $wgMaxSigChars,
320 'label-message' => 'yournick',
321 'validation-callback' => array( 'Preferences', 'validateSignature' ),
322 'section' => 'personal/signature',
323 'filter-callback' => array( 'Preferences', 'cleanSignature' ),
325 $defaultPreferences['fancysig'] = array(
327 'label-message' => 'tog-fancysig',
328 'help-message' => 'prefs-help-signature', // show general help about signature at the bottom of the section
329 'section' => 'personal/signature'
334 if ( $wgEnableEmail ) {
335 $helpMessages[] = $wgEmailConfirmToEdit
336 ?
'prefs-help-email-required'
337 : 'prefs-help-email' ;
339 if( $wgEnableUserEmail ) {
340 // additional messages when users can send email to each other
341 $helpMessages[] = 'prefs-help-email-others';
344 $link = Linker
::link(
345 SpecialPage
::getTitleFor( 'ChangeEmail' ),
346 $context->msg( $user->getEmail() ?
'prefs-changeemail' : 'prefs-setemail' )->escaped(),
348 array( 'returnto' => SpecialPage
::getTitleFor( 'Preferences' ) ) );
350 $emailAddress = $user->getEmail() ?
htmlspecialchars( $user->getEmail() ) : '';
351 if ( $wgAuth->allowPropChange( 'emailaddress' ) ) {
352 $emailAddress .= $emailAddress == '' ?
$link : " ($link)";
355 $defaultPreferences['emailaddress'] = array(
358 'default' => $emailAddress,
359 'label-message' => 'youremail',
360 'section' => 'personal/email',
363 $disableEmailPrefs = false;
365 if ( $wgEmailAuthentication ) {
366 if ( $user->getEmail() ) {
367 if ( $user->getEmailAuthenticationTimestamp() ) {
368 // date and time are separate parameters to facilitate localisation.
369 // $time is kept for backward compat reasons.
370 // 'emailauthenticated' is also used in SpecialConfirmemail.php
371 $displayUser = $context->getUser();
372 $emailTimestamp = $user->getEmailAuthenticationTimestamp();
373 $time = $lang->userTimeAndDate( $emailTimestamp, $displayUser );
374 $d = $lang->userDate( $emailTimestamp, $displayUser );
375 $t = $lang->userTime( $emailTimestamp, $displayUser );
376 $emailauthenticated = $context->msg( 'emailauthenticated',
377 $time, $d, $t )->parse() . '<br />';
378 $disableEmailPrefs = false;
380 $disableEmailPrefs = true;
381 $emailauthenticated = $context->msg( 'emailnotauthenticated' )->parse() . '<br />' .
383 SpecialPage
::getTitleFor( 'Confirmemail' ),
384 $context->msg( 'emailconfirmlink' )->escaped()
388 $disableEmailPrefs = true;
389 $emailauthenticated = $context->msg( 'noemailprefs' )->escaped();
392 $defaultPreferences['emailauthentication'] = array(
395 'section' => 'personal/email',
396 'label-message' => 'prefs-emailconfirm-label',
397 'default' => $emailauthenticated,
402 if ( $wgEnableUserEmail && $user->isAllowed( 'sendemail' ) ) {
403 $defaultPreferences['disablemail'] = array(
406 'section' => 'personal/email',
407 'label-message' => 'allowemail',
408 'disabled' => $disableEmailPrefs,
410 $defaultPreferences['ccmeonemails'] = array(
412 'section' => 'personal/email',
413 'label-message' => 'tog-ccmeonemails',
414 'disabled' => $disableEmailPrefs,
418 if ( $wgEnotifWatchlist ) {
419 $defaultPreferences['enotifwatchlistpages'] = array(
421 'section' => 'personal/email',
422 'label-message' => 'tog-enotifwatchlistpages',
423 'disabled' => $disableEmailPrefs,
426 if ( $wgEnotifUserTalk ) {
427 $defaultPreferences['enotifusertalkpages'] = array(
429 'section' => 'personal/email',
430 'label-message' => 'tog-enotifusertalkpages',
431 'disabled' => $disableEmailPrefs,
434 if ( $wgEnotifUserTalk ||
$wgEnotifWatchlist ) {
435 $defaultPreferences['enotifminoredits'] = array(
437 'section' => 'personal/email',
438 'label-message' => 'tog-enotifminoredits',
439 'disabled' => $disableEmailPrefs,
442 if ( $wgEnotifRevealEditorAddress ) {
443 $defaultPreferences['enotifrevealaddr'] = array(
445 'section' => 'personal/email',
446 'label-message' => 'tog-enotifrevealaddr',
447 'disabled' => $disableEmailPrefs,
456 * @param $context IContextSource
457 * @param $defaultPreferences
459 static function skinPreferences( $user, IContextSource
$context, &$defaultPreferences ) {
460 ## Skin #####################################
461 global $wgAllowUserCss, $wgAllowUserJs;
463 $defaultPreferences['skin'] = array(
465 'options' => self
::generateSkinOptions( $user, $context ),
467 'section' => 'rendering/skin',
470 # Create links to user CSS/JS pages for all skins
471 # This code is basically copied from generateSkinOptions(). It'd
472 # be nice to somehow merge this back in there to avoid redundancy.
473 if ( $wgAllowUserCss ||
$wgAllowUserJs ) {
474 $linkTools = array();
476 if ( $wgAllowUserCss ) {
477 $cssPage = Title
::makeTitleSafe( NS_USER
, $user->getName() . '/common.css' );
478 $linkTools[] = Linker
::link( $cssPage, $context->msg( 'prefs-custom-css' )->escaped() );
481 if ( $wgAllowUserJs ) {
482 $jsPage = Title
::makeTitleSafe( NS_USER
, $user->getName() . '/common.js' );
483 $linkTools[] = Linker
::link( $jsPage, $context->msg( 'prefs-custom-js' )->escaped() );
486 $defaultPreferences['commoncssjs'] = array(
489 'default' => $context->getLanguage()->pipeList( $linkTools ),
490 'label-message' => 'prefs-common-css-js',
491 'section' => 'rendering/skin',
495 $selectedSkin = $user->getOption( 'skin' );
496 if ( in_array( $selectedSkin, array( 'cologneblue', 'standard' ) ) ) {
497 $settings = array_flip( $context->getLanguage()->getQuickbarSettings() );
499 $defaultPreferences['quickbar'] = array(
501 'options' => $settings,
502 'section' => 'rendering/skin',
503 'label-message' => 'qbsettings',
510 * @param $context IContextSource
511 * @param $defaultPreferences Array
513 static function filesPreferences( $user, IContextSource
$context, &$defaultPreferences ) {
514 ## Files #####################################
515 $defaultPreferences['imagesize'] = array(
517 'options' => self
::getImageSizes( $context ),
518 'label-message' => 'imagemaxsize',
519 'section' => 'rendering/files',
521 $defaultPreferences['thumbsize'] = array(
523 'options' => self
::getThumbSizes( $context ),
524 'label-message' => 'thumbsize',
525 'section' => 'rendering/files',
531 * @param $context IContextSource
532 * @param $defaultPreferences
534 static function datetimePreferences( $user, IContextSource
$context, &$defaultPreferences ) {
535 ## Date and time #####################################
536 $dateOptions = self
::getDateOptions( $context );
537 if ( $dateOptions ) {
538 $defaultPreferences['date'] = array(
540 'options' => $dateOptions,
542 'section' => 'datetime/dateformat',
547 $now = wfTimestampNow();
548 $lang = $context->getLanguage();
549 $nowlocal = Xml
::element( 'span', array( 'id' => 'wpLocalTime' ),
550 $lang->time( $now, true ) );
551 $nowserver = $lang->time( $now, false ) .
552 Html
::hidden( 'wpServerTime', (int)substr( $now, 8, 2 ) * 60 +
(int)substr( $now, 10, 2 ) );
554 $defaultPreferences['nowserver'] = array(
557 'label-message' => 'servertime',
558 'default' => $nowserver,
559 'section' => 'datetime/timeoffset',
562 $defaultPreferences['nowlocal'] = array(
565 'label-message' => 'localtime',
566 'default' => $nowlocal,
567 'section' => 'datetime/timeoffset',
570 // Grab existing pref.
571 $tzOffset = $user->getOption( 'timecorrection' );
572 $tz = explode( '|', $tzOffset, 3 );
574 $tzOptions = self
::getTimezoneOptions( $context );
576 $tzSetting = $tzOffset;
577 if ( count( $tz ) > 1 && $tz[0] == 'Offset' ) {
579 $tzSetting = sprintf( '%+03d:%02d', floor( $minDiff / 60 ), abs( $minDiff ) %
60 );
580 } elseif ( count( $tz ) > 1 && $tz[0] == 'ZoneInfo' &&
581 !in_array( $tzOffset, HTMLFormField
::flattenOptions( $tzOptions ) ) )
583 # Timezone offset can vary with DST
584 $userTZ = timezone_open( $tz[2] );
585 if ( $userTZ !== false ) {
586 $minDiff = floor( timezone_offset_get( $userTZ, date_create( 'now' ) ) / 60 );
587 $tzSetting = "ZoneInfo|$minDiff|{$tz[2]}";
591 $defaultPreferences['timecorrection'] = array(
592 'class' => 'HTMLSelectOrOtherField',
593 'label-message' => 'timezonelegend',
594 'options' => $tzOptions,
595 'default' => $tzSetting,
597 'section' => 'datetime/timeoffset',
603 * @param $context IContextSource
604 * @param $defaultPreferences Array
606 static function renderingPreferences( $user, IContextSource
$context, &$defaultPreferences ) {
607 ## Page Rendering ##############################
608 global $wgAllowUserCssPrefs;
609 if ( $wgAllowUserCssPrefs ) {
610 $defaultPreferences['underline'] = array(
613 $context->msg( 'underline-never' )->text() => 0,
614 $context->msg( 'underline-always' )->text() => 1,
615 $context->msg( 'underline-default' )->text() => 2,
617 'label-message' => 'tog-underline',
618 'section' => 'rendering/advancedrendering',
622 $stubThresholdValues = array( 50, 100, 500, 1000, 2000, 5000, 10000 );
623 $stubThresholdOptions = array( $context->msg( 'stub-threshold-disabled' )->text() => 0 );
624 foreach ( $stubThresholdValues as $value ) {
625 $stubThresholdOptions[$context->msg( 'size-bytes', $value )->text()] = $value;
628 $defaultPreferences['stubthreshold'] = array(
629 'type' => 'selectorother',
630 'section' => 'rendering/advancedrendering',
631 'options' => $stubThresholdOptions,
633 'label' => $context->msg( 'stub-threshold' )->text(), // Raw HTML message. Yay?
636 if ( $wgAllowUserCssPrefs ) {
637 $defaultPreferences['highlightbroken'] = array(
639 'section' => 'rendering/advancedrendering',
640 'label' => $context->msg( 'tog-highlightbroken' )->text(), // Raw HTML
642 $defaultPreferences['showtoc'] = array(
644 'section' => 'rendering/advancedrendering',
645 'label-message' => 'tog-showtoc',
648 $defaultPreferences['nocache'] = array(
650 'label-message' => 'tog-nocache',
651 'section' => 'rendering/advancedrendering',
653 $defaultPreferences['showhiddencats'] = array(
655 'section' => 'rendering/advancedrendering',
656 'label-message' => 'tog-showhiddencats'
658 $defaultPreferences['showjumplinks'] = array(
660 'section' => 'rendering/advancedrendering',
661 'label-message' => 'tog-showjumplinks',
664 if ( $wgAllowUserCssPrefs ) {
665 $defaultPreferences['justify'] = array(
667 'section' => 'rendering/advancedrendering',
668 'label-message' => 'tog-justify',
672 $defaultPreferences['numberheadings'] = array(
674 'section' => 'rendering/advancedrendering',
675 'label-message' => 'tog-numberheadings',
681 * @param $context IContextSource
682 * @param $defaultPreferences Array
684 static function editingPreferences( $user, IContextSource
$context, &$defaultPreferences ) {
685 global $wgUseExternalEditor, $wgAllowUserCssPrefs;
687 ## Editing #####################################
689 $defaultPreferences['rows'] = array(
691 'label-message' => 'rows',
692 'section' => 'editing/textboxsize',
697 if ( $wgAllowUserCssPrefs ) {
698 $defaultPreferences['editfont'] = array(
700 'section' => 'editing/advancedediting',
701 'label-message' => 'editfont-style',
703 $context->msg( 'editfont-default' )->text() => 'default',
704 $context->msg( 'editfont-monospace' )->text() => 'monospace',
705 $context->msg( 'editfont-sansserif' )->text() => 'sans-serif',
706 $context->msg( 'editfont-serif' )->text() => 'serif',
710 $defaultPreferences['previewontop'] = array(
712 'section' => 'editing/advancedediting',
713 'label-message' => 'tog-previewontop',
715 $defaultPreferences['previewonfirst'] = array(
717 'section' => 'editing/advancedediting',
718 'label-message' => 'tog-previewonfirst',
721 if ( $wgAllowUserCssPrefs ) {
722 $defaultPreferences['editsection'] = array(
724 'section' => 'editing/advancedediting',
725 'label-message' => 'tog-editsection',
728 $defaultPreferences['editsectiononrightclick'] = array(
730 'section' => 'editing/advancedediting',
731 'label-message' => 'tog-editsectiononrightclick',
733 $defaultPreferences['editondblclick'] = array(
735 'section' => 'editing/advancedediting',
736 'label-message' => 'tog-editondblclick',
738 $defaultPreferences['showtoolbar'] = array(
740 'section' => 'editing/advancedediting',
741 'label-message' => 'tog-showtoolbar',
744 if ( $user->isAllowed( 'minoredit' ) ) {
745 $defaultPreferences['minordefault'] = array(
747 'section' => 'editing/advancedediting',
748 'label-message' => 'tog-minordefault',
752 if ( $wgUseExternalEditor ) {
753 $defaultPreferences['externaleditor'] = array(
755 'section' => 'editing/advancedediting',
756 'label-message' => 'tog-externaleditor',
758 $defaultPreferences['externaldiff'] = array(
760 'section' => 'editing/advancedediting',
761 'label-message' => 'tog-externaldiff',
765 $defaultPreferences['forceeditsummary'] = array(
767 'section' => 'editing/advancedediting',
768 'label-message' => 'tog-forceeditsummary',
772 $defaultPreferences['uselivepreview'] = array(
774 'section' => 'editing/advancedediting',
775 'label-message' => 'tog-uselivepreview',
781 * @param $context IContextSource
782 * @param $defaultPreferences Array
784 static function rcPreferences( $user, IContextSource
$context, &$defaultPreferences ) {
785 global $wgRCMaxAge, $wgRCShowWatchingUsers;
787 ## RecentChanges #####################################
788 $defaultPreferences['rcdays'] = array(
790 'label-message' => 'recentchangesdays',
791 'section' => 'rc/displayrc',
793 'max' => ceil( $wgRCMaxAge / ( 3600 * 24 ) ),
794 'help' => $context->msg( 'recentchangesdays-max' )->numParams(
795 ceil( $wgRCMaxAge / ( 3600 * 24 ) ) )->text()
797 $defaultPreferences['rclimit'] = array(
799 'label-message' => 'recentchangescount',
800 'help-message' => 'prefs-help-recentchangescount',
801 'section' => 'rc/displayrc',
803 $defaultPreferences['usenewrc'] = array(
805 'label-message' => 'tog-usenewrc',
806 'section' => 'rc/advancedrc',
808 $defaultPreferences['hideminor'] = array(
810 'label-message' => 'tog-hideminor',
811 'section' => 'rc/advancedrc',
814 if ( $user->useRCPatrol() ) {
815 $defaultPreferences['hidepatrolled'] = array(
817 'section' => 'rc/advancedrc',
818 'label-message' => 'tog-hidepatrolled',
820 $defaultPreferences['newpageshidepatrolled'] = array(
822 'section' => 'rc/advancedrc',
823 'label-message' => 'tog-newpageshidepatrolled',
827 if ( $wgRCShowWatchingUsers ) {
828 $defaultPreferences['shownumberswatching'] = array(
830 'section' => 'rc/advancedrc',
831 'label-message' => 'tog-shownumberswatching',
838 * @param $context IContextSource
839 * @param $defaultPreferences
841 static function watchlistPreferences( $user, IContextSource
$context, &$defaultPreferences ) {
842 global $wgUseRCPatrol, $wgEnableAPI, $wgRCMaxAge;
844 $watchlistdaysMax = ceil( $wgRCMaxAge / ( 3600 * 24 ) );
846 ## Watchlist #####################################
847 $defaultPreferences['watchlistdays'] = array(
850 'max' => $watchlistdaysMax,
851 'section' => 'watchlist/displaywatchlist',
852 'help' => $context->msg( 'prefs-watchlist-days-max' )->numParams(
853 $watchlistdaysMax )->text(),
854 'label-message' => 'prefs-watchlist-days',
856 $defaultPreferences['wllimit'] = array(
860 'label-message' => 'prefs-watchlist-edits',
861 'help' => $context->msg( 'prefs-watchlist-edits-max' )->escaped(),
862 'section' => 'watchlist/displaywatchlist',
864 $defaultPreferences['extendwatchlist'] = array(
866 'section' => 'watchlist/advancedwatchlist',
867 'label-message' => 'tog-extendwatchlist',
869 $defaultPreferences['watchlisthideminor'] = array(
871 'section' => 'watchlist/advancedwatchlist',
872 'label-message' => 'tog-watchlisthideminor',
874 $defaultPreferences['watchlisthidebots'] = array(
876 'section' => 'watchlist/advancedwatchlist',
877 'label-message' => 'tog-watchlisthidebots',
879 $defaultPreferences['watchlisthideown'] = array(
881 'section' => 'watchlist/advancedwatchlist',
882 'label-message' => 'tog-watchlisthideown',
884 $defaultPreferences['watchlisthideanons'] = array(
886 'section' => 'watchlist/advancedwatchlist',
887 'label-message' => 'tog-watchlisthideanons',
889 $defaultPreferences['watchlisthideliu'] = array(
891 'section' => 'watchlist/advancedwatchlist',
892 'label-message' => 'tog-watchlisthideliu',
895 if ( $wgUseRCPatrol ) {
896 $defaultPreferences['watchlisthidepatrolled'] = array(
898 'section' => 'watchlist/advancedwatchlist',
899 'label-message' => 'tog-watchlisthidepatrolled',
903 if ( $wgEnableAPI ) {
904 # Some random gibberish as a proposed default
905 $hash = sha1( mt_rand() . microtime( true ) );
907 $defaultPreferences['watchlisttoken'] = array(
909 'section' => 'watchlist/advancedwatchlist',
910 'label-message' => 'prefs-watchlist-token',
911 'help' => $context->msg( 'prefs-help-watchlist-token', $hash )->escaped()
916 'edit' => 'watchdefault',
917 'move' => 'watchmoves',
918 'delete' => 'watchdeletion'
922 if ( $user->isAllowed( 'createpage' ) ||
$user->isAllowed( 'createtalk' ) ) {
923 $watchTypes['read'] = 'watchcreations';
926 foreach ( $watchTypes as $action => $pref ) {
927 if ( $user->isAllowed( $action ) ) {
928 $defaultPreferences[$pref] = array(
930 'section' => 'watchlist/advancedwatchlist',
931 'label-message' => "tog-$pref",
939 * @param $context IContextSource
940 * @param $defaultPreferences Array
942 static function searchPreferences( $user, IContextSource
$context, &$defaultPreferences ) {
943 global $wgContLang, $wgEnableMWSuggest, $wgVectorUseSimpleSearch;
945 ## Search #####################################
946 $defaultPreferences['searchlimit'] = array(
948 'label-message' => 'resultsperpage',
949 'section' => 'searchoptions/displaysearchoptions',
953 if ( $wgEnableMWSuggest ) {
954 $defaultPreferences['disablesuggest'] = array(
956 'label-message' => 'mwsuggest-disable',
957 'section' => 'searchoptions/displaysearchoptions',
961 if ( $wgVectorUseSimpleSearch ) {
962 $defaultPreferences['vector-simplesearch'] = array(
964 'label-message' => 'vector-simplesearch-preference',
965 'section' => 'searchoptions/displaysearchoptions'
969 $defaultPreferences['searcheverything'] = array(
971 'label-message' => 'searcheverything-enable',
972 'section' => 'searchoptions/advancedsearchoptions',
975 $nsOptions = array();
977 foreach ( $wgContLang->getNamespaces() as $ns => $name ) {
982 $displayNs = str_replace( '_', ' ', $name );
985 $displayNs = $context->msg( 'blanknamespace' )->text();
988 $displayNs = htmlspecialchars( $displayNs );
989 $nsOptions[$displayNs] = $ns;
992 $defaultPreferences['searchnamespaces'] = array(
993 'type' => 'multiselect',
994 'label-message' => 'defaultns',
995 'options' => $nsOptions,
996 'section' => 'searchoptions/advancedsearchoptions',
997 'prefix' => 'searchNs',
1003 * @param $context IContextSource
1004 * @param $defaultPreferences Array
1006 static function miscPreferences( $user, IContextSource
$context, &$defaultPreferences ) {
1009 ## Misc #####################################
1010 $defaultPreferences['diffonly'] = array(
1012 'section' => 'misc/diffs',
1013 'label-message' => 'tog-diffonly',
1015 $defaultPreferences['norollbackdiff'] = array(
1017 'section' => 'misc/diffs',
1018 'label-message' => 'tog-norollbackdiff',
1021 // Stuff from Language::getExtraUserToggles()
1022 $toggles = $wgContLang->getExtraUserToggles();
1024 foreach ( $toggles as $toggle ) {
1025 $defaultPreferences[$toggle] = array(
1027 'section' => 'personal/i18n',
1028 'label-message' => "tog-$toggle",
1034 * @param $user User The User object
1035 * @param $context IContextSource
1036 * @return Array: text/links to display as key; $skinkey as value
1038 static function generateSkinOptions( $user, IContextSource
$context ) {
1039 global $wgDefaultSkin, $wgAllowUserCss, $wgAllowUserJs;
1042 $mptitle = Title
::newMainPage();
1043 $previewtext = $context->msg( 'skin-preview' )->text();
1045 # Only show members of Skin::getSkinNames() rather than
1046 # $skinNames (skins is all skin names from Language.php)
1047 $validSkinNames = Skin
::getUsableSkins();
1049 # Sort by UI skin name. First though need to update validSkinNames as sometimes
1050 # the skinkey & UI skinname differ (e.g. "standard" skinkey is "Classic" in the UI).
1051 foreach ( $validSkinNames as $skinkey => &$skinname ) {
1052 $msg = $context->msg( "skinname-{$skinkey}" );
1053 if ( $msg->exists() ) {
1054 $skinname = htmlspecialchars( $msg->text() );
1057 asort( $validSkinNames );
1059 foreach ( $validSkinNames as $skinkey => $sn ) {
1060 $linkTools = array();
1062 # Mark the default skin
1063 if ( $skinkey == $wgDefaultSkin ) {
1064 $linkTools[] = $context->msg( 'default' )->escaped();
1067 # Create preview link
1068 $mplink = htmlspecialchars( $mptitle->getLocalURL( "useskin=$skinkey" ) );
1069 $linkTools[] = "<a target='_blank' href=\"$mplink\">$previewtext</a>";
1071 # Create links to user CSS/JS pages
1072 if ( $wgAllowUserCss ) {
1073 $cssPage = Title
::makeTitleSafe( NS_USER
, $user->getName() . '/' . $skinkey . '.css' );
1074 $linkTools[] = Linker
::link( $cssPage, $context->msg( 'prefs-custom-css' )->escaped() );
1077 if ( $wgAllowUserJs ) {
1078 $jsPage = Title
::makeTitleSafe( NS_USER
, $user->getName() . '/' . $skinkey . '.js' );
1079 $linkTools[] = Linker
::link( $jsPage, $context->msg( 'prefs-custom-js' )->escaped() );
1082 $display = $sn . ' ' . $context->msg( 'parentheses', $context->getLanguage()->pipeList( $linkTools ) )->text();
1083 $ret[$display] = $skinkey;
1090 * @param $context IContextSource
1093 static function getDateOptions( IContextSource
$context ) {
1094 $lang = $context->getLanguage();
1095 $dateopts = $lang->getDatePreferences();
1100 if ( !in_array( 'default', $dateopts ) ) {
1101 $dateopts[] = 'default'; // Make sure default is always valid
1105 // KLUGE: site default might not be valid for user language
1106 global $wgDefaultUserOptions;
1107 if ( !in_array( $wgDefaultUserOptions['date'], $dateopts ) ) {
1108 $wgDefaultUserOptions['date'] = 'default';
1111 $epoch = wfTimestampNow();
1112 foreach ( $dateopts as $key ) {
1113 if ( $key == 'default' ) {
1114 $formatted = $context->msg( 'datedefault' )->escaped();
1116 $formatted = htmlspecialchars( $lang->timeanddate( $epoch, false, $key ) );
1118 $ret[$formatted] = $key;
1125 * @param $context IContextSource
1128 static function getImageSizes( IContextSource
$context ) {
1129 global $wgImageLimits;
1132 $pixels = $context->msg( 'unit-pixel' )->text();
1134 foreach ( $wgImageLimits as $index => $limits ) {
1135 $display = "{$limits[0]}×{$limits[1]}" . $pixels;
1136 $ret[$display] = $index;
1143 * @param $context IContextSource
1146 static function getThumbSizes( IContextSource
$context ) {
1147 global $wgThumbLimits;
1150 $pixels = $context->msg( 'unit-pixel' )->text();
1152 foreach ( $wgThumbLimits as $index => $size ) {
1153 $display = $size . $pixels;
1154 $ret[$display] = $index;
1161 * @param $signature string
1162 * @param $alldata array
1163 * @param $form HTMLForm
1164 * @return bool|string
1166 static function validateSignature( $signature, $alldata, $form ) {
1167 global $wgParser, $wgMaxSigChars;
1168 if ( mb_strlen( $signature ) > $wgMaxSigChars ) {
1169 return Xml
::element( 'span', array( 'class' => 'error' ),
1170 $form->msg( 'badsiglength' )->numParams( $wgMaxSigChars )->text() );
1171 } elseif ( isset( $alldata['fancysig'] ) &&
1172 $alldata['fancysig'] &&
1173 false === $wgParser->validateSig( $signature ) ) {
1174 return Xml
::element( 'span', array( 'class' => 'error' ), $form->msg( 'badsig' )->text() );
1181 * @param $signature string
1182 * @param $alldata array
1183 * @param $form HTMLForm
1186 static function cleanSignature( $signature, $alldata, $form ) {
1187 if ( isset( $alldata['fancysig'] ) && $alldata['fancysig'] ) {
1189 $signature = $wgParser->cleanSig( $signature );
1191 // When no fancy sig used, make sure ~{3,5} get removed.
1192 $signature = Parser
::cleanSigInSig( $signature );
1200 * @param $context IContextSource
1201 * @param $formClass string
1202 * @param $remove Array: array of items to remove
1205 static function getFormObject( $user, IContextSource
$context, $formClass = 'PreferencesForm', array $remove = array() ) {
1206 $formDescriptor = Preferences
::getPreferences( $user, $context );
1207 if ( count( $remove ) ) {
1208 $removeKeys = array_flip( $remove );
1209 $formDescriptor = array_diff_key( $formDescriptor, $removeKeys );
1211 $htmlForm = new $formClass( $formDescriptor, $context, 'prefs' );
1213 $htmlForm->setModifiedUser( $user );
1214 $htmlForm->setId( 'mw-prefs-form' );
1215 $htmlForm->setSubmitText( $context->msg( 'saveprefs' )->text() );
1216 # Used message keys: 'accesskey-preferences-save', 'tooltip-preferences-save'
1217 $htmlForm->setSubmitTooltip( 'preferences-save' );
1218 $htmlForm->setSubmitID( 'prefsubmit' );
1219 $htmlForm->setSubmitCallback( array( 'Preferences', 'tryFormSubmit' ) );
1227 static function getTimezoneOptions( IContextSource
$context ) {
1230 global $wgLocalTZoffset, $wgLocaltimezone;
1231 // Check that $wgLocalTZoffset is the same as $wgLocaltimezone
1232 if ( $wgLocalTZoffset == date( 'Z' ) / 60 ) {
1233 $server_tz_msg = $context->msg( 'timezoneuseserverdefault', $wgLocaltimezone )->text();
1235 $tzstring = sprintf( '%+03d:%02d', floor( $wgLocalTZoffset / 60 ), abs( $wgLocalTZoffset ) %
60 );
1236 $server_tz_msg = $context->msg( 'timezoneuseserverdefault', $tzstring )->text();
1238 $opt[$server_tz_msg] = "System|$wgLocalTZoffset";
1239 $opt[$context->msg( 'timezoneuseoffset' )->text()] = 'other';
1240 $opt[$context->msg( 'guesstimezone' )->text()] = 'guess';
1242 if ( function_exists( 'timezone_identifiers_list' ) ) {
1243 # Read timezone list
1244 $tzs = timezone_identifiers_list();
1247 $tzRegions = array();
1248 $tzRegions['Africa'] = $context->msg( 'timezoneregion-africa' )->text();
1249 $tzRegions['America'] = $context->msg( 'timezoneregion-america' )->text();
1250 $tzRegions['Antarctica'] = $context->msg( 'timezoneregion-antarctica' )->text();
1251 $tzRegions['Arctic'] = $context->msg( 'timezoneregion-arctic' )->text();
1252 $tzRegions['Asia'] = $context->msg( 'timezoneregion-asia' )->text();
1253 $tzRegions['Atlantic'] = $context->msg( 'timezoneregion-atlantic' )->text();
1254 $tzRegions['Australia'] = $context->msg( 'timezoneregion-australia' )->text();
1255 $tzRegions['Europe'] = $context->msg( 'timezoneregion-europe' )->text();
1256 $tzRegions['Indian'] = $context->msg( 'timezoneregion-indian' )->text();
1257 $tzRegions['Pacific'] = $context->msg( 'timezoneregion-pacific' )->text();
1258 asort( $tzRegions );
1260 $prefill = array_fill_keys( array_values( $tzRegions ), array() );
1261 $opt = array_merge( $opt, $prefill );
1263 $now = date_create( 'now' );
1265 foreach ( $tzs as $tz ) {
1266 $z = explode( '/', $tz, 2 );
1268 # timezone_identifiers_list() returns a number of
1269 # backwards-compatibility entries. This filters them out of the
1270 # list presented to the user.
1271 if ( count( $z ) != 2 ||
!array_key_exists( $z[0], $tzRegions ) ) {
1276 $z[0] = $tzRegions[$z[0]];
1278 $minDiff = floor( timezone_offset_get( timezone_open( $tz ), $now ) / 60 );
1280 $display = str_replace( '_', ' ', $z[0] . '/' . $z[1] );
1281 $value = "ZoneInfo|$minDiff|$tz";
1283 $opt[$z[0]][$display] = $value;
1294 static function filterIntval( $value, $alldata ){
1295 return intval( $value );
1303 static function filterTimezoneInput( $tz, $alldata ) {
1304 $data = explode( '|', $tz, 3 );
1305 switch ( $data[0] ) {
1310 $data = explode( ':', $tz, 2 );
1311 if ( count( $data ) == 2 ) {
1312 $data[0] = intval( $data[0] );
1313 $data[1] = intval( $data[1] );
1314 $minDiff = abs( $data[0] ) * 60 +
$data[1];
1315 if ( $data[0] < 0 ) $minDiff = - $minDiff;
1317 $minDiff = intval( $data[0] ) * 60;
1320 # Max is +14:00 and min is -12:00, see:
1321 # http://en.wikipedia.org/wiki/Timezone
1322 $minDiff = min( $minDiff, 840 ); # 14:00
1323 $minDiff = max( $minDiff, - 720 ); # -12:00
1324 return 'Offset|' . $minDiff;
1330 * @param $form HTMLForm
1331 * @param $entryPoint string
1332 * @return bool|Status|string
1334 static function tryFormSubmit( $formData, $form, $entryPoint = 'internal' ) {
1335 global $wgHiddenPrefs;
1337 $user = $form->getModifiedUser();
1341 foreach ( array_keys( $formData ) as $name ) {
1342 if ( isset( self
::$saveFilters[$name] ) ) {
1344 call_user_func( self
::$saveFilters[$name], $formData[$name], $formData );
1348 // Stuff that shouldn't be saved as a preference.
1349 $saveBlacklist = array(
1354 // Fortunately, the realname field is MUCH simpler
1355 if ( !in_array( 'realname', $wgHiddenPrefs ) ) {
1356 $realName = $formData['realname'];
1357 $user->setRealName( $realName );
1360 foreach ( $saveBlacklist as $b ) {
1361 unset( $formData[$b] );
1364 # If users have saved a value for a preference which has subsequently been disabled
1365 # via $wgHiddenPrefs, we don't want to destroy that setting in case the preference
1366 # is subsequently re-enabled
1367 # TODO: maintenance script to actually delete these
1368 foreach( $wgHiddenPrefs as $pref ){
1369 # If the user has not set a non-default value here, the default will be returned
1370 # and subsequently discarded
1371 $formData[$pref] = $user->getOption( $pref, null, true );
1374 // Keeps old preferences from interfering due to back-compat
1376 $user->resetOptions();
1378 foreach ( $formData as $key => $value ) {
1379 $user->setOption( $key, $value );
1382 $user->saveSettings();
1391 public static function tryUISubmit( $formData, $form ) {
1392 $res = self
::tryFormSubmit( $formData, $form, 'ui' );
1395 $urlOptions = array( 'success' => 1 );
1397 if ( $res === 'eauth' ) {
1398 $urlOptions['eauth'] = 1;
1401 $urlOptions +
= $form->getExtraSuccessRedirectParameters();
1403 $url = $form->getTitle()->getFullURL( $urlOptions );
1405 $form->getContext()->getOutput()->redirect( $url );
1408 return Status
::newGood();
1412 * Try to set a user's email address.
1413 * This does *not* try to validate the address.
1414 * Caller is responsible for checking $wgAuth.
1416 * @param $newaddr string New email address
1417 * @return Array (true on success or Status on failure, info string)
1419 public static function trySetUserEmail( User
$user, $newaddr ) {
1420 global $wgEnableEmail, $wgEmailAuthentication;
1423 if ( $wgEnableEmail ) {
1424 $oldaddr = $user->getEmail();
1425 if ( ( $newaddr != '' ) && ( $newaddr != $oldaddr ) ) {
1426 # The user has supplied a new email address on the login page
1427 # new behaviour: set this new emailaddr from login-page into user database record
1428 $user->setEmail( $newaddr );
1429 if ( $wgEmailAuthentication ) {
1430 # Mail a temporary password to the dirty address.
1431 # User can come back through the confirmation URL to re-enable email.
1432 $type = $oldaddr != '' ?
'changed' : 'set';
1433 $result = $user->sendConfirmationMail( $type );
1434 if ( !$result->isGood() ) {
1435 return array( $result, 'mailerror' );
1439 } elseif ( $newaddr != $oldaddr ) { // if the address is the same, don't change it
1440 $user->setEmail( $newaddr );
1442 if ( $oldaddr != $newaddr ) {
1443 wfRunHooks( 'PrefsEmailAudit', array( $user, $oldaddr, $newaddr ) );
1447 return array( true, $info );
1454 public static function loadOldSearchNs( $user ) {
1455 $searchableNamespaces = SearchEngine
::searchableNamespaces();
1456 // Back compat with old format
1459 foreach ( $searchableNamespaces as $ns => $name ) {
1460 if ( $user->getOption( 'searchNs' . $ns ) ) {
1469 /** Some tweaks to allow js prefs to work */
1470 class PreferencesForm
extends HTMLForm
{
1471 // Override default value from HTMLForm
1472 protected $mSubSectionBeforeFields = false;
1474 private $modifiedUser;
1476 public function setModifiedUser( $user ) {
1477 $this->modifiedUser
= $user;
1480 public function getModifiedUser() {
1481 if ( $this->modifiedUser
=== null ) {
1482 return $this->getUser();
1484 return $this->modifiedUser
;
1489 * Get extra parameters for the query string when redirecting after
1494 public function getExtraSuccessRedirectParameters() {
1499 * @param $html string
1502 function wrapForm( $html ) {
1503 $html = Xml
::tags( 'div', array( 'id' => 'preferences' ), $html );
1505 return parent
::wrapForm( $html );
1511 function getButtons() {
1512 $html = parent
::getButtons();
1514 $t = SpecialPage
::getTitleFor( 'Preferences', 'reset' );
1516 $html .= "\n" . Linker
::link( $t, $this->msg( 'restoreprefs' )->escaped() );
1518 $html = Xml
::tags( 'div', array( 'class' => 'mw-prefs-buttons' ), $html );
1524 * @param $data array
1527 function filterDataForSubmit( $data ) {
1528 // Support for separating MultiSelect preferences into multiple preferences
1529 // Due to lack of array support.
1530 foreach ( $this->mFlatFields
as $fieldname => $field ) {
1531 $info = $field->mParams
;
1532 if ( $field instanceof HTMLMultiSelectField
) {
1533 $options = HTMLFormField
::flattenOptions( $info['options'] );
1534 $prefix = isset( $info['prefix'] ) ?
$info['prefix'] : $fieldname;
1536 foreach ( $options as $opt ) {
1537 $data["$prefix$opt"] = in_array( $opt, $data[$fieldname] );
1540 unset( $data[$fieldname] );
1547 * Get the whole body of the form.
1549 function getBody() {
1550 return $this->displaySection( $this->mFieldTree
, '', 'mw-prefsection-' );
1554 * Get the <legend> for a given section key. Normally this is the
1555 * prefs-$key message but we'll allow extensions to override it.
1557 function getLegend( $key ) {
1558 $legend = parent
::getLegend( $key );
1559 wfRunHooks( 'PreferencesGetLegend', array( $this, $key, &$legend ) );