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
22 use MediaWiki\MediaWikiServices
;
24 class WebInstallerName
extends WebInstallerPage
{
29 public function execute() {
30 $r = $this->parent
->request
;
31 if ( $r->wasPosted() ) {
32 if ( $this->submit() ) {
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', '' );
47 // Set wgMetaNamespace to something valid before we show the form.
48 // $wgMetaNamespace defaults to $wgSiteName which is 'MediaWiki'
49 $metaNS = $this->getVar( 'wgMetaNamespace' );
52 wfMessage( 'config-ns-other-default' )->inContentLanguage()->text()
55 $pingbackInfo = ( new Pingback() )->getSystemInfo();
56 // Database isn't available in config yet, so take it
58 $pingbackInfo['database'] = $this->getVar( 'wgDBtype' );
61 $this->parent
->getTextBox( [
62 'var' => 'wgSitename',
63 'label' => 'config-site-name',
64 'help' => $this->parent
->getHelpBox( 'config-site-name-help' )
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' )
78 $this->parent
->getTextBox( [
79 'var' => 'wgMetaNamespace',
80 'label' => '', // @todo Needs a label?
81 'attribs' => [ 'class' => 'enabledByOther' ]
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' )
89 $this->parent
->getPasswordBox( [
90 'var' => '_AdminPassword',
91 'label' => 'config-admin-password',
93 $this->parent
->getPasswordBox( [
94 'var' => '_AdminPasswordConfirm',
95 'label' => 'config-admin-password-confirm'
97 $this->parent
->getTextBox( [
98 'var' => '_AdminEmail',
102 'label' => 'config-admin-email',
103 'help' => $this->parent
->getHelpBox( 'config-admin-email-help' )
105 $this->parent
->getCheckBox( [
106 'var' => '_Subscribe',
107 'label' => 'config-subscribe',
108 'help' => $this->parent
->getHelpBox( 'config-subscribe-help' )
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 )
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' ]
131 // Restore the default value
132 $this->setVar( 'wgMetaNamespace', $metaNS );
142 public function submit() {
143 global $wgPasswordPolicy;
146 $this->parent
->setVarsFromRequest( [ 'wgSitename', '_NamespaceType',
147 '_AdminName', '_AdminPassword', '_AdminPasswordConfirm', '_AdminEmail',
148 '_Subscribe', '_SkipOptional', 'wgMetaNamespace', 'wgPingback' ] );
150 // Validate site name
151 if ( strval( $this->getVar( 'wgSitename' ) ) === '' ) {
152 $this->parent
->showError( 'config-site-name-blank' );
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( '&', '&', $name );
164 $name = preg_replace( '/__+/', '_', $name );
165 $name = ucfirst( trim( $name, '_' ) );
166 } elseif ( $nsType == 'generic' ) {
167 $name = wfMessage( 'config-ns-generic' )->text();
169 $name = $this->getVar( 'wgMetaNamespace' );
172 // Validate namespace
173 if ( strpos( $name, ':' ) !== false ) {
176 // Title-style validation
177 $title = Title
::newFromText( $name );
179 $good = $nsType == 'site-name';
181 $name = $title->getDBkey();
186 $this->parent
->showError( 'config-ns-invalid', $name );
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 );
197 $this->setVar( 'wgMetaNamespace', $name );
199 // Validate username for creation
200 $name = $this->getVar( '_AdminName' );
201 if ( strval( $name ) === '' ) {
202 $this->parent
->showError( 'config-admin-name-blank' );
206 $cname = User
::getCanonicalName( $name, 'creatable' );
207 if ( $cname === false ) {
208 $this->parent
->showError( 'config-admin-name-invalid', $name );
211 $this->setVar( '_AdminName', $cname );
217 $pwd = $this->getVar( '_AdminPassword' );
218 $user = User
::newFromName( $cname );
220 $upp = new UserPasswordPolicy(
221 $wgPasswordPolicy['policies'],
222 $wgPasswordPolicy['checks']
224 $status = $upp->checkUserPasswordForGroups(
227 [ 'bureaucrat', 'sysop', 'interface-admin' ] // per Installer::createSysop()
229 $valid = $status->isGood() ?
true : $status->getMessage();
231 $valid = 'config-admin-name-invalid';
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 ) {
241 if ( $msg !== false ) {
242 call_user_func( [ $this->parent
, 'showError' ], $msg );
243 $this->setVar( '_AdminPassword', '' );
244 $this->setVar( '_AdminPasswordConfirm', '' );
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' );
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' );