3da184d1b0ab2d07910bba7ed2e85b237a40d97c
[lhc/web/wiklou.git] / includes / installer / WebInstallerPage.php
1 <?php
2 /**
3 * Base code for web installer pages.
4 *
5 * @file
6 * @ingroup Deployment
7 */
8
9 /**
10 * Abstract class to define pages for the web installer.
11 *
12 * @ingroup Deployment
13 * @since 1.17
14 */
15 abstract class WebInstallerPage {
16
17 /**
18 * The WebInstaller object this WebInstallerPage belongs to.
19 *
20 * @var WebInstaller
21 */
22 public $parent;
23
24 public abstract function execute();
25
26 /**
27 * Constructor.
28 *
29 * @param $parent WebInstaller
30 */
31 public function __construct( WebInstaller $parent ) {
32 $this->parent = $parent;
33 }
34
35 /**
36 * Is this a slow-running page in the installer? If so, WebInstaller will
37 * set_time_limit(0) before calling execute(). Right now this only applies
38 * to Install and Upgrade pages
39 */
40 public function isSlow() {
41 return false;
42 }
43
44 public function addHTML( $html ) {
45 $this->parent->output->addHTML( $html );
46 }
47
48 public function startForm() {
49 $this->addHTML(
50 "<div class=\"config-section\">\n" .
51 Html::openElement(
52 'form',
53 array(
54 'method' => 'post',
55 'action' => $this->parent->getUrl( array( 'page' => $this->getName() ) )
56 )
57 ) . "\n"
58 );
59 }
60
61 public function endForm( $continue = 'continue', $back = 'back' ) {
62 $s = "<div class=\"config-submit\">\n";
63 $id = $this->getId();
64
65 if ( $id === false ) {
66 $s .= Html::hidden( 'lastPage', $this->parent->request->getVal( 'lastPage' ) );
67 }
68
69 if ( $continue ) {
70 // Fake submit button for enter keypress (bug 26267)
71 $s .= Xml::submitButton( wfMsg( "config-$continue" ),
72 array( 'name' => "enter-$continue", 'style' => 'visibility:hidden;overflow:hidden;width:1px;margin:0' ) ) . "\n";
73 }
74
75 if ( $back ) {
76 $s .= Xml::submitButton( wfMsg( "config-$back" ),
77 array(
78 'name' => "submit-$back",
79 'tabindex' => $this->parent->nextTabIndex()
80 ) ) . "\n";
81 }
82
83 if ( $continue ) {
84 $s .= Xml::submitButton( wfMsg( "config-$continue" ),
85 array(
86 'name' => "submit-$continue",
87 'tabindex' => $this->parent->nextTabIndex(),
88 ) ) . "\n";
89 }
90
91 $s .= "</div></form></div>\n";
92 $this->addHTML( $s );
93 }
94
95 public function getName() {
96 return str_replace( 'WebInstaller_', '', get_class( $this ) );
97 }
98
99 protected function getId() {
100 return array_search( $this->getName(), $this->parent->pageSequence );
101 }
102
103 public function getVar( $var ) {
104 return $this->parent->getVar( $var );
105 }
106
107 public function setVar( $name, $value ) {
108 $this->parent->setVar( $name, $value );
109 }
110
111 /**
112 * Get the starting tags of a fieldset.
113 *
114 * @param $legend String: message name
115 *
116 * @return string
117 */
118 protected function getFieldsetStart( $legend ) {
119 return "\n<fieldset><legend>" . wfMsgHtml( $legend ) . "</legend>\n";
120 }
121
122 /**
123 * Get the end tag of a fieldset.
124 *
125 * @returns string
126 */
127 protected function getFieldsetEnd() {
128 return "</fieldset>\n";
129 }
130
131 /**
132 * Opens a textarea used to display the progress of a long operation
133 */
134 protected function startLiveBox() {
135 $this->addHTML(
136 '<div id="config-spinner" style="display:none;"><img src="../skins/common/images/ajax-loader.gif" /></div>' .
137 '<script>jQuery( "#config-spinner" ).show();</script>' .
138 '<div id="config-live-log"><textarea name="LiveLog" rows="10" cols="30" readonly="readonly">'
139 );
140 $this->parent->output->flush();
141 }
142
143 /**
144 * Opposite to startLiveBox()
145 */
146 protected function endLiveBox() {
147 $this->addHTML( '</textarea></div>
148 <script>jQuery( "#config-spinner" ).hide()</script>' );
149 $this->parent->output->flush();
150 }
151 }
152
153 class WebInstaller_Language extends WebInstallerPage {
154
155 public function execute() {
156 global $wgLang;
157 $r = $this->parent->request;
158 $userLang = $r->getVal( 'UserLang' );
159 $contLang = $r->getVal( 'ContLang' );
160
161 $lifetime = intval( ini_get( 'session.gc_maxlifetime' ) );
162 if ( !$lifetime ) {
163 $lifetime = 1440; // PHP default
164 }
165
166 if ( $r->wasPosted() ) {
167 # Do session test
168 if ( $this->parent->getSession( 'test' ) === null ) {
169 $requestTime = $r->getVal( 'LanguageRequestTime' );
170 if ( !$requestTime ) {
171 // The most likely explanation is that the user was knocked back
172 // from another page on POST due to session expiry
173 $msg = 'config-session-expired';
174 } elseif ( time() - $requestTime > $lifetime ) {
175 $msg = 'config-session-expired';
176 } else {
177 $msg = 'config-no-session';
178 }
179 $this->parent->showError( $msg, $wgLang->formatTimePeriod( $lifetime ) );
180 } else {
181 $languages = Language::getLanguageNames();
182 if ( isset( $languages[$userLang] ) ) {
183 $this->setVar( '_UserLang', $userLang );
184 }
185 if ( isset( $languages[$contLang] ) ) {
186 $this->setVar( 'wgLanguageCode', $contLang );
187 }
188 return 'continue';
189 }
190 } elseif ( $this->parent->showSessionWarning ) {
191 # The user was knocked back from another page to the start
192 # This probably indicates a session expiry
193 $this->parent->showError( 'config-session-expired', $wgLang->formatTimePeriod( $lifetime ) );
194 }
195
196 $this->parent->setSession( 'test', true );
197
198 if ( !isset( $languages[$userLang] ) ) {
199 $userLang = $this->getVar( '_UserLang', 'en' );
200 }
201 if ( !isset( $languages[$contLang] ) ) {
202 $contLang = $this->getVar( 'wgLanguageCode', 'en' );
203 }
204 $this->startForm();
205 $s = Html::hidden( 'LanguageRequestTime', time() ) .
206 $this->getLanguageSelector( 'UserLang', 'config-your-language', $userLang, $this->parent->getHelpBox( 'config-your-language-help' ) ) .
207 $this->getLanguageSelector( 'ContLang', 'config-wiki-language', $contLang, $this->parent->getHelpBox( 'config-wiki-language-help' ) );
208 $this->addHTML( $s );
209 $this->endForm( 'continue', false );
210 }
211
212 /**
213 * Get a <select> for selecting languages.
214 *
215 * @return string
216 */
217 public function getLanguageSelector( $name, $label, $selectedCode, $helpHtml = '' ) {
218 global $wgDummyLanguageCodes;
219
220 $s = $helpHtml;
221
222 $s .= Html::openElement( 'select', array( 'id' => $name, 'name' => $name ) ) . "\n";
223
224 $languages = Language::getLanguageNames();
225 ksort( $languages );
226 $dummies = array_flip( $wgDummyLanguageCodes );
227 foreach ( $languages as $code => $lang ) {
228 if ( isset( $dummies[$code] ) ) continue;
229 $s .= "\n" . Xml::option( "$code - $lang", $code, $code == $selectedCode );
230 }
231 $s .= "\n</select>\n";
232 return $this->parent->label( $label, $name, $s );
233 }
234
235 }
236
237 class WebInstaller_ExistingWiki extends WebInstallerPage {
238 public function execute() {
239 // If there is no LocalSettings.php, continue to the installer welcome page
240 $vars = Installer::getExistingLocalSettings();
241 if ( !$vars ) {
242 return 'skip';
243 }
244
245 // Check if the upgrade key supplied to the user has appeared in LocalSettings.php
246 if ( $vars['wgUpgradeKey'] !== false
247 && $this->getVar( '_UpgradeKeySupplied' )
248 && $this->getVar( 'wgUpgradeKey' ) === $vars['wgUpgradeKey'] )
249 {
250 // It's there, so the user is authorized
251 $status = $this->handleExistingUpgrade( $vars );
252 if ( $status->isOK() ) {
253 return 'skip';
254 } else {
255 $this->startForm();
256 $this->parent->showStatusBox( $status );
257 $this->endForm( 'continue' );
258 return 'output';
259 }
260 }
261
262 // If there is no $wgUpgradeKey, tell the user to add one to LocalSettings.php
263 if ( $vars['wgUpgradeKey'] === false ) {
264 if ( $this->getVar( 'wgUpgradeKey', false ) === false ) {
265 $secretKey = $this->getVar( 'wgSecretKey' ); // preserve $wgSecretKey
266 $this->parent->generateKeys();
267 $this->setVar( 'wgSecretKey', $secretKey );
268 $this->setVar( '_UpgradeKeySupplied', true );
269 }
270 $this->startForm();
271 $this->addHTML( $this->parent->getInfoBox(
272 wfMsgNoTrans( 'config-upgrade-key-missing',
273 "<pre>\$wgUpgradeKey = '" . $this->getVar( 'wgUpgradeKey' ) . "';</pre>" )
274 ) );
275 $this->endForm( 'continue' );
276 return 'output';
277 }
278
279 // If there is an upgrade key, but it wasn't supplied, prompt the user to enter it
280
281 $r = $this->parent->request;
282 if ( $r->wasPosted() ) {
283 $key = $r->getText( 'config_wgUpgradeKey' );
284 if( !$key || $key !== $vars['wgUpgradeKey'] ) {
285 $this->parent->showError( 'config-localsettings-badkey' );
286 $this->showKeyForm();
287 return 'output';
288 }
289 // Key was OK
290 $status = $this->handleExistingUpgrade( $vars );
291 if ( $status->isOK() ) {
292 return 'continue';
293 } else {
294 $this->parent->showStatusBox( $status );
295 $this->showKeyForm();
296 return 'output';
297 }
298 } else {
299 $this->showKeyForm();
300 return 'output';
301 }
302 }
303
304 /**
305 * Show the "enter key" form
306 */
307 protected function showKeyForm() {
308 $this->startForm();
309 $this->addHTML(
310 $this->parent->getInfoBox( wfMsgNoTrans( 'config-localsettings-upgrade' ) ).
311 '<br />' .
312 $this->parent->getTextBox( array(
313 'var' => 'wgUpgradeKey',
314 'label' => 'config-localsettings-key',
315 'attribs' => array( 'autocomplete' => 'off' ),
316 ) )
317 );
318 $this->endForm( 'continue' );
319 }
320
321 protected function importVariables( $names, $vars ) {
322 $status = Status::newGood();
323 foreach ( $names as $name ) {
324 if ( !isset( $vars[$name] ) ) {
325 $status->fatal( 'config-localsettings-incomplete', $name );
326 }
327 $this->setVar( $name, $vars[$name] );
328 }
329 return $status;
330 }
331
332 /**
333 * Initiate an upgrade of the existing database
334 * @param $vars Variables from LocalSettings.php and AdminSettings.php
335 * @return Status
336 */
337 protected function handleExistingUpgrade( $vars ) {
338 // Check $wgDBtype
339 if ( !isset( $vars['wgDBtype'] ) || !in_array( $vars['wgDBtype'], Installer::getDBTypes() ) ) {
340 return Status::newFatal( 'config-localsettings-connection-error', '' );
341 }
342
343 // Set the relevant variables from LocalSettings.php
344 $requiredVars = array( 'wgDBtype' );
345 $status = $this->importVariables( $requiredVars , $vars );
346 $installer = $this->parent->getDBInstaller();
347 $status->merge( $this->importVariables( $installer->getGlobalNames(), $vars ) );
348 if ( !$status->isOK() ) {
349 return $status;
350 }
351
352 if ( isset( $vars['wgDBadminuser'] ) ) {
353 $this->setVar( '_InstallUser', $vars['wgDBadminuser'] );
354 } else {
355 $this->setVar( '_InstallUser', $vars['wgDBuser'] );
356 }
357 if ( isset( $vars['wgDBadminpassword'] ) ) {
358 $this->setVar( '_InstallPassword', $vars['wgDBadminpassword'] );
359 } else {
360 $this->setVar( '_InstallPassword', $vars['wgDBpassword'] );
361 }
362
363 // Test the database connection
364 $status = $installer->getConnection();
365 if ( !$status->isOK() ) {
366 // Adjust the error message to explain things correctly
367 $status->replaceMessage( 'config-connection-error',
368 'config-localsettings-connection-error' );
369 return $status;
370 }
371
372 // All good
373 $this->setVar( '_ExistingDBSettings', true );
374 return $status;
375 }
376 }
377
378 class WebInstaller_Welcome extends WebInstallerPage {
379
380 public function execute() {
381 if ( $this->parent->request->wasPosted() ) {
382 if ( $this->getVar( '_Environment' ) ) {
383 return 'continue';
384 }
385 }
386 $this->parent->output->addWikiText( wfMsgNoTrans( 'config-welcome' ) );
387 $status = $this->parent->doEnvironmentChecks();
388 if ( $status->isGood() ) {
389 $this->parent->output->addHTML( '<span class="success-message">' .
390 wfMsgHtml( 'config-env-good' ) . '</span>' );
391 $this->parent->output->addWikiText( wfMsgNoTrans( 'config-copyright',
392 SpecialVersion::getCopyrightAndAuthorList() ) );
393 $this->startForm();
394 $this->endForm();
395 } else {
396 $this->parent->showStatusMessage( $status );
397 }
398 }
399
400 }
401
402 class WebInstaller_DBConnect extends WebInstallerPage {
403
404 public function execute() {
405 if ( $this->getVar( '_ExistingDBSettings' ) ) {
406 return 'skip';
407 }
408
409 $r = $this->parent->request;
410 if ( $r->wasPosted() ) {
411 $status = $this->submit();
412
413 if ( $status->isGood() ) {
414 $this->setVar( '_UpgradeDone', false );
415 return 'continue';
416 } else {
417 $this->parent->showStatusBox( $status );
418 }
419 }
420
421 $this->startForm();
422
423 $types = "<ul class=\"config-settings-block\">\n";
424 $settings = '';
425 $defaultType = $this->getVar( 'wgDBtype' );
426
427 $dbSupport = '';
428 foreach( $this->parent->getDBTypes() as $type ) {
429 $link = DatabaseBase::factory( $type )->getSoftwareLink();
430 $dbSupport .= wfMsgNoTrans( "config-support-$type", $link ) . "\n";
431 }
432 $this->addHTML( $this->parent->getInfoBox(
433 wfMsg( 'config-support-info', $dbSupport ) ) );
434
435 foreach ( $this->parent->getVar( '_CompiledDBs' ) as $type ) {
436 $installer = $this->parent->getDBInstaller( $type );
437 $types .=
438 '<li>' .
439 Xml::radioLabel(
440 $installer->getReadableName(),
441 'DBType',
442 $type,
443 "DBType_$type",
444 $type == $defaultType,
445 array( 'class' => 'dbRadio', 'rel' => "DB_wrapper_$type" )
446 ) .
447 "</li>\n";
448
449 $settings .=
450 Html::openElement( 'div', array( 'id' => 'DB_wrapper_' . $type, 'class' => 'dbWrapper' ) ) .
451 Html::element( 'h3', array(), wfMsg( 'config-header-' . $type ) ) .
452 $installer->getConnectForm() .
453 "</div>\n";
454 }
455 $types .= "</ul><br clear=\"left\"/>\n";
456
457 $this->addHTML(
458 $this->parent->label( 'config-db-type', false, $types ) .
459 $settings
460 );
461
462 $this->endForm();
463 }
464
465 public function submit() {
466 $r = $this->parent->request;
467 $type = $r->getVal( 'DBType' );
468 $this->setVar( 'wgDBtype', $type );
469 $installer = $this->parent->getDBInstaller( $type );
470 if ( !$installer ) {
471 return Status::newFatal( 'config-invalid-db-type' );
472 }
473 return $installer->submitConnectForm();
474 }
475
476 }
477
478 class WebInstaller_Upgrade extends WebInstallerPage {
479 public function isSlow() {
480 return true;
481 }
482
483 public function execute() {
484 if ( $this->getVar( '_UpgradeDone' ) ) {
485 // Allow regeneration of LocalSettings.php, unless we are working
486 // from a pre-existing LocalSettings.php file and we want to avoid
487 // leaking its contents
488 if ( $this->parent->request->wasPosted() && !$this->getVar( '_ExistingDBSettings' ) ) {
489 // Done message acknowledged
490 return 'continue';
491 } else {
492 // Back button click
493 // Show the done message again
494 // Make them click back again if they want to do the upgrade again
495 $this->showDoneMessage();
496 return 'output';
497 }
498 }
499
500 // wgDBtype is generally valid here because otherwise the previous page
501 // (connect) wouldn't have declared its happiness
502 $type = $this->getVar( 'wgDBtype' );
503 $installer = $this->parent->getDBInstaller( $type );
504
505 if ( !$installer->needsUpgrade() ) {
506 return 'skip';
507 }
508
509 if ( $this->parent->request->wasPosted() ) {
510 $installer->preUpgrade();
511
512 $this->startLiveBox();
513 $result = $installer->doUpgrade();
514 $this->endLiveBox();
515
516 if ( $result ) {
517 // If they're going to possibly regenerate LocalSettings, we
518 // need to create the upgrade/secret keys. Bug 26481
519 if( !$this->getVar( '_ExistingDBSettings' ) ) {
520 $this->parent->generateKeys();
521 }
522 $this->setVar( '_UpgradeDone', true );
523 $this->showDoneMessage();
524 return 'output';
525 }
526 }
527
528 $this->startForm();
529 $this->addHTML( $this->parent->getInfoBox(
530 wfMsgNoTrans( 'config-can-upgrade', $GLOBALS['wgVersion'] ) ) );
531 $this->endForm();
532 }
533
534 public function showDoneMessage() {
535 $this->startForm();
536 $regenerate = !$this->getVar( '_ExistingDBSettings' );
537 if ( $regenerate ) {
538 $msg = 'config-upgrade-done';
539 } else {
540 $msg = 'config-upgrade-done-no-regenerate';
541 }
542 $this->parent->disableLinkPopups();
543 $this->addHTML(
544 $this->parent->getInfoBox(
545 wfMsgNoTrans( $msg,
546 $this->getVar( 'wgServer' ) .
547 $this->getVar( 'wgScriptPath' ) . '/index' .
548 $this->getVar( 'wgScriptExtension' )
549 ), 'tick-32.png'
550 )
551 );
552 $this->parent->restoreLinkPopups();
553 $this->endForm( $regenerate ? 'regenerate' : false, false );
554 }
555
556 }
557
558 class WebInstaller_DBSettings extends WebInstallerPage {
559
560 public function execute() {
561 $installer = $this->parent->getDBInstaller( $this->getVar( 'wgDBtype' ) );
562
563 $r = $this->parent->request;
564 if ( $r->wasPosted() ) {
565 $status = $installer->submitSettingsForm();
566 if ( $status === false ) {
567 return 'skip';
568 } elseif ( $status->isGood() ) {
569 return 'continue';
570 } else {
571 $this->parent->showStatusBox( $status );
572 }
573 }
574
575 $form = $installer->getSettingsForm();
576 if ( $form === false ) {
577 return 'skip';
578 }
579
580 $this->startForm();
581 $this->addHTML( $form );
582 $this->endForm();
583 }
584
585 }
586
587 class WebInstaller_Name extends WebInstallerPage {
588
589 public function execute() {
590 $r = $this->parent->request;
591 if ( $r->wasPosted() ) {
592 if ( $this->submit() ) {
593 return 'continue';
594 }
595 }
596
597 $this->startForm();
598
599 // Encourage people to not name their site 'MediaWiki' by blanking the
600 // field. I think that was the intent with the original $GLOBALS['wgSitename']
601 // but these two always were the same so had the effect of making the
602 // installer forget $wgSitename when navigating back to this page.
603 if ( $this->getVar( 'wgSitename' ) == 'MediaWiki' ) {
604 $this->setVar( 'wgSitename', '' );
605 }
606
607 // Set wgMetaNamespace to something valid before we show the form.
608 // $wgMetaNamespace defaults to $wgSiteName which is 'MediaWiki'
609 $metaNS = $this->getVar( 'wgMetaNamespace' );
610 $this->setVar( 'wgMetaNamespace', wfMsgForContent( 'config-ns-other-default' ) );
611
612 $this->addHTML(
613 $this->parent->getTextBox( array(
614 'var' => 'wgSitename',
615 'label' => 'config-site-name',
616 'help' => $this->parent->getHelpBox( 'config-site-name-help' )
617 ) ) .
618 $this->parent->getRadioSet( array(
619 'var' => '_NamespaceType',
620 'label' => 'config-project-namespace',
621 'itemLabelPrefix' => 'config-ns-',
622 'values' => array( 'site-name', 'generic', 'other' ),
623 'commonAttribs' => array( 'class' => 'enableForOther', 'rel' => 'config_wgMetaNamespace' ),
624 'help' => $this->parent->getHelpBox( 'config-project-namespace-help' )
625 ) ) .
626 $this->parent->getTextBox( array(
627 'var' => 'wgMetaNamespace',
628 'label' => '', //TODO: Needs a label?
629 'attribs' => array( 'readonly' => 'readonly', 'class' => 'enabledByOther' ),
630
631 ) ) .
632 $this->getFieldSetStart( 'config-admin-box' ) .
633 $this->parent->getTextBox( array(
634 'var' => '_AdminName',
635 'label' => 'config-admin-name',
636 'help' => $this->parent->getHelpBox( 'config-admin-help' )
637 ) ) .
638 $this->parent->getPasswordBox( array(
639 'var' => '_AdminPassword',
640 'label' => 'config-admin-password',
641 ) ) .
642 $this->parent->getPasswordBox( array(
643 'var' => '_AdminPassword2',
644 'label' => 'config-admin-password-confirm'
645 ) ) .
646 $this->parent->getTextBox( array(
647 'var' => '_AdminEmail',
648 'label' => 'config-admin-email',
649 'help' => $this->parent->getHelpBox( 'config-admin-email-help' )
650 ) ) .
651 $this->parent->getCheckBox( array(
652 'var' => '_Subscribe',
653 'label' => 'config-subscribe',
654 'help' => $this->parent->getHelpBox( 'config-subscribe-help' )
655 ) ) .
656 $this->getFieldSetEnd() .
657 $this->parent->getInfoBox( wfMsg( 'config-almost-done' ) ) .
658 $this->parent->getRadioSet( array(
659 'var' => '_SkipOptional',
660 'itemLabelPrefix' => 'config-optional-',
661 'values' => array( 'continue', 'skip' )
662 ) )
663 );
664
665 // Restore the default value
666 $this->setVar( 'wgMetaNamespace', $metaNS );
667
668 $this->endForm();
669 return 'output';
670 }
671
672 public function submit() {
673 $retVal = true;
674 $this->parent->setVarsFromRequest( array( 'wgSitename', '_NamespaceType',
675 '_AdminName', '_AdminPassword', '_AdminPassword2', '_AdminEmail',
676 '_Subscribe', '_SkipOptional', 'wgMetaNamespace' ) );
677
678 // Validate site name
679 if ( strval( $this->getVar( 'wgSitename' ) ) === '' ) {
680 $this->parent->showError( 'config-site-name-blank' );
681 $retVal = false;
682 }
683
684 // Fetch namespace
685 $nsType = $this->getVar( '_NamespaceType' );
686 if ( $nsType == 'site-name' ) {
687 $name = $this->getVar( 'wgSitename' );
688 // Sanitize for namespace
689 // This algorithm should match the JS one in WebInstallerOutput.php
690 $name = preg_replace( '/[\[\]\{\}|#<>%+? ]/', '_', $name );
691 $name = str_replace( '&', '&amp;', $name );
692 $name = preg_replace( '/__+/', '_', $name );
693 $name = ucfirst( trim( $name, '_' ) );
694 } elseif ( $nsType == 'generic' ) {
695 $name = wfMsg( 'config-ns-generic' );
696 } else { // other
697 $name = $this->getVar( 'wgMetaNamespace' );
698 }
699
700 // Validate namespace
701 if ( strpos( $name, ':' ) !== false ) {
702 $good = false;
703 } else {
704 // Title-style validation
705 $title = Title::newFromText( $name );
706 if ( !$title ) {
707 $good = $nsType == 'site-name';
708 } else {
709 $name = $title->getDBkey();
710 $good = true;
711 }
712 }
713 if ( !$good ) {
714 $this->parent->showError( 'config-ns-invalid', $name );
715 $retVal = false;
716 }
717
718 // Make sure it won't conflict with any existing namespaces
719 global $wgContLang;
720 $nsIndex = $wgContLang->getNsIndex( $name );
721 if( $nsIndex !== false && $nsIndex !== NS_PROJECT ) {
722 $this->parent->showError( 'config-ns-conflict', $name );
723 $retVal = false;
724 }
725
726 $this->setVar( 'wgMetaNamespace', $name );
727
728 // Validate username for creation
729 $name = $this->getVar( '_AdminName' );
730 if ( strval( $name ) === '' ) {
731 $this->parent->showError( 'config-admin-name-blank' );
732 $cname = $name;
733 $retVal = false;
734 } else {
735 $cname = User::getCanonicalName( $name, 'creatable' );
736 if ( $cname === false ) {
737 $this->parent->showError( 'config-admin-name-invalid', $name );
738 $retVal = false;
739 } else {
740 $this->setVar( '_AdminName', $cname );
741 }
742 }
743
744 // Validate password
745 $msg = false;
746 $pwd = $this->getVar( '_AdminPassword' );
747 $user = User::newFromName( $cname );
748 $valid = $user && $user->getPasswordValidity( $pwd );
749 if ( strval( $pwd ) === '' ) {
750 # $user->getPasswordValidity just checks for $wgMinimalPasswordLength.
751 # This message is more specific and helpful.
752 $msg = 'config-admin-password-blank';
753 } elseif ( $pwd !== $this->getVar( '_AdminPassword2' ) ) {
754 $msg = 'config-admin-password-mismatch';
755 } elseif ( $valid !== true ) {
756 # As of writing this will only catch the username being e.g. 'FOO' and
757 # the password 'foo'
758 $msg = $valid;
759 }
760 if ( $msg !== false ) {
761 call_user_func_array( array( $this->parent, 'showError' ), (array)$msg );
762 $this->setVar( '_AdminPassword', '' );
763 $this->setVar( '_AdminPassword2', '' );
764 $retVal = false;
765 }
766
767 // Validate e-mail if provided
768 $email = $this->getVar( '_AdminEmail' );
769 if( $email && !Sanitizer::validateEmail( $email ) ) {
770 $this->parent->showError( 'config-admin-error-bademail' );
771 $retVal = false;
772 }
773 // If they asked to subscribe to mediawiki-announce but didn't give
774 // an e-mail, show an error. Bug 29332
775 if( !$email && $this->getVar( '_Subscribe' ) ) {
776 $this->parent->showError( 'config-subscribe-noemail' );
777 $retVal = false;
778 }
779
780 return $retVal;
781 }
782
783 }
784
785 class WebInstaller_Options extends WebInstallerPage {
786
787 public function execute() {
788 if ( $this->getVar( '_SkipOptional' ) == 'skip' ) {
789 return 'skip';
790 }
791 if ( $this->parent->request->wasPosted() ) {
792 if ( $this->submit() ) {
793 return 'continue';
794 }
795 }
796
797 $emailwrapperStyle = $this->getVar( 'wgEnableEmail' ) ? '' : 'display: none';
798 $this->startForm();
799 $this->addHTML(
800 # User Rights
801 $this->parent->getRadioSet( array(
802 'var' => '_RightsProfile',
803 'label' => 'config-profile',
804 'itemLabelPrefix' => 'config-profile-',
805 'values' => array_keys( $this->parent->rightsProfiles ),
806 ) ) .
807 $this->parent->getInfoBox( wfMsgNoTrans( 'config-profile-help' ) ) .
808
809 # Licensing
810 $this->parent->getRadioSet( array(
811 'var' => '_LicenseCode',
812 'label' => 'config-license',
813 'itemLabelPrefix' => 'config-license-',
814 'values' => array_keys( $this->parent->licenses ),
815 'commonAttribs' => array( 'class' => 'licenseRadio' ),
816 ) ) .
817 $this->getCCChooser() .
818 $this->parent->getHelpBox( 'config-license-help' ) .
819
820 # E-mail
821 $this->getFieldSetStart( 'config-email-settings' ) .
822 $this->parent->getCheckBox( array(
823 'var' => 'wgEnableEmail',
824 'label' => 'config-enable-email',
825 'attribs' => array( 'class' => 'showHideRadio', 'rel' => 'emailwrapper' ),
826 ) ) .
827 $this->parent->getHelpBox( 'config-enable-email-help' ) .
828 "<div id=\"emailwrapper\" style=\"$emailwrapperStyle\">" .
829 $this->parent->getTextBox( array(
830 'var' => 'wgPasswordSender',
831 'label' => 'config-email-sender'
832 ) ) .
833 $this->parent->getHelpBox( 'config-email-sender-help' ) .
834 $this->parent->getCheckBox( array(
835 'var' => 'wgEnableUserEmail',
836 'label' => 'config-email-user',
837 ) ) .
838 $this->parent->getHelpBox( 'config-email-user-help' ) .
839 $this->parent->getCheckBox( array(
840 'var' => 'wgEnotifUserTalk',
841 'label' => 'config-email-usertalk',
842 ) ) .
843 $this->parent->getHelpBox( 'config-email-usertalk-help' ) .
844 $this->parent->getCheckBox( array(
845 'var' => 'wgEnotifWatchlist',
846 'label' => 'config-email-watchlist',
847 ) ) .
848 $this->parent->getHelpBox( 'config-email-watchlist-help' ) .
849 $this->parent->getCheckBox( array(
850 'var' => 'wgEmailAuthentication',
851 'label' => 'config-email-auth',
852 ) ) .
853 $this->parent->getHelpBox( 'config-email-auth-help' ) .
854 "</div>" .
855 $this->getFieldSetEnd()
856 );
857
858 $extensions = $this->parent->findExtensions();
859
860 if( $extensions ) {
861 $extHtml = $this->getFieldSetStart( 'config-extensions' );
862
863 foreach( $extensions as $ext ) {
864 $extHtml .= $this->parent->getCheckBox( array(
865 'var' => "ext-$ext",
866 'rawtext' => $ext,
867 ) );
868 }
869
870 $extHtml .= $this->parent->getHelpBox( 'config-extensions-help' ) .
871 $this->getFieldSetEnd();
872 $this->addHTML( $extHtml );
873 }
874
875 // Having / in paths in Windows looks funny :)
876 $this->setVar( 'wgDeletedDirectory',
877 str_replace(
878 '/', DIRECTORY_SEPARATOR,
879 $this->getVar( 'wgDeletedDirectory' )
880 )
881 );
882
883 $uploadwrapperStyle = $this->getVar( 'wgEnableUploads' ) ? '' : 'display: none';
884 $this->addHTML(
885 # Uploading
886 $this->getFieldSetStart( 'config-upload-settings' ) .
887 $this->parent->getCheckBox( array(
888 'var' => 'wgEnableUploads',
889 'label' => 'config-upload-enable',
890 'attribs' => array( 'class' => 'showHideRadio', 'rel' => 'uploadwrapper' ),
891 'help' => $this->parent->getHelpBox( 'config-upload-help' )
892 ) ) .
893 '<div id="uploadwrapper" style="' . $uploadwrapperStyle . '">' .
894 $this->parent->getTextBox( array(
895 'var' => 'wgDeletedDirectory',
896 'label' => 'config-upload-deleted',
897 'help' => $this->parent->getHelpBox( 'config-upload-deleted-help' )
898 ) ) .
899 '</div>' .
900 $this->parent->getTextBox( array(
901 'var' => 'wgLogo',
902 'label' => 'config-logo',
903 'help' => $this->parent->getHelpBox( 'config-logo-help' )
904 ) )
905 );
906 $this->addHTML(
907 $this->parent->getCheckBox( array(
908 'var' => 'wgUseInstantCommons',
909 'label' => 'config-instantcommons',
910 'help' => $this->parent->getHelpBox( 'config-instantcommons-help' )
911 ) ) .
912 $this->getFieldSetEnd()
913 );
914
915 $caches = array( 'none' );
916 if( count( $this->getVar( '_Caches' ) ) ) {
917 $caches[] = 'accel';
918 }
919 $caches[] = 'memcached';
920
921 $this->addHTML(
922 # Advanced settings
923 $this->getFieldSetStart( 'config-advanced-settings' ) .
924 # Object cache settings
925 $this->parent->getRadioSet( array(
926 'var' => 'wgMainCacheType',
927 'label' => 'config-cache-options',
928 'itemLabelPrefix' => 'config-cache-',
929 'values' => $caches,
930 'value' => 'none',
931 ) ) .
932 $this->parent->getHelpBox( 'config-cache-help' ) .
933 '<div id="config-memcachewrapper">' .
934 $this->parent->getTextArea( array(
935 'var' => '_MemCachedServers',
936 'label' => 'config-memcached-servers',
937 'help' => $this->parent->getHelpBox( 'config-memcached-help' )
938 ) ) .
939 '</div>' .
940 $this->getFieldSetEnd()
941 );
942 $this->endForm();
943 }
944
945 /**
946 * @return string
947 */
948 public function getCCPartnerUrl() {
949 $server = $this->getVar( 'wgServer' );
950 $exitUrl = $server . $this->parent->getUrl( array(
951 'page' => 'Options',
952 'SubmitCC' => 'indeed',
953 'config__LicenseCode' => 'cc',
954 'config_wgRightsUrl' => '[license_url]',
955 'config_wgRightsText' => '[license_name]',
956 'config_wgRightsIcon' => '[license_button]',
957 ) );
958 $styleUrl = $server . dirname( dirname( $this->parent->getUrl() ) ) .
959 '/skins/common/config-cc.css';
960 $iframeUrl = 'http://creativecommons.org/license/?' .
961 wfArrayToCGI( array(
962 'partner' => 'MediaWiki',
963 'exit_url' => $exitUrl,
964 'lang' => $this->getVar( '_UserLang' ),
965 'stylesheet' => $styleUrl,
966 ) );
967 return $iframeUrl;
968 }
969
970 public function getCCChooser() {
971 $iframeAttribs = array(
972 'class' => 'config-cc-iframe',
973 'name' => 'config-cc-iframe',
974 'id' => 'config-cc-iframe',
975 'frameborder' => 0,
976 'width' => '100%',
977 'height' => '100%',
978 );
979 if ( $this->getVar( '_CCDone' ) ) {
980 $iframeAttribs['src'] = $this->parent->getUrl( array( 'ShowCC' => 'yes' ) );
981 } else {
982 $iframeAttribs['src'] = $this->getCCPartnerUrl();
983 }
984 $wrapperStyle = ($this->getVar('_LicenseCode') == 'cc-choose') ? '' : 'display: none';
985
986 return
987 "<div class=\"config-cc-wrapper\" id=\"config-cc-wrapper\" style=\"$wrapperStyle\">\n" .
988 Html::element( 'iframe', $iframeAttribs, '', false /* not short */ ) .
989 "</div>\n";
990 }
991
992 public function getCCDoneBox() {
993 $js = "parent.document.getElementById('config-cc-wrapper').style.height = '$1';";
994 // If you change this height, also change it in config.css
995 $expandJs = str_replace( '$1', '54em', $js );
996 $reduceJs = str_replace( '$1', '70px', $js );
997 return
998 '<p>'.
999 Html::element( 'img', array( 'src' => $this->getVar( 'wgRightsIcon' ) ) ) .
1000 '&#160;&#160;' .
1001 htmlspecialchars( $this->getVar( 'wgRightsText' ) ) .
1002 "</p>\n" .
1003 "<p style=\"text-align: center\">" .
1004 Html::element( 'a',
1005 array(
1006 'href' => $this->getCCPartnerUrl(),
1007 'onclick' => $expandJs,
1008 ),
1009 wfMsg( 'config-cc-again' )
1010 ) .
1011 "</p>\n" .
1012 "<script type=\"text/javascript\">\n" .
1013 # Reduce the wrapper div height
1014 htmlspecialchars( $reduceJs ) .
1015 "\n" .
1016 "</script>\n";
1017 }
1018
1019 public function submitCC() {
1020 $newValues = $this->parent->setVarsFromRequest(
1021 array( 'wgRightsUrl', 'wgRightsText', 'wgRightsIcon' ) );
1022 if ( count( $newValues ) != 3 ) {
1023 $this->parent->showError( 'config-cc-error' );
1024 return;
1025 }
1026 $this->setVar( '_CCDone', true );
1027 $this->addHTML( $this->getCCDoneBox() );
1028 }
1029
1030 public function submit() {
1031 $this->parent->setVarsFromRequest( array( '_RightsProfile', '_LicenseCode',
1032 'wgEnableEmail', 'wgPasswordSender', 'wgEnableUploads', 'wgLogo',
1033 'wgEnableUserEmail', 'wgEnotifUserTalk', 'wgEnotifWatchlist',
1034 'wgEmailAuthentication', 'wgMainCacheType', '_MemCachedServers',
1035 'wgUseInstantCommons' ) );
1036
1037 if ( !in_array( $this->getVar( '_RightsProfile' ),
1038 array_keys( $this->parent->rightsProfiles ) ) )
1039 {
1040 reset( $this->parent->rightsProfiles );
1041 $this->setVar( '_RightsProfile', key( $this->parent->rightsProfiles ) );
1042 }
1043
1044 $code = $this->getVar( '_LicenseCode' );
1045 if ( $code == 'cc-choose' ) {
1046 if ( !$this->getVar( '_CCDone' ) ) {
1047 $this->parent->showError( 'config-cc-not-chosen' );
1048 return false;
1049 }
1050 } elseif ( in_array( $code, array_keys( $this->parent->licenses ) ) ) {
1051 $entry = $this->parent->licenses[$code];
1052 if ( isset( $entry['text'] ) ) {
1053 $this->setVar( 'wgRightsText', $entry['text'] );
1054 } else {
1055 $this->setVar( 'wgRightsText', wfMsg( 'config-license-' . $code ) );
1056 }
1057 $this->setVar( 'wgRightsUrl', $entry['url'] );
1058 $this->setVar( 'wgRightsIcon', $entry['icon'] );
1059 } else {
1060 $this->setVar( 'wgRightsText', '' );
1061 $this->setVar( 'wgRightsUrl', '' );
1062 $this->setVar( 'wgRightsIcon', '' );
1063 }
1064
1065 $extsAvailable = $this->parent->findExtensions();
1066 $extsToInstall = array();
1067 foreach( $extsAvailable as $ext ) {
1068 if( $this->parent->request->getCheck( 'config_ext-' . $ext ) ) {
1069 $extsToInstall[] = $ext;
1070 }
1071 }
1072 $this->parent->setVar( '_Extensions', $extsToInstall );
1073
1074 if( $this->getVar( 'wgMainCacheType' ) == 'memcached' ) {
1075 $memcServers = explode( "\n", $this->getVar( '_MemCachedServers' ) );
1076 if( !$memcServers ) {
1077 $this->parent->showError( 'config-memcache-needservers' );
1078 return false;
1079 }
1080
1081 foreach( $memcServers as $server ) {
1082 $memcParts = explode( ":", $server );
1083 if( !IP::isValid( $memcParts[0] ) ) {
1084 $this->parent->showError( 'config-memcache-badip', $memcParts[0] );
1085 return false;
1086 } elseif( !isset( $memcParts[1] ) ) {
1087 $this->parent->showError( 'config-memcache-noport', $memcParts[0] );
1088 return false;
1089 } elseif( $memcParts[1] < 1 || $memcParts[1] > 65535 ) {
1090 $this->parent->showError( 'config-memcache-badport', 1, 65535 );
1091 return false;
1092 }
1093 }
1094 }
1095 return true;
1096 }
1097
1098 }
1099
1100 class WebInstaller_Install extends WebInstallerPage {
1101 public function isSlow() {
1102 return true;
1103 }
1104
1105 public function execute() {
1106 if( $this->getVar( '_UpgradeDone' ) ) {
1107 return 'skip';
1108 } elseif( $this->getVar( '_InstallDone' ) ) {
1109 return 'continue';
1110 } elseif( $this->parent->request->wasPosted() ) {
1111 $this->startForm();
1112 $this->addHTML("<ul>");
1113 $results = $this->parent->performInstallation(
1114 array( $this, 'startStage'),
1115 array( $this, 'endStage' )
1116 );
1117 $this->addHTML("</ul>");
1118 // PerformInstallation bails on a fatal, so make sure the last item
1119 // completed before giving 'next.' Likewise, only provide back on failure
1120 $lastStep = end( $results );
1121 $continue = $lastStep->isOK() ? 'continue' : false;
1122 $back = $lastStep->isOK() ? false : 'back';
1123 $this->endForm( $continue, $back );
1124 } else {
1125 $this->startForm();
1126 $this->addHTML( $this->parent->getInfoBox( wfMsgNoTrans( 'config-install-begin' ) ) );
1127 $this->endForm();
1128 }
1129 return true;
1130 }
1131
1132 public function startStage( $step ) {
1133 $this->addHTML( "<li>" . wfMsgHtml( "config-install-$step" ) . wfMsg( 'ellipsis') );
1134 if ( $step == 'extension-tables' ) {
1135 $this->startLiveBox();
1136 }
1137 }
1138
1139 /**
1140 * @param $step
1141 * @param $status Status
1142 */
1143 public function endStage( $step, $status ) {
1144 if ( $step == 'extension-tables' ) {
1145 $this->endLiveBox();
1146 }
1147 $msg = $status->isOk() ? 'config-install-step-done' : 'config-install-step-failed';
1148 $html = wfMsgHtml( 'word-separator' ) . wfMsgHtml( $msg );
1149 if ( !$status->isOk() ) {
1150 $html = "<span class=\"error\">$html</span>";
1151 }
1152 $this->addHTML( $html . "</li>\n" );
1153 if( !$status->isGood() ) {
1154 $this->parent->showStatusBox( $status );
1155 }
1156 }
1157
1158 }
1159
1160 class WebInstaller_Complete extends WebInstallerPage {
1161
1162 public function execute() {
1163 // Pop up a dialog box, to make it difficult for the user to forget
1164 // to download the file
1165 $lsUrl = $this->getVar( 'wgServer' ) . $this->parent->getURL( array( 'localsettings' => 1 ) );
1166 if ( isset( $_SERVER['HTTP_USER_AGENT'] ) && strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE' ) !== false ) {
1167 // JS appears the only method that works consistently with IE7+
1168 $this->addHtml( "\n<script type=\"" . $GLOBALS['wgJsMimeType'] . '">jQuery( document ).ready( function() { document.location='
1169 . Xml::encodeJsVar( $lsUrl) . "; } );</script>\n" );
1170 } else {
1171 $this->parent->request->response()->header( "Refresh: 0;url=$lsUrl" );
1172 }
1173
1174 $this->startForm();
1175 $this->parent->disableLinkPopups();
1176 $this->addHTML(
1177 $this->parent->getInfoBox(
1178 wfMsgNoTrans( 'config-install-done',
1179 $lsUrl,
1180 $this->getVar( 'wgServer' ) .
1181 $this->getVar( 'wgScriptPath' ) . '/index' .
1182 $this->getVar( 'wgScriptExtension' ),
1183 '<downloadlink/>'
1184 ), 'tick-32.png'
1185 )
1186 );
1187 $this->parent->restoreLinkPopups();
1188 $this->endForm( false, false );
1189 }
1190 }
1191
1192 class WebInstaller_Restart extends WebInstallerPage {
1193
1194 public function execute() {
1195 $r = $this->parent->request;
1196 if ( $r->wasPosted() ) {
1197 $really = $r->getVal( 'submit-restart' );
1198 if ( $really ) {
1199 $this->parent->reset();
1200 }
1201 return 'continue';
1202 }
1203
1204 $this->startForm();
1205 $s = $this->parent->getWarningBox( wfMsgNoTrans( 'config-help-restart' ) );
1206 $this->addHTML( $s );
1207 $this->endForm( 'restart' );
1208 }
1209
1210 }
1211
1212 abstract class WebInstaller_Document extends WebInstallerPage {
1213
1214 protected abstract function getFileName();
1215
1216 public function execute() {
1217 $text = $this->getFileContents();
1218 $text = InstallDocFormatter::format( $text );
1219 $this->parent->output->addWikiText( $text );
1220 $this->startForm();
1221 $this->endForm( false );
1222 }
1223
1224 public function getFileContents() {
1225 return file_get_contents( dirname( __FILE__ ) . '/../../' . $this->getFileName() );
1226 }
1227
1228 }
1229
1230 class WebInstaller_Readme extends WebInstaller_Document {
1231 protected function getFileName() { return 'README'; }
1232 }
1233
1234 class WebInstaller_ReleaseNotes extends WebInstaller_Document {
1235 protected function getFileName() { return 'RELEASE-NOTES'; }
1236 }
1237
1238 class WebInstaller_UpgradeDoc extends WebInstaller_Document {
1239 protected function getFileName() { return 'UPGRADE'; }
1240 }
1241
1242 class WebInstaller_Copying extends WebInstaller_Document {
1243 protected function getFileName() { return 'COPYING'; }
1244 }
1245