MediaWiki  master
SpecialPreferences.php
Go to the documentation of this file.
1 <?php
27 
34 
36  private $preferencesFactory;
37 
39  private $userOptionsManager;
40 
45  public function __construct(
46  PreferencesFactory $preferencesFactory = null,
47  UserOptionsManager $userOptionsManager = null
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->requireNamedUser( '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(
91  'p',
92  [],
93  $this->msg( 'savedprefs' )->text()
94  ),
95  'mw-preferences-messagebox mw-notify-success'
96  )
97  );
98  }
99 
100  $this->addHelpLink( 'Help:Preferences' );
101 
102  // Load the user from the primary DB to reduce CAS errors on double post (T95839)
103  if ( $this->getRequest()->wasPosted() ) {
104  $user = $this->getUser()->getInstanceForUpdate() ?: $this->getUser();
105  } else {
106  $user = $this->getUser();
107  }
108 
109  $htmlForm = $this->getFormObject( $user, $this->getContext() );
110  $sectionTitles = $htmlForm->getPreferenceSections();
111 
112  $prefTabs = [];
113  foreach ( $sectionTitles as $key ) {
114  $prefTabs[] = [
115  'name' => $key,
116  'label' => $htmlForm->getLegend( $key ),
117  ];
118  }
119  $out->addJsConfigVars( 'wgPreferencesTabs', $prefTabs );
120 
121  $htmlForm->show();
122  }
123 
130  protected function getFormObject( $user, IContextSource $context ) {
131  $form = $this->preferencesFactory->getForm( $user, $context, PreferencesFormOOUI::class );
132  return $form;
133  }
134 
135  protected function showResetForm() {
136  if ( !$this->getAuthority()->isAllowed( 'editmyoptions' ) ) {
137  throw new PermissionsError( 'editmyoptions' );
138  }
139 
140  $this->getOutput()->addWikiMsg( 'prefs-reset-intro' );
141 
142  $desc = [
143  'confirm' => [
144  'type' => 'check',
145  'label-message' => 'prefs-reset-confirm',
146  'required' => true,
147  ],
148  ];
149  // TODO: disable the submit button if the checkbox is not checked
150  HTMLForm::factory( 'ooui', $desc, $this->getContext(), 'prefs-restore' )
151  ->setTitle( $this->getPageTitle( 'reset' ) ) // Reset subpage
152  ->setSubmitTextMsg( 'restoreprefs' )
153  ->setSubmitDestructive()
154  ->setSubmitCallback( [ $this, 'submitReset' ] )
155  ->suppressReset()
156  ->showCancel()
157  ->setCancelTarget( $this->getPageTitle() )
158  ->show();
159  }
160 
161  public function submitReset( $formData ) {
162  if ( !$this->getAuthority()->isAllowed( 'editmyoptions' ) ) {
163  throw new PermissionsError( 'editmyoptions' );
164  }
165 
166  $user = $this->getUser()->getInstanceForUpdate();
167  $this->userOptionsManager->resetOptions( $user, $this->getContext(), 'all' );
168  $user->saveSettings();
169 
170  // Set session data for the success message
171  $this->getRequest()->getSession()->set( 'specialPreferencesSaveSuccess', 1 );
172 
173  $url = $this->getPageTitle()->getFullUrlForRedirect();
174  $this->getOutput()->redirect( $url );
175 
176  return true;
177  }
178 
179  protected function getGroupName() {
180  return 'users';
181  }
182 }
static factory( $displayFormat, $descriptor, IContextSource $context, $messagePrefix='')
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:349
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:236
static successBox( $html, $className='')
Return a success box.
Definition: Html.php:800
Service locator for MediaWiki core services.
A service class to control user options.
Show an error when a user tries to do something they do not have the necessary permissions for.
Parent class for all special pages.
Definition: SpecialPage.php:44
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getOutput()
Get the OutputPage being used for this instance.
getUser()
Shortcut to get the User executing this instance.
requireNamedUser( $reasonMsg='exception-nologin-text', $titleMsg='exception-nologin')
If the user is not logged in or is a temporary user, throws UserNotLoggedIn.
getContext()
Gets the context this SpecialPage is executed in.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getAuthority()
Shortcut to get the Authority executing this instance.
getRequest()
Get the WebRequest being used for this instance.
checkReadOnly()
If the wiki is currently in readonly mode, throws a ReadOnlyError.
getPageTitle( $subpage=false)
Get a self-referential title object.
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
A special page that allows users to change their preferences.
execute( $par)
Default execute method Checks user permissions.
doesWrites()
Indicates whether this special page may perform database writes.
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
__construct(PreferencesFactory $preferencesFactory=null, UserOptionsManager $userOptionsManager=null)
getFormObject( $user, IContextSource $context)
Get the preferences form to use.
Interface for objects which can provide a MediaWiki context on request.
A PreferencesFactory is a MediaWiki service that provides the definitions of preferences for a given ...