MediaWiki  master
SpecialPreferences.php
Go to the documentation of this file.
1 <?php
25 
32  public function __construct() {
33  parent::__construct( 'Preferences' );
34  }
35 
36  public function doesWrites() {
37  return true;
38  }
39 
40  public function execute( $par ) {
41  $this->setHeaders();
42  $this->outputHeader();
43  $out = $this->getOutput();
44  $out->disallowUserJs(); # Prevent hijacked user scripts from sniffing passwords etc.
45 
46  $this->requireLogin( 'prefsnologintext2' );
47  $this->checkReadOnly();
48 
49  if ( $par == 'reset' ) {
50  $this->showResetForm();
51 
52  return;
53  }
54 
55  $out->addModules( 'mediawiki.special.preferences.ooui' );
56  $out->addModuleStyles( [
57  'mediawiki.special.preferences.styles.ooui',
58  'mediawiki.widgets.TagMultiselectWidget.styles',
59  ] );
60  $out->addModuleStyles( 'oojs-ui-widgets.styles' );
61 
62  $session = $this->getRequest()->getSession();
63  if ( $session->get( 'specialPreferencesSaveSuccess' ) ) {
64  // Remove session data for the success message
65  $session->remove( 'specialPreferencesSaveSuccess' );
66  $out->addModuleStyles( 'mediawiki.notification.convertmessagebox.styles' );
67 
68  $out->addHTML(
70  'div',
71  [
72  'class' => 'mw-preferences-messagebox mw-notify-success successbox',
73  'id' => 'mw-preferences-success',
74  'data-mw-autohide' => 'false',
75  ],
76  Html::element( 'p', [], $this->msg( 'savedprefs' )->text() )
77  )
78  );
79  }
80 
81  $this->addHelpLink( 'Help:Preferences' );
82 
83  // Load the user from the master to reduce CAS errors on double post (T95839)
84  if ( $this->getRequest()->wasPosted() ) {
85  $user = $this->getUser()->getInstanceForUpdate() ?: $this->getUser();
86  } else {
87  $user = $this->getUser();
88  }
89 
90  $htmlForm = $this->getFormObject( $user, $this->getContext() );
91  $sectionTitles = $htmlForm->getPreferenceSections();
92 
93  $prefTabs = [];
94  foreach ( $sectionTitles as $key ) {
95  $prefTabs[] = [
96  'name' => $key,
97  'label' => $htmlForm->getLegend( $key ),
98  ];
99  }
100  $out->addJsConfigVars( 'wgPreferencesTabs', $prefTabs );
101 
102  $htmlForm->show();
103  }
104 
111  protected function getFormObject( $user, IContextSource $context ) {
112  $preferencesFactory = MediaWikiServices::getInstance()->getPreferencesFactory();
113  $form = $preferencesFactory->getForm( $user, $context, PreferencesFormOOUI::class );
114  return $form;
115  }
116 
117  protected function showResetForm() {
118  if ( !MediaWikiServices::getInstance()
120  ->userHasRight( $this->getUser(), 'editmyoptions' )
121  ) {
122  throw new PermissionsError( 'editmyoptions' );
123  }
124 
125  $this->getOutput()->addWikiMsg( 'prefs-reset-intro' );
126 
127  $context = new DerivativeContext( $this->getContext() );
128  $context->setTitle( $this->getPageTitle( 'reset' ) ); // Reset subpage
129  HTMLForm::factory( 'ooui', [], $context, 'prefs-restore' )
130  ->setSubmitTextMsg( 'restoreprefs' )
131  ->setSubmitDestructive()
132  ->setSubmitCallback( [ $this, 'submitReset' ] )
133  ->suppressReset()
134  ->show();
135  }
136 
137  public function submitReset( $formData ) {
138  if ( !MediaWikiServices::getInstance()
140  ->userHasRight( $this->getUser(), 'editmyoptions' )
141  ) {
142  throw new PermissionsError( 'editmyoptions' );
143  }
144 
145  $user = $this->getUser()->getInstanceForUpdate();
146  $user->resetOptions( 'all', $this->getContext() );
147  $user->saveSettings();
148 
149  // Set session data for the success message
150  $this->getRequest()->getSession()->set( 'specialPreferencesSaveSuccess', 1 );
151 
152  $url = $this->getPageTitle()->getFullUrlForRedirect();
153  $this->getOutput()->redirect( $url );
154 
155  return true;
156  }
157 
158  protected function getGroupName() {
159  return 'users';
160  }
161 }
SpecialPage\getPageTitle
getPageTitle( $subpage=false)
Get a self-referential title object.
Definition: SpecialPage.php:697
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:827
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:743
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:163
SpecialPreferences
A special page that allows users to change their preferences.
Definition: SpecialPreferences.php:31
SpecialPreferences\execute
execute( $par)
Default execute method Checks user permissions.
Definition: SpecialPreferences.php:40
SpecialPreferences\doesWrites
doesWrites()
Indicates whether this special page may perform database writes.
Definition: SpecialPreferences.php:36
PermissionsError
Show an error when a user tries to do something they do not have the necessary permissions for.
Definition: PermissionsError.php:31
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:863
getPermissionManager
getPermissionManager()
SpecialPage\setHeaders
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!...
Definition: SpecialPage.php:571
SpecialPage\getUser
getUser()
Shortcut to get the User executing this instance.
Definition: SpecialPage.php:753
SpecialPage\getContext
getContext()
Gets the context this SpecialPage is executed in.
Definition: SpecialPage.php:717
SpecialPage\requireLogin
requireLogin( $reasonMsg='exception-nologin-text', $titleMsg='exception-nologin')
If the user is not logged in, throws UserNotLoggedIn error.
Definition: SpecialPage.php:373
SpecialPage
Parent class for all special pages.
Definition: SpecialPage.php:41
SpecialPage\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: SpecialPage.php:733
SpecialPreferences\getFormObject
getFormObject( $user, IContextSource $context)
Get the preferences form to use.
Definition: SpecialPreferences.php:111
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:55
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:209
SpecialPreferences\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialPreferences.php:158
SpecialPage\checkReadOnly
checkReadOnly()
If the wiki is currently in readonly mode, throws a ReadOnlyError.
Definition: SpecialPage.php:356
SpecialPreferences\submitReset
submitReset( $formData)
Definition: SpecialPreferences.php:137
Html\element
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:231
SpecialPreferences\showResetForm
showResetForm()
Definition: SpecialPreferences.php:117
HTMLForm\factory
static factory( $displayFormat,... $arguments)
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:315
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:662
SpecialPreferences\__construct
__construct()
Definition: SpecialPreferences.php:32