3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 namespace MediaWiki\Preferences
;
33 use LanguageConverter
;
34 use MediaWiki\Auth\AuthManager
;
35 use MediaWiki\Auth\PasswordAuthenticationRequest
;
36 use MediaWiki\Config\ServiceOptions
;
37 use MediaWiki\Linker\LinkRenderer
;
38 use MediaWiki\MediaWikiServices
;
39 use MediaWiki\Permissions\PermissionManager
;
47 use PreferencesFormOOUI
;
48 use Psr\Log\LoggerAwareTrait
;
49 use Psr\Log\NullLogger
;
54 use UnexpectedValueException
;
56 use UserGroupMembership
;
60 * This is the default implementation of PreferencesFactory.
62 class DefaultPreferencesFactory
implements PreferencesFactory
{
65 /** @var ServiceOptions */
68 /** @var Language The wiki's content language. */
71 /** @var AuthManager */
72 protected $authManager;
74 /** @var LinkRenderer */
75 protected $linkRenderer;
77 /** @var NamespaceInfo */
80 /** @var PermissionManager */
81 protected $permissionManager;
84 * TODO Make this a const when we drop HHVM support (T192166)
89 public static $constructorOptions = [
90 'AllowRequiringEmailForResets',
95 'DisableLangConversion',
96 'EmailAuthentication',
100 'EnableUserEmailBlacklist',
102 'EnotifRevealEditorAddress',
111 'RCShowWatchingUsers',
112 'RCWatchCategoryMembership',
118 * Do not call this directly. Get it from MediaWikiServices.
120 * @param ServiceOptions $options
121 * @param Language $contLang
122 * @param AuthManager $authManager
123 * @param LinkRenderer $linkRenderer
124 * @param NamespaceInfo $nsInfo
125 * @param PermissionManager|null $permissionManager
127 public function __construct(
128 ServiceOptions
$options,
130 AuthManager
$authManager,
131 LinkRenderer
$linkRenderer,
132 NamespaceInfo
$nsInfo,
133 PermissionManager
$permissionManager = null
135 $options->assertRequiredOptions( self
::$constructorOptions );
137 if ( !$permissionManager ) {
138 // TODO: this is actually hard-deprecated, left for jenkins to pass
139 // together with GlobalPreferences extension. Will be removed in a followup.
140 $permissionManager = MediaWikiServices
::getInstance()->getPermissionManager();
142 $this->options
= $options;
143 $this->contLang
= $contLang;
144 $this->authManager
= $authManager;
145 $this->linkRenderer
= $linkRenderer;
146 $this->nsInfo
= $nsInfo;
147 $this->permissionManager
= $permissionManager;
148 $this->logger
= new NullLogger();
154 public function getSaveBlacklist() {
162 * @throws MWException
164 * @param IContextSource $context
167 public function getFormDescriptor( User
$user, IContextSource
$context ) {
170 OutputPage
::setupOOUI(
171 strtolower( $context->getSkin()->getSkinName() ),
172 $context->getLanguage()->getDir()
175 $canIPUseHTTPS = wfCanIPUseHTTPS( $context->getRequest()->getIP() );
176 $this->profilePreferences( $user, $context, $preferences, $canIPUseHTTPS );
177 $this->skinPreferences( $user, $context, $preferences );
178 $this->datetimePreferences( $user, $context, $preferences );
179 $this->filesPreferences( $context, $preferences );
180 $this->renderingPreferences( $user, $context, $preferences );
181 $this->editingPreferences( $user, $context, $preferences );
182 $this->rcPreferences( $user, $context, $preferences );
183 $this->watchlistPreferences( $user, $context, $preferences );
184 $this->searchPreferences( $preferences );
186 Hooks
::run( 'GetPreferences', [ $user, &$preferences ] );
188 $this->loadPreferenceValues( $user, $context, $preferences );
189 $this->logger
->debug( "Created form descriptor for user '{$user->getName()}'" );
194 * Loads existing values for a given array of preferences
195 * @throws MWException
197 * @param IContextSource $context
198 * @param array &$defaultPreferences Array to load values for
201 private function loadPreferenceValues(
202 User
$user, IContextSource
$context, &$defaultPreferences
204 # # Remove preferences that wikis don't want to use
205 foreach ( $this->options
->get( 'HiddenPrefs' ) as $pref ) {
206 if ( isset( $defaultPreferences[$pref] ) ) {
207 unset( $defaultPreferences[$pref] );
211 # # Make sure that form fields have their parent set. See T43337.
212 $dummyForm = new HTMLForm( [], $context );
214 $disable = !$this->permissionManager
->userHasRight( $user, 'editmyoptions' );
216 $defaultOptions = User
::getDefaultOptions();
217 $userOptions = $user->getOptions();
218 $this->applyFilters( $userOptions, $defaultPreferences, 'filterForForm' );
219 # # Prod in defaults from the user
220 foreach ( $defaultPreferences as $name => &$info ) {
221 $prefFromUser = $this->getOptionFromUser( $name, $info, $userOptions );
222 if ( $disable && !in_array( $name, $this->getSaveBlacklist() ) ) {
223 $info['disabled'] = 'disabled';
225 $field = HTMLForm
::loadInputFromParameters( $name, $info, $dummyForm ); // For validation
226 $globalDefault = $defaultOptions[$name] ??
null;
228 // If it validates, set it as the default
229 if ( isset( $info['default'] ) ) {
230 // Already set, no problem
232 } elseif ( !is_null( $prefFromUser ) && // Make sure we're not just pulling nothing
233 $field->validate( $prefFromUser, $user->getOptions() ) === true ) {
234 $info['default'] = $prefFromUser;
235 } elseif ( $field->validate( $globalDefault, $user->getOptions() ) === true ) {
236 $info['default'] = $globalDefault;
238 $globalDefault = json_encode( $globalDefault );
239 throw new MWException(
240 "Default '$globalDefault' is invalid for preference $name of user $user"
245 return $defaultPreferences;
249 * Pull option from a user account. Handles stuff like array-type preferences.
251 * @param string $name
253 * @param array $userOptions
254 * @return array|string
256 protected function getOptionFromUser( $name, $info, array $userOptions ) {
257 $val = $userOptions[$name] ??
null;
259 // Handling for multiselect preferences
260 if ( ( isset( $info['type'] ) && $info['type'] == 'multiselect' ) ||
261 ( isset( $info['class'] ) && $info['class'] == \HTMLMultiSelectField
::class ) ) {
262 $options = HTMLFormField
::flattenOptions( $info['options'] );
263 $prefix = $info['prefix'] ??
$name;
266 foreach ( $options as $value ) {
267 if ( $userOptions["$prefix$value"] ??
false ) {
273 // Handling for checkmatrix preferences
274 if ( ( isset( $info['type'] ) && $info['type'] == 'checkmatrix' ) ||
275 ( isset( $info['class'] ) && $info['class'] == \HTMLCheckMatrix
::class ) ) {
276 $columns = HTMLFormField
::flattenOptions( $info['columns'] );
277 $rows = HTMLFormField
::flattenOptions( $info['rows'] );
278 $prefix = $info['prefix'] ??
$name;
281 foreach ( $columns as $column ) {
282 foreach ( $rows as $row ) {
283 if ( $userOptions["$prefix$column-$row"] ??
false ) {
284 $val[] = "$column-$row";
294 * @todo Inject user Language instead of using context.
296 * @param IContextSource $context
297 * @param array &$defaultPreferences
298 * @param bool $canIPUseHTTPS Whether the user's IP is likely to be able to access the wiki
302 protected function profilePreferences(
303 User
$user, IContextSource
$context, &$defaultPreferences, $canIPUseHTTPS
305 // retrieving user name for GENDER and misc.
306 $userName = $user->getName();
308 # # User info #####################################
310 $defaultPreferences['username'] = [
312 'label-message' => [ 'username', $userName ],
313 'default' => $userName,
314 'section' => 'personal/info',
317 $lang = $context->getLanguage();
319 # Get groups to which the user belongs
320 $userEffectiveGroups = $user->getEffectiveGroups();
321 $userGroupMemberships = $user->getGroupMemberships();
322 $userGroups = $userMembers = $userTempGroups = $userTempMembers = [];
323 foreach ( $userEffectiveGroups as $ueg ) {
325 // Skip the default * group, seems useless here
329 $groupStringOrObject = $userGroupMemberships[$ueg] ??
$ueg;
331 $userG = UserGroupMembership
::getLink( $groupStringOrObject, $context, 'html' );
332 $userM = UserGroupMembership
::getLink( $groupStringOrObject, $context, 'html',
335 // Store expiring groups separately, so we can place them before non-expiring
336 // groups in the list. This is to avoid the ambiguity of something like
337 // "administrator, bureaucrat (until X date)" -- users might wonder whether the
338 // expiry date applies to both groups, or just the last one
339 if ( $groupStringOrObject instanceof UserGroupMembership
&&
340 $groupStringOrObject->getExpiry()
342 $userTempGroups[] = $userG;
343 $userTempMembers[] = $userM;
345 $userGroups[] = $userG;
346 $userMembers[] = $userM;
350 sort( $userMembers );
351 sort( $userTempGroups );
352 sort( $userTempMembers );
353 $userGroups = array_merge( $userTempGroups, $userGroups );
354 $userMembers = array_merge( $userTempMembers, $userMembers );
356 $defaultPreferences['usergroups'] = [
358 'label' => $context->msg( 'prefs-memberingroups' )->numParams(
359 count( $userGroups ) )->params( $userName )->parse(),
360 'default' => $context->msg( 'prefs-memberingroups-type' )
361 ->rawParams( $lang->commaList( $userGroups ), $lang->commaList( $userMembers ) )
364 'section' => 'personal/info',
367 $contribTitle = SpecialPage
::getTitleFor( "Contributions", $userName );
368 $formattedEditCount = $lang->formatNum( $user->getEditCount() );
369 $editCount = $this->linkRenderer
->makeLink( $contribTitle, $formattedEditCount );
371 $defaultPreferences['editcount'] = [
374 'label-message' => 'prefs-edits',
375 'default' => $editCount,
376 'section' => 'personal/info',
379 if ( $user->getRegistration() ) {
380 $displayUser = $context->getUser();
381 $userRegistration = $user->getRegistration();
382 $defaultPreferences['registrationdate'] = [
384 'label-message' => 'prefs-registration',
385 'default' => $context->msg(
386 'prefs-registration-date-time',
387 $lang->userTimeAndDate( $userRegistration, $displayUser ),
388 $lang->userDate( $userRegistration, $displayUser ),
389 $lang->userTime( $userRegistration, $displayUser )
391 'section' => 'personal/info',
395 $canViewPrivateInfo = $this->permissionManager
->userHasRight( $user, 'viewmyprivateinfo' );
396 $canEditPrivateInfo = $this->permissionManager
->userHasRight( $user, 'editmyprivateinfo' );
398 // Actually changeable stuff
399 $defaultPreferences['realname'] = [
400 // (not really "private", but still shouldn't be edited without permission)
401 'type' => $canEditPrivateInfo && $this->authManager
->allowsPropertyChange( 'realname' )
403 'default' => $user->getRealName(),
404 'section' => 'personal/info',
405 'label-message' => 'yourrealname',
406 'help-message' => 'prefs-help-realname',
409 if ( $canEditPrivateInfo && $this->authManager
->allowsAuthenticationDataChange(
410 new PasswordAuthenticationRequest(), false )->isGood()
412 $defaultPreferences['password'] = [
415 'default' => (string)new \OOUI\
ButtonWidget( [
416 'href' => SpecialPage
::getTitleFor( 'ChangePassword' )->getLinkURL( [
417 'returnto' => SpecialPage
::getTitleFor( 'Preferences' )->getPrefixedText()
419 'label' => $context->msg( 'prefs-resetpass' )->text(),
421 'label-message' => 'yourpassword',
422 'section' => 'personal/info',
425 // Only show prefershttps if secure login is turned on
426 if ( $this->options
->get( 'SecureLogin' ) && $canIPUseHTTPS ) {
427 $defaultPreferences['prefershttps'] = [
429 'label-message' => 'tog-prefershttps',
430 'help-message' => 'prefs-help-prefershttps',
431 'section' => 'personal/info'
435 $languages = Language
::fetchLanguageNames( null, 'mwfile' );
436 $languageCode = $this->options
->get( 'LanguageCode' );
437 if ( !array_key_exists( $languageCode, $languages ) ) {
438 $languages[$languageCode] = $languageCode;
439 // Sort the array again
444 foreach ( $languages as $code => $name ) {
445 $display = LanguageCode
::bcp47( $code ) . ' - ' . $name;
446 $options[$display] = $code;
448 $defaultPreferences['language'] = [
450 'section' => 'personal/i18n',
451 'options' => $options,
452 'label-message' => 'yourlanguage',
455 $defaultPreferences['gender'] = [
457 'section' => 'personal/i18n',
459 $context->msg( 'parentheses' )
460 ->params( $context->msg( 'gender-unknown' )->plain() )
461 ->escaped() => 'unknown',
462 $context->msg( 'gender-female' )->escaped() => 'female',
463 $context->msg( 'gender-male' )->escaped() => 'male',
465 'label-message' => 'yourgender',
466 'help-message' => 'prefs-help-gender',
469 // see if there are multiple language variants to choose from
470 if ( !$this->options
->get( 'DisableLangConversion' ) ) {
471 foreach ( LanguageConverter
::$languagesWithVariants as $langCode ) {
472 if ( $langCode == $this->contLang
->getCode() ) {
473 if ( !$this->contLang
->hasVariants() ) {
477 $variants = $this->contLang
->getVariants();
479 foreach ( $variants as $v ) {
480 $v = str_replace( '_', '-', strtolower( $v ) );
481 $variantArray[$v] = $lang->getVariantname( $v, false );
485 foreach ( $variantArray as $code => $name ) {
486 $display = LanguageCode
::bcp47( $code ) . ' - ' . $name;
487 $options[$display] = $code;
490 $defaultPreferences['variant'] = [
491 'label-message' => 'yourvariant',
493 'options' => $options,
494 'section' => 'personal/i18n',
495 'help-message' => 'prefs-help-variant',
498 $defaultPreferences["variant-$langCode"] = [
505 // show a preview of the old signature first
506 $oldsigWikiText = MediaWikiServices
::getInstance()->getParser()->preSaveTransform(
508 $context->getTitle(),
510 ParserOptions
::newFromContext( $context )
512 $oldsigHTML = Parser
::stripOuterParagraph(
513 $context->getOutput()->parseAsContent( $oldsigWikiText )
515 $defaultPreferences['oldsig'] = [
518 'label-message' => 'tog-oldsig',
519 'default' => $oldsigHTML,
520 'section' => 'personal/signature',
522 $defaultPreferences['nickname'] = [
523 'type' => $this->authManager
->allowsPropertyChange( 'nickname' ) ?
'text' : 'info',
524 'maxlength' => $this->options
->get( 'MaxSigChars' ),
525 'label-message' => 'yournick',
526 'validation-callback' => function ( $signature, $alldata, HTMLForm
$form ) {
527 return $this->validateSignature( $signature, $alldata, $form );
529 'section' => 'personal/signature',
530 'filter-callback' => function ( $signature, array $alldata, HTMLForm
$form ) {
531 return $this->cleanSignature( $signature, $alldata, $form );
534 $defaultPreferences['fancysig'] = [
536 'label-message' => 'tog-fancysig',
537 // show general help about signature at the bottom of the section
538 'help-message' => 'prefs-help-signature',
539 'section' => 'personal/signature'
544 if ( $this->options
->get( 'EnableEmail' ) ) {
545 if ( $canViewPrivateInfo ) {
547 $helpMessages[] = $this->options
->get( 'EmailConfirmToEdit' )
548 ?
'prefs-help-email-required'
549 : 'prefs-help-email';
551 if ( $this->options
->get( 'EnableUserEmail' ) ) {
552 // additional messages when users can send email to each other
553 $helpMessages[] = 'prefs-help-email-others';
556 $emailAddress = $user->getEmail() ?
htmlspecialchars( $user->getEmail() ) : '';
557 if ( $canEditPrivateInfo && $this->authManager
->allowsPropertyChange( 'emailaddress' ) ) {
558 $button = new \OOUI\
ButtonWidget( [
559 'href' => SpecialPage
::getTitleFor( 'ChangeEmail' )->getLinkURL( [
560 'returnto' => SpecialPage
::getTitleFor( 'Preferences' )->getPrefixedText()
563 $context->msg( $user->getEmail() ?
'prefs-changeemail' : 'prefs-setemail' )->text(),
566 $emailAddress .= $emailAddress == '' ?
$button : ( '<br />' . $button );
569 $defaultPreferences['emailaddress'] = [
572 'default' => $emailAddress,
573 'label-message' => 'youremail',
574 'section' => 'personal/email',
575 'help-messages' => $helpMessages,
576 # 'cssclass' chosen below
580 $disableEmailPrefs = false;
582 if ( $this->options
->get( 'EmailAuthentication' ) ) {
583 $emailauthenticationclass = 'mw-email-not-authenticated';
584 if ( $user->getEmail() ) {
585 if ( $user->getEmailAuthenticationTimestamp() ) {
586 // date and time are separate parameters to facilitate localisation.
587 // $time is kept for backward compat reasons.
588 // 'emailauthenticated' is also used in SpecialConfirmemail.php
589 $displayUser = $context->getUser();
590 $emailTimestamp = $user->getEmailAuthenticationTimestamp();
591 $time = $lang->userTimeAndDate( $emailTimestamp, $displayUser );
592 $d = $lang->userDate( $emailTimestamp, $displayUser );
593 $t = $lang->userTime( $emailTimestamp, $displayUser );
594 $emailauthenticated = $context->msg( 'emailauthenticated',
595 $time, $d, $t )->parse() . '<br />';
596 $disableEmailPrefs = false;
597 $emailauthenticationclass = 'mw-email-authenticated';
599 $disableEmailPrefs = true;
600 $emailauthenticated = $context->msg( 'emailnotauthenticated' )->parse() . '<br />' .
601 new \OOUI\
ButtonWidget( [
602 'href' => SpecialPage
::getTitleFor( 'Confirmemail' )->getLinkURL(),
603 'label' => $context->msg( 'emailconfirmlink' )->text(),
605 $emailauthenticationclass = "mw-email-not-authenticated";
608 $disableEmailPrefs = true;
609 $emailauthenticated = $context->msg( 'noemailprefs' )->escaped();
610 $emailauthenticationclass = 'mw-email-none';
613 if ( $canViewPrivateInfo ) {
614 $defaultPreferences['emailauthentication'] = [
617 'section' => 'personal/email',
618 'label-message' => 'prefs-emailconfirm-label',
619 'default' => $emailauthenticated,
620 # Apply the same CSS class used on the input to the message:
621 'cssclass' => $emailauthenticationclass,
626 if ( $this->options
->get( 'AllowRequiringEmailForResets' ) ) {
627 $defaultPreferences['requireemail'] = [
629 'label-message' => 'tog-requireemail',
630 'help-message' => 'prefs-help-requireemail',
631 'section' => 'personal/email',
632 'disabled' => $disableEmailPrefs,
636 if ( $this->options
->get( 'EnableUserEmail' ) &&
637 $this->permissionManager
->userHasRight( $user, 'sendemail' )
639 $defaultPreferences['disablemail'] = [
640 'id' => 'wpAllowEmail',
643 'section' => 'personal/email',
644 'label-message' => 'allowemail',
645 'disabled' => $disableEmailPrefs,
648 $defaultPreferences['email-allow-new-users'] = [
649 'id' => 'wpAllowEmailFromNewUsers',
651 'section' => 'personal/email',
652 'label-message' => 'email-allow-new-users-label',
653 'disabled' => $disableEmailPrefs,
656 $defaultPreferences['ccmeonemails'] = [
658 'section' => 'personal/email',
659 'label-message' => 'tog-ccmeonemails',
660 'disabled' => $disableEmailPrefs,
663 if ( $this->options
->get( 'EnableUserEmailBlacklist' ) ) {
664 $defaultPreferences['email-blacklist'] = [
665 'type' => 'usersmultiselect',
666 'label-message' => 'email-blacklist-label',
667 'section' => 'personal/email',
668 'disabled' => $disableEmailPrefs,
669 'filter' => MultiUsernameFilter
::class,
674 if ( $this->options
->get( 'EnotifWatchlist' ) ) {
675 $defaultPreferences['enotifwatchlistpages'] = [
677 'section' => 'personal/email',
678 'label-message' => 'tog-enotifwatchlistpages',
679 'disabled' => $disableEmailPrefs,
682 if ( $this->options
->get( 'EnotifUserTalk' ) ) {
683 $defaultPreferences['enotifusertalkpages'] = [
685 'section' => 'personal/email',
686 'label-message' => 'tog-enotifusertalkpages',
687 'disabled' => $disableEmailPrefs,
690 if ( $this->options
->get( 'EnotifUserTalk' ) ||
691 $this->options
->get( 'EnotifWatchlist' ) ) {
692 if ( $this->options
->get( 'EnotifMinorEdits' ) ) {
693 $defaultPreferences['enotifminoredits'] = [
695 'section' => 'personal/email',
696 'label-message' => 'tog-enotifminoredits',
697 'disabled' => $disableEmailPrefs,
701 if ( $this->options
->get( 'EnotifRevealEditorAddress' ) ) {
702 $defaultPreferences['enotifrevealaddr'] = [
704 'section' => 'personal/email',
705 'label-message' => 'tog-enotifrevealaddr',
706 'disabled' => $disableEmailPrefs,
715 * @param IContextSource $context
716 * @param array &$defaultPreferences
719 protected function skinPreferences( User
$user, IContextSource
$context, &$defaultPreferences ) {
720 # # Skin #####################################
722 // Skin selector, if there is at least one valid skin
723 $skinOptions = $this->generateSkinOptions( $user, $context );
724 if ( $skinOptions ) {
725 $defaultPreferences['skin'] = [
727 'options' => $skinOptions,
728 'section' => 'rendering/skin',
732 $allowUserCss = $this->options
->get( 'AllowUserCss' );
733 $allowUserJs = $this->options
->get( 'AllowUserJs' );
734 # Create links to user CSS/JS pages for all skins
735 # This code is basically copied from generateSkinOptions(). It'd
736 # be nice to somehow merge this back in there to avoid redundancy.
737 if ( $allowUserCss ||
$allowUserJs ) {
739 $userName = $user->getName();
741 if ( $allowUserCss ) {
742 $cssPage = Title
::makeTitleSafe( NS_USER
, $userName . '/common.css' );
743 $cssLinkText = $context->msg( 'prefs-custom-css' )->text();
744 $linkTools[] = $this->linkRenderer
->makeLink( $cssPage, $cssLinkText );
747 if ( $allowUserJs ) {
748 $jsPage = Title
::makeTitleSafe( NS_USER
, $userName . '/common.js' );
749 $jsLinkText = $context->msg( 'prefs-custom-js' )->text();
750 $linkTools[] = $this->linkRenderer
->makeLink( $jsPage, $jsLinkText );
753 $defaultPreferences['commoncssjs'] = [
756 'default' => $context->getLanguage()->pipeList( $linkTools ),
757 'label-message' => 'prefs-common-config',
758 'section' => 'rendering/skin',
764 * @param IContextSource $context
765 * @param array &$defaultPreferences
767 protected function filesPreferences( IContextSource
$context, &$defaultPreferences ) {
768 # # Files #####################################
769 $defaultPreferences['imagesize'] = [
771 'options' => $this->getImageSizes( $context ),
772 'label-message' => 'imagemaxsize',
773 'section' => 'rendering/files',
775 $defaultPreferences['thumbsize'] = [
777 'options' => $this->getThumbSizes( $context ),
778 'label-message' => 'thumbsize',
779 'section' => 'rendering/files',
785 * @param IContextSource $context
786 * @param array &$defaultPreferences
789 protected function datetimePreferences( $user, IContextSource
$context, &$defaultPreferences ) {
790 # # Date and time #####################################
791 $dateOptions = $this->getDateOptions( $context );
792 if ( $dateOptions ) {
793 $defaultPreferences['date'] = [
795 'options' => $dateOptions,
796 'section' => 'rendering/dateformat',
801 $now = wfTimestampNow();
802 $lang = $context->getLanguage();
803 $nowlocal = Xml
::element( 'span', [ 'id' => 'wpLocalTime' ],
804 $lang->userTime( $now, $user ) );
805 $nowserver = $lang->userTime( $now, $user,
806 [ 'format' => false, 'timecorrection' => false ] ) .
807 Html
::hidden( 'wpServerTime', (int)substr( $now, 8, 2 ) * 60 +
(int)substr( $now, 10, 2 ) );
809 $defaultPreferences['nowserver'] = [
812 'label-message' => 'servertime',
813 'default' => $nowserver,
814 'section' => 'rendering/timeoffset',
817 $defaultPreferences['nowlocal'] = [
820 'label-message' => 'localtime',
821 'default' => $nowlocal,
822 'section' => 'rendering/timeoffset',
825 // Grab existing pref.
826 $tzOffset = $user->getOption( 'timecorrection' );
827 $tz = explode( '|', $tzOffset, 3 );
829 $tzOptions = $this->getTimezoneOptions( $context );
831 $tzSetting = $tzOffset;
832 if ( count( $tz ) > 1 && $tz[0] == 'ZoneInfo' &&
833 !in_array( $tzOffset, HTMLFormField
::flattenOptions( $tzOptions ) )
835 // Timezone offset can vary with DST
837 $userTZ = new DateTimeZone( $tz[2] );
838 $minDiff = floor( $userTZ->getOffset( new DateTime( 'now' ) ) / 60 );
839 $tzSetting = "ZoneInfo|$minDiff|{$tz[2]}";
840 } catch ( Exception
$e ) {
841 // User has an invalid time zone set. Fall back to just using the offset
845 if ( count( $tz ) > 1 && $tz[0] == 'Offset' ) {
847 $tzSetting = sprintf( '%+03d:%02d', floor( $minDiff / 60 ), abs( $minDiff ) %
60 );
850 $defaultPreferences['timecorrection'] = [
851 'class' => \HTMLSelectOrOtherField
::class,
852 'label-message' => 'timezonelegend',
853 'options' => $tzOptions,
854 'default' => $tzSetting,
856 'section' => 'rendering/timeoffset',
857 'id' => 'wpTimeCorrection',
858 'filter' => TimezoneFilter
::class,
859 'placeholder-message' => 'timezone-useoffset-placeholder',
865 * @param MessageLocalizer $l10n
866 * @param array &$defaultPreferences
868 protected function renderingPreferences(
870 MessageLocalizer
$l10n,
873 # # Diffs ####################################
874 $defaultPreferences['diffonly'] = [
876 'section' => 'rendering/diffs',
877 'label-message' => 'tog-diffonly',
879 $defaultPreferences['norollbackdiff'] = [
881 'section' => 'rendering/diffs',
882 'label-message' => 'tog-norollbackdiff',
885 # # Page Rendering ##############################
886 if ( $this->options
->get( 'AllowUserCssPrefs' ) ) {
887 $defaultPreferences['underline'] = [
890 $l10n->msg( 'underline-never' )->text() => 0,
891 $l10n->msg( 'underline-always' )->text() => 1,
892 $l10n->msg( 'underline-default' )->text() => 2,
894 'label-message' => 'tog-underline',
895 'section' => 'rendering/advancedrendering',
899 $stubThresholdValues = [ 50, 100, 500, 1000, 2000, 5000, 10000 ];
900 $stubThresholdOptions = [ $l10n->msg( 'stub-threshold-disabled' )->text() => 0 ];
901 foreach ( $stubThresholdValues as $value ) {
902 $stubThresholdOptions[$l10n->msg( 'size-bytes', $value )->text()] = $value;
905 $defaultPreferences['stubthreshold'] = [
907 'section' => 'rendering/advancedrendering',
908 'options' => $stubThresholdOptions,
909 // This is not a raw HTML message; label-raw is needed for the manual <a></a>
910 'label-raw' => $l10n->msg( 'stub-threshold' )->rawParams(
912 $l10n->msg( 'stub-threshold-sample-link' )->parse() .
916 $defaultPreferences['showhiddencats'] = [
918 'section' => 'rendering/advancedrendering',
919 'label-message' => 'tog-showhiddencats'
922 $defaultPreferences['numberheadings'] = [
924 'section' => 'rendering/advancedrendering',
925 'label-message' => 'tog-numberheadings',
928 if ( $this->permissionManager
->userHasRight( $user, 'rollback' ) ) {
929 $defaultPreferences['showrollbackconfirmation'] = [
931 'section' => 'rendering/advancedrendering',
932 'label-message' => 'tog-showrollbackconfirmation',
939 * @param MessageLocalizer $l10n
940 * @param array &$defaultPreferences
942 protected function editingPreferences( User
$user, MessageLocalizer
$l10n, &$defaultPreferences ) {
943 # # Editing #####################################
944 $defaultPreferences['editsectiononrightclick'] = [
946 'section' => 'editing/advancedediting',
947 'label-message' => 'tog-editsectiononrightclick',
949 $defaultPreferences['editondblclick'] = [
951 'section' => 'editing/advancedediting',
952 'label-message' => 'tog-editondblclick',
955 if ( $this->options
->get( 'AllowUserCssPrefs' ) ) {
956 $defaultPreferences['editfont'] = [
958 'section' => 'editing/editor',
959 'label-message' => 'editfont-style',
961 $l10n->msg( 'editfont-monospace' )->text() => 'monospace',
962 $l10n->msg( 'editfont-sansserif' )->text() => 'sans-serif',
963 $l10n->msg( 'editfont-serif' )->text() => 'serif',
968 if ( $this->permissionManager
->userHasRight( $user, 'minoredit' ) ) {
969 $defaultPreferences['minordefault'] = [
971 'section' => 'editing/editor',
972 'label-message' => 'tog-minordefault',
976 $defaultPreferences['forceeditsummary'] = [
978 'section' => 'editing/editor',
979 'label-message' => 'tog-forceeditsummary',
981 $defaultPreferences['useeditwarning'] = [
983 'section' => 'editing/editor',
984 'label-message' => 'tog-useeditwarning',
987 $defaultPreferences['previewonfirst'] = [
989 'section' => 'editing/preview',
990 'label-message' => 'tog-previewonfirst',
992 $defaultPreferences['previewontop'] = [
994 'section' => 'editing/preview',
995 'label-message' => 'tog-previewontop',
997 $defaultPreferences['uselivepreview'] = [
999 'section' => 'editing/preview',
1000 'label-message' => 'tog-uselivepreview',
1006 * @param MessageLocalizer $l10n
1007 * @param array &$defaultPreferences
1009 protected function rcPreferences( User
$user, MessageLocalizer
$l10n, &$defaultPreferences ) {
1010 $rcMaxAge = $this->options
->get( 'RCMaxAge' );
1011 # # RecentChanges #####################################
1012 $defaultPreferences['rcdays'] = [
1014 'label-message' => 'recentchangesdays',
1015 'section' => 'rc/displayrc',
1017 'max' => ceil( $rcMaxAge / ( 3600 * 24 ) ),
1018 'help' => $l10n->msg( 'recentchangesdays-max' )->numParams(
1019 ceil( $rcMaxAge / ( 3600 * 24 ) ) )->escaped()
1021 $defaultPreferences['rclimit'] = [
1025 'label-message' => 'recentchangescount',
1026 'help-message' => 'prefs-help-recentchangescount',
1027 'section' => 'rc/displayrc',
1028 'filter' => IntvalFilter
::class,
1030 $defaultPreferences['usenewrc'] = [
1032 'label-message' => 'tog-usenewrc',
1033 'section' => 'rc/advancedrc',
1035 $defaultPreferences['hideminor'] = [
1037 'label-message' => 'tog-hideminor',
1038 'section' => 'rc/changesrc',
1040 $defaultPreferences['rcfilters-rc-collapsed'] = [
1043 $defaultPreferences['rcfilters-wl-collapsed'] = [
1046 $defaultPreferences['rcfilters-saved-queries'] = [
1049 $defaultPreferences['rcfilters-wl-saved-queries'] = [
1052 // Override RCFilters preferences for RecentChanges 'limit'
1053 $defaultPreferences['rcfilters-limit'] = [
1056 $defaultPreferences['rcfilters-saved-queries-versionbackup'] = [
1059 $defaultPreferences['rcfilters-wl-saved-queries-versionbackup'] = [
1063 if ( $this->options
->get( 'RCWatchCategoryMembership' ) ) {
1064 $defaultPreferences['hidecategorization'] = [
1066 'label-message' => 'tog-hidecategorization',
1067 'section' => 'rc/changesrc',
1071 if ( $user->useRCPatrol() ) {
1072 $defaultPreferences['hidepatrolled'] = [
1074 'section' => 'rc/changesrc',
1075 'label-message' => 'tog-hidepatrolled',
1079 if ( $user->useNPPatrol() ) {
1080 $defaultPreferences['newpageshidepatrolled'] = [
1082 'section' => 'rc/changesrc',
1083 'label-message' => 'tog-newpageshidepatrolled',
1087 if ( $this->options
->get( 'RCShowWatchingUsers' ) ) {
1088 $defaultPreferences['shownumberswatching'] = [
1090 'section' => 'rc/advancedrc',
1091 'label-message' => 'tog-shownumberswatching',
1095 $defaultPreferences['rcenhancedfilters-disable'] = [
1097 'section' => 'rc/advancedrc',
1098 'label-message' => 'rcfilters-preference-label',
1099 'help-message' => 'rcfilters-preference-help',
1105 * @param IContextSource $context
1106 * @param array &$defaultPreferences
1108 protected function watchlistPreferences(
1109 User
$user, IContextSource
$context, &$defaultPreferences
1111 $watchlistdaysMax = ceil( $this->options
->get( 'RCMaxAge' ) / ( 3600 * 24 ) );
1113 # # Watchlist #####################################
1114 if ( $this->permissionManager
->userHasRight( $user, 'editmywatchlist' ) ) {
1115 $editWatchlistLinks = '';
1116 $editWatchlistModes = [
1117 'edit' => [ 'subpage' => false, 'flags' => [] ],
1118 'raw' => [ 'subpage' => 'raw', 'flags' => [] ],
1119 'clear' => [ 'subpage' => 'clear', 'flags' => [ 'destructive' ] ],
1121 foreach ( $editWatchlistModes as $mode => $options ) {
1122 // Messages: prefs-editwatchlist-edit, prefs-editwatchlist-raw, prefs-editwatchlist-clear
1123 $editWatchlistLinks .=
1124 new \OOUI\
ButtonWidget( [
1125 'href' => SpecialPage
::getTitleFor( 'EditWatchlist', $options['subpage'] )->getLinkURL(),
1126 'flags' => $options[ 'flags' ],
1127 'label' => new \OOUI\
HtmlSnippet(
1128 $context->msg( "prefs-editwatchlist-{$mode}" )->parse()
1133 $defaultPreferences['editwatchlist'] = [
1136 'default' => $editWatchlistLinks,
1137 'label-message' => 'prefs-editwatchlist-label',
1138 'section' => 'watchlist/editwatchlist',
1142 $defaultPreferences['watchlistdays'] = [
1145 'max' => $watchlistdaysMax,
1146 'section' => 'watchlist/displaywatchlist',
1147 'help' => $context->msg( 'prefs-watchlist-days-max' )->numParams(
1148 $watchlistdaysMax )->escaped(),
1149 'label-message' => 'prefs-watchlist-days',
1151 $defaultPreferences['wllimit'] = [
1155 'label-message' => 'prefs-watchlist-edits',
1156 'help' => $context->msg( 'prefs-watchlist-edits-max' )->escaped(),
1157 'section' => 'watchlist/displaywatchlist',
1158 'filter' => IntvalFilter
::class,
1160 $defaultPreferences['extendwatchlist'] = [
1162 'section' => 'watchlist/advancedwatchlist',
1163 'label-message' => 'tog-extendwatchlist',
1165 $defaultPreferences['watchlisthideminor'] = [
1167 'section' => 'watchlist/changeswatchlist',
1168 'label-message' => 'tog-watchlisthideminor',
1170 $defaultPreferences['watchlisthidebots'] = [
1172 'section' => 'watchlist/changeswatchlist',
1173 'label-message' => 'tog-watchlisthidebots',
1175 $defaultPreferences['watchlisthideown'] = [
1177 'section' => 'watchlist/changeswatchlist',
1178 'label-message' => 'tog-watchlisthideown',
1180 $defaultPreferences['watchlisthideanons'] = [
1182 'section' => 'watchlist/changeswatchlist',
1183 'label-message' => 'tog-watchlisthideanons',
1185 $defaultPreferences['watchlisthideliu'] = [
1187 'section' => 'watchlist/changeswatchlist',
1188 'label-message' => 'tog-watchlisthideliu',
1191 if ( !\SpecialWatchlist
::checkStructuredFilterUiEnabled( $user ) ) {
1192 $defaultPreferences['watchlistreloadautomatically'] = [
1194 'section' => 'watchlist/advancedwatchlist',
1195 'label-message' => 'tog-watchlistreloadautomatically',
1199 $defaultPreferences['watchlistunwatchlinks'] = [
1201 'section' => 'watchlist/advancedwatchlist',
1202 'label-message' => 'tog-watchlistunwatchlinks',
1205 if ( $this->options
->get( 'RCWatchCategoryMembership' ) ) {
1206 $defaultPreferences['watchlisthidecategorization'] = [
1208 'section' => 'watchlist/changeswatchlist',
1209 'label-message' => 'tog-watchlisthidecategorization',
1213 if ( $user->useRCPatrol() ) {
1214 $defaultPreferences['watchlisthidepatrolled'] = [
1216 'section' => 'watchlist/changeswatchlist',
1217 'label-message' => 'tog-watchlisthidepatrolled',
1222 'edit' => 'watchdefault',
1223 'move' => 'watchmoves',
1224 'delete' => 'watchdeletion'
1228 if ( $this->permissionManager
->userHasAnyRight( $user, 'createpage', 'createtalk' ) ) {
1229 $watchTypes['read'] = 'watchcreations';
1232 if ( $this->permissionManager
->userHasRight( $user, 'rollback' ) ) {
1233 $watchTypes['rollback'] = 'watchrollback';
1236 if ( $this->permissionManager
->userHasRight( $user, 'upload' ) ) {
1237 $watchTypes['upload'] = 'watchuploads';
1240 foreach ( $watchTypes as $action => $pref ) {
1241 if ( $this->permissionManager
->userHasRight( $user, $action ) ) {
1243 // tog-watchdefault, tog-watchmoves, tog-watchdeletion, tog-watchcreations, tog-watchuploads
1244 // tog-watchrollback
1245 $defaultPreferences[$pref] = [
1247 'section' => 'watchlist/pageswatchlist',
1248 'label-message' => "tog-$pref",
1253 $defaultPreferences['watchlisttoken'] = [
1257 $tokenButton = new \OOUI\
ButtonWidget( [
1258 'href' => SpecialPage
::getTitleFor( 'ResetTokens' )->getLinkURL( [
1259 'returnto' => SpecialPage
::getTitleFor( 'Preferences' )->getPrefixedText()
1261 'label' => $context->msg( 'prefs-watchlist-managetokens' )->text(),
1263 $defaultPreferences['watchlisttoken-info'] = [
1265 'section' => 'watchlist/tokenwatchlist',
1266 'label-message' => 'prefs-watchlist-token',
1267 'help-message' => 'prefs-help-tokenmanagement',
1269 'default' => (string)$tokenButton,
1272 $defaultPreferences['wlenhancedfilters-disable'] = [
1274 'section' => 'watchlist/advancedwatchlist',
1275 'label-message' => 'rcfilters-watchlist-preference-label',
1276 'help-message' => 'rcfilters-watchlist-preference-help',
1281 * @param array &$defaultPreferences
1283 protected function searchPreferences( &$defaultPreferences ) {
1284 foreach ( $this->nsInfo
->getValidNamespaces() as $n ) {
1285 $defaultPreferences['searchNs' . $n] = [
1292 * @param User $user The User object
1293 * @param IContextSource $context
1294 * @return array Text/links to display as key; $skinkey as value
1296 protected function generateSkinOptions( User
$user, IContextSource
$context ) {
1299 $mptitle = Title
::newMainPage();
1300 $previewtext = $context->msg( 'skin-preview' )->escaped();
1302 # Only show skins that aren't disabled in $wgSkipSkins
1303 $validSkinNames = Skin
::getAllowedSkins();
1304 $allInstalledSkins = Skin
::getSkinNames();
1306 // Display the installed skin the user has specifically requested via useskin=….
1307 $useSkin = $context->getRequest()->getRawVal( 'useskin' );
1308 if ( isset( $allInstalledSkins[$useSkin] )
1309 && $context->msg( "skinname-$useSkin" )->exists()
1311 $validSkinNames[$useSkin] = $useSkin;
1314 // Display the skin if the user has set it as a preference already before it was hidden.
1315 $currentUserSkin = $user->getOption( 'skin' );
1316 if ( isset( $allInstalledSkins[$currentUserSkin] )
1317 && $context->msg( "skinname-$currentUserSkin" )->exists()
1319 $validSkinNames[$currentUserSkin] = $currentUserSkin;
1322 foreach ( $validSkinNames as $skinkey => &$skinname ) {
1323 $msg = $context->msg( "skinname-{$skinkey}" );
1324 if ( $msg->exists() ) {
1325 $skinname = htmlspecialchars( $msg->text() );
1329 $defaultSkin = $this->options
->get( 'DefaultSkin' );
1330 $allowUserCss = $this->options
->get( 'AllowUserCss' );
1331 $allowUserJs = $this->options
->get( 'AllowUserJs' );
1333 # Sort by the internal name, so that the ordering is the same for each display language,
1334 # especially if some skin names are translated to use a different alphabet and some are not.
1335 uksort( $validSkinNames, function ( $a, $b ) use ( $defaultSkin ) {
1336 # Display the default first in the list by comparing it as lesser than any other.
1337 if ( strcasecmp( $a, $defaultSkin ) === 0 ) {
1340 if ( strcasecmp( $b, $defaultSkin ) === 0 ) {
1343 return strcasecmp( $a, $b );
1346 $foundDefault = false;
1347 foreach ( $validSkinNames as $skinkey => $sn ) {
1350 # Mark the default skin
1351 if ( strcasecmp( $skinkey, $defaultSkin ) === 0 ) {
1352 $linkTools[] = $context->msg( 'default' )->escaped();
1353 $foundDefault = true;
1356 # Create preview link
1357 $mplink = htmlspecialchars( $mptitle->getLocalURL( [ 'useskin' => $skinkey ] ) );
1358 $linkTools[] = "<a target='_blank' href=\"$mplink\">$previewtext</a>";
1360 # Create links to user CSS/JS pages
1361 if ( $allowUserCss ) {
1362 $cssPage = Title
::makeTitleSafe( NS_USER
, $user->getName() . '/' . $skinkey . '.css' );
1363 $cssLinkText = $context->msg( 'prefs-custom-css' )->text();
1364 $linkTools[] = $this->linkRenderer
->makeLink( $cssPage, $cssLinkText );
1367 if ( $allowUserJs ) {
1368 $jsPage = Title
::makeTitleSafe( NS_USER
, $user->getName() . '/' . $skinkey . '.js' );
1369 $jsLinkText = $context->msg( 'prefs-custom-js' )->text();
1370 $linkTools[] = $this->linkRenderer
->makeLink( $jsPage, $jsLinkText );
1373 $display = $sn . ' ' . $context->msg( 'parentheses' )
1374 ->rawParams( $context->getLanguage()->pipeList( $linkTools ) )
1376 $ret[$display] = $skinkey;
1379 if ( !$foundDefault ) {
1380 // If the default skin is not available, things are going to break horribly because the
1381 // default value for skin selector will not be a valid value. Let's just not show it then.
1389 * @param IContextSource $context
1392 protected function getDateOptions( IContextSource
$context ) {
1393 $lang = $context->getLanguage();
1394 $dateopts = $lang->getDatePreferences();
1399 if ( !in_array( 'default', $dateopts ) ) {
1400 $dateopts[] = 'default'; // Make sure default is always valid T21237
1403 // FIXME KLUGE: site default might not be valid for user language
1404 global $wgDefaultUserOptions;
1405 if ( !in_array( $wgDefaultUserOptions['date'], $dateopts ) ) {
1406 $wgDefaultUserOptions['date'] = 'default';
1409 $epoch = wfTimestampNow();
1410 foreach ( $dateopts as $key ) {
1411 if ( $key == 'default' ) {
1412 $formatted = $context->msg( 'datedefault' )->escaped();
1414 $formatted = htmlspecialchars( $lang->timeanddate( $epoch, false, $key ) );
1416 $ret[$formatted] = $key;
1423 * @param MessageLocalizer $l10n
1426 protected function getImageSizes( MessageLocalizer
$l10n ) {
1428 $pixels = $l10n->msg( 'unit-pixel' )->text();
1430 foreach ( $this->options
->get( 'ImageLimits' ) as $index => $limits ) {
1431 // Note: A left-to-right marker (U+200E) is inserted, see T144386
1432 $display = "{$limits[0]}\u{200E}×{$limits[1]}$pixels";
1433 $ret[$display] = $index;
1440 * @param MessageLocalizer $l10n
1443 protected function getThumbSizes( MessageLocalizer
$l10n ) {
1445 $pixels = $l10n->msg( 'unit-pixel' )->text();
1447 foreach ( $this->options
->get( 'ThumbLimits' ) as $index => $size ) {
1448 $display = $size . $pixels;
1449 $ret[$display] = $index;
1456 * @param string $signature
1457 * @param array $alldata
1458 * @param HTMLForm $form
1459 * @return bool|string
1461 protected function validateSignature( $signature, $alldata, HTMLForm
$form ) {
1462 $maxSigChars = $this->options
->get( 'MaxSigChars' );
1463 if ( mb_strlen( $signature ) > $maxSigChars ) {
1464 return Xml
::element( 'span', [ 'class' => 'error' ],
1465 $form->msg( 'badsiglength' )->numParams( $maxSigChars )->text() );
1466 } elseif ( isset( $alldata['fancysig'] ) &&
1467 $alldata['fancysig'] &&
1468 MediaWikiServices
::getInstance()->getParser()->validateSig( $signature ) === false
1470 return Xml
::element(
1472 [ 'class' => 'error' ],
1473 $form->msg( 'badsig' )->text()
1481 * @param string $signature
1482 * @param array $alldata
1483 * @param HTMLForm $form
1486 protected function cleanSignature( $signature, $alldata, HTMLForm
$form ) {
1487 $parser = MediaWikiServices
::getInstance()->getParser();
1488 if ( isset( $alldata['fancysig'] ) && $alldata['fancysig'] ) {
1489 $signature = $parser->cleanSig( $signature );
1491 // When no fancy sig used, make sure ~{3,5} get removed.
1492 $signature = Parser
::cleanSigInSig( $signature );
1500 * @param IContextSource $context
1501 * @param string $formClass
1502 * @param array $remove Array of items to remove
1505 public function getForm(
1507 IContextSource
$context,
1508 $formClass = PreferencesFormOOUI
::class,
1511 // We use ButtonWidgets in some of the getPreferences() functions
1512 $context->getOutput()->enableOOUI();
1514 $formDescriptor = $this->getFormDescriptor( $user, $context );
1515 if ( count( $remove ) ) {
1516 $removeKeys = array_flip( $remove );
1517 $formDescriptor = array_diff_key( $formDescriptor, $removeKeys );
1520 // Remove type=api preferences. They are not intended for rendering in the form.
1521 foreach ( $formDescriptor as $name => $info ) {
1522 if ( isset( $info['type'] ) && $info['type'] === 'api' ) {
1523 unset( $formDescriptor[$name] );
1528 * @var HTMLForm $htmlForm
1530 $htmlForm = new $formClass( $formDescriptor, $context, 'prefs' );
1532 // This allows users to opt-in to hidden skins. While this should be discouraged and is not
1533 // discoverable, this allows users to still use hidden skins while preventing new users from
1534 // adopting unsupported skins. If no useskin=… parameter was provided, it will not show up
1535 // in the resulting URL.
1536 $htmlForm->setAction( $context->getTitle()->getLocalURL( [
1537 'useskin' => $context->getRequest()->getRawVal( 'useskin' )
1540 $htmlForm->setModifiedUser( $user );
1541 $htmlForm->setId( 'mw-prefs-form' );
1542 $htmlForm->setAutocomplete( 'off' );
1543 $htmlForm->setSubmitText( $context->msg( 'saveprefs' )->text() );
1544 # Used message keys: 'accesskey-preferences-save', 'tooltip-preferences-save'
1545 $htmlForm->setSubmitTooltip( 'preferences-save' );
1546 $htmlForm->setSubmitID( 'prefcontrol' );
1547 $htmlForm->setSubmitCallback(
1548 function ( array $formData, HTMLForm
$form ) use ( $formDescriptor ) {
1549 return $this->submitForm( $formData, $form, $formDescriptor );
1557 * @param IContextSource $context
1560 protected function getTimezoneOptions( IContextSource
$context ) {
1563 $localTZoffset = $this->options
->get( 'LocalTZoffset' );
1564 $timeZoneList = $this->getTimeZoneList( $context->getLanguage() );
1566 $timestamp = MWTimestamp
::getLocalInstance();
1567 // Check that the LocalTZoffset is the same as the local time zone offset
1568 if ( $localTZoffset == $timestamp->format( 'Z' ) / 60 ) {
1569 $timezoneName = $timestamp->getTimezone()->getName();
1570 // Localize timezone
1571 if ( isset( $timeZoneList[$timezoneName] ) ) {
1572 $timezoneName = $timeZoneList[$timezoneName]['name'];
1574 $server_tz_msg = $context->msg(
1575 'timezoneuseserverdefault',
1579 $tzstring = sprintf(
1581 floor( $localTZoffset / 60 ),
1582 abs( $localTZoffset ) %
60
1584 $server_tz_msg = $context->msg( 'timezoneuseserverdefault', $tzstring )->text();
1586 $opt[$server_tz_msg] = "System|$localTZoffset";
1587 $opt[$context->msg( 'timezoneuseoffset' )->text()] = 'other';
1588 $opt[$context->msg( 'guesstimezone' )->text()] = 'guess';
1590 foreach ( $timeZoneList as $timeZoneInfo ) {
1591 $region = $timeZoneInfo['region'];
1592 if ( !isset( $opt[$region] ) ) {
1595 $opt[$region][$timeZoneInfo['name']] = $timeZoneInfo['timecorrection'];
1601 * Handle the form submission if everything validated properly
1603 * @param array $formData
1604 * @param PreferencesFormOOUI $form
1605 * @param array[] $formDescriptor
1606 * @return bool|Status|string
1608 protected function saveFormData( $formData, PreferencesFormOOUI
$form, array $formDescriptor ) {
1609 $user = $form->getModifiedUser();
1610 $hiddenPrefs = $this->options
->get( 'HiddenPrefs' );
1613 if ( !$this->permissionManager
1614 ->userHasAnyRight( $user, 'editmyprivateinfo', 'editmyoptions' )
1616 return Status
::newFatal( 'mypreferencesprotected' );
1620 $this->applyFilters( $formData, $formDescriptor, 'filterFromForm' );
1622 // Fortunately, the realname field is MUCH simpler
1623 // (not really "private", but still shouldn't be edited without permission)
1625 if ( !in_array( 'realname', $hiddenPrefs )
1626 && $this->permissionManager
->userHasRight( $user, 'editmyprivateinfo' )
1627 && array_key_exists( 'realname', $formData )
1629 $realName = $formData['realname'];
1630 $user->setRealName( $realName );
1633 if ( $this->permissionManager
->userHasRight( $user, 'editmyoptions' ) ) {
1634 $oldUserOptions = $user->getOptions();
1636 foreach ( $this->getSaveBlacklist() as $b ) {
1637 unset( $formData[$b] );
1640 # If users have saved a value for a preference which has subsequently been disabled
1641 # via $wgHiddenPrefs, we don't want to destroy that setting in case the preference
1642 # is subsequently re-enabled
1643 foreach ( $hiddenPrefs as $pref ) {
1644 # If the user has not set a non-default value here, the default will be returned
1645 # and subsequently discarded
1646 $formData[$pref] = $user->getOption( $pref, null, true );
1649 // If the user changed the rclimit preference, also change the rcfilters-rclimit preference
1651 isset( $formData['rclimit'] ) &&
1652 intval( $formData[ 'rclimit' ] ) !== $user->getIntOption( 'rclimit' )
1654 $formData['rcfilters-limit'] = $formData['rclimit'];
1657 // Keep old preferences from interfering due to back-compat code, etc.
1658 $user->resetOptions( 'unused', $form->getContext() );
1660 foreach ( $formData as $key => $value ) {
1661 $user->setOption( $key, $value );
1665 'PreferencesFormPreSave',
1666 [ $formData, $form, $user, &$result, $oldUserOptions ]
1670 $user->saveSettings();
1676 * Applies filters to preferences either before or after form usage
1678 * @param array &$preferences
1679 * @param array $formDescriptor
1680 * @param string $verb Name of the filter method to call, either 'filterFromForm' or
1683 protected function applyFilters( array &$preferences, array $formDescriptor, $verb ) {
1684 foreach ( $formDescriptor as $preference => $desc ) {
1685 if ( !isset( $desc['filter'] ) ||
!isset( $preferences[$preference] ) ) {
1688 $filterDesc = $desc['filter'];
1689 if ( $filterDesc instanceof Filter
) {
1690 $filter = $filterDesc;
1691 } elseif ( class_exists( $filterDesc ) ) {
1692 $filter = new $filterDesc();
1693 } elseif ( is_callable( $filterDesc ) ) {
1694 $filter = $filterDesc();
1696 throw new UnexpectedValueException(
1697 "Unrecognized filter type for preference '$preference'"
1700 $preferences[$preference] = $filter->$verb( $preferences[$preference] );
1705 * Save the form data and reload the page
1707 * @param array $formData
1708 * @param PreferencesFormOOUI $form
1709 * @param array $formDescriptor
1712 protected function submitForm(
1714 PreferencesFormOOUI
$form,
1715 array $formDescriptor
1717 $res = $this->saveFormData( $formData, $form, $formDescriptor );
1719 if ( $res === true ) {
1720 $context = $form->getContext();
1723 if ( $res === 'eauth' ) {
1724 $urlOptions['eauth'] = 1;
1727 $urlOptions +
= $form->getExtraSuccessRedirectParameters();
1729 $url = $form->getTitle()->getFullURL( $urlOptions );
1731 // Set session data for the success message
1732 $context->getRequest()->getSession()->set( 'specialPreferencesSaveSuccess', 1 );
1734 $context->getOutput()->redirect( $url );
1737 return ( $res === true ? Status
::newGood() : $res );
1741 * Get a list of all time zones
1742 * @param Language $language Language used for the localized names
1743 * @return array A list of all time zones. The system name of the time zone is used as key and
1744 * the value is an array which contains localized name, the timecorrection value used for
1745 * preferences and the region
1748 protected function getTimeZoneList( Language
$language ) {
1749 $identifiers = DateTimeZone
::listIdentifiers();
1750 // @phan-suppress-next-line PhanTypeComparisonFromArray See phan issue #3162
1751 if ( $identifiers === false ) {
1754 sort( $identifiers );
1757 'Africa' => wfMessage( 'timezoneregion-africa' )->inLanguage( $language )->text(),
1758 'America' => wfMessage( 'timezoneregion-america' )->inLanguage( $language )->text(),
1759 'Antarctica' => wfMessage( 'timezoneregion-antarctica' )->inLanguage( $language )->text(),
1760 'Arctic' => wfMessage( 'timezoneregion-arctic' )->inLanguage( $language )->text(),
1761 'Asia' => wfMessage( 'timezoneregion-asia' )->inLanguage( $language )->text(),
1762 'Atlantic' => wfMessage( 'timezoneregion-atlantic' )->inLanguage( $language )->text(),
1763 'Australia' => wfMessage( 'timezoneregion-australia' )->inLanguage( $language )->text(),
1764 'Europe' => wfMessage( 'timezoneregion-europe' )->inLanguage( $language )->text(),
1765 'Indian' => wfMessage( 'timezoneregion-indian' )->inLanguage( $language )->text(),
1766 'Pacific' => wfMessage( 'timezoneregion-pacific' )->inLanguage( $language )->text(),
1768 asort( $tzRegions );
1772 $now = new DateTime();
1774 foreach ( $identifiers as $identifier ) {
1775 $parts = explode( '/', $identifier, 2 );
1777 // DateTimeZone::listIdentifiers() returns a number of
1778 // backwards-compatibility entries. This filters them out of the
1779 // list presented to the user.
1780 if ( count( $parts ) !== 2 ||
!array_key_exists( $parts[0], $tzRegions ) ) {
1785 $parts[0] = $tzRegions[$parts[0]];
1787 $dateTimeZone = new DateTimeZone( $identifier );
1788 $minDiff = floor( $dateTimeZone->getOffset( $now ) / 60 );
1790 $display = str_replace( '_', ' ', $parts[0] . '/' . $parts[1] );
1791 $value = "ZoneInfo|$minDiff|$identifier";
1793 $timeZoneList[$identifier] = [
1795 'timecorrection' => $value,
1796 'region' => $parts[0],
1800 return $timeZoneList;