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