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();
181 return $this->finish();
184 if ( $this->request
->getVal( 'ShowCC' ) ) {
185 $page = $this->getPageByName( 'Options' );
186 $this->output
->useShortHeader();
187 $this->output
->addHTML( $page->getCCDoneBox() );
188 return $this->finish();
192 $pageName = $this->request
->getVal( 'page' );
194 if ( in_array( $pageName, $this->otherPages
) ) {
197 $page = $this->getPageByName( $pageName );
200 if ( !$pageName ||
!in_array( $pageName, $this->pageSequence
) ) {
201 $pageId = $lowestUnhappy;
203 $pageId = array_search( $pageName, $this->pageSequence
);
206 # If necessary, move back to the lowest-numbered unhappy page
207 if ( $pageId > $lowestUnhappy ) {
208 $pageId = $lowestUnhappy;
209 if ( $lowestUnhappy == 0 ) {
210 # Knocked back to start, possible loss of session data.
211 $this->showSessionWarning
= true;
215 $pageName = $this->pageSequence
[$pageId];
216 $page = $this->getPageByName( $pageName );
219 # If a back button was submitted, go back without submitting the form data.
220 if ( $this->request
->wasPosted() && $this->request
->getBool( 'submit-back' ) ) {
221 if ( $this->request
->getVal( 'lastPage' ) ) {
222 $nextPage = $this->request
->getVal( 'lastPage' );
223 } elseif ( $pageId !== false ) {
225 # Skip the skipped pages
226 $nextPageId = $pageId;
230 $nextPage = $this->pageSequence
[$nextPageId];
231 } while( isset( $this->skippedPages
[$nextPage] ) );
233 $nextPage = $this->pageSequence
[$lowestUnhappy];
236 $this->output
->redirect( $this->getUrl( array( 'page' => $nextPage ) ) );
237 return $this->finish();
241 $this->currentPageName
= $page->getName();
242 $this->startPageWrapper( $pageName );
244 $result = $page->execute();
246 $this->endPageWrapper();
248 if ( $result == 'skip' ) {
249 # Page skipped without explicit submission.
250 # Skip it when we click "back" so that we don't just go forward again.
251 $this->skippedPages
[$pageName] = true;
252 $result = 'continue';
254 unset( $this->skippedPages
[$pageName] );
257 # If it was posted, the page can request a continue to the next page.
258 if ( $result === 'continue' && !$this->output
->headerDone() ) {
259 if ( $pageId !== false ) {
260 $this->happyPages
[$pageId] = true;
263 $lowestUnhappy = $this->getLowestUnhappy();
265 if ( $this->request
->getVal( 'lastPage' ) ) {
266 $nextPage = $this->request
->getVal( 'lastPage' );
267 } elseif ( $pageId !== false ) {
268 $nextPage = $this->pageSequence
[$pageId +
1];
270 $nextPage = $this->pageSequence
[$lowestUnhappy];
273 if ( array_search( $nextPage, $this->pageSequence
) > $lowestUnhappy ) {
274 $nextPage = $this->pageSequence
[$lowestUnhappy];
277 $this->output
->redirect( $this->getUrl( array( 'page' => $nextPage ) ) );
280 return $this->finish();
284 * Find the next page in sequence that hasn't been completed
287 public function getLowestUnhappy() {
288 if ( count( $this->happyPages
) == 0 ) {
291 return max( array_keys( $this->happyPages
) ) +
1;
296 * Start the PHP session. This may be called before execute() to start the PHP session.
298 public function startSession() {
299 if( wfIniGetBool( 'session.auto_start' ) ||
session_id() ) {
304 $this->phpErrors
= array();
305 set_error_handler( array( $this, 'errorHandler' ) );
307 restore_error_handler();
309 if ( $this->phpErrors
) {
310 $this->showError( 'config-session-error', $this->phpErrors
[0] );
318 * Get a hash of data identifying this MW installation.
320 * This is used by mw-config/index.php to prevent multiple installations of MW
321 * on the same cookie domain from interfering with each other.
323 public function getFingerprint() {
324 // Get the base URL of the installation
325 $url = $this->request
->getFullRequestURL();
326 if ( preg_match( '!^(.*)/[^/]*/[^/]*$!', $url, $m ) ) {
329 return md5( serialize( array(
330 'local path' => dirname( dirname( __FILE__
) ),
332 'version' => $GLOBALS['wgVersion']
337 * Show an error message in a box. Parameters are like wfMsg().
339 public function showError( $msg /*...*/ ) {
340 $args = func_get_args();
341 array_shift( $args );
342 $args = array_map( 'htmlspecialchars', $args );
343 $msg = wfMsgReal( $msg, $args, false, false, false );
344 $this->output
->addHTML( $this->getErrorBox( $msg ) );
348 * Temporary error handler for session start debugging.
350 public function errorHandler( $errno, $errstr ) {
351 $this->phpErrors
[] = $errstr;
355 * Clean up from execute()
359 public function finish() {
360 $this->output
->output();
362 $this->session
['happyPages'] = $this->happyPages
;
363 $this->session
['skippedPages'] = $this->skippedPages
;
364 $this->session
['settings'] = $this->settings
;
366 return $this->session
;
370 * We're restarting the installation, reset the session, happyPages, etc
372 public function reset() {
373 $this->session
= array();
374 $this->happyPages
= array();
375 $this->settings
= array();
379 * Get a URL for submission back to the same script.
381 * @param $query: Array
384 public function getUrl( $query = array() ) {
385 $url = $this->request
->getRequestURL();
386 # Remove existing query
387 $url = preg_replace( '/\?.*$/', '', $url );
390 $url .= '?' . wfArrayToCGI( $query );
397 * Get a WebInstallerPage by name.
399 * @param $pageName String
400 * @return WebInstallerPage
402 public function getPageByName( $pageName ) {
403 // Totally lame way to force autoload of WebInstallerPage.php
404 class_exists( 'WebInstallerPage' );
406 $pageClass = 'WebInstaller_' . $pageName;
408 return new $pageClass( $this );
412 * Get a session variable.
414 * @param $name String
417 public function getSession( $name, $default = null ) {
418 if ( !isset( $this->session
[$name] ) ) {
421 return $this->session
[$name];
426 * Set a session variable.
427 * @param $name String key for the variable
428 * @param $value Mixed
430 public function setSession( $name, $value ) {
431 $this->session
[$name] = $value;
435 * Get the next tabindex attribute value.
438 public function nextTabIndex() {
439 return $this->tabIndex++
;
443 * Initializes language-related variables.
445 public function setupLanguage() {
446 global $wgLang, $wgContLang, $wgLanguageCode;
448 if ( $this->getSession( 'test' ) === null && !$this->request
->wasPosted() ) {
449 $wgLanguageCode = $this->getAcceptLanguage();
450 $wgLang = $wgContLang = Language
::factory( $wgLanguageCode );
451 $this->setVar( 'wgLanguageCode', $wgLanguageCode );
452 $this->setVar( '_UserLang', $wgLanguageCode );
454 $wgLanguageCode = $this->getVar( 'wgLanguageCode' );
455 $wgLang = Language
::factory( $this->getVar( '_UserLang' ) );
456 $wgContLang = Language
::factory( $wgLanguageCode );
461 * Retrieves MediaWiki language from Accept-Language HTTP header.
465 public function getAcceptLanguage() {
466 global $wgLanguageCode, $wgRequest;
468 $mwLanguages = Language
::getLanguageNames();
469 $headerLanguages = array_keys( $wgRequest->getAcceptLang() );
471 foreach ( $headerLanguages as $lang ) {
472 if ( isset( $mwLanguages[$lang] ) ) {
477 return $wgLanguageCode;
481 * Called by execute() before page output starts, to show a page list.
483 * @param $currentPageName String
485 private function startPageWrapper( $currentPageName ) {
486 $s = "<div class=\"config-page-wrapper\">\n";
487 $s .= "<div class=\"config-page\">\n";
488 $s .= "<div class=\"config-page-list\"><ul>\n";
491 foreach ( $this->pageSequence
as $id => $pageName ) {
492 $happy = !empty( $this->happyPages
[$id] );
493 $s .= $this->getPageListItem(
495 $happy ||
$lastHappy == $id - 1,
504 $s .= "</ul><br/><ul>\n";
505 $s .= $this->getPageListItem( 'Restart', true, $currentPageName );
506 $s .= "</ul></div>\n"; // end list pane
507 $s .= Html
::element( 'h2', array(),
508 wfMsg( 'config-page-' . strtolower( $currentPageName ) ) );
510 $this->output
->addHTMLNoFlush( $s );
514 * Get a list item for the page list.
516 * @param $pageName String
517 * @param $enabled Boolean
518 * @param $currentPageName String
522 private function getPageListItem( $pageName, $enabled, $currentPageName ) {
523 $s = "<li class=\"config-page-list-item\">";
524 $name = wfMsg( 'config-page-' . strtolower( $pageName ) );
527 $query = array( 'page' => $pageName );
529 if ( !in_array( $pageName, $this->pageSequence
) ) {
530 if ( in_array( $currentPageName, $this->pageSequence
) ) {
531 $query['lastPage'] = $currentPageName;
534 $link = Html
::element( 'a',
536 'href' => $this->getUrl( $query )
541 $link = htmlspecialchars( $name );
544 if ( $pageName == $currentPageName ) {
545 $s .= "<span class=\"config-page-current\">$link</span>";
550 $s .= Html
::element( 'span',
552 'class' => 'config-page-disabled'
564 * Output some stuff after a page is finished.
566 private function endPageWrapper() {
567 $this->output
->addHTMLNoFlush(
568 "<div class=\"visualClear\"></div>\n" .
570 "<div class=\"visualClear\"></div>\n" .
575 * Get HTML for an error box with an icon.
577 * @param $text String: wikitext, get this with wfMsgNoTrans()
579 public function getErrorBox( $text ) {
580 return $this->getInfoBox( $text, 'critical-32.png', 'config-error-box' );
584 * Get HTML for a warning box with an icon.
586 * @param $text String: wikitext, get this with wfMsgNoTrans()
588 public function getWarningBox( $text ) {
589 return $this->getInfoBox( $text, 'warning-32.png', 'config-warning-box' );
593 * Get HTML for an info box with an icon.
595 * @param $text String: wikitext, get this with wfMsgNoTrans()
596 * @param $icon String: icon name, file in skins/common/images
597 * @param $class String: additional class name to add to the wrapper div
599 public function getInfoBox( $text, $icon = 'info-32.png', $class = false ) {
601 "<div class=\"config-info $class\">\n" .
602 "<div class=\"config-info-left\">\n" .
603 Html
::element( 'img',
605 'src' => '../skins/common/images/' . $icon,
606 'alt' => wfMsg( 'config-information' ),
610 "<div class=\"config-info-right\">\n" .
611 $this->parse( $text, true ) . "\n" .
613 "<div style=\"clear: left;\"></div>\n" .
619 * Get small text indented help for a preceding form field.
620 * Parameters like wfMsg().
622 public function getHelpBox( $msg /*, ... */ ) {
623 $args = func_get_args();
624 array_shift( $args );
625 $args = array_map( 'htmlspecialchars', $args );
626 $text = wfMsgReal( $msg, $args, false, false, false );
627 $html = htmlspecialchars( $text );
628 $html = $this->parse( $text, true );
630 return "<div class=\"mw-help-field-container\">\n" .
631 "<span class=\"mw-help-field-hint\">" . wfMsgHtml( 'config-help' ) . "</span>\n" .
632 "<span class=\"mw-help-field-data\">" . $html . "</span>\n" .
638 * @param $msg String key for wfMsg()
640 public function showHelpBox( $msg /*, ... */ ) {
641 $args = func_get_args();
642 $html = call_user_func_array( array( $this, 'getHelpBox' ), $args );
643 $this->output
->addHTML( $html );
647 * Show a short informational message.
648 * Output looks like a list.
652 public function showMessage( $msg /*, ... */ ) {
653 $args = func_get_args();
654 array_shift( $args );
655 $html = '<div class="config-message">' .
656 $this->parse( wfMsgReal( $msg, $args, false, false, false ) ) .
658 $this->output
->addHTML( $html );
662 * @param $status Status
664 public function showStatusMessage( Status
$status ) {
665 $text = $status->getWikiText();
666 $this->output
->addWikiText(
667 "<div class=\"config-message\">\n" .
674 * Label a control by wrapping a config-input div around it and putting a
677 public function label( $msg, $forId, $contents, $helpData = "" ) {
678 if ( strval( $msg ) == '' ) {
679 $labelText = ' ';
681 $labelText = wfMsgHtml( $msg );
684 $attributes = array( 'class' => 'config-label' );
687 $attributes['for'] = $forId;
691 "<div class=\"config-block\">\n" .
692 " <div class=\"config-block-label\">\n" .
695 $labelText ) . "\n" .
698 " <div class=\"config-block-elements\">\n" .
705 * Get a labelled text box to configure a variable.
707 * @param $params Array
709 * var: The variable to be configured (required)
710 * label: The message name for the label (required)
711 * attribs: Additional attributes for the input element (optional)
712 * controlName: The name for the input element (optional)
713 * value: The current value of the variable (optional)
714 * help: The html for the help text (optional)
716 public function getTextBox( $params ) {
717 if ( !isset( $params['controlName'] ) ) {
718 $params['controlName'] = 'config_' . $params['var'];
721 if ( !isset( $params['value'] ) ) {
722 $params['value'] = $this->getVar( $params['var'] );
725 if ( !isset( $params['attribs'] ) ) {
726 $params['attribs'] = array();
728 if ( !isset( $params['help'] ) ) {
729 $params['help'] = "";
734 $params['controlName'],
736 $params['controlName'],
737 30, // intended to be overridden by CSS
739 $params['attribs'] +
array(
740 'id' => $params['controlName'],
741 'class' => 'config-input-text',
742 'tabindex' => $this->nextTabIndex()
750 * Get a labelled textarea to configure a variable
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 getTextArea( $params ) {
762 if ( !isset( $params['controlName'] ) ) {
763 $params['controlName'] = 'config_' . $params['var'];
766 if ( !isset( $params['value'] ) ) {
767 $params['value'] = $this->getVar( $params['var'] );
770 if ( !isset( $params['attribs'] ) ) {
771 $params['attribs'] = array();
773 if ( !isset( $params['help'] ) ) {
774 $params['help'] = "";
779 $params['controlName'],
781 $params['controlName'],
785 $params['attribs'] +
array(
786 'id' => $params['controlName'],
787 'class' => 'config-input-text',
788 'tabindex' => $this->nextTabIndex()
796 * Get a labelled password box to configure a variable.
798 * Implements password hiding
799 * @param $params Array
801 * var: The variable to be configured (required)
802 * label: The message name for the label (required)
803 * attribs: Additional attributes for the input element (optional)
804 * controlName: The name for the input element (optional)
805 * value: The current value of the variable (optional)
806 * help: The html for the help text (optional)
808 public function getPasswordBox( $params ) {
809 if ( !isset( $params['value'] ) ) {
810 $params['value'] = $this->getVar( $params['var'] );
813 if ( !isset( $params['attribs'] ) ) {
814 $params['attribs'] = array();
817 $params['value'] = $this->getFakePassword( $params['value'] );
818 $params['attribs']['type'] = 'password';
820 return $this->getTextBox( $params );
824 * Get a labelled checkbox to configure a boolean variable.
826 * @param $params Array
828 * var: The variable to be configured (required)
829 * label: The message name for the label (required)
830 * attribs: Additional attributes for the input element (optional)
831 * controlName: The name for the input element (optional)
832 * value: The current value of the variable (optional)
833 * help: The html for the help text (optional)
835 public function getCheckBox( $params ) {
836 if ( !isset( $params['controlName'] ) ) {
837 $params['controlName'] = 'config_' . $params['var'];
840 if ( !isset( $params['value'] ) ) {
841 $params['value'] = $this->getVar( $params['var'] );
844 if ( !isset( $params['attribs'] ) ) {
845 $params['attribs'] = array();
847 if ( !isset( $params['help'] ) ) {
848 $params['help'] = "";
850 if( isset( $params['rawtext'] ) ) {
851 $labelText = $params['rawtext'];
853 $labelText = $this->parse( wfMsg( $params['label'] ) );
857 "<div class=\"config-input-check\">\n" .
861 $params['controlName'],
863 $params['attribs'] +
array(
864 'id' => $params['controlName'],
865 'tabindex' => $this->nextTabIndex(),
874 * Get a set of labelled radio buttons.
876 * @param $params Array
878 * var: The variable to be configured (required)
879 * label: The message name for the label (required)
880 * itemLabelPrefix: The message name prefix for the item labels (required)
881 * values: List of allowed values (required)
882 * itemAttribs Array of attribute arrays, outer key is the value name (optional)
883 * commonAttribs Attribute array applied to all items
884 * controlName: The name for the input element (optional)
885 * value: The current value of the variable (optional)
886 * help: The html for the help text (optional)
888 public function getRadioSet( $params ) {
889 if ( !isset( $params['controlName'] ) ) {
890 $params['controlName'] = 'config_' . $params['var'];
893 if ( !isset( $params['value'] ) ) {
894 $params['value'] = $this->getVar( $params['var'] );
897 if ( !isset( $params['label'] ) ) {
900 $label = $params['label'];
902 if ( !isset( $params['help'] ) ) {
903 $params['help'] = "";
906 foreach ( $params['values'] as $value ) {
907 $itemAttribs = array();
909 if ( isset( $params['commonAttribs'] ) ) {
910 $itemAttribs = $params['commonAttribs'];
913 if ( isset( $params['itemAttribs'][$value] ) ) {
914 $itemAttribs = $params['itemAttribs'][$value] +
$itemAttribs;
917 $checked = $value == $params['value'];
918 $id = $params['controlName'] . '_' . $value;
919 $itemAttribs['id'] = $id;
920 $itemAttribs['tabindex'] = $this->nextTabIndex();
924 Xml
::radio( $params['controlName'], $value, $checked, $itemAttribs ) .
926 Xml
::tags( 'label', array( 'for' => $id ), $this->parse(
927 wfMsgNoTrans( $params['itemLabelPrefix'] . strtolower( $value ) )
934 return $this->label( $label, $params['controlName'], $s, $params['help'] );
938 * Output an error or warning box using a Status object.
940 public function showStatusBox( $status ) {
941 if( !$status->isGood() ) {
942 $text = $status->getWikiText();
944 if( $status->isOk() ) {
945 $box = $this->getWarningBox( $text );
947 $box = $this->getErrorBox( $text );
950 $this->output
->addHTML( $box );
955 * Convenience function to set variables based on form data.
956 * Assumes that variables containing "password" in the name are (potentially
959 * @param $varNames Array
960 * @param $prefix String: the prefix added to variables to obtain form names
962 public function setVarsFromRequest( $varNames, $prefix = 'config_' ) {
963 $newValues = array();
965 foreach ( $varNames as $name ) {
966 $value = trim( $this->request
->getVal( $prefix . $name ) );
967 $newValues[$name] = $value;
969 if ( $value === null ) {
971 $this->setVar( $name, false );
973 if ( stripos( $name, 'password' ) !== false ) {
974 $this->setPassword( $name, $value );
976 $this->setVar( $name, $value );
985 * Helper for Installer::docLink()
987 protected function getDocUrl( $page ) {
988 $url = "{$_SERVER['PHP_SELF']}?page=" . urlencode( $page );
990 if ( in_array( $this->currentPageName
, $this->pageSequence
) ) {
991 $url .= '&lastPage=' . urlencode( $this->currentPageName
);
998 * Extension tag hook for a documentation link.
1000 public function docLink( $linkText, $attribs, $parser ) {
1001 $url = $this->getDocUrl( $attribs['href'] );
1002 return '<a href="' . htmlspecialchars( $url ) . '">' .
1003 htmlspecialchars( $linkText ) .
1008 * Helper for "Download LocalSettings" link on WebInstall_Complete
1009 * @return String Html for download link
1011 public function downloadLinkHook( $text, $attribs, $parser ) {
1012 $img = Html
::element( 'img', array(
1013 'src' => '../skins/common/images/download-32.png',
1017 $anchor = Html
::rawElement( 'a',
1018 array( 'href' => $this->getURL( array( 'localsettings' => 1 ) ) ),
1019 $img . ' ' . wfMsgHtml( 'config-download-localsettings' ) );
1020 return Html
::rawElement( 'div', array( 'class' => 'config-download-link' ), $anchor );