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";
504 $s .= $this->getPageListItem( 'Restart', true, $currentPageName );
505 $s .= "</ul></div>\n"; // end list pane
506 $s .= Html
::element( 'h2', array(),
507 wfMsg( 'config-page-' . strtolower( $currentPageName ) ) );
509 $this->output
->addHTMLNoFlush( $s );
513 * Get a list item for the page list.
515 * @param $pageName String
516 * @param $enabled Boolean
517 * @param $currentPageName String
521 private function getPageListItem( $pageName, $enabled, $currentPageName ) {
522 $s = "<li class=\"config-page-list-item\">";
523 $name = wfMsg( 'config-page-' . strtolower( $pageName ) );
526 $query = array( 'page' => $pageName );
528 if ( !in_array( $pageName, $this->pageSequence
) ) {
529 if ( in_array( $currentPageName, $this->pageSequence
) ) {
530 $query['lastPage'] = $currentPageName;
533 $link = Html
::element( 'a',
535 'href' => $this->getUrl( $query )
540 $link = htmlspecialchars( $name );
543 if ( $pageName == $currentPageName ) {
544 $s .= "<span class=\"config-page-current\">$link</span>";
549 $s .= Html
::element( 'span',
551 'class' => 'config-page-disabled'
563 * Output some stuff after a page is finished.
565 private function endPageWrapper() {
566 $this->output
->addHTMLNoFlush(
567 "<div class=\"visualClear\"></div>\n" .
569 "<div class=\"visualClear\"></div>\n" .
574 * Get HTML for an error box with an icon.
576 * @param $text String: wikitext, get this with wfMsgNoTrans()
578 public function getErrorBox( $text ) {
579 return $this->getInfoBox( $text, 'critical-32.png', 'config-error-box' );
583 * Get HTML for a warning box with an icon.
585 * @param $text String: wikitext, get this with wfMsgNoTrans()
587 public function getWarningBox( $text ) {
588 return $this->getInfoBox( $text, 'warning-32.png', 'config-warning-box' );
592 * Get HTML for an info box with an icon.
594 * @param $text String: wikitext, get this with wfMsgNoTrans()
595 * @param $icon String: icon name, file in skins/common/images
596 * @param $class String: additional class name to add to the wrapper div
598 public function getInfoBox( $text, $icon = 'info-32.png', $class = false ) {
600 "<div class=\"config-info $class\">\n" .
601 "<div class=\"config-info-left\">\n" .
602 Html
::element( 'img',
604 'src' => '../skins/common/images/' . $icon,
605 'alt' => wfMsg( 'config-information' ),
609 "<div class=\"config-info-right\">\n" .
610 $this->parse( $text, true ) . "\n" .
612 "<div style=\"clear: left;\"></div>\n" .
618 * Get small text indented help for a preceding form field.
619 * Parameters like wfMsg().
621 public function getHelpBox( $msg /*, ... */ ) {
622 $args = func_get_args();
623 array_shift( $args );
624 $args = array_map( 'htmlspecialchars', $args );
625 $text = wfMsgReal( $msg, $args, false, false, false );
626 $html = htmlspecialchars( $text );
627 $html = $this->parse( $text, true );
629 return "<div class=\"mw-help-field-container\">\n" .
630 "<span class=\"mw-help-field-hint\">" . wfMsgHtml( 'config-help' ) . "</span>\n" .
631 "<span class=\"mw-help-field-data\">" . $html . "</span>\n" .
637 * @param $msg String key for wfMsg()
639 public function showHelpBox( $msg /*, ... */ ) {
640 $args = func_get_args();
641 $html = call_user_func_array( array( $this, 'getHelpBox' ), $args );
642 $this->output
->addHTML( $html );
646 * Show a short informational message.
647 * Output looks like a list.
651 public function showMessage( $msg /*, ... */ ) {
652 $args = func_get_args();
653 array_shift( $args );
654 $html = '<div class="config-message">' .
655 $this->parse( wfMsgReal( $msg, $args, false, false, false ) ) .
657 $this->output
->addHTML( $html );
661 * @param $status Status
663 public function showStatusMessage( Status
$status ) {
664 $text = $status->getWikiText();
665 $this->output
->addWikiText(
666 "<div class=\"config-message\">\n" .
673 * Label a control by wrapping a config-input div around it and putting a
676 public function label( $msg, $forId, $contents, $helpData = "" ) {
677 if ( strval( $msg ) == '' ) {
678 $labelText = ' ';
680 $labelText = wfMsgHtml( $msg );
683 $attributes = array( 'class' => 'config-label' );
686 $attributes['for'] = $forId;
690 "<div class=\"config-block\">\n" .
691 " <div class=\"config-block-label\">\n" .
694 $labelText ) . "\n" .
697 " <div class=\"config-block-elements\">\n" .
704 * Get a labelled text box to configure a variable.
706 * @param $params Array
708 * var: The variable to be configured (required)
709 * label: The message name for the label (required)
710 * attribs: Additional attributes for the input element (optional)
711 * controlName: The name for the input element (optional)
712 * value: The current value of the variable (optional)
713 * help: The html for the help text (optional)
715 public function getTextBox( $params ) {
716 if ( !isset( $params['controlName'] ) ) {
717 $params['controlName'] = 'config_' . $params['var'];
720 if ( !isset( $params['value'] ) ) {
721 $params['value'] = $this->getVar( $params['var'] );
724 if ( !isset( $params['attribs'] ) ) {
725 $params['attribs'] = array();
727 if ( !isset( $params['help'] ) ) {
728 $params['help'] = "";
733 $params['controlName'],
735 $params['controlName'],
736 30, // intended to be overridden by CSS
738 $params['attribs'] +
array(
739 'id' => $params['controlName'],
740 'class' => 'config-input-text',
741 'tabindex' => $this->nextTabIndex()
749 * Get a labelled password box to configure a variable.
751 * Implements password hiding
752 * @param $params Array
754 * var: The variable to be configured (required)
755 * label: The message name for the label (required)
756 * attribs: Additional attributes for the input element (optional)
757 * controlName: The name for the input element (optional)
758 * value: The current value of the variable (optional)
759 * help: The html for the help text (optional)
761 public function getPasswordBox( $params ) {
762 if ( !isset( $params['value'] ) ) {
763 $params['value'] = $this->getVar( $params['var'] );
766 if ( !isset( $params['attribs'] ) ) {
767 $params['attribs'] = array();
770 $params['value'] = $this->getFakePassword( $params['value'] );
771 $params['attribs']['type'] = 'password';
773 return $this->getTextBox( $params );
777 * Get a labelled checkbox to configure a boolean variable.
779 * @param $params Array
781 * var: The variable to be configured (required)
782 * label: The message name for the label (required)
783 * attribs: Additional attributes for the input element (optional)
784 * controlName: The name for the input element (optional)
785 * value: The current value of the variable (optional)
786 * help: The html for the help text (optional)
788 public function getCheckBox( $params ) {
789 if ( !isset( $params['controlName'] ) ) {
790 $params['controlName'] = 'config_' . $params['var'];
793 if ( !isset( $params['value'] ) ) {
794 $params['value'] = $this->getVar( $params['var'] );
797 if ( !isset( $params['attribs'] ) ) {
798 $params['attribs'] = array();
800 if ( !isset( $params['help'] ) ) {
801 $params['help'] = "";
803 if( isset( $params['rawtext'] ) ) {
804 $labelText = $params['rawtext'];
806 $labelText = $this->parse( wfMsg( $params['label'] ) );
810 "<div class=\"config-input-check\">\n" .
814 $params['controlName'],
816 $params['attribs'] +
array(
817 'id' => $params['controlName'],
818 'tabindex' => $this->nextTabIndex(),
827 * Get a set of labelled radio buttons.
829 * @param $params Array
831 * var: The variable to be configured (required)
832 * label: The message name for the label (required)
833 * itemLabelPrefix: The message name prefix for the item labels (required)
834 * values: List of allowed values (required)
835 * itemAttribs Array of attribute arrays, outer key is the value name (optional)
836 * commonAttribs Attribute array applied to all items
837 * controlName: The name for the input element (optional)
838 * value: The current value of the variable (optional)
839 * help: The html for the help text (optional)
841 public function getRadioSet( $params ) {
842 if ( !isset( $params['controlName'] ) ) {
843 $params['controlName'] = 'config_' . $params['var'];
846 if ( !isset( $params['value'] ) ) {
847 $params['value'] = $this->getVar( $params['var'] );
850 if ( !isset( $params['label'] ) ) {
853 $label = $params['label'];
855 if ( !isset( $params['help'] ) ) {
856 $params['help'] = "";
859 foreach ( $params['values'] as $value ) {
860 $itemAttribs = array();
862 if ( isset( $params['commonAttribs'] ) ) {
863 $itemAttribs = $params['commonAttribs'];
866 if ( isset( $params['itemAttribs'][$value] ) ) {
867 $itemAttribs = $params['itemAttribs'][$value] +
$itemAttribs;
870 $checked = $value == $params['value'];
871 $id = $params['controlName'] . '_' . $value;
872 $itemAttribs['id'] = $id;
873 $itemAttribs['tabindex'] = $this->nextTabIndex();
877 Xml
::radio( $params['controlName'], $value, $checked, $itemAttribs ) .
879 Xml
::tags( 'label', array( 'for' => $id ), $this->parse(
880 wfMsgNoTrans( $params['itemLabelPrefix'] . strtolower( $value ) )
887 return $this->label( $label, $params['controlName'], $s, $params['help'] );
891 * Output an error or warning box using a Status object.
893 public function showStatusBox( $status ) {
894 if( !$status->isGood() ) {
895 $text = $status->getWikiText();
897 if( $status->isOk() ) {
898 $box = $this->getWarningBox( $text );
900 $box = $this->getErrorBox( $text );
903 $this->output
->addHTML( $box );
908 * Convenience function to set variables based on form data.
909 * Assumes that variables containing "password" in the name are (potentially
912 * @param $varNames Array
913 * @param $prefix String: the prefix added to variables to obtain form names
915 public function setVarsFromRequest( $varNames, $prefix = 'config_' ) {
916 $newValues = array();
918 foreach ( $varNames as $name ) {
919 $value = trim( $this->request
->getVal( $prefix . $name ) );
920 $newValues[$name] = $value;
922 if ( $value === null ) {
924 $this->setVar( $name, false );
926 if ( stripos( $name, 'password' ) !== false ) {
927 $this->setPassword( $name, $value );
929 $this->setVar( $name, $value );
938 * Helper for Installer::docLink()
940 protected function getDocUrl( $page ) {
941 $url = "{$_SERVER['PHP_SELF']}?page=" . urlencode( $page );
943 if ( in_array( $this->currentPageName
, $this->pageSequence
) ) {
944 $url .= '&lastPage=' . urlencode( $this->currentPageName
);
951 * Helper for "Download LocalSettings" link on WebInstall_Complete
952 * @return String Html for download link
954 public function downloadLinkHook( $text, $attribs, $parser ) {
955 $img = Html
::element( 'img', array(
956 'src' => '../skins/common/images/download-32.png',
960 $anchor = Html
::rawElement( 'a',
961 array( 'href' => $this->getURL( array( 'localsettings' => 1 ) ) ),
962 $img . ' ' . wfMsgHtml( 'config-download-localsettings' ) );
963 return Html
::rawElement( 'div', array( 'class' => 'config-download-link' ), $anchor );