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;
86 public $currentPageName;
91 * @param WebRequest $request
93 public function __construct( WebRequest
$request ) {
94 parent
::__construct();
95 $this->output
= new WebInstallerOutput( $this );
96 $this->request
= $request;
102 * @param $session Array: initial session array
104 * @return Array: new session array
106 public function execute( array $session ) {
107 $this->session
= $session;
109 if ( isset( $session['settings'] ) ) {
110 $this->settings
= $session['settings'] +
$this->settings
;
114 $this->setupLanguage();
116 if( $this->getVar( '_InstallDone' ) && $this->request
->getVal( 'localsettings' ) )
118 $ls = new LocalSettingsGenerator( $this );
119 $this->request
->response()->header('Content-type: text/plain');
121 $this->request
->response()->header(
122 'Content-Disposition: attachment; filename="LocalSettings.php"'
126 return $this->session
;
129 if ( isset( $session['happyPages'] ) ) {
130 $this->happyPages
= $session['happyPages'];
132 $this->happyPages
= array();
135 if ( isset( $session['skippedPages'] ) ) {
136 $this->skippedPages
= $session['skippedPages'];
138 $this->skippedPages
= array();
141 $lowestUnhappy = $this->getLowestUnhappy();
143 # Special case for Creative Commons partner chooser box.
144 if ( $this->request
->getVal( 'SubmitCC' ) ) {
145 $page = $this->getPageByName( 'Options' );
146 $this->output
->useShortHeader();
148 return $this->finish();
151 if ( $this->request
->getVal( 'ShowCC' ) ) {
152 $page = $this->getPageByName( 'Options' );
153 $this->output
->useShortHeader();
154 $this->output
->addHTML( $page->getCCDoneBox() );
155 return $this->finish();
159 $pageName = $this->request
->getVal( 'page' );
161 if ( in_array( $pageName, $this->otherPages
) ) {
164 $page = $this->getPageByName( $pageName );
167 if ( !$pageName ||
!in_array( $pageName, $this->pageSequence
) ) {
168 $pageId = $lowestUnhappy;
170 $pageId = array_search( $pageName, $this->pageSequence
);
173 # If necessary, move back to the lowest-numbered unhappy page
174 if ( $pageId > $lowestUnhappy ) {
175 $pageId = $lowestUnhappy;
176 if ( $lowestUnhappy == 0 ) {
177 # Knocked back to start, possible loss of session data.
178 $this->showSessionWarning
= true;
182 $pageName = $this->pageSequence
[$pageId];
183 $page = $this->getPageByName( $pageName );
186 # If a back button was submitted, go back without submitting the form data.
187 if ( $this->request
->wasPosted() && $this->request
->getBool( 'submit-back' ) ) {
188 if ( $this->request
->getVal( 'lastPage' ) ) {
189 $nextPage = $this->request
->getVal( 'lastPage' );
190 } elseif ( $pageId !== false ) {
192 # Skip the skipped pages
193 $nextPageId = $pageId;
197 $nextPage = $this->pageSequence
[$nextPageId];
198 } while( isset( $this->skippedPages
[$nextPage] ) );
200 $nextPage = $this->pageSequence
[$lowestUnhappy];
203 $this->output
->redirect( $this->getUrl( array( 'page' => $nextPage ) ) );
204 return $this->finish();
208 $this->currentPageName
= $page->getName();
209 $this->startPageWrapper( $pageName );
210 $localSettings = $this->getLocalSettingsStatus();
212 if( !$localSettings->isGood() ) {
213 $this->showStatusBox( $localSettings );
216 $result = $page->execute();
219 $this->endPageWrapper();
221 if ( $result == 'skip' ) {
222 # Page skipped without explicit submission.
223 # Skip it when we click "back" so that we don't just go forward again.
224 $this->skippedPages
[$pageName] = true;
225 $result = 'continue';
227 unset( $this->skippedPages
[$pageName] );
230 # If it was posted, the page can request a continue to the next page.
231 if ( $result === 'continue' && !$this->output
->headerDone() ) {
232 if ( $pageId !== false ) {
233 $this->happyPages
[$pageId] = true;
236 $lowestUnhappy = $this->getLowestUnhappy();
238 if ( $this->request
->getVal( 'lastPage' ) ) {
239 $nextPage = $this->request
->getVal( 'lastPage' );
240 } elseif ( $pageId !== false ) {
241 $nextPage = $this->pageSequence
[$pageId +
1];
243 $nextPage = $this->pageSequence
[$lowestUnhappy];
246 if ( array_search( $nextPage, $this->pageSequence
) > $lowestUnhappy ) {
247 $nextPage = $this->pageSequence
[$lowestUnhappy];
250 $this->output
->redirect( $this->getUrl( array( 'page' => $nextPage ) ) );
253 return $this->finish();
256 public function getLowestUnhappy() {
257 if ( count( $this->happyPages
) == 0 ) {
260 return max( array_keys( $this->happyPages
) ) +
1;
265 * Start the PHP session. This may be called before execute() to start the PHP session.
267 public function startSession() {
268 $sessPath = $this->getSessionSavePath();
270 if( $sessPath != '' ) {
271 if( strval( ini_get( 'open_basedir' ) ) != '' ) {
272 // we need to skip the following check when open_basedir is on.
273 // The session path probably *wont* be writable by the current
274 // user, and telling them to change it is bad. Bug 23021.
275 } elseif( !is_dir( $sessPath ) ||
!is_writeable( $sessPath ) ) {
276 $this->showError( 'config-session-path-bad', $sessPath );
280 // If the path is unset it'll default to some system bit, which *probably* is ok...
281 // not sure how to actually get what will be used.
284 if( wfIniGetBool( 'session.auto_start' ) ||
session_id() ) {
289 $this->phpErrors
= array();
290 set_error_handler( array( $this, 'errorHandler' ) );
292 restore_error_handler();
294 if ( $this->phpErrors
) {
295 $this->showError( 'config-session-error', $this->phpErrors
[0] );
303 * Get the value of session.save_path
305 * Per http://www.php.net/manual/en/session.configuration.php#ini.session.save-path,
306 * this might have some additional preceding parts which need to be
311 private function getSessionSavePath() {
312 $path = ini_get( 'session.save_path' );
313 $path = ltrim( substr( $path, strrpos( $path, ';' ) ), ';');
319 * Show an error message in a box. Parameters are like wfMsg().
321 public function showError( $msg /*...*/ ) {
322 $args = func_get_args();
323 array_shift( $args );
324 $args = array_map( 'htmlspecialchars', $args );
325 $msg = wfMsgReal( $msg, $args, false, false, false );
326 $this->output
->addHTML( $this->getErrorBox( $msg ) );
330 * Temporary error handler for session start debugging.
332 public function errorHandler( $errno, $errstr ) {
333 $this->phpErrors
[] = $errstr;
337 * Clean up from execute()
341 public function finish() {
342 $this->output
->output();
344 $this->session
['happyPages'] = $this->happyPages
;
345 $this->session
['skippedPages'] = $this->skippedPages
;
346 $this->session
['settings'] = $this->settings
;
348 return $this->session
;
352 * Get a URL for submission back to the same script.
354 * @param $query: Array
356 public function getUrl( $query = array() ) {
357 $url = $this->request
->getRequestURL();
358 # Remove existing query
359 $url = preg_replace( '/\?.*$/', '', $url );
362 $url .= '?' . wfArrayToCGI( $query );
369 * Get a WebInstallerPage from the main sequence, by ID.
373 * @return WebInstallerPage
375 public function getPageById( $id ) {
376 return $this->getPageByName( $this->pageSequence
[$id] );
380 * Get a WebInstallerPage by name.
382 * @param $pageName String
384 * @return WebInstallerPage
386 public function getPageByName( $pageName ) {
387 // Totally lame way to force autoload of WebInstallerPage.php
388 class_exists( 'WebInstallerPage' );
390 $pageClass = 'WebInstaller_' . $pageName;
392 return new $pageClass( $this );
396 * Get a session variable.
398 * @param $name String
401 public function getSession( $name, $default = null ) {
402 if ( !isset( $this->session
[$name] ) ) {
405 return $this->session
[$name];
410 * Set a session variable.
412 public function setSession( $name, $value ) {
413 $this->session
[$name] = $value;
417 * Get the next tabindex attribute value.
419 public function nextTabIndex() {
420 return $this->tabIndex++
;
424 * Initializes language-related variables.
426 public function setupLanguage() {
427 global $wgLang, $wgContLang, $wgLanguageCode;
429 if ( $this->getSession( 'test' ) === null && !$this->request
->wasPosted() ) {
430 $wgLanguageCode = $this->getAcceptLanguage();
431 $wgLang = $wgContLang = Language
::factory( $wgLanguageCode );
432 $this->setVar( 'wgLanguageCode', $wgLanguageCode );
433 $this->setVar( '_UserLang', $wgLanguageCode );
435 $wgLanguageCode = $this->getVar( 'wgLanguageCode' );
436 $wgLang = Language
::factory( $this->getVar( '_UserLang' ) );
437 $wgContLang = Language
::factory( $wgLanguageCode );
442 * Retrieves MediaWiki language from Accept-Language HTTP header.
446 public function getAcceptLanguage() {
447 global $wgLanguageCode, $wgRequest;
449 $mwLanguages = Language
::getLanguageNames();
450 $headerLanguages = array_keys( $wgRequest->getAcceptLang() );
452 foreach ( $headerLanguages as $lang ) {
453 if ( isset( $mwLanguages[$lang] ) ) {
458 return $wgLanguageCode;
462 * Called by execute() before page output starts, to show a page list.
464 * @param $currentPageName String
466 public function startPageWrapper( $currentPageName ) {
467 $s = "<div class=\"config-page-wrapper\">\n" .
468 "<div class=\"config-page-list\"><ul>\n";
471 foreach ( $this->pageSequence
as $id => $pageName ) {
472 $happy = !empty( $this->happyPages
[$id] );
473 $s .= $this->getPageListItem(
475 $happy ||
$lastHappy == $id - 1,
484 $s .= "</ul><br/><ul>\n";
486 foreach ( $this->otherPages
as $pageName ) {
487 $s .= $this->getPageListItem( $pageName, true, $currentPageName );
490 $s .= "</ul></div>\n". // end list pane
491 "<div class=\"config-page\">\n" .
492 Xml
::element( 'h2', array(),
493 wfMsg( 'config-page-' . strtolower( $currentPageName ) ) );
495 $this->output
->addHTMLNoFlush( $s );
499 * Get a list item for the page list.
501 * @param $pageName String
502 * @param $enabled Boolean
503 * @param $currentPageName String
507 public function getPageListItem( $pageName, $enabled, $currentPageName ) {
508 $s = "<li class=\"config-page-list-item\">";
509 $name = wfMsg( 'config-page-' . strtolower( $pageName ) );
512 $query = array( 'page' => $pageName );
514 if ( !in_array( $pageName, $this->pageSequence
) ) {
515 if ( in_array( $currentPageName, $this->pageSequence
) ) {
516 $query['lastPage'] = $currentPageName;
519 $link = Xml
::element( 'a',
521 'href' => $this->getUrl( $query )
526 $link = htmlspecialchars( $name );
529 if ( $pageName == $currentPageName ) {
530 $s .= "<span class=\"config-page-current\">$link</span>";
535 $s .= Xml
::element( 'span',
537 'class' => 'config-page-disabled'
549 * Output some stuff after a page is finished.
551 public function endPageWrapper() {
552 $this->output
->addHTMLNoFlush(
554 "<br style=\"clear:both\"/>\n" .
559 * Get HTML for an error box with an icon.
561 * @param $text String: wikitext, get this with wfMsgNoTrans()
563 public function getErrorBox( $text ) {
564 return $this->getInfoBox( $text, 'critical-32.png', 'config-error-box' );
568 * Get HTML for a warning box with an icon.
570 * @param $text String: wikitext, get this with wfMsgNoTrans()
572 public function getWarningBox( $text ) {
573 return $this->getInfoBox( $text, 'warning-32.png', 'config-warning-box' );
577 * Get HTML for an info box with an icon.
579 * @param $text String: wikitext, get this with wfMsgNoTrans()
580 * @param $icon String: icon name, file in skins/common/images
581 * @param $class String: additional class name to add to the wrapper div
583 public function getInfoBox( $text, $icon = 'info-32.png', $class = false ) {
585 "<div class=\"config-info $class\">\n" .
586 "<div class=\"config-info-left\">\n" .
589 'src' => '../skins/common/images/' . $icon,
590 'alt' => wfMsg( 'config-information' ),
594 "<div class=\"config-info-right\">\n" .
595 $this->parse( $text ) . "\n" .
597 "<div style=\"clear: left;\"></div>\n" .
603 * Get small text indented help for a preceding form field.
604 * Parameters like wfMsg().
606 public function getHelpBox( $msg /*, ... */ ) {
607 $args = func_get_args();
608 array_shift( $args );
609 $args = array_map( 'htmlspecialchars', $args );
611 $text = wfMsgReal( $msg, $args, false, false, false );
612 $html = $this->parse( $text, true );
613 $id = $this->helpId++
;
614 $alt = wfMsg( 'help' );
617 "<div class=\"config-help-wrapper\">\n" .
618 "<div class=\"config-help-message\">\n" .
621 "<div class=\"config-show-help\">\n" .
623 wfMsgHtml( 'config-show-help' ) .
625 "<div class=\"config-hide-help\">\n" .
627 wfMsgHtml( 'config-hide-help' ) .
628 "</a></div>\n</div>\n";
634 public function showHelpBox( $msg /*, ... */ ) {
635 $args = func_get_args();
636 $html = call_user_func_array( array( $this, 'getHelpBox' ), $args );
637 $this->output
->addHTML( $html );
641 * Show a short informational message.
642 * Output looks like a list.
646 public function showMessage( $msg /*, ... */ ) {
647 $args = func_get_args();
648 array_shift( $args );
649 $html = '<div class="config-message">' .
650 $this->parse( wfMsgReal( $msg, $args, false, false, false ) ) .
652 $this->output
->addHTML( $html );
656 * @param Status $status
658 public function showStatusMessage( Status
$status ) {
659 $text = $status->getWikiText();
660 $this->output
->addWikiText(
661 "<div class=\"config-message\">\n" .
668 * Label a control by wrapping a config-input div around it and putting a
671 public function label( $msg, $forId, $contents ) {
672 if ( strval( $msg ) == '' ) {
673 $labelText = ' ';
675 $labelText = wfMsgHtml( $msg );
678 $attributes = array( 'class' => 'config-label' );
681 $attributes['for'] = $forId;
685 "<div class=\"config-input\">\n" .
688 $labelText ) . "\n" .
694 * Get a labelled text box to configure a variable.
696 * @param $params Array
698 * var: The variable to be configured (required)
699 * label: The message name for the label (required)
700 * attribs: Additional attributes for the input element (optional)
701 * controlName: The name for the input element (optional)
702 * value: The current value of the variable (optional)
704 public function getTextBox( $params ) {
705 if ( !isset( $params['controlName'] ) ) {
706 $params['controlName'] = 'config_' . $params['var'];
709 if ( !isset( $params['value'] ) ) {
710 $params['value'] = $this->getVar( $params['var'] );
713 if ( !isset( $params['attribs'] ) ) {
714 $params['attribs'] = array();
720 $params['controlName'],
722 $params['controlName'],
723 30, // intended to be overridden by CSS
725 $params['attribs'] +
array(
726 'id' => $params['controlName'],
727 'class' => 'config-input-text',
728 'tabindex' => $this->nextTabIndex()
735 * Get a labelled password box to configure a variable.
737 * Implements password hiding
738 * @param $params Array
740 * var: The variable to be configured (required)
741 * label: The message name for the label (required)
742 * attribs: Additional attributes for the input element (optional)
743 * controlName: The name for the input element (optional)
744 * value: The current value of the variable (optional)
746 public function getPasswordBox( $params ) {
747 if ( !isset( $params['value'] ) ) {
748 $params['value'] = $this->getVar( $params['var'] );
751 if ( !isset( $params['attribs'] ) ) {
752 $params['attribs'] = array();
755 $params['value'] = $this->getFakePassword( $params['value'] );
756 $params['attribs']['type'] = 'password';
758 return $this->getTextBox( $params );
762 * Get a labelled checkbox to configure a boolean 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)
772 public function getCheckBox( $params ) {
773 if ( !isset( $params['controlName'] ) ) {
774 $params['controlName'] = 'config_' . $params['var'];
777 if ( !isset( $params['value'] ) ) {
778 $params['value'] = $this->getVar( $params['var'] );
781 if ( !isset( $params['attribs'] ) ) {
782 $params['attribs'] = array();
785 if( isset( $params['rawtext'] ) ) {
786 $labelText = $params['rawtext'];
788 $labelText = $this->parse( wfMsg( $params['label'] ) );
792 "<div class=\"config-input-check\">\n" .
795 $params['controlName'],
797 $params['attribs'] +
array(
798 'id' => $params['controlName'],
799 'class' => 'config-input-text',
800 'tabindex' => $this->nextTabIndex(),
809 * Get a set of labelled radio buttons.
811 * @param $params Array
813 * var: The variable to be configured (required)
814 * label: The message name for the label (required)
815 * itemLabelPrefix: The message name prefix for the item labels (required)
816 * values: List of allowed values (required)
817 * itemAttribs Array of attribute arrays, outer key is the value name (optional)
818 * commonAttribs Attribute array applied to all items
819 * controlName: The name for the input element (optional)
820 * value: The current value of the variable (optional)
822 public function getRadioSet( $params ) {
823 if ( !isset( $params['controlName'] ) ) {
824 $params['controlName'] = 'config_' . $params['var'];
827 if ( !isset( $params['value'] ) ) {
828 $params['value'] = $this->getVar( $params['var'] );
831 if ( !isset( $params['label'] ) ) {
834 $label = $this->parse( wfMsgNoTrans( $params['label'] ) );
837 $s = "<label class=\"config-label\">\n" .
840 "<ul class=\"config-settings-block\">\n";
841 foreach ( $params['values'] as $value ) {
842 $itemAttribs = array();
844 if ( isset( $params['commonAttribs'] ) ) {
845 $itemAttribs = $params['commonAttribs'];
848 if ( isset( $params['itemAttribs'][$value] ) ) {
849 $itemAttribs = $params['itemAttribs'][$value] +
$itemAttribs;
852 $checked = $value == $params['value'];
853 $id = $params['controlName'] . '_' . $value;
854 $itemAttribs['id'] = $id;
855 $itemAttribs['tabindex'] = $this->nextTabIndex();
859 Xml
::radio( $params['controlName'], $value, $checked, $itemAttribs ) .
861 Xml
::tags( 'label', array( 'for' => $id ), $this->parse(
862 wfMsgNoTrans( $params['itemLabelPrefix'] . strtolower( $value ) )
872 * Output an error or warning box using a Status object.
874 public function showStatusBox( $status ) {
875 if( !$status->isGood() ) {
876 $text = $status->getWikiText();
878 if( $status->isOk() ) {
879 $box = $this->getWarningBox( $text );
881 $box = $this->getErrorBox( $text );
884 $this->output
->addHTML( $box );
889 * Convenience function to set variables based on form data.
890 * Assumes that variables containing "password" in the name are (potentially
893 * @param $varNames Array
894 * @param $prefix String: the prefix added to variables to obtain form names
896 public function setVarsFromRequest( $varNames, $prefix = 'config_' ) {
897 $newValues = array();
899 foreach ( $varNames as $name ) {
900 $value = trim( $this->request
->getVal( $prefix . $name ) );
901 $newValues[$name] = $value;
903 if ( $value === null ) {
905 $this->setVar( $name, false );
907 if ( stripos( $name, 'password' ) !== false ) {
908 $this->setPassword( $name, $value );
910 $this->setVar( $name, $value );
919 * Get the starting tags of a fieldset.
921 * @param $legend String: message name
923 public function getFieldsetStart( $legend ) {
924 return "\n<fieldset><legend>" . wfMsgHtml( $legend ) . "</legend>\n";
928 * Get the end tag of a fieldset.
930 public function getFieldsetEnd() {
931 return "</fieldset>\n";
935 * Helper for Installer::docLink()
937 public function getDocUrl( $page ) {
938 $url = "{$_SERVER['PHP_SELF']}?page=" . urlencode( $page );
940 if ( in_array( $this->currentPageName
, $this->pageSequence
) ) {
941 $url .= '&lastPage=' . urlencode( $this->currentPageName
);