4 * Class for the core installer web interface.
9 class WebInstaller
extends CoreInstaller
{
12 * @var WebInstallerOutput
24 * Cached session array.
31 * Captured PHP error text. Temporary.
36 * The main sequence of page names. These will be displayed in turn.
39 * * Add a config-page-<name> message
40 * * Add a WebInstaller_<name> class
42 public $pageSequence = array(
55 * Out of sequence pages, selectable by the user at any time.
57 public $otherPages = array(
62 'UpgradeDoc', // Can't use Upgrade due to Upgrade step
66 * Array of pages which have declared that they have been submitted, have validated
67 * their input, and need no further processing.
72 * List of "skipped" pages. These are pages that will automatically continue
73 * to the next page on any GET request. To avoid breaking the "back" button,
74 * they need to be skipped during a back operation.
79 * Flag indicating that session data may have been lost.
81 public $showSessionWarning = false;
85 public $currentPageName;
90 * @param WebRequest $request
92 public function __construct( WebRequest
$request ) {
93 parent
::__construct();
94 $this->output
= new WebInstallerOutput( $this );
95 $this->request
= $request;
101 * @param $session Array: initial session array
103 * @return Array: new session array
105 public function execute( array $session ) {
106 $this->session
= $session;
108 if ( isset( $session['settings'] ) ) {
109 $this->settings
= $session['settings'] +
$this->settings
;
113 $this->setupLanguage();
115 if( $this->getVar( '_InstallDone' ) && $this->request
->getVal( 'localsettings' ) )
117 $ls = new LocalSettingsGenerator( $this );
118 $this->request
->response()->header('Content-type: text/plain');
120 $this->request
->response()->header(
121 'Content-Disposition: attachment; filename="LocalSettings.php"'
125 return $this->session
;
128 if ( isset( $session['happyPages'] ) ) {
129 $this->happyPages
= $session['happyPages'];
131 $this->happyPages
= array();
134 if ( isset( $session['skippedPages'] ) ) {
135 $this->skippedPages
= $session['skippedPages'];
137 $this->skippedPages
= array();
140 $lowestUnhappy = $this->getLowestUnhappy();
142 # Special case for Creative Commons partner chooser box.
143 if ( $this->request
->getVal( 'SubmitCC' ) ) {
144 $page = $this->getPageByName( 'Options' );
145 $this->output
->useShortHeader();
147 return $this->finish();
150 if ( $this->request
->getVal( 'ShowCC' ) ) {
151 $page = $this->getPageByName( 'Options' );
152 $this->output
->useShortHeader();
153 $this->output
->addHTML( $page->getCCDoneBox() );
154 return $this->finish();
158 $pageName = $this->request
->getVal( 'page' );
160 if ( in_array( $pageName, $this->otherPages
) ) {
163 $page = $this->getPageByName( $pageName );
166 if ( !$pageName ||
!in_array( $pageName, $this->pageSequence
) ) {
167 $pageId = $lowestUnhappy;
169 $pageId = array_search( $pageName, $this->pageSequence
);
172 # If necessary, move back to the lowest-numbered unhappy page
173 if ( $pageId > $lowestUnhappy ) {
174 $pageId = $lowestUnhappy;
175 if ( $lowestUnhappy == 0 ) {
176 # Knocked back to start, possible loss of session data.
177 $this->showSessionWarning
= true;
181 $pageName = $this->pageSequence
[$pageId];
182 $page = $this->getPageByName( $pageName );
185 # If a back button was submitted, go back without submitting the form data.
186 if ( $this->request
->wasPosted() && $this->request
->getBool( 'submit-back' ) ) {
187 if ( $this->request
->getVal( 'lastPage' ) ) {
188 $nextPage = $this->request
->getVal( 'lastPage' );
189 } elseif ( $pageId !== false ) {
191 # Skip the skipped pages
192 $nextPageId = $pageId;
196 $nextPage = $this->pageSequence
[$nextPageId];
197 } while( isset( $this->skippedPages
[$nextPage] ) );
199 $nextPage = $this->pageSequence
[$lowestUnhappy];
202 $this->output
->redirect( $this->getUrl( array( 'page' => $nextPage ) ) );
203 return $this->finish();
207 $this->currentPageName
= $page->getName();
208 $this->startPageWrapper( $pageName );
209 $localSettings = $this->getLocalSettingsStatus();
211 if( !$localSettings->isGood() ) {
212 $this->showStatusBox( $localSettings );
215 $result = $page->execute();
218 $this->endPageWrapper();
220 if ( $result == 'skip' ) {
221 # Page skipped without explicit submission.
222 # Skip it when we click "back" so that we don't just go forward again.
223 $this->skippedPages
[$pageName] = true;
224 $result = 'continue';
226 unset( $this->skippedPages
[$pageName] );
229 # If it was posted, the page can request a continue to the next page.
230 if ( $result === 'continue' && !$this->output
->headerDone() ) {
231 if ( $pageId !== false ) {
232 $this->happyPages
[$pageId] = true;
235 $lowestUnhappy = $this->getLowestUnhappy();
237 if ( $this->request
->getVal( 'lastPage' ) ) {
238 $nextPage = $this->request
->getVal( 'lastPage' );
239 } elseif ( $pageId !== false ) {
240 $nextPage = $this->pageSequence
[$pageId +
1];
242 $nextPage = $this->pageSequence
[$lowestUnhappy];
245 if ( array_search( $nextPage, $this->pageSequence
) > $lowestUnhappy ) {
246 $nextPage = $this->pageSequence
[$lowestUnhappy];
249 $this->output
->redirect( $this->getUrl( array( 'page' => $nextPage ) ) );
252 return $this->finish();
255 public function getLowestUnhappy() {
256 if ( count( $this->happyPages
) == 0 ) {
259 return max( array_keys( $this->happyPages
) ) +
1;
264 * Start the PHP session. This may be called before execute() to start the PHP session.
266 public function startSession() {
267 $sessPath = $this->getSessionSavePath();
269 if( $sessPath != '' ) {
270 if( strval( ini_get( 'open_basedir' ) ) != '' ) {
271 // we need to skip the following check when open_basedir is on.
272 // The session path probably *wont* be writable by the current
273 // user, and telling them to change it is bad. Bug 23021.
274 } elseif( !is_dir( $sessPath ) ||
!is_writeable( $sessPath ) ) {
275 $this->showError( 'config-session-path-bad', $sessPath );
279 // If the path is unset it'll default to some system bit, which *probably* is ok...
280 // not sure how to actually get what will be used.
283 if( wfIniGetBool( 'session.auto_start' ) ||
session_id() ) {
288 $this->phpErrors
= array();
289 set_error_handler( array( $this, 'errorHandler' ) );
291 restore_error_handler();
293 if ( $this->phpErrors
) {
294 $this->showError( 'config-session-error', $this->phpErrors
[0] );
302 * Get the value of session.save_path
304 * Per http://www.php.net/manual/en/session.configuration.php#ini.session.save-path,
305 * this might have some additional preceding parts which need to be
310 private function getSessionSavePath() {
311 $path = ini_get( 'session.save_path' );
312 $path = ltrim( substr( $path, strrpos( $path, ';' ) ), ';');
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 from the main sequence, by ID.
372 * @return WebInstallerPage
374 public function getPageById( $id ) {
375 return $this->getPageByName( $this->pageSequence
[$id] );
379 * Get a WebInstallerPage by name.
381 * @param $pageName String
383 * @return WebInstallerPage
385 public function getPageByName( $pageName ) {
386 // Totally lame way to force autoload of WebInstallerPage.php
387 class_exists( 'WebInstallerPage' );
389 $pageClass = 'WebInstaller_' . $pageName;
391 return new $pageClass( $this );
395 * Get a session variable.
397 * @param $name String
400 public function getSession( $name, $default = null ) {
401 if ( !isset( $this->session
[$name] ) ) {
404 return $this->session
[$name];
409 * Set a session variable.
411 public function setSession( $name, $value ) {
412 $this->session
[$name] = $value;
416 * Get the next tabindex attribute value.
418 public function nextTabIndex() {
419 return $this->tabIndex++
;
423 * Initializes language-related variables.
425 public function setupLanguage() {
426 global $wgLang, $wgContLang, $wgLanguageCode;
428 if ( $this->getSession( 'test' ) === null && !$this->request
->wasPosted() ) {
429 $wgLanguageCode = $this->getAcceptLanguage();
430 $wgLang = $wgContLang = Language
::factory( $wgLanguageCode );
431 $this->setVar( 'wgLanguageCode', $wgLanguageCode );
432 $this->setVar( '_UserLang', $wgLanguageCode );
434 $wgLanguageCode = $this->getVar( 'wgLanguageCode' );
435 $wgLang = Language
::factory( $this->getVar( '_UserLang' ) );
436 $wgContLang = Language
::factory( $wgLanguageCode );
441 * Retrieves MediaWiki language from Accept-Language HTTP header.
445 public function getAcceptLanguage() {
446 global $wgLanguageCode, $wgRequest;
448 $mwLanguages = Language
::getLanguageNames();
449 $headerLanguages = array_keys( $wgRequest->getAcceptLang() );
451 foreach ( $headerLanguages as $lang ) {
452 if ( isset( $mwLanguages[$lang] ) ) {
457 return $wgLanguageCode;
461 * Called by execute() before page output starts, to show a page list.
463 * @param $currentPageName String
465 public function startPageWrapper( $currentPageName ) {
466 $s = "<div class=\"config-page-wrapper\">\n" .
467 "<div class=\"config-page-list\"><ul>\n";
470 foreach ( $this->pageSequence
as $id => $pageName ) {
471 $happy = !empty( $this->happyPages
[$id] );
472 $s .= $this->getPageListItem(
474 $happy ||
$lastHappy == $id - 1,
483 $s .= "</ul><br/><ul>\n";
485 foreach ( $this->otherPages
as $pageName ) {
486 $s .= $this->getPageListItem( $pageName, true, $currentPageName );
489 $s .= "</ul></div>\n". // end list pane
490 "<div class=\"config-page\">\n" .
491 Xml
::element( 'h2', array(),
492 wfMsg( 'config-page-' . strtolower( $currentPageName ) ) );
494 $this->output
->addHTMLNoFlush( $s );
498 * Get a list item for the page list.
500 * @param $pageName String
501 * @param $enabled Boolean
502 * @param $currentPageName String
506 public function getPageListItem( $pageName, $enabled, $currentPageName ) {
507 $s = "<li class=\"config-page-list-item\">";
508 $name = wfMsg( 'config-page-' . strtolower( $pageName ) );
511 $query = array( 'page' => $pageName );
513 if ( !in_array( $pageName, $this->pageSequence
) ) {
514 if ( in_array( $currentPageName, $this->pageSequence
) ) {
515 $query['lastPage'] = $currentPageName;
518 $link = Xml
::element( 'a',
520 'href' => $this->getUrl( $query )
525 $link = htmlspecialchars( $name );
528 if ( $pageName == $currentPageName ) {
529 $s .= "<span class=\"config-page-current\">$link</span>";
534 $s .= Xml
::element( 'span',
536 'class' => 'config-page-disabled'
548 * Output some stuff after a page is finished.
550 public function endPageWrapper() {
551 $this->output
->addHTMLNoFlush(
553 "<br style=\"clear:both\"/>\n" .
558 * Get HTML for an error box with an icon.
560 * @param $text String: wikitext, get this with wfMsgNoTrans()
562 public function getErrorBox( $text ) {
563 return $this->getInfoBox( $text, 'critical-32.png', 'config-error-box' );
567 * Get HTML for a warning box with an icon.
569 * @param $text String: wikitext, get this with wfMsgNoTrans()
571 public function getWarningBox( $text ) {
572 return $this->getInfoBox( $text, 'warning-32.png', 'config-warning-box' );
576 * Get HTML for an info box with an icon.
578 * @param $text String: wikitext, get this with wfMsgNoTrans()
579 * @param $icon String: icon name, file in skins/common/images
580 * @param $class String: additional class name to add to the wrapper div
582 public function getInfoBox( $text, $icon = 'info-32.png', $class = false ) {
584 "<div class=\"config-info $class\">\n" .
585 "<div class=\"config-info-left\">\n" .
588 'src' => '../skins/common/images/' . $icon,
589 'alt' => wfMsg( 'config-information' ),
593 "<div class=\"config-info-right\">\n" .
594 $this->parse( $text ) . "\n" .
596 "<div style=\"clear: left;\"></div>\n" .
602 * Get small text indented help for a preceding form field.
603 * Parameters like wfMsg().
605 public function getHelpBox( $msg /*, ... */ ) {
606 $args = func_get_args();
607 array_shift( $args );
608 $args = array_map( 'htmlspecialchars', $args );
609 $text = wfMsgReal( $msg, $args, false, false, false );
610 $html = $this->parse( $text, true );
613 "<div class=\"config-help-wrapper\">\n" .
614 "<div class=\"config-help-message\">\n" .
617 "<div class=\"config-show-help\">\n" .
619 wfMsgHtml( 'config-show-help' ) .
621 "<div class=\"config-hide-help\">\n" .
623 wfMsgHtml( 'config-hide-help' ) .
624 "</a></div>\n</div>\n";
630 public function showHelpBox( $msg /*, ... */ ) {
631 $args = func_get_args();
632 $html = call_user_func_array( array( $this, 'getHelpBox' ), $args );
633 $this->output
->addHTML( $html );
637 * Show a short informational message.
638 * Output looks like a list.
642 public function showMessage( $msg /*, ... */ ) {
643 $args = func_get_args();
644 array_shift( $args );
645 $html = '<div class="config-message">' .
646 $this->parse( wfMsgReal( $msg, $args, false, false, false ) ) .
648 $this->output
->addHTML( $html );
652 * @param Status $status
654 public function showStatusMessage( Status
$status ) {
655 $text = $status->getWikiText();
656 $this->output
->addWikiText(
657 "<div class=\"config-message\">\n" .
664 * Label a control by wrapping a config-input div around it and putting a
667 public function label( $msg, $forId, $contents ) {
668 if ( strval( $msg ) == '' ) {
669 $labelText = ' ';
671 $labelText = wfMsgHtml( $msg );
674 $attributes = array( 'class' => 'config-label' );
677 $attributes['for'] = $forId;
681 "<div class=\"config-input\">\n" .
684 $labelText ) . "\n" .
690 * Get a labelled text box to configure a variable.
692 * @param $params Array
694 * var: The variable to be configured (required)
695 * label: The message name for the label (required)
696 * attribs: Additional attributes for the input element (optional)
697 * controlName: The name for the input element (optional)
698 * value: The current value of the variable (optional)
700 public function getTextBox( $params ) {
701 if ( !isset( $params['controlName'] ) ) {
702 $params['controlName'] = 'config_' . $params['var'];
705 if ( !isset( $params['value'] ) ) {
706 $params['value'] = $this->getVar( $params['var'] );
709 if ( !isset( $params['attribs'] ) ) {
710 $params['attribs'] = array();
716 $params['controlName'],
718 $params['controlName'],
719 30, // intended to be overridden by CSS
721 $params['attribs'] +
array(
722 'id' => $params['controlName'],
723 'class' => 'config-input-text',
724 'tabindex' => $this->nextTabIndex()
731 * Get a labelled password box to configure a variable.
733 * Implements password hiding
734 * @param $params Array
736 * var: The variable to be configured (required)
737 * label: The message name for the label (required)
738 * attribs: Additional attributes for the input element (optional)
739 * controlName: The name for the input element (optional)
740 * value: The current value of the variable (optional)
742 public function getPasswordBox( $params ) {
743 if ( !isset( $params['value'] ) ) {
744 $params['value'] = $this->getVar( $params['var'] );
747 if ( !isset( $params['attribs'] ) ) {
748 $params['attribs'] = array();
751 $params['value'] = $this->getFakePassword( $params['value'] );
752 $params['attribs']['type'] = 'password';
754 return $this->getTextBox( $params );
758 * Get a labelled checkbox to configure a boolean variable.
760 * @param $params Array
762 * var: The variable to be configured (required)
763 * label: The message name for the label (required)
764 * attribs: Additional attributes for the input element (optional)
765 * controlName: The name for the input element (optional)
766 * value: The current value of the variable (optional)
768 public function getCheckBox( $params ) {
769 if ( !isset( $params['controlName'] ) ) {
770 $params['controlName'] = 'config_' . $params['var'];
773 if ( !isset( $params['value'] ) ) {
774 $params['value'] = $this->getVar( $params['var'] );
777 if ( !isset( $params['attribs'] ) ) {
778 $params['attribs'] = array();
781 if( isset( $params['rawtext'] ) ) {
782 $labelText = $params['rawtext'];
784 $labelText = $this->parse( wfMsg( $params['label'] ) );
788 "<div class=\"config-input-check\">\n" .
791 $params['controlName'],
793 $params['attribs'] +
array(
794 'id' => $params['controlName'],
795 'class' => 'config-input-text',
796 'tabindex' => $this->nextTabIndex(),
805 * Get a set of labelled radio buttons.
807 * @param $params Array
809 * var: The variable to be configured (required)
810 * label: The message name for the label (required)
811 * itemLabelPrefix: The message name prefix for the item labels (required)
812 * values: List of allowed values (required)
813 * itemAttribs Array of attribute arrays, outer key is the value name (optional)
814 * commonAttribs Attribute array applied to all items
815 * controlName: The name for the input element (optional)
816 * value: The current value of the variable (optional)
818 public function getRadioSet( $params ) {
819 if ( !isset( $params['controlName'] ) ) {
820 $params['controlName'] = 'config_' . $params['var'];
823 if ( !isset( $params['value'] ) ) {
824 $params['value'] = $this->getVar( $params['var'] );
827 if ( !isset( $params['label'] ) ) {
830 $label = $this->parse( wfMsgNoTrans( $params['label'] ) );
833 $s = "<label class=\"config-label\">\n" .
836 "<ul class=\"config-settings-block\">\n";
837 foreach ( $params['values'] as $value ) {
838 $itemAttribs = array();
840 if ( isset( $params['commonAttribs'] ) ) {
841 $itemAttribs = $params['commonAttribs'];
844 if ( isset( $params['itemAttribs'][$value] ) ) {
845 $itemAttribs = $params['itemAttribs'][$value] +
$itemAttribs;
848 $checked = $value == $params['value'];
849 $id = $params['controlName'] . '_' . $value;
850 $itemAttribs['id'] = $id;
851 $itemAttribs['tabindex'] = $this->nextTabIndex();
855 Xml
::radio( $params['controlName'], $value, $checked, $itemAttribs ) .
857 Xml
::tags( 'label', array( 'for' => $id ), $this->parse(
858 wfMsgNoTrans( $params['itemLabelPrefix'] . strtolower( $value ) )
868 * Output an error or warning box using a Status object.
870 public function showStatusBox( $status ) {
871 if( !$status->isGood() ) {
872 $text = $status->getWikiText();
874 if( $status->isOk() ) {
875 $box = $this->getWarningBox( $text );
877 $box = $this->getErrorBox( $text );
880 $this->output
->addHTML( $box );
885 * Convenience function to set variables based on form data.
886 * Assumes that variables containing "password" in the name are (potentially
889 * @param $varNames Array
890 * @param $prefix String: the prefix added to variables to obtain form names
892 public function setVarsFromRequest( $varNames, $prefix = 'config_' ) {
893 $newValues = array();
895 foreach ( $varNames as $name ) {
896 $value = trim( $this->request
->getVal( $prefix . $name ) );
897 $newValues[$name] = $value;
899 if ( $value === null ) {
901 $this->setVar( $name, false );
903 if ( stripos( $name, 'password' ) !== false ) {
904 $this->setPassword( $name, $value );
906 $this->setVar( $name, $value );
915 * Get the starting tags of a fieldset.
917 * @param $legend String: message name
919 public function getFieldsetStart( $legend ) {
920 return "\n<fieldset><legend>" . wfMsgHtml( $legend ) . "</legend>\n";
924 * Get the end tag of a fieldset.
926 public function getFieldsetEnd() {
927 return "</fieldset>\n";
931 * Helper for Installer::docLink()
933 public function getDocUrl( $page ) {
934 $url = "{$_SERVER['PHP_SELF']}?page=" . urlencode( $page );
936 if ( in_array( $this->currentPageName
, $this->pageSequence
) ) {
937 $url .= '&lastPage=' . urlencode( $this->currentPageName
);