3 * Core installer web interface.
10 * Class for the core installer web interface.
15 class WebInstaller
extends Installer
{
18 * @var WebInstallerOutput
30 * Cached session array.
37 * Captured PHP error text. Temporary.
43 * The main sequence of page names. These will be displayed in turn.
46 * * Add a config-page-<name> message
47 * * Add a WebInstaller_<name> class
50 public $pageSequence = array(
64 * Out of sequence pages, selectable by the user at any time.
67 protected $otherPages = array(
72 'UpgradeDoc', // Can't use Upgrade due to Upgrade step
76 * Array of pages which have declared that they have been submitted, have validated
77 * their input, and need no further processing.
80 protected $happyPages;
83 * List of "skipped" pages. These are pages that will automatically continue
84 * to the next page on any GET request. To avoid breaking the "back" button,
85 * they need to be skipped during a back operation.
88 protected $skippedPages;
91 * Flag indicating that session data may have been lost.
94 public $showSessionWarning = false;
97 * Numeric index of the page we're on
100 protected $tabIndex = 1;
103 * Name of the page we're on
106 protected $currentPageName;
111 * @param $request WebRequest
113 public function __construct( WebRequest
$request ) {
114 parent
::__construct();
115 $this->output
= new WebInstallerOutput( $this );
116 $this->request
= $request;
118 // Add parser hook for WebInstaller_Complete
120 $wgParser->setHook( 'downloadlink', array( $this, 'downloadLinkHook' ) );
126 * @param $session Array: initial session array
128 * @return Array: new session array
130 public function execute( array $session ) {
131 $this->session
= $session;
133 if ( isset( $session['settings'] ) ) {
134 $this->settings
= $session['settings'] +
$this->settings
;
138 $this->setupLanguage();
140 if( ( $this->getVar( '_InstallDone' ) ||
$this->getVar( '_UpgradeDone' ) )
141 && $this->request
->getVal( 'localsettings' ) )
143 $this->request
->response()->header( 'Content-type: application/x-httpd-php' );
144 $this->request
->response()->header(
145 'Content-Disposition: attachment; filename="LocalSettings.php"'
148 $ls = new LocalSettingsGenerator( $this );
150 return $this->session
;
153 $cssDir = $this->request
->getVal( 'css' );
155 $cssDir = ( $cssDir == 'rtl' ?
'rtl' : 'ltr' );
156 $this->request
->response()->header( 'Content-type: text/css' );
157 echo $this->output
->getCSS( $cssDir );
158 return $this->session
;
161 if ( isset( $session['happyPages'] ) ) {
162 $this->happyPages
= $session['happyPages'];
164 $this->happyPages
= array();
167 if ( isset( $session['skippedPages'] ) ) {
168 $this->skippedPages
= $session['skippedPages'];
170 $this->skippedPages
= array();
173 $lowestUnhappy = $this->getLowestUnhappy();
175 # Special case for Creative Commons partner chooser box.
176 if ( $this->request
->getVal( 'SubmitCC' ) ) {
177 $page = $this->getPageByName( 'Options' );
178 $this->output
->useShortHeader();
180 return $this->finish();
183 if ( $this->request
->getVal( 'ShowCC' ) ) {
184 $page = $this->getPageByName( 'Options' );
185 $this->output
->useShortHeader();
186 $this->output
->addHTML( $page->getCCDoneBox() );
187 return $this->finish();
191 $pageName = $this->request
->getVal( 'page' );
193 if ( in_array( $pageName, $this->otherPages
) ) {
196 $page = $this->getPageByName( $pageName );
199 if ( !$pageName ||
!in_array( $pageName, $this->pageSequence
) ) {
200 $pageId = $lowestUnhappy;
202 $pageId = array_search( $pageName, $this->pageSequence
);
205 # If necessary, move back to the lowest-numbered unhappy page
206 if ( $pageId > $lowestUnhappy ) {
207 $pageId = $lowestUnhappy;
208 if ( $lowestUnhappy == 0 ) {
209 # Knocked back to start, possible loss of session data.
210 $this->showSessionWarning
= true;
214 $pageName = $this->pageSequence
[$pageId];
215 $page = $this->getPageByName( $pageName );
218 # If a back button was submitted, go back without submitting the form data.
219 if ( $this->request
->wasPosted() && $this->request
->getBool( 'submit-back' ) ) {
220 if ( $this->request
->getVal( 'lastPage' ) ) {
221 $nextPage = $this->request
->getVal( 'lastPage' );
222 } elseif ( $pageId !== false ) {
224 # Skip the skipped pages
225 $nextPageId = $pageId;
229 $nextPage = $this->pageSequence
[$nextPageId];
230 } while( isset( $this->skippedPages
[$nextPage] ) );
232 $nextPage = $this->pageSequence
[$lowestUnhappy];
235 $this->output
->redirect( $this->getUrl( array( 'page' => $nextPage ) ) );
236 return $this->finish();
240 $this->currentPageName
= $page->getName();
241 $this->startPageWrapper( $pageName );
243 $result = $page->execute();
245 $this->endPageWrapper();
247 if ( $result == 'skip' ) {
248 # Page skipped without explicit submission.
249 # Skip it when we click "back" so that we don't just go forward again.
250 $this->skippedPages
[$pageName] = true;
251 $result = 'continue';
253 unset( $this->skippedPages
[$pageName] );
256 # If it was posted, the page can request a continue to the next page.
257 if ( $result === 'continue' && !$this->output
->headerDone() ) {
258 if ( $pageId !== false ) {
259 $this->happyPages
[$pageId] = true;
262 $lowestUnhappy = $this->getLowestUnhappy();
264 if ( $this->request
->getVal( 'lastPage' ) ) {
265 $nextPage = $this->request
->getVal( 'lastPage' );
266 } elseif ( $pageId !== false ) {
267 $nextPage = $this->pageSequence
[$pageId +
1];
269 $nextPage = $this->pageSequence
[$lowestUnhappy];
272 if ( array_search( $nextPage, $this->pageSequence
) > $lowestUnhappy ) {
273 $nextPage = $this->pageSequence
[$lowestUnhappy];
276 $this->output
->redirect( $this->getUrl( array( 'page' => $nextPage ) ) );
279 return $this->finish();
283 * Find the next page in sequence that hasn't been completed
286 public function getLowestUnhappy() {
287 if ( count( $this->happyPages
) == 0 ) {
290 return max( array_keys( $this->happyPages
) ) +
1;
295 * Start the PHP session. This may be called before execute() to start the PHP session.
297 public function startSession() {
298 if( wfIniGetBool( 'session.auto_start' ) ||
session_id() ) {
303 $this->phpErrors
= array();
304 set_error_handler( array( $this, 'errorHandler' ) );
306 restore_error_handler();
308 if ( $this->phpErrors
) {
309 $this->showError( 'config-session-error', $this->phpErrors
[0] );
317 * Get a hash of data identifying this MW installation.
319 * This is used by config/index.php to prevent multiple installations of MW
320 * on the same cookie domain from interfering with each other.
322 public function getFingerprint() {
323 // Get the base URL of the installation
324 $url = $this->request
->getFullRequestURL();
325 if ( preg_match( '!^(.*)/[^/]*/[^/]*$!', $url, $m ) ) {
328 return md5( serialize( array(
329 'local path' => dirname( dirname( __FILE__
) ),
331 'version' => $GLOBALS['wgVersion']
336 * Show an error message in a box. Parameters are like wfMsg().
338 public function showError( $msg /*...*/ ) {
339 $args = func_get_args();
340 array_shift( $args );
341 $args = array_map( 'htmlspecialchars', $args );
342 $msg = wfMsgReal( $msg, $args, false, false, false );
343 $this->output
->addHTML( $this->getErrorBox( $msg ) );
347 * Temporary error handler for session start debugging.
349 public function errorHandler( $errno, $errstr ) {
350 $this->phpErrors
[] = $errstr;
354 * Clean up from execute()
358 public function finish() {
359 $this->output
->output();
361 $this->session
['happyPages'] = $this->happyPages
;
362 $this->session
['skippedPages'] = $this->skippedPages
;
363 $this->session
['settings'] = $this->settings
;
365 return $this->session
;
369 * We're restarting the installation, reset the session, happyPages, etc
371 public function reset() {
372 $this->session
= array();
373 $this->happyPages
= array();
374 $this->settings
= array();
378 * Get a URL for submission back to the same script.
380 * @param $query: Array
383 public function getUrl( $query = array() ) {
384 $url = $this->request
->getRequestURL();
385 # Remove existing query
386 $url = preg_replace( '/\?.*$/', '', $url );
389 $url .= '?' . wfArrayToCGI( $query );
396 * Get a WebInstallerPage by name.
398 * @param $pageName String
399 * @return WebInstallerPage
401 public function getPageByName( $pageName ) {
402 // Totally lame way to force autoload of WebInstallerPage.php
403 class_exists( 'WebInstallerPage' );
405 $pageClass = 'WebInstaller_' . $pageName;
407 return new $pageClass( $this );
411 * Get a session variable.
413 * @param $name String
416 public function getSession( $name, $default = null ) {
417 if ( !isset( $this->session
[$name] ) ) {
420 return $this->session
[$name];
425 * Set a session variable.
426 * @param $name String key for the variable
427 * @param $value Mixed
429 public function setSession( $name, $value ) {
430 $this->session
[$name] = $value;
434 * Get the next tabindex attribute value.
437 public function nextTabIndex() {
438 return $this->tabIndex++
;
442 * Initializes language-related variables.
444 public function setupLanguage() {
445 global $wgLang, $wgContLang, $wgLanguageCode;
447 if ( $this->getSession( 'test' ) === null && !$this->request
->wasPosted() ) {
448 $wgLanguageCode = $this->getAcceptLanguage();
449 $wgLang = $wgContLang = Language
::factory( $wgLanguageCode );
450 $this->setVar( 'wgLanguageCode', $wgLanguageCode );
451 $this->setVar( '_UserLang', $wgLanguageCode );
453 $wgLanguageCode = $this->getVar( 'wgLanguageCode' );
454 $wgLang = Language
::factory( $this->getVar( '_UserLang' ) );
455 $wgContLang = Language
::factory( $wgLanguageCode );
460 * Retrieves MediaWiki language from Accept-Language HTTP header.
464 public function getAcceptLanguage() {
465 global $wgLanguageCode, $wgRequest;
467 $mwLanguages = Language
::getLanguageNames();
468 $headerLanguages = array_keys( $wgRequest->getAcceptLang() );
470 foreach ( $headerLanguages as $lang ) {
471 if ( isset( $mwLanguages[$lang] ) ) {
476 return $wgLanguageCode;
480 * Called by execute() before page output starts, to show a page list.
482 * @param $currentPageName String
484 private function startPageWrapper( $currentPageName ) {
485 $s = "<div class=\"config-page-wrapper\">\n";
486 $s .= "<div class=\"config-page\">\n";
487 $s .= "<div class=\"config-page-list\"><ul>\n";
490 foreach ( $this->pageSequence
as $id => $pageName ) {
491 $happy = !empty( $this->happyPages
[$id] );
492 $s .= $this->getPageListItem(
494 $happy ||
$lastHappy == $id - 1,
503 $s .= "</ul><br/><ul>\n";
505 foreach ( $this->otherPages
as $pageName ) {
506 $s .= $this->getPageListItem( $pageName, true, $currentPageName );
509 $s .= "</ul></div>\n"; // end list pane
510 $s .= Html
::element( 'h2', array(),
511 wfMsg( 'config-page-' . strtolower( $currentPageName ) ) );
513 $this->output
->addHTMLNoFlush( $s );
517 * Get a list item for the page list.
519 * @param $pageName String
520 * @param $enabled Boolean
521 * @param $currentPageName String
525 private function getPageListItem( $pageName, $enabled, $currentPageName ) {
526 $s = "<li class=\"config-page-list-item\">";
527 $name = wfMsg( 'config-page-' . strtolower( $pageName ) );
530 $query = array( 'page' => $pageName );
532 if ( !in_array( $pageName, $this->pageSequence
) ) {
533 if ( in_array( $currentPageName, $this->pageSequence
) ) {
534 $query['lastPage'] = $currentPageName;
537 $link = Html
::element( 'a',
539 'href' => $this->getUrl( $query )
544 $link = htmlspecialchars( $name );
547 if ( $pageName == $currentPageName ) {
548 $s .= "<span class=\"config-page-current\">$link</span>";
553 $s .= Html
::element( 'span',
555 'class' => 'config-page-disabled'
567 * Output some stuff after a page is finished.
569 private function endPageWrapper() {
570 $this->output
->addHTMLNoFlush(
571 "<div class=\"visualClear\"></div>\n" .
573 "<div class=\"visualClear\"></div>\n" .
578 * Get HTML for an error box with an icon.
580 * @param $text String: wikitext, get this with wfMsgNoTrans()
582 public function getErrorBox( $text ) {
583 return $this->getInfoBox( $text, 'critical-32.png', 'config-error-box' );
587 * Get HTML for a warning box with an icon.
589 * @param $text String: wikitext, get this with wfMsgNoTrans()
591 public function getWarningBox( $text ) {
592 return $this->getInfoBox( $text, 'warning-32.png', 'config-warning-box' );
596 * Get HTML for an info box with an icon.
598 * @param $text String: wikitext, get this with wfMsgNoTrans()
599 * @param $icon String: icon name, file in skins/common/images
600 * @param $class String: additional class name to add to the wrapper div
602 public function getInfoBox( $text, $icon = 'info-32.png', $class = false ) {
604 "<div class=\"config-info $class\">\n" .
605 "<div class=\"config-info-left\">\n" .
606 Html
::element( 'img',
608 'src' => '../skins/common/images/' . $icon,
609 'alt' => wfMsg( 'config-information' ),
613 "<div class=\"config-info-right\">\n" .
614 $this->parse( $text, true ) . "\n" .
616 "<div style=\"clear: left;\"></div>\n" .
622 * Get small text indented help for a preceding form field.
623 * Parameters like wfMsg().
625 public function getHelpBox( $msg /*, ... */ ) {
626 $args = func_get_args();
627 array_shift( $args );
628 $args = array_map( 'htmlspecialchars', $args );
629 $text = wfMsgReal( $msg, $args, false, false, false );
630 $html = htmlspecialchars( $text );
631 $html = $this->parse( $text, true );
633 return "<div class=\"mw-help-field-container\">\n" .
634 "<span class=\"mw-help-field-hint\">" . wfMsgHtml( 'config-help' ) . "</span>\n" .
635 "<span class=\"mw-help-field-data\">" . $html . "</span>\n" .
641 * @param $msg String key for wfMsg()
643 public function showHelpBox( $msg /*, ... */ ) {
644 $args = func_get_args();
645 $html = call_user_func_array( array( $this, 'getHelpBox' ), $args );
646 $this->output
->addHTML( $html );
650 * Show a short informational message.
651 * Output looks like a list.
655 public function showMessage( $msg /*, ... */ ) {
656 $args = func_get_args();
657 array_shift( $args );
658 $html = '<div class="config-message">' .
659 $this->parse( wfMsgReal( $msg, $args, false, false, false ) ) .
661 $this->output
->addHTML( $html );
665 * @param $status Status
667 public function showStatusMessage( Status
$status ) {
668 $text = $status->getWikiText();
669 $this->output
->addWikiText(
670 "<div class=\"config-message\">\n" .
677 * Label a control by wrapping a config-input div around it and putting a
680 public function label( $msg, $forId, $contents, $helpData = "" ) {
681 if ( strval( $msg ) == '' ) {
682 $labelText = ' ';
684 $labelText = wfMsgHtml( $msg );
687 $attributes = array( 'class' => 'config-label' );
690 $attributes['for'] = $forId;
694 "<div class=\"config-block\">\n" .
695 " <div class=\"config-block-label\">\n" .
698 $labelText ) . "\n" .
701 " <div class=\"config-block-elements\">\n" .
708 * Get a labelled text box to configure a variable.
710 * @param $params Array
712 * var: The variable to be configured (required)
713 * label: The message name for the label (required)
714 * attribs: Additional attributes for the input element (optional)
715 * controlName: The name for the input element (optional)
716 * value: The current value of the variable (optional)
717 * help: The html for the help text (optional)
719 public function getTextBox( $params ) {
720 if ( !isset( $params['controlName'] ) ) {
721 $params['controlName'] = 'config_' . $params['var'];
724 if ( !isset( $params['value'] ) ) {
725 $params['value'] = $this->getVar( $params['var'] );
728 if ( !isset( $params['attribs'] ) ) {
729 $params['attribs'] = array();
731 if ( !isset( $params['help'] ) ) {
732 $params['help'] = "";
737 $params['controlName'],
739 $params['controlName'],
740 30, // intended to be overridden by CSS
742 $params['attribs'] +
array(
743 'id' => $params['controlName'],
744 'class' => 'config-input-text',
745 'tabindex' => $this->nextTabIndex()
753 * Get a labelled password box to configure a variable.
755 * Implements password hiding
756 * @param $params Array
758 * var: The variable to be configured (required)
759 * label: The message name for the label (required)
760 * attribs: Additional attributes for the input element (optional)
761 * controlName: The name for the input element (optional)
762 * value: The current value of the variable (optional)
763 * help: The html for the help text (optional)
765 public function getPasswordBox( $params ) {
766 if ( !isset( $params['value'] ) ) {
767 $params['value'] = $this->getVar( $params['var'] );
770 if ( !isset( $params['attribs'] ) ) {
771 $params['attribs'] = array();
774 $params['value'] = $this->getFakePassword( $params['value'] );
775 $params['attribs']['type'] = 'password';
777 return $this->getTextBox( $params );
781 * Get a labelled checkbox to configure a boolean variable.
783 * @param $params Array
785 * var: The variable to be configured (required)
786 * label: The message name for the label (required)
787 * attribs: Additional attributes for the input element (optional)
788 * controlName: The name for the input element (optional)
789 * value: The current value of the variable (optional)
790 * help: The html for the help text (optional)
792 public function getCheckBox( $params ) {
793 if ( !isset( $params['controlName'] ) ) {
794 $params['controlName'] = 'config_' . $params['var'];
797 if ( !isset( $params['value'] ) ) {
798 $params['value'] = $this->getVar( $params['var'] );
801 if ( !isset( $params['attribs'] ) ) {
802 $params['attribs'] = array();
804 if ( !isset( $params['help'] ) ) {
805 $params['help'] = "";
807 if( isset( $params['rawtext'] ) ) {
808 $labelText = $params['rawtext'];
810 $labelText = $this->parse( wfMsg( $params['label'] ) );
814 "<div class=\"config-input-check\">\n" .
818 $params['controlName'],
820 $params['attribs'] +
array(
821 'id' => $params['controlName'],
822 'tabindex' => $this->nextTabIndex(),
831 * Get a set of labelled radio buttons.
833 * @param $params Array
835 * var: The variable to be configured (required)
836 * label: The message name for the label (required)
837 * itemLabelPrefix: The message name prefix for the item labels (required)
838 * values: List of allowed values (required)
839 * itemAttribs Array of attribute arrays, outer key is the value name (optional)
840 * commonAttribs Attribute array applied to all items
841 * controlName: The name for the input element (optional)
842 * value: The current value of the variable (optional)
843 * help: The html for the help text (optional)
845 public function getRadioSet( $params ) {
846 if ( !isset( $params['controlName'] ) ) {
847 $params['controlName'] = 'config_' . $params['var'];
850 if ( !isset( $params['value'] ) ) {
851 $params['value'] = $this->getVar( $params['var'] );
854 if ( !isset( $params['label'] ) ) {
857 $label = $params['label'];
859 if ( !isset( $params['help'] ) ) {
860 $params['help'] = "";
863 foreach ( $params['values'] as $value ) {
864 $itemAttribs = array();
866 if ( isset( $params['commonAttribs'] ) ) {
867 $itemAttribs = $params['commonAttribs'];
870 if ( isset( $params['itemAttribs'][$value] ) ) {
871 $itemAttribs = $params['itemAttribs'][$value] +
$itemAttribs;
874 $checked = $value == $params['value'];
875 $id = $params['controlName'] . '_' . $value;
876 $itemAttribs['id'] = $id;
877 $itemAttribs['tabindex'] = $this->nextTabIndex();
881 Xml
::radio( $params['controlName'], $value, $checked, $itemAttribs ) .
883 Xml
::tags( 'label', array( 'for' => $id ), $this->parse(
884 wfMsgNoTrans( $params['itemLabelPrefix'] . strtolower( $value ) )
891 return $this->label( $label, $params['controlName'], $s, $params['help'] );
895 * Output an error or warning box using a Status object.
897 public function showStatusBox( $status ) {
898 if( !$status->isGood() ) {
899 $text = $status->getWikiText();
901 if( $status->isOk() ) {
902 $box = $this->getWarningBox( $text );
904 $box = $this->getErrorBox( $text );
907 $this->output
->addHTML( $box );
912 * Convenience function to set variables based on form data.
913 * Assumes that variables containing "password" in the name are (potentially
916 * @param $varNames Array
917 * @param $prefix String: the prefix added to variables to obtain form names
919 public function setVarsFromRequest( $varNames, $prefix = 'config_' ) {
920 $newValues = array();
922 foreach ( $varNames as $name ) {
923 $value = trim( $this->request
->getVal( $prefix . $name ) );
924 $newValues[$name] = $value;
926 if ( $value === null ) {
928 $this->setVar( $name, false );
930 if ( stripos( $name, 'password' ) !== false ) {
931 $this->setPassword( $name, $value );
933 $this->setVar( $name, $value );
942 * Helper for Installer::docLink()
944 protected function getDocUrl( $page ) {
945 $url = "{$_SERVER['PHP_SELF']}?page=" . urlencode( $page );
947 if ( in_array( $this->currentPageName
, $this->pageSequence
) ) {
948 $url .= '&lastPage=' . urlencode( $this->currentPageName
);
955 * Helper for "Download LocalSettings" link on WebInstall_Complete
956 * @return String Html for download link
958 public function downloadLinkHook( $text, $attribs, $parser ) {
959 $img = Html
::element( 'img', array(
960 'src' => '../skins/common/images/download-32.png',
964 $anchor = Html
::rawElement( 'a',
965 array( 'href' => $this->getURL( array( 'localsettings' => 1 ) ) ),
966 $img . ' ' . wfMsgHtml( 'config-download-localsettings' ) );
967 return Html
::rawElement( 'div', array( 'class' => 'config-download-link' ), $anchor );