3 * Core installer web interface.
10 * Class for the core installer web interface.
15 class WebInstaller
extends CoreInstaller
{
18 * @var WebInstallerOutput
30 * Cached session array.
37 * Captured PHP error text. Temporary.
42 * The main sequence of page names. These will be displayed in turn.
45 * * Add a config-page-<name> message
46 * * Add a WebInstaller_<name> class
48 public $pageSequence = array(
62 * Out of sequence pages, selectable by the user at any time.
64 public $otherPages = array(
69 'UpgradeDoc', // Can't use Upgrade due to Upgrade step
73 * Array of pages which have declared that they have been submitted, have validated
74 * their input, and need no further processing.
79 * List of "skipped" pages. These are pages that will automatically continue
80 * to the next page on any GET request. To avoid breaking the "back" button,
81 * they need to be skipped during a back operation.
86 * Flag indicating that session data may have been lost.
88 public $showSessionWarning = false;
92 public $currentPageName;
97 * @param $request WebRequest
99 public function __construct( WebRequest
$request ) {
100 parent
::__construct();
101 $this->output
= new WebInstallerOutput( $this );
102 $this->request
= $request;
104 // Add parser hook for WebInstaller_Complete
106 $wgParser->setHook( 'downloadlink', array( $this, 'downloadLinkHook' ) );
112 * @param $session Array: initial session array
114 * @return Array: new session array
116 public function execute( array $session ) {
117 $this->session
= $session;
119 if ( isset( $session['settings'] ) ) {
120 $this->settings
= $session['settings'] +
$this->settings
;
124 $this->setupLanguage();
126 if( ( $this->getVar( '_InstallDone' ) ||
$this->getVar( '_UpgradeDone' ) )
127 && $this->request
->getVal( 'localsettings' ) )
129 $this->request
->response()->header( 'Content-type: application/x-httpd-php' );
130 $this->request
->response()->header(
131 'Content-Disposition: attachment; filename="LocalSettings.php"'
134 $ls = new LocalSettingsGenerator( $this );
136 return $this->session
;
139 $cssDir = $this->request
->getVal( 'css' );
141 $cssDir = ( $cssDir == 'rtl' ?
'rtl' : 'ltr' );
142 $this->request
->response()->header( 'Content-type: text/css' );
143 echo $this->output
->getCSS( $cssDir );
144 return $this->session
;
147 if ( isset( $session['happyPages'] ) ) {
148 $this->happyPages
= $session['happyPages'];
150 $this->happyPages
= array();
153 if ( isset( $session['skippedPages'] ) ) {
154 $this->skippedPages
= $session['skippedPages'];
156 $this->skippedPages
= array();
159 $lowestUnhappy = $this->getLowestUnhappy();
161 # Special case for Creative Commons partner chooser box.
162 if ( $this->request
->getVal( 'SubmitCC' ) ) {
163 $page = $this->getPageByName( 'Options' );
164 $this->output
->useShortHeader();
166 return $this->finish();
169 if ( $this->request
->getVal( 'ShowCC' ) ) {
170 $page = $this->getPageByName( 'Options' );
171 $this->output
->useShortHeader();
172 $this->output
->addHTML( $page->getCCDoneBox() );
173 return $this->finish();
177 $pageName = $this->request
->getVal( 'page' );
179 if ( in_array( $pageName, $this->otherPages
) ) {
182 $page = $this->getPageByName( $pageName );
185 if ( !$pageName ||
!in_array( $pageName, $this->pageSequence
) ) {
186 $pageId = $lowestUnhappy;
188 $pageId = array_search( $pageName, $this->pageSequence
);
191 # If necessary, move back to the lowest-numbered unhappy page
192 if ( $pageId > $lowestUnhappy ) {
193 $pageId = $lowestUnhappy;
194 if ( $lowestUnhappy == 0 ) {
195 # Knocked back to start, possible loss of session data.
196 $this->showSessionWarning
= true;
200 $pageName = $this->pageSequence
[$pageId];
201 $page = $this->getPageByName( $pageName );
204 # If a back button was submitted, go back without submitting the form data.
205 if ( $this->request
->wasPosted() && $this->request
->getBool( 'submit-back' ) ) {
206 if ( $this->request
->getVal( 'lastPage' ) ) {
207 $nextPage = $this->request
->getVal( 'lastPage' );
208 } elseif ( $pageId !== false ) {
210 # Skip the skipped pages
211 $nextPageId = $pageId;
215 $nextPage = $this->pageSequence
[$nextPageId];
216 } while( isset( $this->skippedPages
[$nextPage] ) );
218 $nextPage = $this->pageSequence
[$lowestUnhappy];
221 $this->output
->redirect( $this->getUrl( array( 'page' => $nextPage ) ) );
222 return $this->finish();
226 $this->currentPageName
= $page->getName();
227 $this->startPageWrapper( $pageName );
229 $result = $page->execute();
231 $this->endPageWrapper();
233 if ( $result == 'skip' ) {
234 # Page skipped without explicit submission.
235 # Skip it when we click "back" so that we don't just go forward again.
236 $this->skippedPages
[$pageName] = true;
237 $result = 'continue';
239 unset( $this->skippedPages
[$pageName] );
242 # If it was posted, the page can request a continue to the next page.
243 if ( $result === 'continue' && !$this->output
->headerDone() ) {
244 if ( $pageId !== false ) {
245 $this->happyPages
[$pageId] = true;
248 $lowestUnhappy = $this->getLowestUnhappy();
250 if ( $this->request
->getVal( 'lastPage' ) ) {
251 $nextPage = $this->request
->getVal( 'lastPage' );
252 } elseif ( $pageId !== false ) {
253 $nextPage = $this->pageSequence
[$pageId +
1];
255 $nextPage = $this->pageSequence
[$lowestUnhappy];
258 if ( array_search( $nextPage, $this->pageSequence
) > $lowestUnhappy ) {
259 $nextPage = $this->pageSequence
[$lowestUnhappy];
262 $this->output
->redirect( $this->getUrl( array( 'page' => $nextPage ) ) );
265 return $this->finish();
268 public function getLowestUnhappy() {
269 if ( count( $this->happyPages
) == 0 ) {
272 return max( array_keys( $this->happyPages
) ) +
1;
277 * Start the PHP session. This may be called before execute() to start the PHP session.
279 public function startSession() {
280 if( wfIniGetBool( 'session.auto_start' ) ||
session_id() ) {
285 $this->phpErrors
= array();
286 set_error_handler( array( $this, 'errorHandler' ) );
288 restore_error_handler();
290 if ( $this->phpErrors
) {
291 $this->showError( 'config-session-error', $this->phpErrors
[0] );
299 * Get a hash of data identifying this MW installation.
301 * This is used by config/index.php to prevent multiple installations of MW
302 * on the same cookie domain from interfering with each other.
304 public function getFingerprint() {
305 // Get the base URL of the installation
306 $url = $this->request
->getFullRequestURL();
307 if ( preg_match( '!^(.*)/[^/]*/[^/]*$!', $url, $m ) ) {
310 return md5( serialize( array(
311 'local path' => dirname( dirname( __FILE__
) ),
313 'version' => $GLOBALS['wgVersion']
318 * Show an error message in a box. Parameters are like wfMsg().
320 public function showError( $msg /*...*/ ) {
321 $args = func_get_args();
322 array_shift( $args );
323 $args = array_map( 'htmlspecialchars', $args );
324 $msg = wfMsgReal( $msg, $args, false, false, false );
325 $this->output
->addHTML( $this->getErrorBox( $msg ) );
329 * Temporary error handler for session start debugging.
331 public function errorHandler( $errno, $errstr ) {
332 $this->phpErrors
[] = $errstr;
336 * Clean up from execute()
340 public function finish() {
341 $this->output
->output();
343 $this->session
['happyPages'] = $this->happyPages
;
344 $this->session
['skippedPages'] = $this->skippedPages
;
345 $this->session
['settings'] = $this->settings
;
347 return $this->session
;
351 * Get a URL for submission back to the same script.
353 * @param $query: Array
355 public function getUrl( $query = array() ) {
356 $url = $this->request
->getRequestURL();
357 # Remove existing query
358 $url = preg_replace( '/\?.*$/', '', $url );
361 $url .= '?' . wfArrayToCGI( $query );
368 * Get a WebInstallerPage by name.
370 * @param $pageName String
372 * @return WebInstallerPage
374 public function getPageByName( $pageName ) {
375 // Totally lame way to force autoload of WebInstallerPage.php
376 class_exists( 'WebInstallerPage' );
378 $pageClass = 'WebInstaller_' . $pageName;
380 return new $pageClass( $this );
384 * Get a session variable.
386 * @param $name String
389 public function getSession( $name, $default = null ) {
390 if ( !isset( $this->session
[$name] ) ) {
393 return $this->session
[$name];
398 * Set a session variable.
400 public function setSession( $name, $value ) {
401 $this->session
[$name] = $value;
405 * Get the next tabindex attribute value.
407 public function nextTabIndex() {
408 return $this->tabIndex++
;
412 * Initializes language-related variables.
414 public function setupLanguage() {
415 global $wgLang, $wgContLang, $wgLanguageCode;
417 if ( $this->getSession( 'test' ) === null && !$this->request
->wasPosted() ) {
418 $wgLanguageCode = $this->getAcceptLanguage();
419 $wgLang = $wgContLang = Language
::factory( $wgLanguageCode );
420 $this->setVar( 'wgLanguageCode', $wgLanguageCode );
421 $this->setVar( '_UserLang', $wgLanguageCode );
423 $wgLanguageCode = $this->getVar( 'wgLanguageCode' );
424 $wgLang = Language
::factory( $this->getVar( '_UserLang' ) );
425 $wgContLang = Language
::factory( $wgLanguageCode );
430 * Retrieves MediaWiki language from Accept-Language HTTP header.
434 public function getAcceptLanguage() {
435 global $wgLanguageCode, $wgRequest;
437 $mwLanguages = Language
::getLanguageNames();
438 $headerLanguages = array_keys( $wgRequest->getAcceptLang() );
440 foreach ( $headerLanguages as $lang ) {
441 if ( isset( $mwLanguages[$lang] ) ) {
446 return $wgLanguageCode;
450 * Called by execute() before page output starts, to show a page list.
452 * @param $currentPageName String
454 private function startPageWrapper( $currentPageName ) {
455 $s = "<div class=\"config-page-wrapper\">\n";
456 $s .= "<div class=\"config-page\">\n";
457 $s .= "<div class=\"config-page-list\"><ul>\n";
460 foreach ( $this->pageSequence
as $id => $pageName ) {
461 $happy = !empty( $this->happyPages
[$id] );
462 $s .= $this->getPageListItem(
464 $happy ||
$lastHappy == $id - 1,
473 $s .= "</ul><br/><ul>\n";
475 foreach ( $this->otherPages
as $pageName ) {
476 $s .= $this->getPageListItem( $pageName, true, $currentPageName );
479 $s .= "</ul></div>\n"; // end list pane
480 $s .= Html
::element( 'h2', array(),
481 wfMsg( 'config-page-' . strtolower( $currentPageName ) ) );
483 $this->output
->addHTMLNoFlush( $s );
487 * Get a list item for the page list.
489 * @param $pageName String
490 * @param $enabled Boolean
491 * @param $currentPageName String
495 private function getPageListItem( $pageName, $enabled, $currentPageName ) {
496 $s = "<li class=\"config-page-list-item\">";
497 $name = wfMsg( 'config-page-' . strtolower( $pageName ) );
500 $query = array( 'page' => $pageName );
502 if ( !in_array( $pageName, $this->pageSequence
) ) {
503 if ( in_array( $currentPageName, $this->pageSequence
) ) {
504 $query['lastPage'] = $currentPageName;
507 $link = Html
::element( 'a',
509 'href' => $this->getUrl( $query )
514 $link = htmlspecialchars( $name );
517 if ( $pageName == $currentPageName ) {
518 $s .= "<span class=\"config-page-current\">$link</span>";
523 $s .= Html
::element( 'span',
525 'class' => 'config-page-disabled'
537 * Output some stuff after a page is finished.
539 private function endPageWrapper() {
540 $this->output
->addHTMLNoFlush(
541 "<div class=\"visualClear\"></div>\n" .
543 "<div class=\"visualClear\"></div>\n" .
548 * Get HTML for an error box with an icon.
550 * @param $text String: wikitext, get this with wfMsgNoTrans()
552 public function getErrorBox( $text ) {
553 return $this->getInfoBox( $text, 'critical-32.png', 'config-error-box' );
557 * Get HTML for a warning box with an icon.
559 * @param $text String: wikitext, get this with wfMsgNoTrans()
561 public function getWarningBox( $text ) {
562 return $this->getInfoBox( $text, 'warning-32.png', 'config-warning-box' );
566 * Get HTML for an info box with an icon.
568 * @param $text String: wikitext, get this with wfMsgNoTrans()
569 * @param $icon String: icon name, file in skins/common/images
570 * @param $class String: additional class name to add to the wrapper div
572 public function getInfoBox( $text, $icon = 'info-32.png', $class = false ) {
574 "<div class=\"config-info $class\">\n" .
575 "<div class=\"config-info-left\">\n" .
576 Html
::element( 'img',
578 'src' => '../skins/common/images/' . $icon,
579 'alt' => wfMsg( 'config-information' ),
583 "<div class=\"config-info-right\">\n" .
584 $this->parse( $text, true ) . "\n" .
586 "<div style=\"clear: left;\"></div>\n" .
592 * Get small text indented help for a preceding form field.
593 * Parameters like wfMsg().
595 public function getHelpBox( $msg /*, ... */ ) {
596 $args = func_get_args();
597 array_shift( $args );
598 $args = array_map( 'htmlspecialchars', $args );
599 $text = wfMsgReal( $msg, $args, false, false, false );
600 $html = htmlspecialchars( $text );
601 $html = $this->parse( $text, true );
603 return "<div class=\"mw-help-field-container\">\n" .
604 "<span class=\"mw-help-field-hint\">" . wfMsgHtml( 'config-help' ) . "</span>\n" .
605 "<span class=\"mw-help-field-data\">" . $html . "</span>\n" .
612 public function showHelpBox( $msg /*, ... */ ) {
613 $args = func_get_args();
614 $html = call_user_func_array( array( $this, 'getHelpBox' ), $args );
615 $this->output
->addHTML( $html );
619 * Show a short informational message.
620 * Output looks like a list.
624 public function showMessage( $msg /*, ... */ ) {
625 $args = func_get_args();
626 array_shift( $args );
627 $html = '<div class="config-message">' .
628 $this->parse( wfMsgReal( $msg, $args, false, false, false ) ) .
630 $this->output
->addHTML( $html );
634 * @param $status Status
636 public function showStatusMessage( Status
$status ) {
637 $text = $status->getWikiText();
638 $this->output
->addWikiText(
639 "<div class=\"config-message\">\n" .
646 * Label a control by wrapping a config-input div around it and putting a
649 public function label( $msg, $forId, $contents, $helpData = "" ) {
650 if ( strval( $msg ) == '' ) {
651 $labelText = ' ';
653 $labelText = wfMsgHtml( $msg );
656 $attributes = array( 'class' => 'config-label' );
659 $attributes['for'] = $forId;
663 "<div class=\"config-block\">\n" .
664 " <div class=\"config-block-label\">\n" .
667 $labelText ) . "\n" .
670 " <div class=\"config-block-elements\">\n" .
677 * Get a labelled text box to configure a variable.
679 * @param $params Array
681 * var: The variable to be configured (required)
682 * label: The message name for the label (required)
683 * attribs: Additional attributes for the input element (optional)
684 * controlName: The name for the input element (optional)
685 * value: The current value of the variable (optional)
686 * help: The html for the help text (optional)
688 public function getTextBox( $params ) {
689 if ( !isset( $params['controlName'] ) ) {
690 $params['controlName'] = 'config_' . $params['var'];
693 if ( !isset( $params['value'] ) ) {
694 $params['value'] = $this->getVar( $params['var'] );
697 if ( !isset( $params['attribs'] ) ) {
698 $params['attribs'] = array();
700 if ( !isset( $params['help'] ) ) {
701 $params['help'] = "";
706 $params['controlName'],
708 $params['controlName'],
709 30, // intended to be overridden by CSS
711 $params['attribs'] +
array(
712 'id' => $params['controlName'],
713 'class' => 'config-input-text',
714 'tabindex' => $this->nextTabIndex()
722 * Get a labelled password box to configure a variable.
724 * Implements password hiding
725 * @param $params Array
727 * var: The variable to be configured (required)
728 * label: The message name for the label (required)
729 * attribs: Additional attributes for the input element (optional)
730 * controlName: The name for the input element (optional)
731 * value: The current value of the variable (optional)
732 * help: The html for the help text (optional)
734 public function getPasswordBox( $params ) {
735 if ( !isset( $params['value'] ) ) {
736 $params['value'] = $this->getVar( $params['var'] );
739 if ( !isset( $params['attribs'] ) ) {
740 $params['attribs'] = array();
743 $params['value'] = $this->getFakePassword( $params['value'] );
744 $params['attribs']['type'] = 'password';
746 return $this->getTextBox( $params );
750 * Get a labelled checkbox to configure a boolean 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 getCheckBox( $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'] = "";
776 if( isset( $params['rawtext'] ) ) {
777 $labelText = $params['rawtext'];
779 $labelText = $this->parse( wfMsg( $params['label'] ) );
783 "<div class=\"config-input-check\">\n" .
787 $params['controlName'],
789 $params['attribs'] +
array(
790 'id' => $params['controlName'],
791 'tabindex' => $this->nextTabIndex(),
800 * Get a set of labelled radio buttons.
802 * @param $params Array
804 * var: The variable to be configured (required)
805 * label: The message name for the label (required)
806 * itemLabelPrefix: The message name prefix for the item labels (required)
807 * values: List of allowed values (required)
808 * itemAttribs Array of attribute arrays, outer key is the value name (optional)
809 * commonAttribs Attribute array applied to all items
810 * controlName: The name for the input element (optional)
811 * value: The current value of the variable (optional)
812 * help: The html for the help text (optional)
814 public function getRadioSet( $params ) {
815 if ( !isset( $params['controlName'] ) ) {
816 $params['controlName'] = 'config_' . $params['var'];
819 if ( !isset( $params['value'] ) ) {
820 $params['value'] = $this->getVar( $params['var'] );
823 if ( !isset( $params['label'] ) ) {
826 $label = $params['label'];
828 if ( !isset( $params['help'] ) ) {
829 $params['help'] = "";
832 foreach ( $params['values'] as $value ) {
833 $itemAttribs = array();
835 if ( isset( $params['commonAttribs'] ) ) {
836 $itemAttribs = $params['commonAttribs'];
839 if ( isset( $params['itemAttribs'][$value] ) ) {
840 $itemAttribs = $params['itemAttribs'][$value] +
$itemAttribs;
843 $checked = $value == $params['value'];
844 $id = $params['controlName'] . '_' . $value;
845 $itemAttribs['id'] = $id;
846 $itemAttribs['tabindex'] = $this->nextTabIndex();
850 Xml
::radio( $params['controlName'], $value, $checked, $itemAttribs ) .
852 Xml
::tags( 'label', array( 'for' => $id ), $this->parse(
853 wfMsgNoTrans( $params['itemLabelPrefix'] . strtolower( $value ) )
860 return $this->label( $label, $params['controlName'], $s, $params['help'] );
864 * Output an error or warning box using a Status object.
866 public function showStatusBox( $status ) {
867 if( !$status->isGood() ) {
868 $text = $status->getWikiText();
870 if( $status->isOk() ) {
871 $box = $this->getWarningBox( $text );
873 $box = $this->getErrorBox( $text );
876 $this->output
->addHTML( $box );
881 * Convenience function to set variables based on form data.
882 * Assumes that variables containing "password" in the name are (potentially
885 * @param $varNames Array
886 * @param $prefix String: the prefix added to variables to obtain form names
888 public function setVarsFromRequest( $varNames, $prefix = 'config_' ) {
889 $newValues = array();
891 foreach ( $varNames as $name ) {
892 $value = trim( $this->request
->getVal( $prefix . $name ) );
893 $newValues[$name] = $value;
895 if ( $value === null ) {
897 $this->setVar( $name, false );
899 if ( stripos( $name, 'password' ) !== false ) {
900 $this->setPassword( $name, $value );
902 $this->setVar( $name, $value );
911 * Helper for Installer::docLink()
913 protected function getDocUrl( $page ) {
914 $url = "{$_SERVER['PHP_SELF']}?page=" . urlencode( $page );
916 if ( in_array( $this->currentPageName
, $this->pageSequence
) ) {
917 $url .= '&lastPage=' . urlencode( $this->currentPageName
);
923 public function downloadLinkHook( $text, $attribs, $parser ) {
924 $img = Html
::element( 'img', array(
925 'src' => '../skins/common/images/download-32.png',
929 $anchor = Html
::rawElement( 'a',
930 array( 'href' => $this->getURL( array( 'localsettings' => 1 ) ) ),
931 $img . ' ' . wfMsgHtml( 'config-download-localsettings' ) );
932 return Html
::rawElement( 'div', array( 'class' => 'config-download-link' ), $anchor );