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