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