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