MediaWiki  1.33.0
SpecialPreferences.php
Go to the documentation of this file.
1 <?php
25 
32  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(
69  Html::rawElement(
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 ( !$this->getUser()->isAllowed( 'editmyoptions' ) ) {
119  throw new PermissionsError( 'editmyoptions' );
120  }
121 
122  $this->getOutput()->addWikiMsg( 'prefs-reset-intro' );
123 
124  $context = new DerivativeContext( $this->getContext() );
125  $context->setTitle( $this->getPageTitle( 'reset' ) ); // Reset subpage
126  $htmlForm = HTMLForm::factory( 'ooui', [], $context, 'prefs-restore' );
127 
128  $htmlForm->setSubmitTextMsg( 'restoreprefs' );
129  $htmlForm->setSubmitDestructive();
130  $htmlForm->setSubmitCallback( [ $this, 'submitReset' ] );
131  $htmlForm->suppressReset();
132 
133  $htmlForm->show();
134  }
135 
136  public function submitReset( $formData ) {
137  if ( !$this->getUser()->isAllowed( 'editmyoptions' ) ) {
138  throw new PermissionsError( 'editmyoptions' );
139  }
140 
141  $user = $this->getUser()->getInstanceForUpdate();
142  $user->resetOptions( 'all', $this->getContext() );
143  $user->saveSettings();
144 
145  // Set session data for the success message
146  $this->getRequest()->getSession()->set( 'specialPreferencesSaveSuccess', 1 );
147 
148  $url = $this->getPageTitle()->getFullUrlForRedirect();
149  $this->getOutput()->redirect( $url );
150 
151  return true;
152  }
153 
154  protected function getGroupName() {
155  return 'users';
156  }
157 }
SpecialPage\getPageTitle
getPageTitle( $subpage=false)
Get a self-referential title object.
Definition: SpecialPage.php:678
$user
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1476
SpecialPage\msg
msg( $key)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:796
$context
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext $context
Definition: hooks.txt:2636
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:725
$out
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition: hooks.txt:780
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:28
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
DerivativeContext
An IContextSource implementation which will inherit context from another source but allow individual ...
Definition: DerivativeContext.php:30
SpecialPage\addHelpLink
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Definition: SpecialPage.php:832
user
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such and we might be restricted by PHP settings such as safe mode or open_basedir We cannot assume that the software even has read access anywhere useful Many shared hosts run all users web applications under the same user
Definition: distributors.txt:9
HTMLForm\factory
static factory( $displayFormat)
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:286
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
SpecialPage\setHeaders
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
Definition: SpecialPage.php:531
SpecialPage\getUser
getUser()
Shortcut to get the User executing this instance.
Definition: SpecialPage.php:735
etc
Using a hook running we can avoid having all this option specific stuff in our mainline code Using the function We ve cleaned up the code here by removing clumps of infrequently used code and moving them off somewhere else It s much easier for someone working with this code to see what s _really_ going and make changes or fix bugs In we can take all the code that deals with the little used title reversing etc
Definition: hooks.txt:91
SpecialPage\getContext
getContext()
Gets the context this SpecialPage is executed in.
Definition: SpecialPage.php:698
SpecialPage\requireLogin
requireLogin( $reasonMsg='exception-nologin-text', $titleMsg='exception-nologin')
If the user is not logged in, throws UserNotLoggedIn error.
Definition: SpecialPage.php:339
SpecialPage
Parent class for all special pages.
Definition: SpecialPage.php:36
SpecialPage\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: SpecialPage.php:715
SpecialPreferences\getFormObject
getFormObject( $user, IContextSource $context)
Get the preferences form to use.
Definition: SpecialPreferences.php:111
scripts
The package scripts
Definition: README.txt:1
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:53
text
This list may contain false positives That usually means there is additional text with links below the first Each row contains links to the first and second as well as the first line of the second redirect text
Definition: All_system_messages.txt:1267
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
from
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for and distribution as defined by Sections through of this document Licensor shall mean the copyright owner or entity authorized by the copyright owner that is granting the License Legal Entity shall mean the union of the acting entity and all other entities that control are controlled by or are under common control with that entity For the purposes of this definition control direct or to cause the direction or management of such whether by contract or including but not limited to software source documentation and configuration files Object form shall mean any form resulting from mechanical transformation or translation of a Source including but not limited to compiled object generated and conversions to other media types Work shall mean the work of whether in Source or Object made available under the as indicated by a copyright notice that is included in or attached to the whether in Source or Object that is based or other modifications as a an original work of authorship For the purposes of this Derivative Works shall not include works that remain separable from
Definition: APACHE-LICENSE-2.0.txt:43
SpecialPreferences\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialPreferences.php:154
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
SpecialPage\checkReadOnly
checkReadOnly()
If the wiki is currently in readonly mode, throws a ReadOnlyError.
Definition: SpecialPage.php:322
SpecialPreferences\submitReset
submitReset( $formData)
Definition: SpecialPreferences.php:136
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
SpecialPreferences\showResetForm
showResetForm()
Definition: SpecialPreferences.php:117
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:633
SpecialPreferences\__construct
__construct()
Definition: SpecialPreferences.php:32