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 if( $page->isSlow() ) {
251 $this->disableTimeLimit();
254 $result = $page->execute();
256 $this->endPageWrapper();
258 if ( $result == 'skip' ) {
259 # Page skipped without explicit submission.
260 # Skip it when we click "back" so that we don't just go forward again.
261 $this->skippedPages
[$pageName] = true;
262 $result = 'continue';
264 unset( $this->skippedPages
[$pageName] );
267 # If it was posted, the page can request a continue to the next page.
268 if ( $result === 'continue' && !$this->output
->headerDone() ) {
269 if ( $pageId !== false ) {
270 $this->happyPages
[$pageId] = true;
273 $lowestUnhappy = $this->getLowestUnhappy();
275 if ( $this->request
->getVal( 'lastPage' ) ) {
276 $nextPage = $this->request
->getVal( 'lastPage' );
277 } elseif ( $pageId !== false ) {
278 $nextPage = $this->pageSequence
[$pageId +
1];
280 $nextPage = $this->pageSequence
[$lowestUnhappy];
283 if ( array_search( $nextPage, $this->pageSequence
) > $lowestUnhappy ) {
284 $nextPage = $this->pageSequence
[$lowestUnhappy];
287 $this->output
->redirect( $this->getUrl( array( 'page' => $nextPage ) ) );
290 return $this->finish();
294 * Find the next page in sequence that hasn't been completed
297 public function getLowestUnhappy() {
298 if ( count( $this->happyPages
) == 0 ) {
301 return max( array_keys( $this->happyPages
) ) +
1;
306 * Start the PHP session. This may be called before execute() to start the PHP session.
310 public function startSession() {
311 if( wfIniGetBool( 'session.auto_start' ) ||
session_id() ) {
316 $this->phpErrors
= array();
317 set_error_handler( array( $this, 'errorHandler' ) );
319 restore_error_handler();
321 if ( $this->phpErrors
) {
322 $this->showError( 'config-session-error', $this->phpErrors
[0] );
330 * Get a hash of data identifying this MW installation.
332 * This is used by mw-config/index.php to prevent multiple installations of MW
333 * on the same cookie domain from interfering with each other.
337 public function getFingerprint() {
338 // Get the base URL of the installation
339 $url = $this->request
->getFullRequestURL();
340 if ( preg_match( '!^(.*\?)!', $url, $m) ) {
344 if ( preg_match( '!^(.*)/[^/]*/[^/]*$!', $url, $m ) ) {
345 // This... seems to try to get the base path from
346 // the /mw-config/index.php. Kinda scary though?
349 return md5( serialize( array(
350 'local path' => dirname( dirname( __FILE__
) ),
352 'version' => $GLOBALS['wgVersion']
357 * Show an error message in a box. Parameters are like wfMsg().
359 public function showError( $msg /*...*/ ) {
360 $args = func_get_args();
361 array_shift( $args );
362 $args = array_map( 'htmlspecialchars', $args );
363 $msg = wfMsgReal( $msg, $args, false, false, false );
364 $this->output
->addHTML( $this->getErrorBox( $msg ) );
368 * Temporary error handler for session start debugging.
370 public function errorHandler( $errno, $errstr ) {
371 $this->phpErrors
[] = $errstr;
375 * Clean up from execute()
379 public function finish() {
380 $this->output
->output();
382 $this->session
['happyPages'] = $this->happyPages
;
383 $this->session
['skippedPages'] = $this->skippedPages
;
384 $this->session
['settings'] = $this->settings
;
386 return $this->session
;
390 * We're restarting the installation, reset the session, happyPages, etc
392 public function reset() {
393 $this->session
= array();
394 $this->happyPages
= array();
395 $this->settings
= array();
399 * Get a URL for submission back to the same script.
401 * @param $query array
404 public function getUrl( $query = array() ) {
405 $url = $this->request
->getRequestURL();
406 # Remove existing query
407 $url = preg_replace( '/\?.*$/', '', $url );
410 $url .= '?' . wfArrayToCGI( $query );
417 * Get a WebInstallerPage by name.
419 * @param $pageName String
420 * @return WebInstallerPage
422 public function getPageByName( $pageName ) {
423 $pageClass = 'WebInstaller_' . $pageName;
425 return new $pageClass( $this );
429 * Get a session variable.
431 * @param $name String
434 public function getSession( $name, $default = null ) {
435 if ( !isset( $this->session
[$name] ) ) {
438 return $this->session
[$name];
443 * Set a session variable.
444 * @param $name String key for the variable
445 * @param $value Mixed
447 public function setSession( $name, $value ) {
448 $this->session
[$name] = $value;
452 * Get the next tabindex attribute value.
455 public function nextTabIndex() {
456 return $this->tabIndex++
;
460 * Initializes language-related variables.
462 public function setupLanguage() {
463 global $wgLang, $wgContLang, $wgLanguageCode;
465 if ( $this->getSession( 'test' ) === null && !$this->request
->wasPosted() ) {
466 $wgLanguageCode = $this->getAcceptLanguage();
467 $wgLang = $wgContLang = Language
::factory( $wgLanguageCode );
468 $this->setVar( 'wgLanguageCode', $wgLanguageCode );
469 $this->setVar( '_UserLang', $wgLanguageCode );
471 $wgLanguageCode = $this->getVar( 'wgLanguageCode' );
472 $wgLang = Language
::factory( $this->getVar( '_UserLang' ) );
473 $wgContLang = Language
::factory( $wgLanguageCode );
478 * Retrieves MediaWiki language from Accept-Language HTTP header.
482 public function getAcceptLanguage() {
483 global $wgLanguageCode, $wgRequest;
485 $mwLanguages = Language
::getLanguageNames();
486 $headerLanguages = array_keys( $wgRequest->getAcceptLang() );
488 foreach ( $headerLanguages as $lang ) {
489 if ( isset( $mwLanguages[$lang] ) ) {
494 return $wgLanguageCode;
498 * Called by execute() before page output starts, to show a page list.
500 * @param $currentPageName String
502 private function startPageWrapper( $currentPageName ) {
503 $s = "<div class=\"config-page-wrapper\">\n";
504 $s .= "<div class=\"config-page\">\n";
505 $s .= "<div class=\"config-page-list\"><ul>\n";
508 foreach ( $this->pageSequence
as $id => $pageName ) {
509 $happy = !empty( $this->happyPages
[$id] );
510 $s .= $this->getPageListItem(
512 $happy ||
$lastHappy == $id - 1,
521 $s .= "</ul><br/><ul>\n";
522 $s .= $this->getPageListItem( 'Restart', true, $currentPageName );
523 $s .= "</ul></div>\n"; // end list pane
524 $s .= Html
::element( 'h2', array(),
525 wfMsg( 'config-page-' . strtolower( $currentPageName ) ) );
527 $this->output
->addHTMLNoFlush( $s );
531 * Get a list item for the page list.
533 * @param $pageName String
534 * @param $enabled Boolean
535 * @param $currentPageName String
539 private function getPageListItem( $pageName, $enabled, $currentPageName ) {
540 $s = "<li class=\"config-page-list-item\">";
541 $name = wfMsg( 'config-page-' . strtolower( $pageName ) );
544 $query = array( 'page' => $pageName );
546 if ( !in_array( $pageName, $this->pageSequence
) ) {
547 if ( in_array( $currentPageName, $this->pageSequence
) ) {
548 $query['lastPage'] = $currentPageName;
551 $link = Html
::element( 'a',
553 'href' => $this->getUrl( $query )
558 $link = htmlspecialchars( $name );
561 if ( $pageName == $currentPageName ) {
562 $s .= "<span class=\"config-page-current\">$link</span>";
567 $s .= Html
::element( 'span',
569 'class' => 'config-page-disabled'
581 * Output some stuff after a page is finished.
583 private function endPageWrapper() {
584 $this->output
->addHTMLNoFlush(
585 "<div class=\"visualClear\"></div>\n" .
587 "<div class=\"visualClear\"></div>\n" .
592 * Get HTML for an error box with an icon.
594 * @param $text String: wikitext, get this with wfMsgNoTrans()
598 public function getErrorBox( $text ) {
599 return $this->getInfoBox( $text, 'critical-32.png', 'config-error-box' );
603 * Get HTML for a warning box with an icon.
605 * @param $text String: wikitext, get this with wfMsgNoTrans()
609 public function getWarningBox( $text ) {
610 return $this->getInfoBox( $text, 'warning-32.png', 'config-warning-box' );
614 * Get HTML for an info box with an icon.
616 * @param $text String: wikitext, get this with wfMsgNoTrans()
617 * @param $icon String: icon name, file in skins/common/images
618 * @param $class String: additional class name to add to the wrapper div
622 public function getInfoBox( $text, $icon = false, $class = false ) {
623 $text = $this->parse( $text, true );
624 $icon = ( $icon == false ) ?
'../skins/common/images/info-32.png' : '../skins/common/images/'.$icon;
625 $alt = wfMsg( 'config-information' );
626 return Html
::infoBox( $text, $icon, $alt, $class, false );
630 * Get small text indented help for a preceding form field.
631 * Parameters like wfMsg().
635 public function getHelpBox( $msg /*, ... */ ) {
636 $args = func_get_args();
637 array_shift( $args );
638 $args = array_map( 'htmlspecialchars', $args );
639 $text = wfMsgReal( $msg, $args, false, false, false );
640 $html = htmlspecialchars( $text );
641 $html = $this->parse( $text, true );
643 return "<div class=\"mw-help-field-container\">\n" .
644 "<span class=\"mw-help-field-hint\">" . wfMsgHtml( 'config-help' ) . "</span>\n" .
645 "<span class=\"mw-help-field-data\">" . $html . "</span>\n" .
651 * @param $msg String key for wfMsg()
653 public function showHelpBox( $msg /*, ... */ ) {
654 $args = func_get_args();
655 $html = call_user_func_array( array( $this, 'getHelpBox' ), $args );
656 $this->output
->addHTML( $html );
660 * Show a short informational message.
661 * Output looks like a list.
665 public function showMessage( $msg /*, ... */ ) {
666 $args = func_get_args();
667 array_shift( $args );
668 $html = '<div class="config-message">' .
669 $this->parse( wfMsgReal( $msg, $args, false, false, false ) ) .
671 $this->output
->addHTML( $html );
675 * @param $status Status
677 public function showStatusMessage( Status
$status ) {
678 $text = $status->getWikiText();
679 $this->output
->addWikiText(
680 "<div class=\"config-message\">\n" .
687 * Label a control by wrapping a config-input div around it and putting a
692 public function label( $msg, $forId, $contents, $helpData = "" ) {
693 if ( strval( $msg ) == '' ) {
694 $labelText = ' ';
696 $labelText = wfMsgHtml( $msg );
699 $attributes = array( 'class' => 'config-label' );
702 $attributes['for'] = $forId;
706 "<div class=\"config-block\">\n" .
707 " <div class=\"config-block-label\">\n" .
710 $labelText ) . "\n" .
713 " <div class=\"config-block-elements\">\n" .
720 * Get a labelled text box to configure a variable.
722 * @param $params Array
724 * var: The variable to be configured (required)
725 * label: The message name for the label (required)
726 * attribs: Additional attributes for the input element (optional)
727 * controlName: The name for the input element (optional)
728 * value: The current value of the variable (optional)
729 * help: The html for the help text (optional)
733 public function getTextBox( $params ) {
734 if ( !isset( $params['controlName'] ) ) {
735 $params['controlName'] = 'config_' . $params['var'];
738 if ( !isset( $params['value'] ) ) {
739 $params['value'] = $this->getVar( $params['var'] );
742 if ( !isset( $params['attribs'] ) ) {
743 $params['attribs'] = array();
745 if ( !isset( $params['help'] ) ) {
746 $params['help'] = "";
751 $params['controlName'],
753 $params['controlName'],
754 30, // intended to be overridden by CSS
756 $params['attribs'] +
array(
757 'id' => $params['controlName'],
758 'class' => 'config-input-text',
759 'tabindex' => $this->nextTabIndex()
767 * Get a labelled textarea to configure a variable
769 * @param $params Array
771 * var: The variable to be configured (required)
772 * label: The message name for the label (required)
773 * attribs: Additional attributes for the input element (optional)
774 * controlName: The name for the input element (optional)
775 * value: The current value of the variable (optional)
776 * help: The html for the help text (optional)
780 public function getTextArea( $params ) {
781 if ( !isset( $params['controlName'] ) ) {
782 $params['controlName'] = 'config_' . $params['var'];
785 if ( !isset( $params['value'] ) ) {
786 $params['value'] = $this->getVar( $params['var'] );
789 if ( !isset( $params['attribs'] ) ) {
790 $params['attribs'] = array();
792 if ( !isset( $params['help'] ) ) {
793 $params['help'] = "";
798 $params['controlName'],
800 $params['controlName'],
804 $params['attribs'] +
array(
805 'id' => $params['controlName'],
806 'class' => 'config-input-text',
807 'tabindex' => $this->nextTabIndex()
815 * Get a labelled password box to configure a variable.
817 * Implements password hiding
818 * @param $params Array
820 * var: The variable to be configured (required)
821 * label: The message name for the label (required)
822 * attribs: Additional attributes for the input element (optional)
823 * controlName: The name for the input element (optional)
824 * value: The current value of the variable (optional)
825 * help: The html for the help text (optional)
829 public function getPasswordBox( $params ) {
830 if ( !isset( $params['value'] ) ) {
831 $params['value'] = $this->getVar( $params['var'] );
834 if ( !isset( $params['attribs'] ) ) {
835 $params['attribs'] = array();
838 $params['value'] = $this->getFakePassword( $params['value'] );
839 $params['attribs']['type'] = 'password';
841 return $this->getTextBox( $params );
845 * Get a labelled checkbox to configure a boolean variable.
847 * @param $params Array
849 * var: The variable to be configured (required)
850 * label: The message name for the label (required)
851 * attribs: Additional attributes for the input element (optional)
852 * controlName: The name for the input element (optional)
853 * value: The current value of the variable (optional)
854 * help: The html for the help text (optional)
858 public function getCheckBox( $params ) {
859 if ( !isset( $params['controlName'] ) ) {
860 $params['controlName'] = 'config_' . $params['var'];
863 if ( !isset( $params['value'] ) ) {
864 $params['value'] = $this->getVar( $params['var'] );
867 if ( !isset( $params['attribs'] ) ) {
868 $params['attribs'] = array();
870 if ( !isset( $params['help'] ) ) {
871 $params['help'] = "";
873 if( isset( $params['rawtext'] ) ) {
874 $labelText = $params['rawtext'];
876 $labelText = $this->parse( wfMsg( $params['label'] ) );
880 "<div class=\"config-input-check\">\n" .
884 $params['controlName'],
886 $params['attribs'] +
array(
887 'id' => $params['controlName'],
888 'tabindex' => $this->nextTabIndex(),
897 * Get a set of labelled radio buttons.
899 * @param $params Array
901 * var: The variable to be configured (required)
902 * label: The message name for the label (required)
903 * itemLabelPrefix: The message name prefix for the item labels (required)
904 * values: List of allowed values (required)
905 * itemAttribs Array of attribute arrays, outer key is the value name (optional)
906 * commonAttribs Attribute array applied to all items
907 * controlName: The name for the input element (optional)
908 * value: The current value of the variable (optional)
909 * help: The html for the help text (optional)
913 public function getRadioSet( $params ) {
914 if ( !isset( $params['controlName'] ) ) {
915 $params['controlName'] = 'config_' . $params['var'];
918 if ( !isset( $params['value'] ) ) {
919 $params['value'] = $this->getVar( $params['var'] );
922 if ( !isset( $params['label'] ) ) {
925 $label = $params['label'];
927 if ( !isset( $params['help'] ) ) {
928 $params['help'] = "";
931 foreach ( $params['values'] as $value ) {
932 $itemAttribs = array();
934 if ( isset( $params['commonAttribs'] ) ) {
935 $itemAttribs = $params['commonAttribs'];
938 if ( isset( $params['itemAttribs'][$value] ) ) {
939 $itemAttribs = $params['itemAttribs'][$value] +
$itemAttribs;
942 $checked = $value == $params['value'];
943 $id = $params['controlName'] . '_' . $value;
944 $itemAttribs['id'] = $id;
945 $itemAttribs['tabindex'] = $this->nextTabIndex();
949 Xml
::radio( $params['controlName'], $value, $checked, $itemAttribs ) .
951 Xml
::tags( 'label', array( 'for' => $id ), $this->parse(
952 wfMsgNoTrans( $params['itemLabelPrefix'] . strtolower( $value ) )
959 return $this->label( $label, $params['controlName'], $s, $params['help'] );
963 * Output an error or warning box using a Status object.
965 * @param $status Status
967 public function showStatusBox( $status ) {
968 if( !$status->isGood() ) {
969 $text = $status->getWikiText();
971 if( $status->isOk() ) {
972 $box = $this->getWarningBox( $text );
974 $box = $this->getErrorBox( $text );
977 $this->output
->addHTML( $box );
982 * Convenience function to set variables based on form data.
983 * Assumes that variables containing "password" in the name are (potentially
986 * @param $varNames Array
987 * @param $prefix String: the prefix added to variables to obtain form names
991 public function setVarsFromRequest( $varNames, $prefix = 'config_' ) {
992 $newValues = array();
994 foreach ( $varNames as $name ) {
995 $value = trim( $this->request
->getVal( $prefix . $name ) );
996 $newValues[$name] = $value;
998 if ( $value === null ) {
1000 $this->setVar( $name, false );
1002 if ( stripos( $name, 'password' ) !== false ) {
1003 $this->setPassword( $name, $value );
1005 $this->setVar( $name, $value );
1014 * Helper for Installer::docLink()
1018 protected function getDocUrl( $page ) {
1019 $url = "{$_SERVER['PHP_SELF']}?page=" . urlencode( $page );
1021 if ( in_array( $this->currentPageName
, $this->pageSequence
) ) {
1022 $url .= '&lastPage=' . urlencode( $this->currentPageName
);
1029 * Extension tag hook for a documentation link.
1033 public function docLink( $linkText, $attribs, $parser ) {
1034 $url = $this->getDocUrl( $attribs['href'] );
1035 return '<a href="' . htmlspecialchars( $url ) . '">' .
1036 htmlspecialchars( $linkText ) .
1041 * Helper for "Download LocalSettings" link on WebInstall_Complete
1043 * @return String Html for download link
1045 public function downloadLinkHook( $text, $attribs, $parser ) {
1046 $img = Html
::element( 'img', array(
1047 'src' => '../skins/common/images/download-32.png',
1051 $anchor = Html
::rawElement( 'a',
1052 array( 'href' => $this->getURL( array( 'localsettings' => 1 ) ) ),
1053 $img . ' ' . wfMsgHtml( 'config-download-localsettings' ) );
1054 return Html
::rawElement( 'div', array( 'class' => 'config-download-link' ), $anchor );