4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
23 namespace MediaWiki\Installer
;
25 use MediaWiki\Config\HashConfig
;
26 use MediaWiki\Json\FormatJson
;
27 use MediaWiki\MainConfigNames
;
28 use MediaWiki\MediaWikiServices
;
29 use MediaWiki\Parser\Sanitizer
;
30 use MediaWiki\Password\UserPasswordPolicy
;
31 use MediaWiki\Title\Title
;
32 use MediaWiki\User\User
;
33 use MediaWiki\User\UserRigorOptions
;
35 class WebInstallerName
extends WebInstallerPage
{
40 public function execute() {
41 $r = $this->parent
->request
;
42 if ( $r->wasPosted() && $this->submit() ) {
48 // Encourage people to not name their site 'MediaWiki' by blanking the
49 // field. I think that was the intent with the original $GLOBALS['wgSitename']
50 // but these two always were the same so had the effect of making the
51 // installer forget $wgSitename when navigating back to this page.
52 if ( $this->getVar( 'wgSitename' ) == 'MediaWiki' ) {
53 $this->setVar( 'wgSitename', '' );
56 // Set wgMetaNamespace to something valid before we show the form.
57 // $wgMetaNamespace defaults to $wgSiteName which is 'MediaWiki'
58 $metaNS = $this->getVar( 'wgMetaNamespace' );
61 wfMessage( 'config-ns-other-default' )->inContentLanguage()->text()
64 // Database isn't available in config yet, so take it
66 $pingbackConf = new HashConfig( [
67 MainConfigNames
::DBtype
=> $this->getVar( 'wgDBtype' ),
69 $pingbackInfo = Pingback
::getSystemInfo( $pingbackConf );
72 $this->parent
->getTextBox( [
73 'var' => 'wgSitename',
74 'label' => 'config-site-name',
75 'help' => $this->parent
->getHelpBox( 'config-site-name-help' )
77 // getRadioSet() builds a set of labeled radio buttons.
78 // For grep: The following messages are used as the item labels:
79 // config-ns-site-name, config-ns-generic, config-ns-other
80 $this->parent
->getRadioSet( [
81 'var' => '_NamespaceType',
82 'label' => 'config-project-namespace',
83 'itemLabelPrefix' => 'config-ns-',
84 'values' => [ 'site-name', 'generic', 'other' ],
85 'commonAttribs' => [ 'class' => 'enableForOther',
86 'rel' => 'config_wgMetaNamespace' ],
87 'help' => $this->parent
->getHelpBox( 'config-project-namespace-help' )
89 $this->parent
->getTextBox( [
90 'var' => 'wgMetaNamespace',
91 'label' => '', // @todo Needs a label?
92 'attribs' => [ 'class' => 'enabledByOther' ]
94 $this->getFieldsetStart( 'config-admin-box' ) .
95 $this->parent
->getTextBox( [
96 'var' => '_AdminName',
97 'label' => 'config-admin-name',
98 'help' => $this->parent
->getHelpBox( 'config-admin-help' )
100 $this->parent
->getPasswordBox( [
101 'var' => '_AdminPassword',
102 'label' => 'config-admin-password',
104 $this->parent
->getPasswordBox( [
105 'var' => '_AdminPasswordConfirm',
106 'label' => 'config-admin-password-confirm'
108 $this->parent
->getTextBox( [
109 'var' => '_AdminEmail',
113 'label' => 'config-admin-email',
114 'help' => $this->parent
->getHelpBox( 'config-admin-email-help' )
116 $this->parent
->getCheckBox( [
117 'var' => '_Subscribe',
118 'label' => 'config-subscribe',
119 'help' => $this->parent
->getHelpBox( 'config-subscribe-help' )
121 $this->parent
->getCheckBox( [
122 'var' => 'wgPingback',
123 'label' => 'config-pingback',
124 'help' => $this->parent
->getHelpBox(
125 'config-pingback-help',
126 FormatJson
::encode( $pingbackInfo, true )
130 $this->getFieldsetEnd() .
131 $this->parent
->getInfoBox( wfMessage( 'config-almost-done' )->plain() ) .
132 // getRadioSet() builds a set of labeled radio buttons.
133 // For grep: The following messages are used as the item labels:
134 // config-optional-continue, config-optional-skip
135 $this->parent
->getRadioSet( [
136 'var' => '_SkipOptional',
137 'itemLabelPrefix' => 'config-optional-',
138 'values' => [ 'continue', 'skip' ]
142 // Restore the default value
143 $this->setVar( 'wgMetaNamespace', $metaNS );
153 public function submit() {
154 global $wgPasswordPolicy;
157 $this->parent
->setVarsFromRequest( [ 'wgSitename', '_NamespaceType',
158 '_AdminName', '_AdminPassword', '_AdminPasswordConfirm', '_AdminEmail',
159 '_Subscribe', '_SkipOptional', 'wgMetaNamespace', 'wgPingback' ] );
161 // Validate site name
162 if ( strval( $this->getVar( 'wgSitename' ) ) === '' ) {
163 $this->parent
->showError( 'config-site-name-blank' );
168 $nsType = $this->getVar( '_NamespaceType' );
169 if ( $nsType == 'site-name' ) {
170 $name = $this->getVar( 'wgSitename' );
171 // Sanitize for namespace
172 // This algorithm should match the JS one in WebInstallerOutput.php
173 $name = preg_replace( '/[\[\]\{\}|#<>%+? ]/', '_', $name );
174 $name = str_replace( '&', '&', $name );
175 $name = preg_replace( '/__+/', '_', $name );
176 $name = ucfirst( trim( $name, '_' ) );
177 } elseif ( $nsType == 'generic' ) {
178 $name = wfMessage( 'config-ns-generic' )->text();
180 $name = $this->getVar( 'wgMetaNamespace' );
183 // Validate namespace
184 if ( strpos( $name, ':' ) !== false ) {
187 // Title-style validation
188 $title = Title
::newFromText( $name );
190 $good = $nsType == 'site-name';
192 $name = $title->getDBkey();
197 $this->parent
->showError( 'config-ns-invalid', $name );
201 // Make sure it won't conflict with any existing namespaces
202 $nsIndex = MediaWikiServices
::getInstance()->getContentLanguage()->getNsIndex( $name );
203 if ( $nsIndex !== false && $nsIndex !== NS_PROJECT
) {
204 $this->parent
->showError( 'config-ns-conflict', $name );
208 $this->setVar( 'wgMetaNamespace', $name );
210 // Validate username for creation
211 $name = $this->getVar( '_AdminName' );
212 if ( strval( $name ) === '' ) {
213 $this->parent
->showError( 'config-admin-name-blank' );
217 $userNameUtils = MediaWikiServices
::getInstance()->getUserNameUtils();
218 $cname = $userNameUtils->getCanonical( $name, UserRigorOptions
::RIGOR_CREATABLE
);
219 if ( $cname === false ) {
220 $this->parent
->showError( 'config-admin-name-invalid', $name );
223 $this->setVar( '_AdminName', $cname );
229 $pwd = $this->getVar( '_AdminPassword' );
230 $user = User
::newFromName( $cname );
232 $upp = new UserPasswordPolicy(
233 $wgPasswordPolicy['policies'],
234 $wgPasswordPolicy['checks']
236 $status = $upp->checkUserPasswordForGroups(
239 [ 'bureaucrat', 'sysop', 'interface-admin' ] // per Installer::createSysop()
241 $valid = $status->isGood() ?
true : $status->getMessage();
243 $valid = 'config-admin-name-invalid';
245 if ( strval( $pwd ) === '' ) {
246 // Provide a more specific and helpful message if password field is left blank
247 $msg = 'config-admin-password-blank';
248 } elseif ( $pwd !== $this->getVar( '_AdminPasswordConfirm' ) ) {
249 $msg = 'config-admin-password-mismatch';
250 } elseif ( $valid !== true ) {
253 if ( $msg !== false ) {
254 call_user_func( [ $this->parent
, 'showError' ], $msg );
255 $this->setVar( '_AdminPassword', '' );
256 $this->setVar( '_AdminPasswordConfirm', '' );
260 // Validate e-mail if provided
261 $email = $this->getVar( '_AdminEmail' );
262 if ( $email && !Sanitizer
::validateEmail( $email ) ) {
263 $this->parent
->showError( 'config-admin-error-bademail' );
266 // If they asked to subscribe to mediawiki-announce but didn't give
267 // an e-mail, show an error. T31332
268 if ( !$email && $this->getVar( '_Subscribe' ) ) {
269 $this->parent
->showError( 'config-subscribe-noemail' );