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