MediaWiki REL1_31
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' );
56 $out->addModuleStyles( 'mediawiki.special.preferences.styles' );
57
58 $session = $this->getRequest()->getSession();
59 if ( $session->get( 'specialPreferencesSaveSuccess' ) ) {
60 // Remove session data for the success message
61 $session->remove( 'specialPreferencesSaveSuccess' );
62 $out->addModuleStyles( 'mediawiki.notification.convertmessagebox.styles' );
63
64 $out->addHTML(
65 Html::rawElement(
66 'div',
67 [
68 'class' => 'mw-preferences-messagebox mw-notify-success successbox',
69 'id' => 'mw-preferences-success',
70 'data-mw-autohide' => 'false',
71 ],
72 Html::element( 'p', [], $this->msg( 'savedprefs' )->text() )
73 )
74 );
75 }
76
77 $this->addHelpLink( 'Help:Preferences' );
78
79 // Load the user from the master to reduce CAS errors on double post (T95839)
80 if ( $this->getRequest()->wasPosted() ) {
81 $user = $this->getUser()->getInstanceForUpdate() ?: $this->getUser();
82 } else {
83 $user = $this->getUser();
84 }
85
86 $htmlForm = $this->getFormObject( $user, $this->getContext() );
87 $sectionTitles = $htmlForm->getPreferenceSections();
88
89 $prefTabs = '';
90 foreach ( $sectionTitles as $key ) {
91 $prefTabs .= Html::rawElement( 'li',
92 [
93 'role' => 'presentation',
94 'class' => ( $key === 'personal' ) ? 'selected' : null
95 ],
96 Html::rawElement( 'a',
97 [
98 'id' => 'preftab-' . $key,
99 'role' => 'tab',
100 'href' => '#mw-prefsection-' . $key,
101 'aria-controls' => 'mw-prefsection-' . $key,
102 'aria-selected' => ( $key === 'personal' ) ? 'true' : 'false',
103 'tabIndex' => ( $key === 'personal' ) ? 0 : -1,
104 ],
105 $htmlForm->getLegend( $key )
106 )
107 );
108 }
109
110 $out->addHTML(
111 Html::rawElement( 'ul',
112 [
113 'id' => 'preftoc',
114 'role' => 'tablist'
115 ],
116 $prefTabs )
117 );
118 $htmlForm->show();
119 }
120
127 protected function getFormObject( $user, IContextSource $context ) {
128 $preferencesFactory = MediaWikiServices::getInstance()->getPreferencesFactory();
129 $form = $preferencesFactory->getForm( $user, $context );
130 return $form;
131 }
132
133 protected function showResetForm() {
134 if ( !$this->getUser()->isAllowed( 'editmyoptions' ) ) {
135 throw new PermissionsError( 'editmyoptions' );
136 }
137
138 $this->getOutput()->addWikiMsg( 'prefs-reset-intro' );
139
140 $context = new DerivativeContext( $this->getContext() );
141 $context->setTitle( $this->getPageTitle( 'reset' ) ); // Reset subpage
142 $htmlForm = new HTMLForm( [], $context, 'prefs-restore' );
143
144 $htmlForm->setSubmitTextMsg( 'restoreprefs' );
145 $htmlForm->setSubmitDestructive();
146 $htmlForm->setSubmitCallback( [ $this, 'submitReset' ] );
147 $htmlForm->suppressReset();
148
149 $htmlForm->show();
150 }
151
152 public function submitReset( $formData ) {
153 if ( !$this->getUser()->isAllowed( 'editmyoptions' ) ) {
154 throw new PermissionsError( 'editmyoptions' );
155 }
156
157 $user = $this->getUser()->getInstanceForUpdate();
158 $user->resetOptions( 'all', $this->getContext() );
159 $user->saveSettings();
160
161 // Set session data for the success message
162 $this->getRequest()->getSession()->set( 'specialPreferencesSaveSuccess', 1 );
163
164 $url = $this->getPageTitle()->getFullUrlForRedirect();
165 $this->getOutput()->redirect( $url );
166
167 return true;
168 }
169
170 protected function getGroupName() {
171 return 'users';
172 }
173}
The package scripts
Definition README.txt:1
An IContextSource implementation which will inherit context from another source but allow individual ...
Object handling generic submission, CSRF protection, layout and other logic for UI forms.
Definition HTMLForm.php:130
MediaWikiServices is the service locator for the application scope of MediaWiki.
Show an error when a user tries to do something they do not have the necessary permissions for.
Parent class for all special pages.
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.
requireLogin( $reasonMsg='exception-nologin-text', $titleMsg='exception-nologin')
If the user is not logged in, throws UserNotLoggedIn error.
getUser()
Shortcut to get the User executing this instance.
getContext()
Gets the context this SpecialPage is executed in.
msg( $key)
Wrapper around wfMessage that sets the current context.
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...
getFormObject( $user, IContextSource $context)
Get the preferences form to use.
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add text
Definition design.txt:18
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:2811
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub 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:864
Interface for objects which can provide a MediaWiki context on request.