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 );
150 $rightsProfile = $this->rightsProfiles[$this->getVar( '_RightsProfile' )];
151 foreach( $rightsProfile as $group => $rightsArr ) {
152 $ls->setGroupRights( $group, $rightsArr );
155 return $this->session;
158 $cssDir = $this->request->getVal( 'css' );
160 $cssDir = ( $cssDir == 'rtl' ? 'rtl' : 'ltr' );
161 $this->request->response()->header( 'Content-type: text/css' );
162 echo $this->output->getCSS( $cssDir );
163 return $this->session;
166 if ( isset( $session['happyPages'] ) ) {
167 $this->happyPages = $session['happyPages'];
169 $this->happyPages = array();
172 if ( isset( $session['skippedPages'] ) ) {
173 $this->skippedPages = $session['skippedPages'];
175 $this->skippedPages = array();
178 $lowestUnhappy = $this->getLowestUnhappy();
180 # Special case for Creative Commons partner chooser box.
181 if ( $this->request->getVal( 'SubmitCC' ) ) {
182 $page = $this->getPageByName( 'Options' );
183 $this->output->useShortHeader();
184 $this->output->allowFrames();
186 return $this->finish();
189 if ( $this->request->getVal( 'ShowCC' ) ) {
190 $page = $this->getPageByName( 'Options' );
191 $this->output->useShortHeader();
192 $this->output->allowFrames();
193 $this->output->addHTML( $page->getCCDoneBox() );
194 return $this->finish();
198 $pageName = $this->request->getVal( 'page' );
200 if ( in_array( $pageName, $this->otherPages ) ) {
203 $page = $this->getPageByName( $pageName );
206 if ( !$pageName || !in_array( $pageName, $this->pageSequence ) ) {
207 $pageId = $lowestUnhappy;
209 $pageId = array_search( $pageName, $this->pageSequence );
212 # If necessary, move back to the lowest-numbered unhappy page
213 if ( $pageId > $lowestUnhappy ) {
214 $pageId = $lowestUnhappy;
215 if ( $lowestUnhappy == 0 ) {
216 # Knocked back to start, possible loss of session data.
217 $this->showSessionWarning = true;
221 $pageName = $this->pageSequence[$pageId];
222 $page = $this->getPageByName( $pageName );
225 # If a back button was submitted, go back without submitting the form data.
226 if ( $this->request->wasPosted() && $this->request->getBool( 'submit-back' ) ) {
227 if ( $this->request->getVal( 'lastPage' ) ) {
228 $nextPage = $this->request->getVal( 'lastPage' );
229 } elseif ( $pageId !== false ) {
231 # Skip the skipped pages
232 $nextPageId = $pageId;
236 $nextPage = $this->pageSequence[$nextPageId];
237 } while( isset( $this->skippedPages[$nextPage] ) );
239 $nextPage = $this->pageSequence[$lowestUnhappy];
242 $this->output->redirect( $this->getUrl( array( 'page' => $nextPage ) ) );
243 return $this->finish();
247 $this->currentPageName = $page->getName();
248 $this->startPageWrapper( $pageName );
250 $result = $page->execute();
252 $this->endPageWrapper();
254 if ( $result == 'skip' ) {
255 # Page skipped without explicit submission.
256 # Skip it when we click "back" so that we don't just go forward again.
257 $this->skippedPages[$pageName] = true;
258 $result = 'continue';
260 unset( $this->skippedPages[$pageName] );
263 # If it was posted, the page can request a continue to the next page.
264 if ( $result === 'continue' && !$this->output->headerDone() ) {
265 if ( $pageId !== false ) {
266 $this->happyPages[$pageId] = true;
269 $lowestUnhappy = $this->getLowestUnhappy();
271 if ( $this->request->getVal( 'lastPage' ) ) {
272 $nextPage = $this->request->getVal( 'lastPage' );
273 } elseif ( $pageId !== false ) {
274 $nextPage = $this->pageSequence[$pageId + 1];
276 $nextPage = $this->pageSequence[$lowestUnhappy];
279 if ( array_search( $nextPage, $this->pageSequence ) > $lowestUnhappy ) {
280 $nextPage = $this->pageSequence[$lowestUnhappy];
283 $this->output->redirect( $this->getUrl( array( 'page' => $nextPage ) ) );
286 return $this->finish();
290 * Find the next page in sequence that hasn't been completed
293 public function getLowestUnhappy() {
294 if ( count( $this->happyPages ) == 0 ) {
297 return max( array_keys( $this->happyPages ) ) + 1;
302 * Start the PHP session. This may be called before execute() to start the PHP session.
304 public function startSession() {
305 if( wfIniGetBool( 'session.auto_start' ) || session_id() ) {
310 $this->phpErrors = array();
311 set_error_handler( array( $this, 'errorHandler' ) );
313 restore_error_handler();
315 if ( $this->phpErrors ) {
316 $this->showError( 'config-session-error', $this->phpErrors[0] );
324 * Get a hash of data identifying this MW installation.
326 * This is used by mw-config/index.php to prevent multiple installations of MW
327 * 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 // Totally lame way to force autoload of WebInstallerPage.php
416 class_exists( 'WebInstallerPage' );
418 $pageClass = 'WebInstaller_' . $pageName;
420 return new $pageClass( $this );
424 * Get a session variable.
426 * @param $name String
429 public function getSession( $name, $default = null ) {
430 if ( !isset( $this->session[$name] ) ) {
433 return $this->session[$name];
438 * Set a session variable.
439 * @param $name String key for the variable
440 * @param $value Mixed
442 public function setSession( $name, $value ) {
443 $this->session[$name] = $value;
447 * Get the next tabindex attribute value.
450 public function nextTabIndex() {
451 return $this->tabIndex++;
455 * Initializes language-related variables.
457 public function setupLanguage() {
458 global $wgLang, $wgContLang, $wgLanguageCode;
460 if ( $this->getSession( 'test' ) === null && !$this->request->wasPosted() ) {
461 $wgLanguageCode = $this->getAcceptLanguage();
462 $wgLang = $wgContLang = Language::factory( $wgLanguageCode );
463 $this->setVar( 'wgLanguageCode', $wgLanguageCode );
464 $this->setVar( '_UserLang', $wgLanguageCode );
466 $wgLanguageCode = $this->getVar( 'wgLanguageCode' );
467 $wgLang = Language::factory( $this->getVar( '_UserLang' ) );
468 $wgContLang = Language::factory( $wgLanguageCode );
473 * Retrieves MediaWiki language from Accept-Language HTTP header.
477 public function getAcceptLanguage() {
478 global $wgLanguageCode, $wgRequest;
480 $mwLanguages = Language::getLanguageNames();
481 $headerLanguages = array_keys( $wgRequest->getAcceptLang() );
483 foreach ( $headerLanguages as $lang ) {
484 if ( isset( $mwLanguages[$lang] ) ) {
489 return $wgLanguageCode;
493 * Called by execute() before page output starts, to show a page list.
495 * @param $currentPageName String
497 private function startPageWrapper( $currentPageName ) {
498 $s = "<div class=\"config-page-wrapper\">\n";
499 $s .= "<div class=\"config-page\">\n";
500 $s .= "<div class=\"config-page-list\"><ul>\n";
503 foreach ( $this->pageSequence as $id => $pageName ) {
504 $happy = !empty( $this->happyPages[$id] );
505 $s .= $this->getPageListItem(
507 $happy || $lastHappy == $id - 1,
516 $s .= "</ul><br/><ul>\n";
517 $s .= $this->getPageListItem( 'Restart', true, $currentPageName );
518 $s .= "</ul></div>\n"; // end list pane
519 $s .= Html::element( 'h2', array(),
520 wfMsg( 'config-page-' . strtolower( $currentPageName ) ) );
522 $this->output->addHTMLNoFlush( $s );
526 * Get a list item for the page list.
528 * @param $pageName String
529 * @param $enabled Boolean
530 * @param $currentPageName String
534 private function getPageListItem( $pageName, $enabled, $currentPageName ) {
535 $s = "<li class=\"config-page-list-item\">";
536 $name = wfMsg( 'config-page-' . strtolower( $pageName ) );
539 $query = array( 'page' => $pageName );
541 if ( !in_array( $pageName, $this->pageSequence ) ) {
542 if ( in_array( $currentPageName, $this->pageSequence ) ) {
543 $query['lastPage'] = $currentPageName;
546 $link = Html::element( 'a',
548 'href' => $this->getUrl( $query )
553 $link = htmlspecialchars( $name );
556 if ( $pageName == $currentPageName ) {
557 $s .= "<span class=\"config-page-current\">$link</span>";
562 $s .= Html::element( 'span',
564 'class' => 'config-page-disabled'
576 * Output some stuff after a page is finished.
578 private function endPageWrapper() {
579 $this->output->addHTMLNoFlush(
580 "<div class=\"visualClear\"></div>\n" .
582 "<div class=\"visualClear\"></div>\n" .
587 * Get HTML for an error box with an icon.
589 * @param $text String: wikitext, get this with wfMsgNoTrans()
591 public function getErrorBox( $text ) {
592 return $this->getInfoBox( $text, 'critical-32.png', 'config-error-box' );
596 * Get HTML for a warning box with an icon.
598 * @param $text String: wikitext, get this with wfMsgNoTrans()
600 public function getWarningBox( $text ) {
601 return $this->getInfoBox( $text, 'warning-32.png', 'config-warning-box' );
605 * Get HTML for an info box with an icon.
607 * @param $text String: wikitext, get this with wfMsgNoTrans()
608 * @param $icon String: icon name, file in skins/common/images
609 * @param $class String: additional class name to add to the wrapper div
611 public function getInfoBox( $text, $icon = 'info-32.png', $class = false ) {
613 "<div class=\"config-info $class\">\n" .
614 "<div class=\"config-info-left\">\n" .
615 Html::element( 'img',
617 'src' => '../skins/common/images/' . $icon,
618 'alt' => wfMsg( 'config-information' ),
622 "<div class=\"config-info-right\">\n" .
623 $this->parse( $text, true ) . "\n" .
625 "<div style=\"clear: left;\"></div>\n" .
631 * Get small text indented help for a preceding form field.
632 * Parameters like wfMsg().
634 public function getHelpBox( $msg /*, ... */ ) {
635 $args = func_get_args();
636 array_shift( $args );
637 $args = array_map( 'htmlspecialchars', $args );
638 $text = wfMsgReal( $msg, $args, false, false, false );
639 $html = htmlspecialchars( $text );
640 $html = $this->parse( $text, true );
642 return "<div class=\"mw-help-field-container\">\n" .
643 "<span class=\"mw-help-field-hint\">" . wfMsgHtml( 'config-help' ) . "</span>\n" .
644 "<span class=\"mw-help-field-data\">" . $html . "</span>\n" .
650 * @param $msg String key for wfMsg()
652 public function showHelpBox( $msg /*, ... */ ) {
653 $args = func_get_args();
654 $html = call_user_func_array( array( $this, 'getHelpBox' ), $args );
655 $this->output->addHTML( $html );
659 * Show a short informational message.
660 * Output looks like a list.
664 public function showMessage( $msg /*, ... */ ) {
665 $args = func_get_args();
666 array_shift( $args );
667 $html = '<div class="config-message">' .
668 $this->parse( wfMsgReal( $msg, $args, false, false, false ) ) .
670 $this->output->addHTML( $html );
674 * @param $status Status
676 public function showStatusMessage( Status $status ) {
677 $text = $status->getWikiText();
678 $this->output->addWikiText(
679 "<div class=\"config-message\">\n" .
686 * Label a control by wrapping a config-input div around it and putting a
689 public function label( $msg, $forId, $contents, $helpData = "" ) {
690 if ( strval( $msg ) == '' ) {
691 $labelText = ' ';
693 $labelText = wfMsgHtml( $msg );
696 $attributes = array( 'class' => 'config-label' );
699 $attributes['for'] = $forId;
703 "<div class=\"config-block\">\n" .
704 " <div class=\"config-block-label\">\n" .
707 $labelText ) . "\n" .
710 " <div class=\"config-block-elements\">\n" .
717 * Get a labelled text box to configure a variable.
719 * @param $params Array
721 * var: The variable to be configured (required)
722 * label: The message name for the label (required)
723 * attribs: Additional attributes for the input element (optional)
724 * controlName: The name for the input element (optional)
725 * value: The current value of the variable (optional)
726 * help: The html for the help text (optional)
728 public function getTextBox( $params ) {
729 if ( !isset( $params['controlName'] ) ) {
730 $params['controlName'] = 'config_' . $params['var'];
733 if ( !isset( $params['value'] ) ) {
734 $params['value'] = $this->getVar( $params['var'] );
737 if ( !isset( $params['attribs'] ) ) {
738 $params['attribs'] = array();
740 if ( !isset( $params['help'] ) ) {
741 $params['help'] = "";
746 $params['controlName'],
748 $params['controlName'],
749 30, // intended to be overridden by CSS
751 $params['attribs'] + array(
752 'id' => $params['controlName'],
753 'class' => 'config-input-text',
754 'tabindex' => $this->nextTabIndex()
762 * Get a labelled textarea to configure a variable
764 * @param $params Array
766 * var: The variable to be configured (required)
767 * label: The message name for the label (required)
768 * attribs: Additional attributes for the input element (optional)
769 * controlName: The name for the input element (optional)
770 * value: The current value of the variable (optional)
771 * help: The html for the help text (optional)
773 public function getTextArea( $params ) {
774 if ( !isset( $params['controlName'] ) ) {
775 $params['controlName'] = 'config_' . $params['var'];
778 if ( !isset( $params['value'] ) ) {
779 $params['value'] = $this->getVar( $params['var'] );
782 if ( !isset( $params['attribs'] ) ) {
783 $params['attribs'] = array();
785 if ( !isset( $params['help'] ) ) {
786 $params['help'] = "";
791 $params['controlName'],
793 $params['controlName'],
797 $params['attribs'] + array(
798 'id' => $params['controlName'],
799 'class' => 'config-input-text',
800 'tabindex' => $this->nextTabIndex()
808 * Get a labelled password box to configure a variable.
810 * Implements password hiding
811 * @param $params Array
813 * var: The variable to be configured (required)
814 * label: The message name for the label (required)
815 * attribs: Additional attributes for the input element (optional)
816 * controlName: The name for the input element (optional)
817 * value: The current value of the variable (optional)
818 * help: The html for the help text (optional)
820 public function getPasswordBox( $params ) {
821 if ( !isset( $params['value'] ) ) {
822 $params['value'] = $this->getVar( $params['var'] );
825 if ( !isset( $params['attribs'] ) ) {
826 $params['attribs'] = array();
829 $params['value'] = $this->getFakePassword( $params['value'] );
830 $params['attribs']['type'] = 'password';
832 return $this->getTextBox( $params );
836 * Get a labelled checkbox to configure a boolean variable.
838 * @param $params Array
840 * var: The variable to be configured (required)
841 * label: The message name for the label (required)
842 * attribs: Additional attributes for the input element (optional)
843 * controlName: The name for the input element (optional)
844 * value: The current value of the variable (optional)
845 * help: The html for the help text (optional)
847 public function getCheckBox( $params ) {
848 if ( !isset( $params['controlName'] ) ) {
849 $params['controlName'] = 'config_' . $params['var'];
852 if ( !isset( $params['value'] ) ) {
853 $params['value'] = $this->getVar( $params['var'] );
856 if ( !isset( $params['attribs'] ) ) {
857 $params['attribs'] = array();
859 if ( !isset( $params['help'] ) ) {
860 $params['help'] = "";
862 if( isset( $params['rawtext'] ) ) {
863 $labelText = $params['rawtext'];
865 $labelText = $this->parse( wfMsg( $params['label'] ) );
869 "<div class=\"config-input-check\">\n" .
873 $params['controlName'],
875 $params['attribs'] + array(
876 'id' => $params['controlName'],
877 'tabindex' => $this->nextTabIndex(),
886 * Get a set of labelled radio buttons.
888 * @param $params Array
890 * var: The variable to be configured (required)
891 * label: The message name for the label (required)
892 * itemLabelPrefix: The message name prefix for the item labels (required)
893 * values: List of allowed values (required)
894 * itemAttribs Array of attribute arrays, outer key is the value name (optional)
895 * commonAttribs Attribute array applied to all items
896 * controlName: The name for the input element (optional)
897 * value: The current value of the variable (optional)
898 * help: The html for the help text (optional)
900 public function getRadioSet( $params ) {
901 if ( !isset( $params['controlName'] ) ) {
902 $params['controlName'] = 'config_' . $params['var'];
905 if ( !isset( $params['value'] ) ) {
906 $params['value'] = $this->getVar( $params['var'] );
909 if ( !isset( $params['label'] ) ) {
912 $label = $params['label'];
914 if ( !isset( $params['help'] ) ) {
915 $params['help'] = "";
918 foreach ( $params['values'] as $value ) {
919 $itemAttribs = array();
921 if ( isset( $params['commonAttribs'] ) ) {
922 $itemAttribs = $params['commonAttribs'];
925 if ( isset( $params['itemAttribs'][$value] ) ) {
926 $itemAttribs = $params['itemAttribs'][$value] + $itemAttribs;
929 $checked = $value == $params['value'];
930 $id = $params['controlName'] . '_' . $value;
931 $itemAttribs['id'] = $id;
932 $itemAttribs['tabindex'] = $this->nextTabIndex();
936 Xml::radio( $params['controlName'], $value, $checked, $itemAttribs ) .
938 Xml::tags( 'label', array( 'for' => $id ), $this->parse(
939 wfMsgNoTrans( $params['itemLabelPrefix'] . strtolower( $value ) )
946 return $this->label( $label, $params['controlName'], $s, $params['help'] );
950 * Output an error or warning box using a Status object.
952 public function showStatusBox( $status ) {
953 if( !$status->isGood() ) {
954 $text = $status->getWikiText();
956 if( $status->isOk() ) {
957 $box = $this->getWarningBox( $text );
959 $box = $this->getErrorBox( $text );
962 $this->output->addHTML( $box );
967 * Convenience function to set variables based on form data.
968 * Assumes that variables containing "password" in the name are (potentially
971 * @param $varNames Array
972 * @param $prefix String: the prefix added to variables to obtain form names
974 public function setVarsFromRequest( $varNames, $prefix = 'config_' ) {
975 $newValues = array();
977 foreach ( $varNames as $name ) {
978 $value = trim( $this->request->getVal( $prefix . $name ) );
979 $newValues[$name] = $value;
981 if ( $value === null ) {
983 $this->setVar( $name, false );
985 if ( stripos( $name, 'password' ) !== false ) {
986 $this->setPassword( $name, $value );
988 $this->setVar( $name, $value );
997 * Helper for Installer::docLink()
999 protected function getDocUrl( $page ) {
1000 $url = "{$_SERVER['PHP_SELF']}?page=" . urlencode( $page );
1002 if ( in_array( $this->currentPageName, $this->pageSequence ) ) {
1003 $url .= '&lastPage=' . urlencode( $this->currentPageName );
1010 * Extension tag hook for a documentation link.
1012 public function docLink( $linkText, $attribs, $parser ) {
1013 $url = $this->getDocUrl( $attribs['href'] );
1014 return '<a href="' . htmlspecialchars( $url ) . '">' .
1015 htmlspecialchars( $linkText ) .
1020 * Helper for "Download LocalSettings" link on WebInstall_Complete
1021 * @return String Html for download link
1023 public function downloadLinkHook( $text, $attribs, $parser ) {
1024 $img = Html::element( 'img', array(
1025 'src' => '../skins/common/images/download-32.png',
1029 $anchor = Html::rawElement( 'a',
1030 array( 'href' => $this->getURL( array( 'localsettings' => 1 ) ) ),
1031 $img . ' ' . wfMsgHtml( 'config-download-localsettings' ) );
1032 return Html::rawElement( 'div', array( 'class' => 'config-download-link' ), $anchor );