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' ),
34 static function getPreferences( $user ) {
35 if ( self
::$defaultPreferences )
36 return self
::$defaultPreferences;
38 $defaultPreferences = array();
40 self
::profilePreferences( $user, $defaultPreferences );
41 self
::skinPreferences( $user, $defaultPreferences );
42 self
::filesPreferences( $user, $defaultPreferences );
43 self
::mathPreferences( $user, $defaultPreferences );
44 self
::datetimePreferences( $user, $defaultPreferences );
45 self
::renderingPreferences( $user, $defaultPreferences );
46 self
::editingPreferences( $user, $defaultPreferences );
47 self
::rcPreferences( $user, $defaultPreferences );
48 self
::watchlistPreferences( $user, $defaultPreferences );
49 self
::searchPreferences( $user, $defaultPreferences );
50 self
::miscPreferences( $user, $defaultPreferences );
52 wfRunHooks( 'GetPreferences', array( $user, &$defaultPreferences ) );
54 ## Remove preferences that wikis don't want to use
55 global $wgHiddenPrefs;
56 foreach ( $wgHiddenPrefs as $pref ) {
57 if ( isset( $defaultPreferences[$pref] ) ) {
58 unset( $defaultPreferences[$pref] );
62 ## Prod in defaults from the user
63 foreach ( $defaultPreferences as $name => &$info ) {
64 $prefFromUser = self
::getOptionFromUser( $name, $info, $user );
65 $field = HTMLForm
::loadInputFromParameters( $info ); // For validation
66 $defaultOptions = User
::getDefaultOptions();
67 $globalDefault = isset( $defaultOptions[$name] )
68 ?
$defaultOptions[$name]
71 // If it validates, set it as the default
72 if ( isset( $info['default'] ) ) {
73 // Already set, no problem
75 } elseif ( !is_null( $prefFromUser ) && // Make sure we're not just pulling nothing
76 $field->validate( $prefFromUser, $user->mOptions
) === true ) {
77 $info['default'] = $prefFromUser;
78 } elseif ( $field->validate( $globalDefault, $user->mOptions
) === true ) {
79 $info['default'] = $globalDefault;
81 throw new MWException( "Global default '$globalDefault' is invalid for field $name" );
85 self
::$defaultPreferences = $defaultPreferences;
87 return $defaultPreferences;
90 // Pull option from a user account. Handles stuff like array-type preferences.
91 static function getOptionFromUser( $name, $info, $user ) {
92 $val = $user->getOption( $name );
94 // Handling for array-type preferences
95 if ( ( isset( $info['type'] ) && $info['type'] == 'multiselect' ) ||
96 ( isset( $info['class'] ) && $info['class'] == 'HTMLMultiSelectField' ) ) {
97 $options = HTMLFormField
::flattenOptions( $info['options'] );
98 $prefix = isset( $info['prefix'] ) ?
$info['prefix'] : $name;
101 foreach ( $options as $label => $value ) {
102 if ( $user->getOption( "$prefix$value" ) ) {
111 static function profilePreferences( $user, &$defaultPreferences ) {
112 global $wgLang, $wgUser;
113 ## User info #####################################
115 $defaultPreferences['username'] = array(
117 'label-message' => 'username',
118 'default' => $user->getName(),
119 'section' => 'personal/info',
122 $defaultPreferences['userid'] = array(
124 'label-message' => 'uid',
125 'default' => $user->getId(),
126 'section' => 'personal/info',
129 # Get groups to which the user belongs
130 $userEffectiveGroups = $user->getEffectiveGroups();
131 $userGroups = $userMembers = array();
132 foreach ( $userEffectiveGroups as $ueg ) {
134 // Skip the default * group, seems useless here
137 $groupName = User
::getGroupName( $ueg );
138 $userGroups[] = User
::makeGroupLinkHTML( $ueg, $groupName );
140 $memberName = User
::getGroupMember( $ueg );
141 $userMembers[] = User
::makeGroupLinkHTML( $ueg, $memberName );
143 asort( $userGroups );
144 asort( $userMembers );
146 $defaultPreferences['usergroups'] = array(
149 'prefs-memberingroups', 'parseinline',
150 $wgLang->formatNum( count( $userGroups ) )
152 'default' => wfMsgExt(
153 'prefs-memberingroups-type', array(),
154 $wgLang->commaList( $userGroups ),
155 $wgLang->commaList( $userMembers )
158 'section' => 'personal/info',
161 $defaultPreferences['editcount'] = array(
163 'label-message' => 'prefs-edits',
164 'default' => $wgLang->formatNum( $user->getEditCount() ),
165 'section' => 'personal/info',
168 if ( $user->getRegistration() ) {
169 $defaultPreferences['registrationdate'] = array(
171 'label-message' => 'prefs-registration',
172 'default' => wfMsgExt(
173 'prefs-registration-date-time', 'parsemag',
174 $wgLang->timeanddate( $user->getRegistration(), true ),
175 $wgLang->date( $user->getRegistration(), true ),
176 $wgLang->time( $user->getRegistration(), true )
178 'section' => 'personal/info',
182 // Actually changeable stuff
184 $defaultPreferences['realname'] = array(
185 'type' => $wgAuth->allowPropChange( 'realname' ) ?
'text' : 'info',
186 'default' => $user->getRealName(),
187 'section' => 'personal/info',
188 'label-message' => 'yourrealname',
189 'help-message' => 'prefs-help-realname',
192 $defaultPreferences['gender'] = array(
194 'section' => 'personal/info',
196 wfMsg( 'gender-male' ) => 'male',
197 wfMsg( 'gender-female' ) => 'female',
198 wfMsg( 'gender-unknown' ) => 'unknown',
200 'label-message' => 'yourgender',
201 'help-message' => 'prefs-help-gender',
204 if ( $wgAuth->allowPasswordChange() ) {
205 $link = $wgUser->getSkin()->link( SpecialPage
::getTitleFor( 'Resetpass' ),
206 wfMsgHtml( 'prefs-resetpass' ), array(),
207 array( 'returnto' => SpecialPage
::getTitleFor( 'Preferences' ) ) );
209 $defaultPreferences['password'] = array(
213 'label-message' => 'yourpassword',
214 'section' => 'personal/info',
217 global $wgCookieExpiration;
218 if ( $wgCookieExpiration > 0 ) {
219 $defaultPreferences['rememberpassword'] = array(
222 'tog-rememberpassword',
224 $wgLang->formatNum( ceil( $wgCookieExpiration / ( 3600 * 24 ) ) )
226 'section' => 'personal/info',
231 global $wgContLanguageCode;
232 $languages = Language
::getLanguageNames( false );
233 if ( !array_key_exists( $wgContLanguageCode, $languages ) ) {
234 $languages[$wgContLanguageCode] = $wgContLanguageCode;
239 foreach ( $languages as $code => $name ) {
240 $display = wfBCP47( $code ) . ' - ' . $name;
241 $options[$display] = $code;
243 $defaultPreferences['language'] = array(
245 'section' => 'personal/i18n',
246 'options' => $options,
247 'label-message' => 'yourlanguage',
250 global $wgContLang, $wgDisableLangConversion;
251 global $wgDisableTitleConversion;
252 /* see if there are multiple language variants to choose from*/
253 $variantArray = array();
254 if ( !$wgDisableLangConversion ) {
255 $variants = $wgContLang->getVariants();
257 $languages = Language
::getLanguageNames( true );
258 foreach ( $variants as $v ) {
259 $v = str_replace( '_', '-', strtolower( $v ) );
260 if ( array_key_exists( $v, $languages ) ) {
261 // If it doesn't have a name, we'll pretend it doesn't exist
262 $variantArray[$v] = $languages[$v];
267 foreach ( $variantArray as $code => $name ) {
268 $display = wfBCP47( $code ) . ' - ' . $name;
269 $options[$display] = $code;
272 if ( count( $variantArray ) > 1 ) {
273 $defaultPreferences['variant'] = array(
274 'label-message' => 'yourvariant',
276 'options' => $options,
277 'section' => 'personal/i18n',
282 if ( count( $variantArray ) > 1 && !$wgDisableLangConversion && !$wgDisableTitleConversion ) {
283 $defaultPreferences['noconvertlink'] =
286 'section' => 'personal/i18n',
287 'label-message' => 'tog-noconvertlink',
291 global $wgMaxSigChars, $wgOut, $wgParser;
293 // show a preview of the old signature first
294 $oldsigWikiText = $wgParser->preSaveTransform( "~~~", new Title
, $user, new ParserOptions
);
295 $oldsigHTML = $wgOut->parseInline( $oldsigWikiText );
296 $defaultPreferences['oldsig'] = array(
299 'label-message' => 'tog-oldsig',
300 'default' => $oldsigHTML,
301 'section' => 'personal/signature',
303 $defaultPreferences['nickname'] = array(
304 'type' => $wgAuth->allowPropChange( 'nickname' ) ?
'text' : 'info',
305 'maxlength' => $wgMaxSigChars,
306 'label-message' => 'yournick',
307 'validation-callback' => array( 'Preferences', 'validateSignature' ),
308 'section' => 'personal/signature',
309 'filter-callback' => array( 'Preferences', 'cleanSignature' ),
311 $defaultPreferences['fancysig'] = array(
313 'label-message' => 'tog-fancysig',
314 'help-message' => 'prefs-help-signature', // show general help about signature at the bottom of the section
315 'section' => 'personal/signature'
320 global $wgEnableEmail;
321 if ( $wgEnableEmail ) {
322 global $wgEmailConfirmToEdit;
324 $defaultPreferences['emailaddress'] = array(
325 'type' => $wgAuth->allowPropChange( 'emailaddress' ) ?
'email' : 'info',
326 'default' => $user->getEmail(),
327 'section' => 'personal/email',
328 'label-message' => 'youremail',
329 'help-message' => $wgEmailConfirmToEdit
330 ?
'prefs-help-email-required'
331 : 'prefs-help-email',
332 'validation-callback' => array( 'Preferences', 'validateEmail' ),
335 global $wgEnableUserEmail, $wgEmailAuthentication;
337 $disableEmailPrefs = false;
339 if ( $wgEmailAuthentication ) {
340 if ( $user->getEmail() ) {
341 if ( $user->getEmailAuthenticationTimestamp() ) {
342 // date and time are separate parameters to facilitate localisation.
343 // $time is kept for backward compat reasons.
344 // 'emailauthenticated' is also used in SpecialConfirmemail.php
345 $time = $wgLang->timeAndDate( $user->getEmailAuthenticationTimestamp(), true );
346 $d = $wgLang->date( $user->getEmailAuthenticationTimestamp(), true );
347 $t = $wgLang->time( $user->getEmailAuthenticationTimestamp(), true );
348 $emailauthenticated = wfMsgExt(
349 'emailauthenticated', 'parseinline',
350 array( $time, $d, $t )
352 $disableEmailPrefs = false;
354 $disableEmailPrefs = true;
355 $skin = $wgUser->getSkin();
356 $emailauthenticated = wfMsgExt( 'emailnotauthenticated', 'parseinline' ) . '<br />' .
358 SpecialPage
::getTitleFor( 'Confirmemail' ),
359 wfMsg( 'emailconfirmlink' ),
362 array( 'known', 'noclasses' )
366 $disableEmailPrefs = true;
367 $emailauthenticated = wfMsgHtml( 'noemailprefs' );
370 $defaultPreferences['emailauthentication'] = array(
373 'section' => 'personal/email',
374 'label-message' => 'prefs-emailconfirm-label',
375 'default' => $emailauthenticated,
380 if ( $wgEnableUserEmail && $user->isAllowed( 'sendemail' ) ) {
381 $defaultPreferences['disablemail'] = array(
384 'section' => 'personal/email',
385 'label-message' => 'allowemail',
386 'disabled' => $disableEmailPrefs,
388 $defaultPreferences['ccmeonemails'] = array(
390 'section' => 'personal/email',
391 'label-message' => 'tog-ccmeonemails',
392 'disabled' => $disableEmailPrefs,
396 global $wgEnotifWatchlist;
397 if ( $wgEnotifWatchlist ) {
398 $defaultPreferences['enotifwatchlistpages'] = array(
400 'section' => 'personal/email',
401 'label-message' => 'tog-enotifwatchlistpages',
402 'disabled' => $disableEmailPrefs,
405 global $wgEnotifUserTalk;
406 if ( $wgEnotifUserTalk ) {
407 $defaultPreferences['enotifusertalkpages'] = array(
409 'section' => 'personal/email',
410 'label-message' => 'tog-enotifusertalkpages',
411 'disabled' => $disableEmailPrefs,
414 if ( $wgEnotifUserTalk ||
$wgEnotifWatchlist ) {
415 $defaultPreferences['enotifminoredits'] = array(
417 'section' => 'personal/email',
418 'label-message' => 'tog-enotifminoredits',
419 'disabled' => $disableEmailPrefs,
422 global $wgEnotifRevealEditorAddress;
423 if ( $wgEnotifRevealEditorAddress ) {
424 $defaultPreferences['enotifrevealaddr'] = array(
426 'section' => 'personal/email',
427 'label-message' => 'tog-enotifrevealaddr',
428 'disabled' => $disableEmailPrefs,
435 static function skinPreferences( $user, &$defaultPreferences ) {
436 ## Skin #####################################
437 global $wgLang, $wgAllowUserCss, $wgAllowUserJs;
439 $defaultPreferences['skin'] = array(
441 'options' => self
::generateSkinOptions( $user ),
443 'section' => 'rendering/skin',
446 # Create links to user CSS/JS pages for all skins
447 # This code is basically copied from generateSkinOptions(). It'd
448 # be nice to somehow merge this back in there to avoid redundancy.
449 if ( $wgAllowUserCss ||
$wgAllowUserJs ) {
450 $sk = $user->getSkin();
451 $linkTools = array();
453 if ( $wgAllowUserCss ) {
454 $cssPage = Title
::makeTitleSafe( NS_USER
, $user->getName() . '/common.css' );
455 $linkTools[] = $sk->link( $cssPage, wfMsgHtml( 'prefs-custom-css' ) );
458 if ( $wgAllowUserJs ) {
459 $jsPage = Title
::makeTitleSafe( NS_USER
, $user->getName() . '/common.js' );
460 $linkTools[] = $sk->link( $jsPage, wfMsgHtml( 'prefs-custom-js' ) );
463 $defaultPreferences['commoncssjs'] = array(
466 'default' => $wgLang->pipeList( $linkTools ),
467 'label-message' => 'prefs-common-css-js',
468 'section' => 'rendering/skin',
472 $selectedSkin = $user->getOption( 'skin' );
473 if ( in_array( $selectedSkin, array( 'cologneblue', 'standard' ) ) ) {
476 $settings = array_flip( $wgLang->getQuickbarSettings() );
478 $defaultPreferences['quickbar'] = array(
480 'options' => $settings,
481 'section' => 'rendering/skin',
482 'label-message' => 'qbsettings',
487 static function mathPreferences( $user, &$defaultPreferences ) {
488 ## Math #####################################
489 global $wgUseTeX, $wgLang;
491 $defaultPreferences['math'] = array(
493 'options' => array_flip( array_map( 'wfMsgHtml', $wgLang->getMathNames() ) ),
495 'section' => 'rendering/math',
500 static function filesPreferences( $user, &$defaultPreferences ) {
501 ## Files #####################################
502 $defaultPreferences['imagesize'] = array(
504 'options' => self
::getImageSizes(),
505 'label-message' => 'imagemaxsize',
506 'section' => 'rendering/files',
508 $defaultPreferences['thumbsize'] = array(
510 'options' => self
::getThumbSizes(),
511 'label-message' => 'thumbsize',
512 'section' => 'rendering/files',
516 static function datetimePreferences( $user, &$defaultPreferences ) {
519 ## Date and time #####################################
520 $dateOptions = self
::getDateOptions();
521 if ( $dateOptions ) {
522 $defaultPreferences['date'] = array(
524 'options' => $dateOptions,
526 'section' => 'datetime/dateformat',
531 $nowlocal = Xml
::element( 'span', array( 'id' => 'wpLocalTime' ),
532 $wgLang->time( $now = wfTimestampNow(), true ) );
533 $nowserver = $wgLang->time( $now, false ) .
534 Xml
::hidden( 'wpServerTime', substr( $now, 8, 2 ) * 60 +
substr( $now, 10, 2 ) );
536 $defaultPreferences['nowserver'] = array(
539 'label-message' => 'servertime',
540 'default' => $nowserver,
541 'section' => 'datetime/timeoffset',
544 $defaultPreferences['nowlocal'] = array(
547 'label-message' => 'localtime',
548 'default' => $nowlocal,
549 'section' => 'datetime/timeoffset',
552 // Grab existing pref.
553 $tzOffset = $user->getOption( 'timecorrection' );
554 $tz = explode( '|', $tzOffset, 2 );
556 $tzSetting = $tzOffset;
557 if ( count( $tz ) > 1 && $tz[0] == 'Offset' ) {
559 $tzSetting = sprintf( '%+03d:%02d', floor( $minDiff / 60 ), abs( $minDiff ) %
60 );
562 $defaultPreferences['timecorrection'] = array(
563 'class' => 'HTMLSelectOrOtherField',
564 'label-message' => 'timezonelegend',
565 'options' => self
::getTimezoneOptions(),
566 'default' => $tzSetting,
568 'section' => 'datetime/timeoffset',
572 static function renderingPreferences( $user, &$defaultPreferences ) {
573 ## Page Rendering ##############################
574 global $wgAllowUserCssPrefs;
575 if ( $wgAllowUserCssPrefs ) {
576 $defaultPreferences['underline'] = array(
579 wfMsg( 'underline-never' ) => 0,
580 wfMsg( 'underline-always' ) => 1,
581 wfMsg( 'underline-default' ) => 2,
583 'label-message' => 'tog-underline',
584 'section' => 'rendering/advancedrendering',
588 $stubThresholdValues = array( 0, 50, 100, 500, 1000, 2000, 5000, 10000 );
589 $stubThresholdOptions = array();
590 foreach ( $stubThresholdValues as $value ) {
591 $stubThresholdOptions[wfMsg( 'size-bytes', $value )] = $value;
594 $defaultPreferences['stubthreshold'] = array(
595 'type' => 'selectorother',
596 'section' => 'rendering/advancedrendering',
597 'options' => $stubThresholdOptions,
599 'label' => wfMsg( 'stub-threshold' ), // Raw HTML message. Yay?
602 if ( $wgAllowUserCssPrefs ) {
603 $defaultPreferences['highlightbroken'] = array(
605 'section' => 'rendering/advancedrendering',
606 'label' => wfMsg( 'tog-highlightbroken' ), // Raw HTML
608 $defaultPreferences['showtoc'] = array(
610 'section' => 'rendering/advancedrendering',
611 'label-message' => 'tog-showtoc',
614 $defaultPreferences['nocache'] = array(
616 'label-message' => 'tog-nocache',
617 'section' => 'rendering/advancedrendering',
619 $defaultPreferences['showhiddencats'] = array(
621 'section' => 'rendering/advancedrendering',
622 'label-message' => 'tog-showhiddencats'
624 $defaultPreferences['showjumplinks'] = array(
626 'section' => 'rendering/advancedrendering',
627 'label-message' => 'tog-showjumplinks',
630 if ( $wgAllowUserCssPrefs ) {
631 $defaultPreferences['justify'] = array(
633 'section' => 'rendering/advancedrendering',
634 'label-message' => 'tog-justify',
638 $defaultPreferences['numberheadings'] = array(
640 'section' => 'rendering/advancedrendering',
641 'label-message' => 'tog-numberheadings',
645 static function editingPreferences( $user, &$defaultPreferences ) {
646 global $wgUseExternalEditor, $wgAllowUserCssPrefs;
648 ## Editing #####################################
649 $defaultPreferences['cols'] = array(
651 'label-message' => 'columns',
652 'section' => 'editing/textboxsize',
656 $defaultPreferences['rows'] = array(
658 'label-message' => 'rows',
659 'section' => 'editing/textboxsize',
664 if ( $wgAllowUserCssPrefs ) {
665 $defaultPreferences['editfont'] = array(
667 'section' => 'editing/advancedediting',
668 'label-message' => 'editfont-style',
670 wfMsg( 'editfont-default' ) => 'default',
671 wfMsg( 'editfont-monospace' ) => 'monospace',
672 wfMsg( 'editfont-sansserif' ) => 'sans-serif',
673 wfMsg( 'editfont-serif' ) => 'serif',
677 $defaultPreferences['previewontop'] = array(
679 'section' => 'editing/advancedediting',
680 'label-message' => 'tog-previewontop',
682 $defaultPreferences['previewonfirst'] = array(
684 'section' => 'editing/advancedediting',
685 'label-message' => 'tog-previewonfirst',
688 if ( $wgAllowUserCssPrefs ) {
689 $defaultPreferences['editsection'] = array(
691 'section' => 'editing/advancedediting',
692 'label-message' => 'tog-editsection',
695 $defaultPreferences['editsectiononrightclick'] = array(
697 'section' => 'editing/advancedediting',
698 'label-message' => 'tog-editsectiononrightclick',
700 $defaultPreferences['editondblclick'] = array(
702 'section' => 'editing/advancedediting',
703 'label-message' => 'tog-editondblclick',
705 $defaultPreferences['showtoolbar'] = array(
707 'section' => 'editing/advancedediting',
708 'label-message' => 'tog-showtoolbar',
711 if ( $wgUseExternalEditor ) {
712 $defaultPreferences['externaleditor'] = array(
714 'section' => 'editing/advancedediting',
715 'label-message' => 'tog-externaleditor',
717 $defaultPreferences['externaldiff'] = array(
719 'section' => 'editing/advancedediting',
720 'label-message' => 'tog-externaldiff',
724 $defaultPreferences['forceeditsummary'] = array(
726 'section' => 'editing/advancedediting',
727 'label-message' => 'tog-forceeditsummary',
731 $defaultPreferences['uselivepreview'] = array(
733 'section' => 'editing/advancedediting',
734 'label-message' => 'tog-uselivepreview',
738 static function rcPreferences( $user, &$defaultPreferences ) {
739 global $wgRCMaxAge, $wgUseRCPatrol, $wgLang;
741 ## RecentChanges #####################################
742 $defaultPreferences['rcdays'] = array(
744 'label-message' => 'recentchangesdays',
745 'section' => 'rc/displayrc',
747 'max' => ceil( $wgRCMaxAge / ( 3600 * 24 ) ),
749 'recentchangesdays-max',
751 $wgLang->formatNum( ceil( $wgRCMaxAge / ( 3600 * 24 ) ) )
754 $defaultPreferences['rclimit'] = array(
756 'label-message' => 'recentchangescount',
757 'help-message' => 'prefs-help-recentchangescount',
758 'section' => 'rc/displayrc',
760 $defaultPreferences['usenewrc'] = array(
762 'label-message' => 'tog-usenewrc',
763 'section' => 'rc/advancedrc',
765 $defaultPreferences['hideminor'] = array(
767 'label-message' => 'tog-hideminor',
768 'section' => 'rc/advancedrc',
771 if ( $wgUseRCPatrol ) {
772 $defaultPreferences['hidepatrolled'] = array(
774 'section' => 'rc/advancedrc',
775 'label-message' => 'tog-hidepatrolled',
777 $defaultPreferences['newpageshidepatrolled'] = array(
779 'section' => 'rc/advancedrc',
780 'label-message' => 'tog-newpageshidepatrolled',
784 global $wgRCShowWatchingUsers;
785 if ( $wgRCShowWatchingUsers ) {
786 $defaultPreferences['shownumberswatching'] = array(
788 'section' => 'rc/advancedrc',
789 'label-message' => 'tog-shownumberswatching',
794 static function watchlistPreferences( $user, &$defaultPreferences ) {
795 global $wgUseRCPatrol, $wgEnableAPI;
797 ## Watchlist #####################################
798 $defaultPreferences['watchlistdays'] = array(
802 'section' => 'watchlist/displaywatchlist',
803 'help' => wfMsgHtml( 'prefs-watchlist-days-max' ),
804 'label-message' => 'prefs-watchlist-days',
806 $defaultPreferences['wllimit'] = array(
810 'label-message' => 'prefs-watchlist-edits',
811 'help' => wfMsgHtml( 'prefs-watchlist-edits-max' ),
812 'section' => 'watchlist/displaywatchlist',
814 $defaultPreferences['extendwatchlist'] = array(
816 'section' => 'watchlist/advancedwatchlist',
817 'label-message' => 'tog-extendwatchlist',
819 $defaultPreferences['watchlisthideminor'] = array(
821 'section' => 'watchlist/advancedwatchlist',
822 'label-message' => 'tog-watchlisthideminor',
824 $defaultPreferences['watchlisthidebots'] = array(
826 'section' => 'watchlist/advancedwatchlist',
827 'label-message' => 'tog-watchlisthidebots',
829 $defaultPreferences['watchlisthideown'] = array(
831 'section' => 'watchlist/advancedwatchlist',
832 'label-message' => 'tog-watchlisthideown',
834 $defaultPreferences['watchlisthideanons'] = array(
836 'section' => 'watchlist/advancedwatchlist',
837 'label-message' => 'tog-watchlisthideanons',
839 $defaultPreferences['watchlisthideliu'] = array(
841 'section' => 'watchlist/advancedwatchlist',
842 'label-message' => 'tog-watchlisthideliu',
845 if ( $wgUseRCPatrol ) {
846 $defaultPreferences['watchlisthidepatrolled'] = array(
848 'section' => 'watchlist/advancedwatchlist',
849 'label-message' => 'tog-watchlisthidepatrolled',
853 if ( $wgEnableAPI ) {
854 # Some random gibberish as a proposed default
855 $hash = sha1( mt_rand() . microtime( true ) );
857 $defaultPreferences['watchlisttoken'] = array(
859 'section' => 'watchlist/advancedwatchlist',
860 'label-message' => 'prefs-watchlist-token',
861 'help' => wfMsgHtml( 'prefs-help-watchlist-token', $hash )
866 'edit' => 'watchdefault',
867 'move' => 'watchmoves',
868 'delete' => 'watchdeletion'
872 if ( $user->isAllowed( 'createpage' ) ||
$user->isAllowed( 'createtalk' ) ) {
873 $watchTypes['read'] = 'watchcreations';
876 foreach ( $watchTypes as $action => $pref ) {
877 if ( $user->isAllowed( $action ) ) {
878 $defaultPreferences[$pref] = array(
880 'section' => 'watchlist/advancedwatchlist',
881 'label-message' => "tog-$pref",
887 static function searchPreferences( $user, &$defaultPreferences ) {
890 ## Search #####################################
891 $defaultPreferences['searchlimit'] = array(
893 'label-message' => 'resultsperpage',
894 'section' => 'searchoptions/displaysearchoptions',
897 $defaultPreferences['contextlines'] = array(
899 'label-message' => 'contextlines',
900 'section' => 'searchoptions/displaysearchoptions',
903 $defaultPreferences['contextchars'] = array(
905 'label-message' => 'contextchars',
906 'section' => 'searchoptions/displaysearchoptions',
910 global $wgEnableMWSuggest;
911 if ( $wgEnableMWSuggest ) {
912 $defaultPreferences['disablesuggest'] = array(
914 'label-message' => 'mwsuggest-disable',
915 'section' => 'searchoptions/displaysearchoptions',
919 global $wgVectorUseSimpleSearch;
920 if ( $wgVectorUseSimpleSearch ) {
921 $defaultPreferences['vector-simplesearch'] = array(
923 'label-message' => 'vector-simplesearch-preference',
924 'section' => 'searchoptions/displaysearchoptions'
928 $defaultPreferences['searcheverything'] = array(
930 'label-message' => 'searcheverything-enable',
931 'section' => 'searchoptions/advancedsearchoptions',
934 // Searchable namespaces back-compat with old format
935 $searchableNamespaces = SearchEngine
::searchableNamespaces();
937 $nsOptions = array();
939 foreach ( $wgContLang->getNamespaces() as $ns => $name ) {
944 $displayNs = str_replace( '_', ' ', $name );
947 $displayNs = wfMsg( 'blanknamespace' );
950 $displayNs = htmlspecialchars( $displayNs );
951 $nsOptions[$displayNs] = $ns;
954 $defaultPreferences['searchnamespaces'] = array(
955 'type' => 'multiselect',
956 'label-message' => 'defaultns',
957 'options' => $nsOptions,
958 'section' => 'searchoptions/advancedsearchoptions',
959 'prefix' => 'searchNs',
963 static function miscPreferences( $user, &$defaultPreferences ) {
964 ## Misc #####################################
965 $defaultPreferences['diffonly'] = array(
967 'section' => 'misc/diffs',
968 'label-message' => 'tog-diffonly',
970 $defaultPreferences['norollbackdiff'] = array(
972 'section' => 'misc/diffs',
973 'label-message' => 'tog-norollbackdiff',
976 // Stuff from Language::getExtraUserToggles()
979 $toggles = $wgContLang->getExtraUserToggles();
981 foreach ( $toggles as $toggle ) {
982 $defaultPreferences[$toggle] = array(
984 'section' => 'personal/i18n',
985 'label-message' => "tog-$toggle",
991 * @param $user The User object
992 * @return Array: text/links to display as key; $skinkey as value
994 static function generateSkinOptions( $user ) {
995 global $wgDefaultSkin, $wgLang, $wgAllowUserCss, $wgAllowUserJs;
998 $mptitle = Title
::newMainPage();
999 $previewtext = wfMsgHtml( 'skin-preview' );
1001 # Only show members of Skin::getSkinNames() rather than
1002 # $skinNames (skins is all skin names from Language.php)
1003 $validSkinNames = Skin
::getUsableSkins();
1005 # Sort by UI skin name. First though need to update validSkinNames as sometimes
1006 # the skinkey & UI skinname differ (e.g. "standard" skinkey is "Classic" in the UI).
1007 foreach ( $validSkinNames as $skinkey => &$skinname ) {
1008 $msgName = "skinname-{$skinkey}";
1009 $localisedSkinName = wfMsg( $msgName );
1010 if ( !wfEmptyMsg( $msgName, $localisedSkinName ) ) {
1011 $skinname = htmlspecialchars( $localisedSkinName );
1014 asort( $validSkinNames );
1015 $sk = $user->getSkin();
1017 foreach ( $validSkinNames as $skinkey => $sn ) {
1018 $linkTools = array();
1020 # Mark the default skin
1021 if ( $skinkey == $wgDefaultSkin ) {
1022 $linkTools[] = wfMsgHtml( 'default' );
1025 # Create preview link
1026 $mplink = htmlspecialchars( $mptitle->getLocalURL( "useskin=$skinkey" ) );
1027 $linkTools[] = "<a target='_blank' href=\"$mplink\">$previewtext</a>";
1029 # Create links to user CSS/JS pages
1030 if ( $wgAllowUserCss ) {
1031 $cssPage = Title
::makeTitleSafe( NS_USER
, $user->getName() . '/' . $skinkey . '.css' );
1032 $linkTools[] = $sk->link( $cssPage, wfMsgHtml( 'prefs-custom-css' ) );
1035 if ( $wgAllowUserJs ) {
1036 $jsPage = Title
::makeTitleSafe( NS_USER
, $user->getName() . '/' . $skinkey . '.js' );
1037 $linkTools[] = $sk->link( $jsPage, wfMsgHtml( 'prefs-custom-js' ) );
1040 $display = $sn . ' ' . wfMsg( 'parentheses', $wgLang->pipeList( $linkTools ) );
1041 $ret[$display] = $skinkey;
1047 static function getDateOptions() {
1049 $dateopts = $wgLang->getDatePreferences();
1054 if ( !in_array( 'default', $dateopts ) ) {
1055 $dateopts[] = 'default'; // Make sure default is always valid
1059 // KLUGE: site default might not be valid for user language
1060 global $wgDefaultUserOptions;
1061 if ( !in_array( $wgDefaultUserOptions['date'], $dateopts ) ) {
1062 $wgDefaultUserOptions['date'] = 'default';
1066 $epoch = wfTimestampNow();
1067 foreach ( $dateopts as $key ) {
1068 if ( $key == 'default' ) {
1069 $formatted = wfMsgHtml( 'datedefault' );
1071 $formatted = htmlspecialchars( $wgLang->timeanddate( $epoch, false, $key ) );
1073 $ret[$formatted] = $key;
1079 static function getImageSizes() {
1080 global $wgImageLimits;
1084 foreach ( $wgImageLimits as $index => $limits ) {
1085 $display = "{$limits[0]}×{$limits[1]}" . wfMsg( 'unit-pixel' );
1086 $ret[$display] = $index;
1092 static function getThumbSizes() {
1093 global $wgThumbLimits;
1097 foreach ( $wgThumbLimits as $index => $size ) {
1098 $display = $size . wfMsg( 'unit-pixel' );
1099 $ret[$display] = $index;
1105 static function validateSignature( $signature, $alldata ) {
1106 global $wgParser, $wgMaxSigChars, $wgLang;
1107 if ( mb_strlen( $signature ) > $wgMaxSigChars ) {
1108 return Xml
::element( 'span', array( 'class' => 'error' ),
1109 wfMsgExt( 'badsiglength', 'parsemag',
1110 $wgLang->formatNum( $wgMaxSigChars )
1113 } elseif ( !empty( $alldata['fancysig'] ) &&
1114 false === $wgParser->validateSig( $signature ) ) {
1115 return Xml
::element( 'span', array( 'class' => 'error' ), wfMsg( 'badsig' ) );
1121 static function cleanSignature( $signature, $alldata ) {
1123 if ( $alldata['fancysig'] ) {
1124 $signature = $wgParser->cleanSig( $signature );
1126 // When no fancy sig used, make sure ~{3,5} get removed.
1127 $signature = $wgParser->cleanSigInSig( $signature );
1133 static function validateEmail( $email, $alldata ) {
1134 if ( $email && !User
::isValidEmailAddr( $email ) ) {
1135 return wfMsgExt( 'invalidemailaddress', 'parseinline' );
1138 global $wgEmailConfirmToEdit;
1139 if ( $wgEmailConfirmToEdit && !$email ) {
1140 return wfMsgExt( 'noemailtitle', 'parseinline' );
1145 static function getFormObject( $user, $formClass = 'PreferencesForm' ) {
1146 $formDescriptor = Preferences
::getPreferences( $user );
1147 $htmlForm = new $formClass( $formDescriptor, 'prefs' );
1149 $htmlForm->setSubmitText( wfMsg( 'saveprefs' ) );
1150 # Used message keys: 'accesskey-preferences-save', 'tooltip-preferences-save'
1151 $htmlForm->setSubmitTooltip( 'preferences-save' );
1152 $htmlForm->setTitle( SpecialPage
::getTitleFor( 'Preferences' ) );
1153 $htmlForm->setSubmitID( 'prefsubmit' );
1154 $htmlForm->setSubmitCallback( array( 'Preferences', 'tryFormSubmit' ) );
1159 static function getTimezoneOptions() {
1162 global $wgLocalTZoffset;
1164 $opt[wfMsg( 'timezoneuseserverdefault' )] = "System|$wgLocalTZoffset";
1165 $opt[wfMsg( 'timezoneuseoffset' )] = 'other';
1166 $opt[wfMsg( 'guesstimezone' )] = 'guess';
1168 if ( function_exists( 'timezone_identifiers_list' ) ) {
1169 # Read timezone list
1170 $tzs = timezone_identifiers_list();
1173 $tzRegions = array();
1174 $tzRegions['Africa'] = wfMsg( 'timezoneregion-africa' );
1175 $tzRegions['America'] = wfMsg( 'timezoneregion-america' );
1176 $tzRegions['Antarctica'] = wfMsg( 'timezoneregion-antarctica' );
1177 $tzRegions['Arctic'] = wfMsg( 'timezoneregion-arctic' );
1178 $tzRegions['Asia'] = wfMsg( 'timezoneregion-asia' );
1179 $tzRegions['Atlantic'] = wfMsg( 'timezoneregion-atlantic' );
1180 $tzRegions['Australia'] = wfMsg( 'timezoneregion-australia' );
1181 $tzRegions['Europe'] = wfMsg( 'timezoneregion-europe' );
1182 $tzRegions['Indian'] = wfMsg( 'timezoneregion-indian' );
1183 $tzRegions['Pacific'] = wfMsg( 'timezoneregion-pacific' );
1184 asort( $tzRegions );
1186 $prefill = array_fill_keys( array_values( $tzRegions ), array() );
1187 $opt = array_merge( $opt, $prefill );
1189 $now = date_create( 'now' );
1191 foreach ( $tzs as $tz ) {
1192 $z = explode( '/', $tz, 2 );
1194 # timezone_identifiers_list() returns a number of
1195 # backwards-compatibility entries. This filters them out of the
1196 # list presented to the user.
1197 if ( count( $z ) != 2 ||
!array_key_exists( $z[0], $tzRegions ) ) {
1202 $z[0] = $tzRegions[$z[0]];
1204 $minDiff = floor( timezone_offset_get( timezone_open( $tz ), $now ) / 60 );
1206 $display = str_replace( '_', ' ', $z[0] . '/' . $z[1] );
1207 $value = "ZoneInfo|$minDiff|$tz";
1209 $opt[$z[0]][$display] = $value;
1215 static function filterTimezoneInput( $tz, $alldata ) {
1216 $data = explode( '|', $tz, 3 );
1217 switch ( $data[0] ) {
1222 $data = explode( ':', $tz, 2 );
1224 if ( count( $data ) == 2 ) {
1225 $data[0] = intval( $data[0] );
1226 $data[1] = intval( $data[1] );
1227 $minDiff = abs( $data[0] ) * 60 +
$data[1];
1228 if ( $data[0] < 0 ) $minDiff = - $minDiff;
1230 $minDiff = intval( $data[0] ) * 60;
1233 # Max is +14:00 and min is -12:00, see:
1234 # http://en.wikipedia.org/wiki/Timezone
1235 $minDiff = min( $minDiff, 840 ); # 14:00
1236 $minDiff = max( $minDiff, - 720 ); # -12:00
1237 return 'Offset|' . $minDiff;
1241 static function tryFormSubmit( $formData, $entryPoint = 'internal' ) {
1242 global $wgUser, $wgEmailAuthentication, $wgEnableEmail;
1247 foreach ( array_keys( $formData ) as $name ) {
1248 if ( isset( self
::$saveFilters[$name] ) ) {
1250 call_user_func( self
::$saveFilters[$name], $formData[$name], $formData );
1254 // Stuff that shouldn't be saved as a preference.
1255 $saveBlacklist = array(
1260 if ( $wgEnableEmail ) {
1261 $newaddr = $formData['emailaddress'];
1262 $oldaddr = $wgUser->getEmail();
1263 if ( ( $newaddr != '' ) && ( $newaddr != $oldaddr ) ) {
1264 # the user has supplied a new email address on the login page
1265 # new behaviour: set this new emailaddr from login-page into user database record
1266 $wgUser->setEmail( $newaddr );
1267 # but flag as "dirty" = unauthenticated
1268 $wgUser->invalidateEmail();
1269 if ( $wgEmailAuthentication ) {
1270 # Mail a temporary password to the dirty address.
1271 # User can come back through the confirmation URL to re-enable email.
1272 $result = $wgUser->sendConfirmationMail( $oldaddr != '' );
1273 if ( WikiError
::isError( $result ) ) {
1274 return wfMsg( 'mailerror', htmlspecialchars( $result->getMessage() ) );
1275 } elseif ( $entryPoint == 'ui' ) {
1280 $wgUser->setEmail( $newaddr );
1282 if ( $oldaddr != $newaddr ) {
1283 wfRunHooks( 'PrefsEmailAudit', array( $wgUser, $oldaddr, $newaddr ) );
1287 // Fortunately, the realname field is MUCH simpler
1288 global $wgHiddenPrefs;
1289 if ( !in_array( 'realname', $wgHiddenPrefs ) ) {
1290 $realName = $formData['realname'];
1291 $wgUser->setRealName( $realName );
1294 foreach ( $saveBlacklist as $b ) {
1295 unset( $formData[$b] );
1298 // Keeps old preferences from interfering due to back-compat
1300 $wgUser->resetOptions();
1302 foreach ( $formData as $key => $value ) {
1303 $wgUser->setOption( $key, $value );
1306 $wgUser->saveSettings();
1311 public static function tryUISubmit( $formData ) {
1312 $res = self
::tryFormSubmit( $formData, 'ui' );
1315 $urlOptions = array( 'success' );
1317 if ( $res === 'eauth' ) {
1318 $urlOptions[] = 'eauth';
1321 $queryString = implode( '&', $urlOptions );
1323 $url = SpecialPage
::getTitleFor( 'Preferences' )->getFullURL( $queryString );
1326 $wgOut->redirect( $url );
1332 public static function loadOldSearchNs( $user ) {
1333 $searchableNamespaces = SearchEngine
::searchableNamespaces();
1334 // Back compat with old format
1337 foreach ( $searchableNamespaces as $ns => $name ) {
1338 if ( $user->getOption( 'searchNs' . $ns ) ) {
1347 /** Some tweaks to allow js prefs to work */
1348 class PreferencesForm
extends HTMLForm
{
1349 function wrapForm( $html ) {
1350 $html = Xml
::tags( 'div', array( 'id' => 'preferences' ), $html );
1352 return parent
::wrapForm( $html );
1355 function getButtons() {
1356 $html = parent
::getButtons();
1360 $sk = $wgUser->getSkin();
1361 $t = SpecialPage
::getTitleFor( 'Preferences', 'reset' );
1363 $html .= "\n" . $sk->link( $t, wfMsgHtml( 'restoreprefs' ) );
1365 $html = Xml
::tags( 'div', array( 'class' => 'mw-prefs-buttons' ), $html );
1370 function filterDataForSubmit( $data ) {
1371 // Support for separating MultiSelect preferences into multiple preferences
1372 // Due to lack of array support.
1373 foreach ( $this->mFlatFields
as $fieldname => $field ) {
1374 $info = $field->mParams
;
1375 if ( $field instanceof HTMLMultiSelectField
) {
1376 $options = HTMLFormField
::flattenOptions( $info['options'] );
1377 $prefix = isset( $info['prefix'] ) ?
$info['prefix'] : $fieldname;
1379 foreach ( $options as $opt ) {
1380 $data["$prefix$opt"] = in_array( $opt, $data[$fieldname] );
1383 unset( $data[$fieldname] );