Remove Profiler specific code from fileOpPerfTest
[mediawiki.git] / includes / installer / WebInstallerName.php
blobdcd30cf2bc10693fd4af5bb71a01da2b606cd903
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.
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
18 * @file
19 * @ingroup Deployment
22 class WebInstallerName extends WebInstallerPage {
24 /**
25 * @return string
27 public function execute() {
28 $r = $this->parent->request;
29 if ( $r->wasPosted() ) {
30 if ( $this->submit() ) {
31 return 'continue';
35 $this->startForm();
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', '' );
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()
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',
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->getFieldsetEnd() .
104 $this->parent->getInfoBox( wfMessage( 'config-almost-done' )->text() ) .
105 // getRadioSet() builds a set of labeled radio buttons.
106 // For grep: The following messages are used as the item labels:
107 // config-optional-continue, config-optional-skip
108 $this->parent->getRadioSet( [
109 'var' => '_SkipOptional',
110 'itemLabelPrefix' => 'config-optional-',
111 'values' => [ 'continue', 'skip' ]
115 // Restore the default value
116 $this->setVar( 'wgMetaNamespace', $metaNS );
118 $this->endForm();
120 return 'output';
124 * @return bool
126 public function submit() {
127 global $wgPasswordPolicy;
129 $retVal = true;
130 $this->parent->setVarsFromRequest( [ 'wgSitename', '_NamespaceType',
131 '_AdminName', '_AdminPassword', '_AdminPasswordConfirm', '_AdminEmail',
132 '_Subscribe', '_SkipOptional', 'wgMetaNamespace' ] );
134 // Validate site name
135 if ( strval( $this->getVar( 'wgSitename' ) ) === '' ) {
136 $this->parent->showError( 'config-site-name-blank' );
137 $retVal = false;
140 // Fetch namespace
141 $nsType = $this->getVar( '_NamespaceType' );
142 if ( $nsType == 'site-name' ) {
143 $name = $this->getVar( 'wgSitename' );
144 // Sanitize for namespace
145 // This algorithm should match the JS one in WebInstallerOutput.php
146 $name = preg_replace( '/[\[\]\{\}|#<>%+? ]/', '_', $name );
147 $name = str_replace( '&', '&amp;', $name );
148 $name = preg_replace( '/__+/', '_', $name );
149 $name = ucfirst( trim( $name, '_' ) );
150 } elseif ( $nsType == 'generic' ) {
151 $name = wfMessage( 'config-ns-generic' )->text();
152 } else { // other
153 $name = $this->getVar( 'wgMetaNamespace' );
156 // Validate namespace
157 if ( strpos( $name, ':' ) !== false ) {
158 $good = false;
159 } else {
160 // Title-style validation
161 $title = Title::newFromText( $name );
162 if ( !$title ) {
163 $good = $nsType == 'site-name';
164 } else {
165 $name = $title->getDBkey();
166 $good = true;
169 if ( !$good ) {
170 $this->parent->showError( 'config-ns-invalid', $name );
171 $retVal = false;
174 // Make sure it won't conflict with any existing namespaces
175 global $wgContLang;
176 $nsIndex = $wgContLang->getNsIndex( $name );
177 if ( $nsIndex !== false && $nsIndex !== NS_PROJECT ) {
178 $this->parent->showError( 'config-ns-conflict', $name );
179 $retVal = false;
182 $this->setVar( 'wgMetaNamespace', $name );
184 // Validate username for creation
185 $name = $this->getVar( '_AdminName' );
186 if ( strval( $name ) === '' ) {
187 $this->parent->showError( 'config-admin-name-blank' );
188 $cname = $name;
189 $retVal = false;
190 } else {
191 $cname = User::getCanonicalName( $name, 'creatable' );
192 if ( $cname === false ) {
193 $this->parent->showError( 'config-admin-name-invalid', $name );
194 $retVal = false;
195 } else {
196 $this->setVar( '_AdminName', $cname );
200 // Validate password
201 $msg = false;
202 $pwd = $this->getVar( '_AdminPassword' );
203 $user = User::newFromName( $cname );
204 if ( $user ) {
205 $upp = new UserPasswordPolicy(
206 $wgPasswordPolicy['policies'],
207 $wgPasswordPolicy['checks']
209 $status = $upp->checkUserPasswordForGroups(
210 $user,
211 $pwd,
212 [ 'bureaucrat', 'sysop' ] // per Installer::createSysop()
214 $valid = $status->isGood() ? true : $status->getMessage();
215 } else {
216 $valid = 'config-admin-name-invalid';
218 if ( strval( $pwd ) === '' ) {
219 // Provide a more specific and helpful message if password field is left blank
220 $msg = 'config-admin-password-blank';
221 } elseif ( $pwd !== $this->getVar( '_AdminPasswordConfirm' ) ) {
222 $msg = 'config-admin-password-mismatch';
223 } elseif ( $valid !== true ) {
224 $msg = $valid;
226 if ( $msg !== false ) {
227 call_user_func( [ $this->parent, 'showError' ], $msg );
228 $this->setVar( '_AdminPassword', '' );
229 $this->setVar( '_AdminPasswordConfirm', '' );
230 $retVal = false;
233 // Validate e-mail if provided
234 $email = $this->getVar( '_AdminEmail' );
235 if ( $email && !Sanitizer::validateEmail( $email ) ) {
236 $this->parent->showError( 'config-admin-error-bademail' );
237 $retVal = false;
239 // If they asked to subscribe to mediawiki-announce but didn't give
240 // an e-mail, show an error. Bug 29332
241 if ( !$email && $this->getVar( '_Subscribe' ) ) {
242 $this->parent->showError( 'config-subscribe-noemail' );
243 $retVal = false;
246 return $retVal;