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