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;
120 $wgParser->setHook( 'downloadlink', array( $this, 'downloadLinkHook' ) );
121 $wgParser->setHook( 'doclink', array( $this, 'docLink' ) );
127 * @param $session Array: initial session array
129 * @return Array: new session array
131 public function execute( array $session ) {
132 $this->session
= $session;
134 if ( isset( $session['settings'] ) ) {
135 $this->settings
= $session['settings'] +
$this->settings
;
139 $this->setupLanguage();
141 if( ( $this->getVar( '_InstallDone' ) ||
$this->getVar( '_UpgradeDone' ) )
142 && $this->request
->getVal( 'localsettings' ) )
144 $this->request
->response()->header( 'Content-type: application/x-httpd-php' );
145 $this->request
->response()->header(
146 'Content-Disposition: attachment; filename="LocalSettings.php"'
149 $ls = new LocalSettingsGenerator( $this );
151 return $this->session
;
154 $cssDir = $this->request
->getVal( 'css' );
156 $cssDir = ( $cssDir == 'rtl' ?
'rtl' : 'ltr' );
157 $this->request
->response()->header( 'Content-type: text/css' );
158 echo $this->output
->getCSS( $cssDir );
159 return $this->session
;
162 if ( isset( $session['happyPages'] ) ) {
163 $this->happyPages
= $session['happyPages'];
165 $this->happyPages
= array();
168 if ( isset( $session['skippedPages'] ) ) {
169 $this->skippedPages
= $session['skippedPages'];
171 $this->skippedPages
= array();
174 $lowestUnhappy = $this->getLowestUnhappy();
176 # Special case for Creative Commons partner chooser box.
177 if ( $this->request
->getVal( 'SubmitCC' ) ) {
178 $page = $this->getPageByName( 'Options' );
179 $this->output
->useShortHeader();
180 $this->output
->allowFrames();
182 return $this->finish();
185 if ( $this->request
->getVal( 'ShowCC' ) ) {
186 $page = $this->getPageByName( 'Options' );
187 $this->output
->useShortHeader();
188 $this->output
->allowFrames();
189 $this->output
->addHTML( $page->getCCDoneBox() );
190 return $this->finish();
194 $pageName = $this->request
->getVal( 'page' );
196 if ( in_array( $pageName, $this->otherPages
) ) {
199 $page = $this->getPageByName( $pageName );
202 if ( !$pageName ||
!in_array( $pageName, $this->pageSequence
) ) {
203 $pageId = $lowestUnhappy;
205 $pageId = array_search( $pageName, $this->pageSequence
);
208 # If necessary, move back to the lowest-numbered unhappy page
209 if ( $pageId > $lowestUnhappy ) {
210 $pageId = $lowestUnhappy;
211 if ( $lowestUnhappy == 0 ) {
212 # Knocked back to start, possible loss of session data.
213 $this->showSessionWarning
= true;
217 $pageName = $this->pageSequence
[$pageId];
218 $page = $this->getPageByName( $pageName );
221 # If a back button was submitted, go back without submitting the form data.
222 if ( $this->request
->wasPosted() && $this->request
->getBool( 'submit-back' ) ) {
223 if ( $this->request
->getVal( 'lastPage' ) ) {
224 $nextPage = $this->request
->getVal( 'lastPage' );
225 } elseif ( $pageId !== false ) {
227 # Skip the skipped pages
228 $nextPageId = $pageId;
232 $nextPage = $this->pageSequence
[$nextPageId];
233 } while( isset( $this->skippedPages
[$nextPage] ) );
235 $nextPage = $this->pageSequence
[$lowestUnhappy];
238 $this->output
->redirect( $this->getUrl( array( 'page' => $nextPage ) ) );
239 return $this->finish();
243 $this->currentPageName
= $page->getName();
244 $this->startPageWrapper( $pageName );
246 $result = $page->execute();
248 $this->endPageWrapper();
250 if ( $result == 'skip' ) {
251 # Page skipped without explicit submission.
252 # Skip it when we click "back" so that we don't just go forward again.
253 $this->skippedPages
[$pageName] = true;
254 $result = 'continue';
256 unset( $this->skippedPages
[$pageName] );
259 # If it was posted, the page can request a continue to the next page.
260 if ( $result === 'continue' && !$this->output
->headerDone() ) {
261 if ( $pageId !== false ) {
262 $this->happyPages
[$pageId] = true;
265 $lowestUnhappy = $this->getLowestUnhappy();
267 if ( $this->request
->getVal( 'lastPage' ) ) {
268 $nextPage = $this->request
->getVal( 'lastPage' );
269 } elseif ( $pageId !== false ) {
270 $nextPage = $this->pageSequence
[$pageId +
1];
272 $nextPage = $this->pageSequence
[$lowestUnhappy];
275 if ( array_search( $nextPage, $this->pageSequence
) > $lowestUnhappy ) {
276 $nextPage = $this->pageSequence
[$lowestUnhappy];
279 $this->output
->redirect( $this->getUrl( array( 'page' => $nextPage ) ) );
282 return $this->finish();
286 * Find the next page in sequence that hasn't been completed
289 public function getLowestUnhappy() {
290 if ( count( $this->happyPages
) == 0 ) {
293 return max( array_keys( $this->happyPages
) ) +
1;
298 * Start the PHP session. This may be called before execute() to start the PHP session.
302 public function startSession() {
303 if( wfIniGetBool( 'session.auto_start' ) ||
session_id() ) {
308 $this->phpErrors
= array();
309 set_error_handler( array( $this, 'errorHandler' ) );
311 restore_error_handler();
313 if ( $this->phpErrors
) {
314 $this->showError( 'config-session-error', $this->phpErrors
[0] );
322 * Get a hash of data identifying this MW installation.
324 * This is used by mw-config/index.php to prevent multiple installations of MW
325 * on the same cookie domain from interfering with each other.
329 public function getFingerprint() {
330 // Get the base URL of the installation
331 $url = $this->request
->getFullRequestURL();
332 if ( preg_match( '!^(.*\?)!', $url, $m) ) {
336 if ( preg_match( '!^(.*)/[^/]*/[^/]*$!', $url, $m ) ) {
337 // This... seems to try to get the base path from
338 // the /mw-config/index.php. Kinda scary though?
341 return md5( serialize( array(
342 'local path' => dirname( dirname( __FILE__
) ),
344 'version' => $GLOBALS['wgVersion']
349 * Show an error message in a box. Parameters are like wfMsg().
351 public function showError( $msg /*...*/ ) {
352 $args = func_get_args();
353 array_shift( $args );
354 $args = array_map( 'htmlspecialchars', $args );
355 $msg = wfMsgReal( $msg, $args, false, false, false );
356 $this->output
->addHTML( $this->getErrorBox( $msg ) );
360 * Temporary error handler for session start debugging.
362 public function errorHandler( $errno, $errstr ) {
363 $this->phpErrors
[] = $errstr;
367 * Clean up from execute()
371 public function finish() {
372 $this->output
->output();
374 $this->session
['happyPages'] = $this->happyPages
;
375 $this->session
['skippedPages'] = $this->skippedPages
;
376 $this->session
['settings'] = $this->settings
;
378 return $this->session
;
382 * We're restarting the installation, reset the session, happyPages, etc
384 public function reset() {
385 $this->session
= array();
386 $this->happyPages
= array();
387 $this->settings
= array();
391 * Get a URL for submission back to the same script.
393 * @param $query array
396 public function getUrl( $query = array() ) {
397 $url = $this->request
->getRequestURL();
398 # Remove existing query
399 $url = preg_replace( '/\?.*$/', '', $url );
402 $url .= '?' . wfArrayToCGI( $query );
409 * Get a WebInstallerPage by name.
411 * @param $pageName String
412 * @return WebInstallerPage
414 public function getPageByName( $pageName ) {
415 $pageClass = 'WebInstaller_' . $pageName;
417 return new $pageClass( $this );
421 * Get a session variable.
423 * @param $name String
426 public function getSession( $name, $default = null ) {
427 if ( !isset( $this->session
[$name] ) ) {
430 return $this->session
[$name];
435 * Set a session variable.
436 * @param $name String key for the variable
437 * @param $value Mixed
439 public function setSession( $name, $value ) {
440 $this->session
[$name] = $value;
444 * Get the next tabindex attribute value.
447 public function nextTabIndex() {
448 return $this->tabIndex++
;
452 * Initializes language-related variables.
454 public function setupLanguage() {
455 global $wgLang, $wgContLang, $wgLanguageCode;
457 if ( $this->getSession( 'test' ) === null && !$this->request
->wasPosted() ) {
458 $wgLanguageCode = $this->getAcceptLanguage();
459 $wgLang = $wgContLang = Language
::factory( $wgLanguageCode );
460 $this->setVar( 'wgLanguageCode', $wgLanguageCode );
461 $this->setVar( '_UserLang', $wgLanguageCode );
463 $wgLanguageCode = $this->getVar( 'wgLanguageCode' );
464 $wgLang = Language
::factory( $this->getVar( '_UserLang' ) );
465 $wgContLang = Language
::factory( $wgLanguageCode );
470 * Retrieves MediaWiki language from Accept-Language HTTP header.
474 public function getAcceptLanguage() {
475 global $wgLanguageCode, $wgRequest;
477 $mwLanguages = Language
::getLanguageNames();
478 $headerLanguages = array_keys( $wgRequest->getAcceptLang() );
480 foreach ( $headerLanguages as $lang ) {
481 if ( isset( $mwLanguages[$lang] ) ) {
486 return $wgLanguageCode;
490 * Called by execute() before page output starts, to show a page list.
492 * @param $currentPageName String
494 private function startPageWrapper( $currentPageName ) {
495 $s = "<div class=\"config-page-wrapper\">\n";
496 $s .= "<div class=\"config-page\">\n";
497 $s .= "<div class=\"config-page-list\"><ul>\n";
500 foreach ( $this->pageSequence
as $id => $pageName ) {
501 $happy = !empty( $this->happyPages
[$id] );
502 $s .= $this->getPageListItem(
504 $happy ||
$lastHappy == $id - 1,
513 $s .= "</ul><br/><ul>\n";
514 $s .= $this->getPageListItem( 'Restart', true, $currentPageName );
515 $s .= "</ul></div>\n"; // end list pane
516 $s .= Html
::element( 'h2', array(),
517 wfMsg( 'config-page-' . strtolower( $currentPageName ) ) );
519 $this->output
->addHTMLNoFlush( $s );
523 * Get a list item for the page list.
525 * @param $pageName String
526 * @param $enabled Boolean
527 * @param $currentPageName String
531 private function getPageListItem( $pageName, $enabled, $currentPageName ) {
532 $s = "<li class=\"config-page-list-item\">";
533 $name = wfMsg( 'config-page-' . strtolower( $pageName ) );
536 $query = array( 'page' => $pageName );
538 if ( !in_array( $pageName, $this->pageSequence
) ) {
539 if ( in_array( $currentPageName, $this->pageSequence
) ) {
540 $query['lastPage'] = $currentPageName;
543 $link = Html
::element( 'a',
545 'href' => $this->getUrl( $query )
550 $link = htmlspecialchars( $name );
553 if ( $pageName == $currentPageName ) {
554 $s .= "<span class=\"config-page-current\">$link</span>";
559 $s .= Html
::element( 'span',
561 'class' => 'config-page-disabled'
573 * Output some stuff after a page is finished.
575 private function endPageWrapper() {
576 $this->output
->addHTMLNoFlush(
577 "<div class=\"visualClear\"></div>\n" .
579 "<div class=\"visualClear\"></div>\n" .
584 * Get HTML for an error box with an icon.
586 * @param $text String: wikitext, get this with wfMsgNoTrans()
590 public function getErrorBox( $text ) {
591 return $this->getInfoBox( $text, 'critical-32.png', 'config-error-box' );
595 * Get HTML for a warning box with an icon.
597 * @param $text String: wikitext, get this with wfMsgNoTrans()
601 public function getWarningBox( $text ) {
602 return $this->getInfoBox( $text, 'warning-32.png', 'config-warning-box' );
606 * Get HTML for an info box with an icon.
608 * @param $text String: wikitext, get this with wfMsgNoTrans()
609 * @param $icon String: icon name, file in skins/common/images
610 * @param $class String: additional class name to add to the wrapper div
614 public function getInfoBox( $text, $icon = 'info-32.png', $class = false ) {
616 "<div class=\"config-info $class\">\n" .
617 "<div class=\"config-info-left\">\n" .
618 Html
::element( 'img',
620 'src' => '../skins/common/images/' . $icon,
621 'alt' => wfMsg( 'config-information' ),
625 "<div class=\"config-info-right\">\n" .
626 $this->parse( $text, true ) . "\n" .
628 "<div style=\"clear: left;\"></div>\n" .
634 * Get small text indented help for a preceding form field.
635 * Parameters like wfMsg().
639 public function getHelpBox( $msg /*, ... */ ) {
640 $args = func_get_args();
641 array_shift( $args );
642 $args = array_map( 'htmlspecialchars', $args );
643 $text = wfMsgReal( $msg, $args, false, false, false );
644 $html = htmlspecialchars( $text );
645 $html = $this->parse( $text, true );
647 return "<div class=\"mw-help-field-container\">\n" .
648 "<span class=\"mw-help-field-hint\">" . wfMsgHtml( 'config-help' ) . "</span>\n" .
649 "<span class=\"mw-help-field-data\">" . $html . "</span>\n" .
655 * @param $msg String key for wfMsg()
657 public function showHelpBox( $msg /*, ... */ ) {
658 $args = func_get_args();
659 $html = call_user_func_array( array( $this, 'getHelpBox' ), $args );
660 $this->output
->addHTML( $html );
664 * Show a short informational message.
665 * Output looks like a list.
669 public function showMessage( $msg /*, ... */ ) {
670 $args = func_get_args();
671 array_shift( $args );
672 $html = '<div class="config-message">' .
673 $this->parse( wfMsgReal( $msg, $args, false, false, false ) ) .
675 $this->output
->addHTML( $html );
679 * @param $status Status
681 public function showStatusMessage( Status
$status ) {
682 $text = $status->getWikiText();
683 $this->output
->addWikiText(
684 "<div class=\"config-message\">\n" .
691 * Label a control by wrapping a config-input div around it and putting a
696 public function label( $msg, $forId, $contents, $helpData = "" ) {
697 if ( strval( $msg ) == '' ) {
698 $labelText = ' ';
700 $labelText = wfMsgHtml( $msg );
703 $attributes = array( 'class' => 'config-label' );
706 $attributes['for'] = $forId;
710 "<div class=\"config-block\">\n" .
711 " <div class=\"config-block-label\">\n" .
714 $labelText ) . "\n" .
717 " <div class=\"config-block-elements\">\n" .
724 * Get a labelled text box to configure a variable.
726 * @param $params Array
728 * var: The variable to be configured (required)
729 * label: The message name for the label (required)
730 * attribs: Additional attributes for the input element (optional)
731 * controlName: The name for the input element (optional)
732 * value: The current value of the variable (optional)
733 * help: The html for the help text (optional)
737 public function getTextBox( $params ) {
738 if ( !isset( $params['controlName'] ) ) {
739 $params['controlName'] = 'config_' . $params['var'];
742 if ( !isset( $params['value'] ) ) {
743 $params['value'] = $this->getVar( $params['var'] );
746 if ( !isset( $params['attribs'] ) ) {
747 $params['attribs'] = array();
749 if ( !isset( $params['help'] ) ) {
750 $params['help'] = "";
755 $params['controlName'],
757 $params['controlName'],
758 30, // intended to be overridden by CSS
760 $params['attribs'] +
array(
761 'id' => $params['controlName'],
762 'class' => 'config-input-text',
763 'tabindex' => $this->nextTabIndex()
771 * Get a labelled textarea to configure a variable
773 * @param $params Array
775 * var: The variable to be configured (required)
776 * label: The message name for the label (required)
777 * attribs: Additional attributes for the input element (optional)
778 * controlName: The name for the input element (optional)
779 * value: The current value of the variable (optional)
780 * help: The html for the help text (optional)
784 public function getTextArea( $params ) {
785 if ( !isset( $params['controlName'] ) ) {
786 $params['controlName'] = 'config_' . $params['var'];
789 if ( !isset( $params['value'] ) ) {
790 $params['value'] = $this->getVar( $params['var'] );
793 if ( !isset( $params['attribs'] ) ) {
794 $params['attribs'] = array();
796 if ( !isset( $params['help'] ) ) {
797 $params['help'] = "";
802 $params['controlName'],
804 $params['controlName'],
808 $params['attribs'] +
array(
809 'id' => $params['controlName'],
810 'class' => 'config-input-text',
811 'tabindex' => $this->nextTabIndex()
819 * Get a labelled password box to configure a variable.
821 * Implements password hiding
822 * @param $params Array
824 * var: The variable to be configured (required)
825 * label: The message name for the label (required)
826 * attribs: Additional attributes for the input element (optional)
827 * controlName: The name for the input element (optional)
828 * value: The current value of the variable (optional)
829 * help: The html for the help text (optional)
833 public function getPasswordBox( $params ) {
834 if ( !isset( $params['value'] ) ) {
835 $params['value'] = $this->getVar( $params['var'] );
838 if ( !isset( $params['attribs'] ) ) {
839 $params['attribs'] = array();
842 $params['value'] = $this->getFakePassword( $params['value'] );
843 $params['attribs']['type'] = 'password';
845 return $this->getTextBox( $params );
849 * Get a labelled checkbox to configure a boolean variable.
851 * @param $params Array
853 * var: The variable to be configured (required)
854 * label: The message name for the label (required)
855 * attribs: Additional attributes for the input element (optional)
856 * controlName: The name for the input element (optional)
857 * value: The current value of the variable (optional)
858 * help: The html for the help text (optional)
862 public function getCheckBox( $params ) {
863 if ( !isset( $params['controlName'] ) ) {
864 $params['controlName'] = 'config_' . $params['var'];
867 if ( !isset( $params['value'] ) ) {
868 $params['value'] = $this->getVar( $params['var'] );
871 if ( !isset( $params['attribs'] ) ) {
872 $params['attribs'] = array();
874 if ( !isset( $params['help'] ) ) {
875 $params['help'] = "";
877 if( isset( $params['rawtext'] ) ) {
878 $labelText = $params['rawtext'];
880 $labelText = $this->parse( wfMsg( $params['label'] ) );
884 "<div class=\"config-input-check\">\n" .
888 $params['controlName'],
890 $params['attribs'] +
array(
891 'id' => $params['controlName'],
892 'tabindex' => $this->nextTabIndex(),
901 * Get a set of labelled radio buttons.
903 * @param $params Array
905 * var: The variable to be configured (required)
906 * label: The message name for the label (required)
907 * itemLabelPrefix: The message name prefix for the item labels (required)
908 * values: List of allowed values (required)
909 * itemAttribs Array of attribute arrays, outer key is the value name (optional)
910 * commonAttribs Attribute array applied to all items
911 * controlName: The name for the input element (optional)
912 * value: The current value of the variable (optional)
913 * help: The html for the help text (optional)
917 public function getRadioSet( $params ) {
918 if ( !isset( $params['controlName'] ) ) {
919 $params['controlName'] = 'config_' . $params['var'];
922 if ( !isset( $params['value'] ) ) {
923 $params['value'] = $this->getVar( $params['var'] );
926 if ( !isset( $params['label'] ) ) {
929 $label = $params['label'];
931 if ( !isset( $params['help'] ) ) {
932 $params['help'] = "";
935 foreach ( $params['values'] as $value ) {
936 $itemAttribs = array();
938 if ( isset( $params['commonAttribs'] ) ) {
939 $itemAttribs = $params['commonAttribs'];
942 if ( isset( $params['itemAttribs'][$value] ) ) {
943 $itemAttribs = $params['itemAttribs'][$value] +
$itemAttribs;
946 $checked = $value == $params['value'];
947 $id = $params['controlName'] . '_' . $value;
948 $itemAttribs['id'] = $id;
949 $itemAttribs['tabindex'] = $this->nextTabIndex();
953 Xml
::radio( $params['controlName'], $value, $checked, $itemAttribs ) .
955 Xml
::tags( 'label', array( 'for' => $id ), $this->parse(
956 wfMsgNoTrans( $params['itemLabelPrefix'] . strtolower( $value ) )
963 return $this->label( $label, $params['controlName'], $s, $params['help'] );
967 * Output an error or warning box using a Status object.
969 public function showStatusBox( $status ) {
970 if( !$status->isGood() ) {
971 $text = $status->getWikiText();
973 if( $status->isOk() ) {
974 $box = $this->getWarningBox( $text );
976 $box = $this->getErrorBox( $text );
979 $this->output
->addHTML( $box );
984 * Convenience function to set variables based on form data.
985 * Assumes that variables containing "password" in the name are (potentially
988 * @param $varNames Array
989 * @param $prefix String: the prefix added to variables to obtain form names
993 public function setVarsFromRequest( $varNames, $prefix = 'config_' ) {
994 $newValues = array();
996 foreach ( $varNames as $name ) {
997 $value = trim( $this->request
->getVal( $prefix . $name ) );
998 $newValues[$name] = $value;
1000 if ( $value === null ) {
1002 $this->setVar( $name, false );
1004 if ( stripos( $name, 'password' ) !== false ) {
1005 $this->setPassword( $name, $value );
1007 $this->setVar( $name, $value );
1016 * Helper for Installer::docLink()
1020 protected function getDocUrl( $page ) {
1021 $url = "{$_SERVER['PHP_SELF']}?page=" . urlencode( $page );
1023 if ( in_array( $this->currentPageName
, $this->pageSequence
) ) {
1024 $url .= '&lastPage=' . urlencode( $this->currentPageName
);
1031 * Extension tag hook for a documentation link.
1035 public function docLink( $linkText, $attribs, $parser ) {
1036 $url = $this->getDocUrl( $attribs['href'] );
1037 return '<a href="' . htmlspecialchars( $url ) . '">' .
1038 htmlspecialchars( $linkText ) .
1043 * Helper for "Download LocalSettings" link on WebInstall_Complete
1045 * @return String Html for download link
1047 public function downloadLinkHook( $text, $attribs, $parser ) {
1048 $img = Html
::element( 'img', array(
1049 'src' => '../skins/common/images/download-32.png',
1053 $anchor = Html
::rawElement( 'a',
1054 array( 'href' => $this->getURL( array( 'localsettings' => 1 ) ) ),
1055 $img . ' ' . wfMsgHtml( 'config-download-localsettings' ) );
1056 return Html
::rawElement( 'div', array( 'class' => 'config-download-link' ), $anchor );