Fix PreferencesForm alias
[lhc/web/wiklou.git] / includes / specials / forms / PreferencesFormLegacy.php
1 <?php
2 /**
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.
7 *
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.
12 *
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
17 *
18 * @file
19 */
20
21 use MediaWiki\MediaWikiServices;
22
23 /**
24 * Form to edit user preferences.
25 *
26 * @since 1.32
27 */
28 class PreferencesFormLegacy extends HTMLForm {
29 // Override default value from HTMLForm
30 protected $mSubSectionBeforeFields = false;
31
32 private $modifiedUser;
33
34 /**
35 * @param User $user
36 */
37 public function setModifiedUser( $user ) {
38 $this->modifiedUser = $user;
39 }
40
41 /**
42 * @return User
43 */
44 public function getModifiedUser() {
45 if ( $this->modifiedUser === null ) {
46 return $this->getUser();
47 } else {
48 return $this->modifiedUser;
49 }
50 }
51
52 /**
53 * Get extra parameters for the query string when redirecting after
54 * successful save.
55 *
56 * @return array
57 */
58 public function getExtraSuccessRedirectParameters() {
59 return [];
60 }
61
62 /**
63 * @param string $html
64 * @return string
65 */
66 function wrapForm( $html ) {
67 $html = Xml::tags( 'div', [ 'id' => 'preferences' ], $html );
68
69 return parent::wrapForm( $html );
70 }
71
72 /**
73 * @return string
74 */
75 function getButtons() {
76 $attrs = [ 'id' => 'mw-prefs-restoreprefs' ];
77
78 if ( !$this->getModifiedUser()->isAllowedAny( 'editmyprivateinfo', 'editmyoptions' ) ) {
79 return '';
80 }
81
82 $html = parent::getButtons();
83
84 if ( $this->getModifiedUser()->isAllowed( 'editmyoptions' ) ) {
85 $t = $this->getTitle()->getSubpage( 'reset' );
86
87 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
88 $html .= "\n" . $linkRenderer->makeLink( $t, $this->msg( 'restoreprefs' )->text(),
89 Html::buttonAttributes( $attrs, [ 'mw-ui-quiet' ] ) );
90
91 $html = Xml::tags( 'div', [ 'class' => 'mw-prefs-buttons' ], $html );
92 }
93
94 return $html;
95 }
96
97 /**
98 * Separate multi-option preferences into multiple preferences, since we
99 * have to store them separately
100 * @param array $data
101 * @return array
102 */
103 function filterDataForSubmit( $data ) {
104 foreach ( $this->mFlatFields as $fieldname => $field ) {
105 if ( $field instanceof HTMLNestedFilterable ) {
106 $info = $field->mParams;
107 $prefix = isset( $info['prefix'] ) ? $info['prefix'] : $fieldname;
108 foreach ( $field->filterDataForSubmit( $data[$fieldname] ) as $key => $value ) {
109 $data["$prefix$key"] = $value;
110 }
111 unset( $data[$fieldname] );
112 }
113 }
114
115 return $data;
116 }
117
118 /**
119 * Get the whole body of the form.
120 * @return string
121 */
122 function getBody() {
123 return $this->displaySection( $this->mFieldTree, '', 'mw-prefsection-' );
124 }
125
126 /**
127 * Get the "<legend>" for a given section key. Normally this is the
128 * prefs-$key message but we'll allow extensions to override it.
129 * @param string $key
130 * @return string
131 */
132 function getLegend( $key ) {
133 $legend = parent::getLegend( $key );
134 Hooks::run( 'PreferencesGetLegend', [ $this, $key, &$legend ] );
135 return $legend;
136 }
137
138 /**
139 * Get the keys of each top level preference section.
140 * @return array of section keys
141 */
142 function getPreferenceSections() {
143 return array_keys( array_filter( $this->mFieldTree, 'is_array' ) );
144 }
145 }
146
147 /**
148 * Retain the old class name for backwards compatibility.
149 * In the future, this alias will be changed to point to PreferencesFormOOUI.
150 *
151 * @deprecated since 1.32
152 */
153 class_alias( PreferencesFormLegacy::class, 'PreferencesForm' );