MediaWiki  master
SpecialPreferences.php
Go to the documentation of this file.
1 <?php
28 
35 
38 
41 
44 
50  public function __construct(
54  ) {
55  parent::__construct( 'Preferences' );
56  // This class is extended and therefore falls back to global state - T265924
57  $services = MediaWikiServices::getInstance();
58  $this->preferencesFactory = $preferencesFactory ?? $services->getPreferencesFactory();
59  $this->permissionManager = $permissionManager ?? $services->getPermissionManager();
60  $this->userOptionsManager = $userOptionsManager ?? $services->getUserOptionsManager();
61  }
62 
63  public function doesWrites() {
64  return true;
65  }
66 
67  public function execute( $par ) {
68  $this->setHeaders();
69  $this->outputHeader();
70  $out = $this->getOutput();
71  $out->disallowUserJs(); # Prevent hijacked user scripts from sniffing passwords etc.
72 
73  $this->requireLogin( 'prefsnologintext2' );
74  $this->checkReadOnly();
75 
76  if ( $par == 'reset' ) {
77  $this->showResetForm();
78 
79  return;
80  }
81 
82  $out->addModules( 'mediawiki.special.preferences.ooui' );
83  $out->addModuleStyles( [
84  'mediawiki.special.preferences.styles.ooui',
85  'mediawiki.widgets.TagMultiselectWidget.styles',
86  ] );
87  $out->addModuleStyles( 'oojs-ui-widgets.styles' );
88 
89  $session = $this->getRequest()->getSession();
90  if ( $session->get( 'specialPreferencesSaveSuccess' ) ) {
91  // Remove session data for the success message
92  $session->remove( 'specialPreferencesSaveSuccess' );
93  $out->addModuleStyles( 'mediawiki.notification.convertmessagebox.styles' );
94 
95  $out->addHTML(
97  'div',
98  [
99  'class' => 'mw-preferences-messagebox mw-notify-success successbox',
100  'id' => 'mw-preferences-success',
101  'data-mw-autohide' => 'false',
102  ],
103  Html::element( 'p', [], $this->msg( 'savedprefs' )->text() )
104  )
105  );
106  }
107 
108  $this->addHelpLink( 'Help:Preferences' );
109 
110  // Load the user from the master to reduce CAS errors on double post (T95839)
111  if ( $this->getRequest()->wasPosted() ) {
112  $user = $this->getUser()->getInstanceForUpdate() ?: $this->getUser();
113  } else {
114  $user = $this->getUser();
115  }
116 
117  $htmlForm = $this->getFormObject( $user, $this->getContext() );
118  $sectionTitles = $htmlForm->getPreferenceSections();
119 
120  $prefTabs = [];
121  foreach ( $sectionTitles as $key ) {
122  $prefTabs[] = [
123  'name' => $key,
124  'label' => $htmlForm->getLegend( $key ),
125  ];
126  }
127  $out->addJsConfigVars( 'wgPreferencesTabs', $prefTabs );
128 
129  $htmlForm->show();
130  }
131 
138  protected function getFormObject( $user, IContextSource $context ) {
139  $form = $this->preferencesFactory->getForm( $user, $context, PreferencesFormOOUI::class );
140  return $form;
141  }
142 
143  protected function showResetForm() {
144  if ( !$this->permissionManager->userHasRight( $this->getUser(), 'editmyoptions' ) ) {
145  throw new PermissionsError( 'editmyoptions' );
146  }
147 
148  $this->getOutput()->addWikiMsg( 'prefs-reset-intro' );
149 
150  $context = new DerivativeContext( $this->getContext() );
151  $context->setTitle( $this->getPageTitle( 'reset' ) ); // Reset subpage
152  HTMLForm::factory( 'ooui', [], $context, 'prefs-restore' )
153  ->setSubmitTextMsg( 'restoreprefs' )
154  ->setSubmitDestructive()
155  ->setSubmitCallback( [ $this, 'submitReset' ] )
156  ->suppressReset()
157  ->show();
158  }
159 
160  public function submitReset( $formData ) {
161  if ( !$this->permissionManager->userHasRight( $this->getUser(), 'editmyoptions' ) ) {
162  throw new PermissionsError( 'editmyoptions' );
163  }
164 
165  $user = $this->getUser()->getInstanceForUpdate();
166  $this->userOptionsManager->resetOptions( $user, $this->getContext(), 'all' );
167  $user->saveSettings();
168 
169  // Set session data for the success message
170  $this->getRequest()->getSession()->set( 'specialPreferencesSaveSuccess', 1 );
171 
172  $url = $this->getPageTitle()->getFullUrlForRedirect();
173  $this->getOutput()->redirect( $url );
174 
175  return true;
176  }
177 
178  protected function getGroupName() {
179  return 'users';
180  }
181 }
SpecialPage\getPageTitle
getPageTitle( $subpage=false)
Get a self-referential title object.
Definition: SpecialPage.php:742
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:900
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:788
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:166
SpecialPreferences\$userOptionsManager
UserOptionsManager $userOptionsManager
Definition: SpecialPreferences.php:43
SpecialPreferences
A special page that allows users to change their preferences.
Definition: SpecialPreferences.php:34
SpecialPreferences\execute
execute( $par)
Default execute method Checks user permissions.
Definition: SpecialPreferences.php:67
SpecialPreferences\doesWrites
doesWrites()
Indicates whether this special page may perform database writes.
Definition: SpecialPreferences.php:63
PermissionsError
Show an error when a user tries to do something they do not have the necessary permissions for.
Definition: PermissionsError.php:31
SpecialPreferences\$preferencesFactory
PreferencesFactory $preferencesFactory
Definition: SpecialPreferences.php:37
DerivativeContext
An IContextSource implementation which will inherit context from another source but allow individual ...
Definition: DerivativeContext.php:31
SpecialPage\addHelpLink
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Definition: SpecialPage.php:936
SpecialPage\setHeaders
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!...
Definition: SpecialPage.php:616
SpecialPage\getUser
getUser()
Shortcut to get the User executing this instance.
Definition: SpecialPage.php:798
SpecialPage\getContext
getContext()
Gets the context this SpecialPage is executed in.
Definition: SpecialPage.php:762
SpecialPage\requireLogin
requireLogin( $reasonMsg='exception-nologin-text', $titleMsg='exception-nologin')
If the user is not logged in, throws UserNotLoggedIn error.
Definition: SpecialPage.php:386
MediaWiki\Permissions\PermissionManager
A service class for checking permissions To obtain an instance, use MediaWikiServices::getInstance()-...
Definition: PermissionManager.php:51
SpecialPage
Parent class for all special pages.
Definition: SpecialPage.php:42
MediaWiki\Preferences\PreferencesFactory
A PreferencesFactory is a MediaWiki service that provides the definitions of preferences for a given ...
Definition: PreferencesFactory.php:54
SpecialPage\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: SpecialPage.php:778
SpecialPreferences\getFormObject
getFormObject( $user, IContextSource $context)
Get the preferences form to use.
Definition: SpecialPreferences.php:138
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:55
MediaWiki\User\UserOptionsManager
A service class to control user options.
Definition: UserOptionsManager.php:44
SpecialPreferences\$permissionManager
PermissionManager $permissionManager
Definition: SpecialPreferences.php:40
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:212
SpecialPreferences\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialPreferences.php:178
SpecialPage\checkReadOnly
checkReadOnly()
If the wiki is currently in readonly mode, throws a ReadOnlyError.
Definition: SpecialPage.php:369
SpecialPreferences\submitReset
submitReset( $formData)
Definition: SpecialPreferences.php:160
Html\element
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:234
SpecialPreferences\showResetForm
showResetForm()
Definition: SpecialPreferences.php:143
SpecialPreferences\__construct
__construct(PreferencesFactory $preferencesFactory=null, PermissionManager $permissionManager=null, UserOptionsManager $userOptionsManager=null)
Definition: SpecialPreferences.php:50
HTMLForm\factory
static factory( $displayFormat,... $arguments)
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:321
SpecialPage\outputHeader
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
Definition: SpecialPage.php:707