Merge "Improve database endAtomic() error messages"
[lhc/web/wiklou.git] / includes / installer / WebInstallerName.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 * @ingroup Deployment
20 */
21
22 class WebInstallerName extends WebInstallerPage {
23
24 /**
25 * @return string
26 */
27 public function execute() {
28 $r = $this->parent->request;
29 if ( $r->wasPosted() ) {
30 if ( $this->submit() ) {
31 return 'continue';
32 }
33 }
34
35 $this->startForm();
36
37 // Encourage people to not name their site 'MediaWiki' by blanking the
38 // field. I think that was the intent with the original $GLOBALS['wgSitename']
39 // but these two always were the same so had the effect of making the
40 // installer forget $wgSitename when navigating back to this page.
41 if ( $this->getVar( 'wgSitename' ) == 'MediaWiki' ) {
42 $this->setVar( 'wgSitename', '' );
43 }
44
45 // Set wgMetaNamespace to something valid before we show the form.
46 // $wgMetaNamespace defaults to $wgSiteName which is 'MediaWiki'
47 $metaNS = $this->getVar( 'wgMetaNamespace' );
48 $this->setVar(
49 'wgMetaNamespace',
50 wfMessage( 'config-ns-other-default' )->inContentLanguage()->text()
51 );
52
53 $this->addHTML(
54 $this->parent->getTextBox( [
55 'var' => 'wgSitename',
56 'label' => 'config-site-name',
57 'help' => $this->parent->getHelpBox( 'config-site-name-help' )
58 ] ) .
59 // getRadioSet() builds a set of labeled radio buttons.
60 // For grep: The following messages are used as the item labels:
61 // config-ns-site-name, config-ns-generic, config-ns-other
62 $this->parent->getRadioSet( [
63 'var' => '_NamespaceType',
64 'label' => 'config-project-namespace',
65 'itemLabelPrefix' => 'config-ns-',
66 'values' => [ 'site-name', 'generic', 'other' ],
67 'commonAttribs' => [ 'class' => 'enableForOther',
68 'rel' => 'config_wgMetaNamespace' ],
69 'help' => $this->parent->getHelpBox( 'config-project-namespace-help' )
70 ] ) .
71 $this->parent->getTextBox( [
72 'var' => 'wgMetaNamespace',
73 'label' => '', // @todo Needs a label?
74 'attribs' => [ 'readonly' => 'readonly', 'class' => 'enabledByOther' ]
75 ] ) .
76 $this->getFieldsetStart( 'config-admin-box' ) .
77 $this->parent->getTextBox( [
78 'var' => '_AdminName',
79 'label' => 'config-admin-name',
80 'help' => $this->parent->getHelpBox( 'config-admin-help' )
81 ] ) .
82 $this->parent->getPasswordBox( [
83 'var' => '_AdminPassword',
84 'label' => 'config-admin-password',
85 ] ) .
86 $this->parent->getPasswordBox( [
87 'var' => '_AdminPasswordConfirm',
88 'label' => 'config-admin-password-confirm'
89 ] ) .
90 $this->parent->getTextBox( [
91 'var' => '_AdminEmail',
92 'attribs' => [
93 'dir' => 'ltr',
94 ],
95 'label' => 'config-admin-email',
96 'help' => $this->parent->getHelpBox( 'config-admin-email-help' )
97 ] ) .
98 $this->parent->getCheckBox( [
99 'var' => '_Subscribe',
100 'label' => 'config-subscribe',
101 'help' => $this->parent->getHelpBox( 'config-subscribe-help' )
102 ] ) .
103 $this->parent->getCheckBox( [
104 'var' => 'wgPingback',
105 'label' => 'config-pingback',
106 'help' => $this->parent->getHelpBox( 'config-pingback-help' ),
107 'value' => true,
108 ] ) .
109 $this->getFieldsetEnd() .
110 $this->parent->getInfoBox( wfMessage( 'config-almost-done' )->text() ) .
111 // getRadioSet() builds a set of labeled radio buttons.
112 // For grep: The following messages are used as the item labels:
113 // config-optional-continue, config-optional-skip
114 $this->parent->getRadioSet( [
115 'var' => '_SkipOptional',
116 'itemLabelPrefix' => 'config-optional-',
117 'values' => [ 'continue', 'skip' ]
118 ] )
119 );
120
121 // Restore the default value
122 $this->setVar( 'wgMetaNamespace', $metaNS );
123
124 $this->endForm();
125
126 return 'output';
127 }
128
129 /**
130 * @return bool
131 */
132 public function submit() {
133 global $wgPasswordPolicy;
134
135 $retVal = true;
136 $this->parent->setVarsFromRequest( [ 'wgSitename', '_NamespaceType',
137 '_AdminName', '_AdminPassword', '_AdminPasswordConfirm', '_AdminEmail',
138 '_Subscribe', '_SkipOptional', 'wgMetaNamespace', 'wgPingback' ] );
139
140 // Validate site name
141 if ( strval( $this->getVar( 'wgSitename' ) ) === '' ) {
142 $this->parent->showError( 'config-site-name-blank' );
143 $retVal = false;
144 }
145
146 // Fetch namespace
147 $nsType = $this->getVar( '_NamespaceType' );
148 if ( $nsType == 'site-name' ) {
149 $name = $this->getVar( 'wgSitename' );
150 // Sanitize for namespace
151 // This algorithm should match the JS one in WebInstallerOutput.php
152 $name = preg_replace( '/[\[\]\{\}|#<>%+? ]/', '_', $name );
153 $name = str_replace( '&', '&amp;', $name );
154 $name = preg_replace( '/__+/', '_', $name );
155 $name = ucfirst( trim( $name, '_' ) );
156 } elseif ( $nsType == 'generic' ) {
157 $name = wfMessage( 'config-ns-generic' )->text();
158 } else { // other
159 $name = $this->getVar( 'wgMetaNamespace' );
160 }
161
162 // Validate namespace
163 if ( strpos( $name, ':' ) !== false ) {
164 $good = false;
165 } else {
166 // Title-style validation
167 $title = Title::newFromText( $name );
168 if ( !$title ) {
169 $good = $nsType == 'site-name';
170 } else {
171 $name = $title->getDBkey();
172 $good = true;
173 }
174 }
175 if ( !$good ) {
176 $this->parent->showError( 'config-ns-invalid', $name );
177 $retVal = false;
178 }
179
180 // Make sure it won't conflict with any existing namespaces
181 global $wgContLang;
182 $nsIndex = $wgContLang->getNsIndex( $name );
183 if ( $nsIndex !== false && $nsIndex !== NS_PROJECT ) {
184 $this->parent->showError( 'config-ns-conflict', $name );
185 $retVal = false;
186 }
187
188 $this->setVar( 'wgMetaNamespace', $name );
189
190 // Validate username for creation
191 $name = $this->getVar( '_AdminName' );
192 if ( strval( $name ) === '' ) {
193 $this->parent->showError( 'config-admin-name-blank' );
194 $cname = $name;
195 $retVal = false;
196 } else {
197 $cname = User::getCanonicalName( $name, 'creatable' );
198 if ( $cname === false ) {
199 $this->parent->showError( 'config-admin-name-invalid', $name );
200 $retVal = false;
201 } else {
202 $this->setVar( '_AdminName', $cname );
203 }
204 }
205
206 // Validate password
207 $msg = false;
208 $pwd = $this->getVar( '_AdminPassword' );
209 $user = User::newFromName( $cname );
210 if ( $user ) {
211 $upp = new UserPasswordPolicy(
212 $wgPasswordPolicy['policies'],
213 $wgPasswordPolicy['checks']
214 );
215 $status = $upp->checkUserPasswordForGroups(
216 $user,
217 $pwd,
218 [ 'bureaucrat', 'sysop' ] // per Installer::createSysop()
219 );
220 $valid = $status->isGood() ? true : $status->getMessage();
221 } else {
222 $valid = 'config-admin-name-invalid';
223 }
224 if ( strval( $pwd ) === '' ) {
225 // Provide a more specific and helpful message if password field is left blank
226 $msg = 'config-admin-password-blank';
227 } elseif ( $pwd !== $this->getVar( '_AdminPasswordConfirm' ) ) {
228 $msg = 'config-admin-password-mismatch';
229 } elseif ( $valid !== true ) {
230 $msg = $valid;
231 }
232 if ( $msg !== false ) {
233 call_user_func( [ $this->parent, 'showError' ], $msg );
234 $this->setVar( '_AdminPassword', '' );
235 $this->setVar( '_AdminPasswordConfirm', '' );
236 $retVal = false;
237 }
238
239 // Validate e-mail if provided
240 $email = $this->getVar( '_AdminEmail' );
241 if ( $email && !Sanitizer::validateEmail( $email ) ) {
242 $this->parent->showError( 'config-admin-error-bademail' );
243 $retVal = false;
244 }
245 // If they asked to subscribe to mediawiki-announce but didn't give
246 // an e-mail, show an error. Bug 29332
247 if ( !$email && $this->getVar( '_Subscribe' ) ) {
248 $this->parent->showError( 'config-subscribe-noemail' );
249 $retVal = false;
250 }
251
252 return $retVal;
253 }
254
255 }