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