MediaWiki REL1_30
SpecialPreferences.php
Go to the documentation of this file.
1<?php
30 function __construct() {
31 parent::__construct( 'Preferences' );
32 }
33
34 public function doesWrites() {
35 return true;
36 }
37
38 public function execute( $par ) {
39 $this->setHeaders();
40 $this->outputHeader();
41 $out = $this->getOutput();
42 $out->disallowUserJs(); # Prevent hijacked user scripts from sniffing passwords etc.
43
44 $this->requireLogin( 'prefsnologintext2' );
45 $this->checkReadOnly();
46
47 if ( $par == 'reset' ) {
48 $this->showResetForm();
49
50 return;
51 }
52
53 $out->addModules( 'mediawiki.special.preferences' );
54 $out->addModuleStyles( 'mediawiki.special.preferences.styles' );
55
56 $session = $this->getRequest()->getSession();
57 if ( $session->get( 'specialPreferencesSaveSuccess' ) ) {
58 // Remove session data for the success message
59 $session->remove( 'specialPreferencesSaveSuccess' );
60 $out->addModuleStyles( 'mediawiki.notification.convertmessagebox.styles' );
61
62 $out->addHTML(
63 Html::rawElement(
64 'div',
65 [
66 'class' => 'mw-preferences-messagebox mw-notify-success successbox',
67 'id' => 'mw-preferences-success',
68 'data-mw-autohide' => 'false',
69 ],
70 Html::element( 'p', [], $this->msg( 'savedprefs' )->text() )
71 )
72 );
73 }
74
75 $this->addHelpLink( 'Help:Preferences' );
76
77 // Load the user from the master to reduce CAS errors on double post (T95839)
78 if ( $this->getRequest()->wasPosted() ) {
79 $user = $this->getUser()->getInstanceForUpdate() ?: $this->getUser();
80 } else {
81 $user = $this->getUser();
82 }
83
84 $htmlForm = $this->getFormObject( $user, $this->getContext() );
85 $htmlForm->setSubmitCallback( [ 'Preferences', 'tryUISubmit' ] );
86 $sectionTitles = $htmlForm->getPreferenceSections();
87
88 $prefTabs = '';
89 foreach ( $sectionTitles as $key ) {
90 $prefTabs .= Html::rawElement( 'li',
91 [
92 'role' => 'presentation',
93 'class' => ( $key === 'personal' ) ? 'selected' : null
94 ],
95 Html::rawElement( 'a',
96 [
97 'id' => 'preftab-' . $key,
98 'role' => 'tab',
99 'href' => '#mw-prefsection-' . $key,
100 'aria-controls' => 'mw-prefsection-' . $key,
101 'aria-selected' => ( $key === 'personal' ) ? 'true' : 'false',
102 'tabIndex' => ( $key === 'personal' ) ? 0 : -1,
103 ],
104 $htmlForm->getLegend( $key )
105 )
106 );
107 }
108
109 $out->addHTML(
110 Html::rawElement( 'ul',
111 [
112 'id' => 'preftoc',
113 'role' => 'tablist'
114 ],
115 $prefTabs )
116 );
117 $htmlForm->show();
118 }
119
126 protected function getFormObject( $user, IContextSource $context ) {
127 return Preferences::getFormObject( $user, $context );
128 }
129
130 private function showResetForm() {
131 if ( !$this->getUser()->isAllowed( 'editmyoptions' ) ) {
132 throw new PermissionsError( 'editmyoptions' );
133 }
134
135 $this->getOutput()->addWikiMsg( 'prefs-reset-intro' );
136
137 $context = new DerivativeContext( $this->getContext() );
138 $context->setTitle( $this->getPageTitle( 'reset' ) ); // Reset subpage
139 $htmlForm = new HTMLForm( [], $context, 'prefs-restore' );
140
141 $htmlForm->setSubmitTextMsg( 'restoreprefs' );
142 $htmlForm->setSubmitDestructive();
143 $htmlForm->setSubmitCallback( [ $this, 'submitReset' ] );
144 $htmlForm->suppressReset();
145
146 $htmlForm->show();
147 }
148
149 public function submitReset( $formData ) {
150 if ( !$this->getUser()->isAllowed( 'editmyoptions' ) ) {
151 throw new PermissionsError( 'editmyoptions' );
152 }
153
154 $user = $this->getUser()->getInstanceForUpdate();
155 $user->resetOptions( 'all', $this->getContext() );
156 $user->saveSettings();
157
158 // Set session data for the success message
159 $this->getRequest()->getSession()->set( 'specialPreferencesSaveSuccess', 1 );
160
161 $url = $this->getPageTitle()->getFullUrlForRedirect();
162 $this->getOutput()->redirect( $url );
163
164 return true;
165 }
166
167 protected function getGroupName() {
168 return 'users';
169 }
170}
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:128
Show an error when a user tries to do something they do not have the necessary permissions for.
static getFormObject( $user, IContextSource $context, $formClass='PreferencesForm', array $remove=[])
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:2780
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:862
Interface for objects which can provide a MediaWiki context on request.