3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 use MediaWiki\MediaWikiServices
;
24 * Form to edit user preferences.
28 class PreferencesFormOOUI
extends OOUIHTMLForm
{
29 // Override default value from HTMLForm
30 protected $mSubSectionBeforeFields = false;
32 private $modifiedUser;
37 public function setModifiedUser( $user ) {
38 $this->modifiedUser
= $user;
44 public function getModifiedUser() {
45 if ( $this->modifiedUser
=== null ) {
46 return $this->getUser();
48 return $this->modifiedUser
;
53 * Get extra parameters for the query string when redirecting after
58 public function getExtraSuccessRedirectParameters() {
66 function wrapForm( $html ) {
67 $html = Xml
::tags( 'div', [ 'id' => 'preferences' ], $html );
69 return parent
::wrapForm( $html );
75 function getButtons() {
76 $permissionManager = MediaWikiServices
::getInstance()->getPermissionManager();
77 if ( !$permissionManager->userHasAnyRight(
78 $this->getModifiedUser(),
85 $html = parent
::getButtons();
87 if ( $permissionManager->userHasRight( $this->getModifiedUser(), 'editmyoptions' ) ) {
88 $t = $this->getTitle()->getSubpage( 'reset' );
90 $html .= new OOUI\
ButtonWidget( [
92 'id' => 'mw-prefs-restoreprefs',
93 'label' => $this->msg( 'restoreprefs' )->text(),
94 'href' => $t->getLinkURL(),
95 'flags' => [ 'destructive' ],
99 $html = Xml
::tags( 'div', [ 'class' => 'mw-prefs-buttons' ], $html );
106 * Separate multi-option preferences into multiple preferences, since we
107 * have to store them separately
111 function filterDataForSubmit( $data ) {
112 foreach ( $this->mFlatFields
as $fieldname => $field ) {
113 if ( $field instanceof HTMLNestedFilterable
) {
114 // @phan-suppress-next-next-line PhanUndeclaredProperty All HTMLForm fields have mParams,
115 // but the instanceof confuses phan, which doesn't support intersections
116 $info = $field->mParams
;
117 $prefix = $info['prefix'] ??
$fieldname;
118 foreach ( $field->filterDataForSubmit( $data[$fieldname] ) as $key => $value ) {
119 $data["$prefix$key"] = $value;
121 unset( $data[$fieldname] );
128 protected function wrapFieldSetSection( $legend, $section, $attributes, $isRoot ) {
129 $layout = parent
::wrapFieldSetSection( $legend, $section, $attributes, $isRoot );
131 $layout->addClasses( [ 'mw-prefs-fieldset-wrapper' ] );
132 $layout->removeClasses( [ 'oo-ui-panelLayout-framed' ] );
138 * Get the whole body of the form.
143 foreach ( $this->mFieldTree
as $key => $val ) {
144 if ( !is_array( $val ) ) {
145 wfDebug( __METHOD__
. " encountered a field not attached to a section: '$key'" );
148 $label = $this->getLegend( $key );
150 $this->getHeaderText( $key ) .
151 $this->displaySection(
152 $this->mFieldTree
[$key],
154 "mw-prefsection-$key-"
156 $this->getFooterText( $key );
158 $tabPanels[] = new OOUI\
TabPanelLayout( 'mw-prefsection-' . $key, [
159 'classes' => [ 'mw-htmlform-autoinfuse-lazy' ],
161 'content' => new OOUI\
FieldsetLayout( [
162 'classes' => [ 'mw-prefs-section-fieldset' ],
163 'id' => "mw-prefsection-$key",
167 'content' => new OOUI\
HtmlSnippet( $content )
176 $indexLayout = new OOUI\
IndexLayout( [
179 'autoFocus' => false,
180 'classes' => [ 'mw-prefs-tabs' ],
182 $indexLayout->addTabPanels( $tabPanels );
184 return new OOUI\
PanelLayout( [
187 'classes' => [ 'mw-prefs-tabs-wrapper' ],
188 'content' => $indexLayout
193 * Get the "<legend>" for a given section key. Normally this is the
194 * prefs-$key message but we'll allow extensions to override it.
198 function getLegend( $key ) {
199 $legend = parent
::getLegend( $key );
200 Hooks
::run( 'PreferencesGetLegend', [ $this, $key, &$legend ] );
205 * Get the keys of each top level preference section.
206 * @return string[] List of section keys
208 function getPreferenceSections() {
209 return array_keys( array_filter( $this->mFieldTree
, 'is_array' ) );