SpecialPreferences.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * Implements Special:Preferences
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup SpecialPage
  22. */
  23. use MediaWiki\MediaWikiServices;
  24. /**
  25. * A special page that allows users to change their preferences
  26. *
  27. * @ingroup SpecialPage
  28. */
  29. class SpecialPreferences extends SpecialPage {
  30. function __construct() {
  31. parent::__construct( 'Preferences' );
  32. }
  33. public function doesWrites() {
  34. return true;
  35. }
  36. public function execute( $par ) {
  37. $this->setHeaders();
  38. $this->outputHeader();
  39. $out = $this->getOutput();
  40. $out->disallowUserJs(); # Prevent hijacked user scripts from sniffing passwords etc.
  41. $this->requireLogin( 'prefsnologintext2' );
  42. $this->checkReadOnly();
  43. if ( $par == 'reset' ) {
  44. $this->showResetForm();
  45. return;
  46. }
  47. $out->addModules( 'mediawiki.special.preferences.ooui' );
  48. $out->addModuleStyles( [
  49. 'mediawiki.special.preferences.styles.ooui',
  50. 'mediawiki.widgets.TagMultiselectWidget.styles',
  51. ] );
  52. $out->addModuleStyles( 'oojs-ui-widgets.styles' );
  53. $session = $this->getRequest()->getSession();
  54. if ( $session->get( 'specialPreferencesSaveSuccess' ) ) {
  55. // Remove session data for the success message
  56. $session->remove( 'specialPreferencesSaveSuccess' );
  57. $out->addModuleStyles( 'mediawiki.notification.convertmessagebox.styles' );
  58. $out->addHTML(
  59. Html::rawElement(
  60. 'div',
  61. [
  62. 'class' => 'mw-preferences-messagebox mw-notify-success successbox',
  63. 'id' => 'mw-preferences-success',
  64. 'data-mw-autohide' => 'false',
  65. ],
  66. Html::element( 'p', [], $this->msg( 'savedprefs' )->text() )
  67. )
  68. );
  69. }
  70. $this->addHelpLink( 'Help:Preferences' );
  71. // Load the user from the master to reduce CAS errors on double post (T95839)
  72. if ( $this->getRequest()->wasPosted() ) {
  73. $user = $this->getUser()->getInstanceForUpdate() ?: $this->getUser();
  74. } else {
  75. $user = $this->getUser();
  76. }
  77. $htmlForm = $this->getFormObject( $user, $this->getContext() );
  78. $sectionTitles = $htmlForm->getPreferenceSections();
  79. $prefTabs = [];
  80. foreach ( $sectionTitles as $key ) {
  81. $prefTabs[] = [
  82. 'name' => $key,
  83. 'label' => $htmlForm->getLegend( $key ),
  84. ];
  85. }
  86. $out->addJsConfigVars( 'wgPreferencesTabs', $prefTabs );
  87. $htmlForm->show();
  88. }
  89. /**
  90. * Get the preferences form to use.
  91. * @param User $user The user.
  92. * @param IContextSource $context The context.
  93. * @return PreferencesFormOOUI|HTMLForm
  94. */
  95. protected function getFormObject( $user, IContextSource $context ) {
  96. $preferencesFactory = MediaWikiServices::getInstance()->getPreferencesFactory();
  97. $form = $preferencesFactory->getForm( $user, $context, PreferencesFormOOUI::class );
  98. return $form;
  99. }
  100. protected function showResetForm() {
  101. if ( !MediaWikiServices::getInstance()
  102. ->getPermissionManager()
  103. ->userHasRight( $this->getUser(), 'editmyoptions' )
  104. ) {
  105. throw new PermissionsError( 'editmyoptions' );
  106. }
  107. $this->getOutput()->addWikiMsg( 'prefs-reset-intro' );
  108. $context = new DerivativeContext( $this->getContext() );
  109. $context->setTitle( $this->getPageTitle( 'reset' ) ); // Reset subpage
  110. $htmlForm = HTMLForm::factory( 'ooui', [], $context, 'prefs-restore' );
  111. $htmlForm->setSubmitTextMsg( 'restoreprefs' );
  112. $htmlForm->setSubmitDestructive();
  113. $htmlForm->setSubmitCallback( [ $this, 'submitReset' ] );
  114. $htmlForm->suppressReset();
  115. $htmlForm->show();
  116. }
  117. public function submitReset( $formData ) {
  118. if ( !MediaWikiServices::getInstance()
  119. ->getPermissionManager()
  120. ->userHasRight( $this->getUser(), 'editmyoptions' )
  121. ) {
  122. throw new PermissionsError( 'editmyoptions' );
  123. }
  124. $user = $this->getUser()->getInstanceForUpdate();
  125. $user->resetOptions( 'all', $this->getContext() );
  126. $user->saveSettings();
  127. // Set session data for the success message
  128. $this->getRequest()->getSession()->set( 'specialPreferencesSaveSuccess', 1 );
  129. $url = $this->getPageTitle()->getFullUrlForRedirect();
  130. $this->getOutput()->redirect( $url );
  131. return true;
  132. }
  133. protected function getGroupName() {
  134. return 'users';
  135. }
  136. }