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(
61 * Out of sequence pages, selectable by the user at any time.
63 public $otherPages = array(
68 'UpgradeDoc', // Can't use Upgrade due to Upgrade step
72 * Array of pages which have declared that they have been submitted, have validated
73 * their input, and need no further processing.
78 * List of "skipped" pages. These are pages that will automatically continue
79 * to the next page on any GET request. To avoid breaking the "back" button,
80 * they need to be skipped during a back operation.
85 * Flag indicating that session data may have been lost.
87 public $showSessionWarning = false;
91 public $currentPageName;
96 * @param $request WebRequest
98 public function __construct( WebRequest
$request ) {
99 parent
::__construct();
100 $this->output
= new WebInstallerOutput( $this );
101 $this->request
= $request;
107 * @param $session Array: initial session array
109 * @return Array: new session array
111 public function execute( array $session ) {
112 $this->session
= $session;
114 if ( isset( $session['settings'] ) ) {
115 $this->settings
= $session['settings'] +
$this->settings
;
119 $this->setupLanguage();
121 if( ( $this->getVar( '_InstallDone' ) ||
$this->getVar( '_UpgradeDone' ) )
122 && $this->request
->getVal( 'localsettings' ) )
124 $this->request
->response()->header( 'Content-type: text/plain' );
125 $this->request
->response()->header(
126 'Content-Disposition: attachment; filename="LocalSettings.php"'
129 $ls = new LocalSettingsGenerator( $this );
131 return $this->session
;
134 $cssDir = $this->request
->getVal( 'css' );
136 $cssDir = ( $cssDir == 'rtl' ?
'rtl' : 'ltr' );
137 $this->request
->response()->header( 'Content-type: text/css' );
138 echo $this->output
->getCSS( $cssDir );
139 return $this->session
;
142 if ( isset( $session['happyPages'] ) ) {
143 $this->happyPages
= $session['happyPages'];
145 $this->happyPages
= array();
148 if ( isset( $session['skippedPages'] ) ) {
149 $this->skippedPages
= $session['skippedPages'];
151 $this->skippedPages
= array();
154 $lowestUnhappy = $this->getLowestUnhappy();
156 # Special case for Creative Commons partner chooser box.
157 if ( $this->request
->getVal( 'SubmitCC' ) ) {
158 $page = $this->getPageByName( 'Options' );
159 $this->output
->useShortHeader();
161 return $this->finish();
164 if ( $this->request
->getVal( 'ShowCC' ) ) {
165 $page = $this->getPageByName( 'Options' );
166 $this->output
->useShortHeader();
167 $this->output
->addHTML( $page->getCCDoneBox() );
168 return $this->finish();
172 $pageName = $this->request
->getVal( 'page' );
174 # Check LocalSettings status
175 $localSettings = $this->getLocalSettingsStatus();
177 if( !$localSettings->isGood() && $this->getVar( '_LocalSettingsLocked' ) ) {
178 $pageName = 'Locked';
180 $page = $this->getPageByName( $pageName );
181 $page->setLocalSettingsStatus( $localSettings );
182 } elseif ( in_array( $pageName, $this->otherPages
) ) {
185 $page = $this->getPageByName( $pageName );
188 if ( !$pageName ||
!in_array( $pageName, $this->pageSequence
) ) {
189 $pageId = $lowestUnhappy;
191 $pageId = array_search( $pageName, $this->pageSequence
);
194 # If necessary, move back to the lowest-numbered unhappy page
195 if ( $pageId > $lowestUnhappy ) {
196 $pageId = $lowestUnhappy;
197 if ( $lowestUnhappy == 0 ) {
198 # Knocked back to start, possible loss of session data.
199 $this->showSessionWarning
= true;
203 $pageName = $this->pageSequence
[$pageId];
204 $page = $this->getPageByName( $pageName );
207 # If a back button was submitted, go back without submitting the form data.
208 if ( $this->request
->wasPosted() && $this->request
->getBool( 'submit-back' ) ) {
209 if ( $this->request
->getVal( 'lastPage' ) ) {
210 $nextPage = $this->request
->getVal( 'lastPage' );
211 } elseif ( $pageId !== false ) {
213 # Skip the skipped pages
214 $nextPageId = $pageId;
218 $nextPage = $this->pageSequence
[$nextPageId];
219 } while( isset( $this->skippedPages
[$nextPage] ) );
221 $nextPage = $this->pageSequence
[$lowestUnhappy];
224 $this->output
->redirect( $this->getUrl( array( 'page' => $nextPage ) ) );
225 return $this->finish();
229 $this->currentPageName
= $page->getName();
230 $this->startPageWrapper( $pageName );
232 $result = $page->execute();
234 $this->endPageWrapper();
236 if ( $result == 'skip' ) {
237 # Page skipped without explicit submission.
238 # Skip it when we click "back" so that we don't just go forward again.
239 $this->skippedPages
[$pageName] = true;
240 $result = 'continue';
242 unset( $this->skippedPages
[$pageName] );
245 # If it was posted, the page can request a continue to the next page.
246 if ( $result === 'continue' && !$this->output
->headerDone() ) {
247 if ( $pageId !== false ) {
248 $this->happyPages
[$pageId] = true;
251 $lowestUnhappy = $this->getLowestUnhappy();
253 if ( $this->request
->getVal( 'lastPage' ) ) {
254 $nextPage = $this->request
->getVal( 'lastPage' );
255 } elseif ( $pageId !== false ) {
256 $nextPage = $this->pageSequence
[$pageId +
1];
258 $nextPage = $this->pageSequence
[$lowestUnhappy];
261 if ( array_search( $nextPage, $this->pageSequence
) > $lowestUnhappy ) {
262 $nextPage = $this->pageSequence
[$lowestUnhappy];
265 $this->output
->redirect( $this->getUrl( array( 'page' => $nextPage ) ) );
268 return $this->finish();
271 public function getLowestUnhappy() {
272 if ( count( $this->happyPages
) == 0 ) {
275 return max( array_keys( $this->happyPages
) ) +
1;
280 * Start the PHP session. This may be called before execute() to start the PHP session.
282 public function startSession() {
283 $sessPath = $this->getSessionSavePath();
285 if( $sessPath != '' ) {
286 if( strval( ini_get( 'open_basedir' ) ) != '' ) {
287 // we need to skip the following check when open_basedir is on.
288 // The session path probably *wont* be writable by the current
289 // user, and telling them to change it is bad. Bug 23021.
290 } elseif( !is_dir( $sessPath ) ||
!is_writeable( $sessPath ) ) {
291 $this->showError( 'config-session-path-bad', $sessPath );
295 // If the path is unset it'll default to some system bit, which *probably* is ok...
296 // not sure how to actually get what will be used.
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 the value of session.save_path
320 * Per http://www.php.net/manual/en/session.configuration.php#ini.session.save-path,
321 * this may have an initial integer value to indicate the depth of session
322 * storage (eg /tmp/a/b/c). Explode on ; and check and see if this part is
323 * there or not. Should also allow paths with semicolons in them (if you
324 * really wanted your session files stored in /tmp/some;dir) which PHP
325 * supposedly supports.
329 private function getSessionSavePath() {
330 $parts = explode( ';', ini_get( 'session.save_path' ), 2 );
331 return count( $parts ) == 1 ?
$parts[0] : $parts[1];
335 * Show an error message in a box. Parameters are like wfMsg().
337 public function showError( $msg /*...*/ ) {
338 $args = func_get_args();
339 array_shift( $args );
340 $args = array_map( 'htmlspecialchars', $args );
341 $msg = wfMsgReal( $msg, $args, false, false, false );
342 $this->output
->addHTML( $this->getErrorBox( $msg ) );
346 * Temporary error handler for session start debugging.
348 public function errorHandler( $errno, $errstr ) {
349 $this->phpErrors
[] = $errstr;
353 * Clean up from execute()
357 public function finish() {
358 $this->output
->output();
360 $this->session
['happyPages'] = $this->happyPages
;
361 $this->session
['skippedPages'] = $this->skippedPages
;
362 $this->session
['settings'] = $this->settings
;
364 return $this->session
;
368 * Get a URL for submission back to the same script.
370 * @param $query: Array
372 public function getUrl( $query = array() ) {
373 $url = $this->request
->getRequestURL();
374 # Remove existing query
375 $url = preg_replace( '/\?.*$/', '', $url );
378 $url .= '?' . wfArrayToCGI( $query );
385 * Get a WebInstallerPage by name.
387 * @param $pageName String
389 * @return WebInstallerPage
391 public function getPageByName( $pageName ) {
392 // Totally lame way to force autoload of WebInstallerPage.php
393 class_exists( 'WebInstallerPage' );
395 $pageClass = 'WebInstaller_' . $pageName;
397 return new $pageClass( $this );
401 * Get a session variable.
403 * @param $name String
406 public function getSession( $name, $default = null ) {
407 if ( !isset( $this->session
[$name] ) ) {
410 return $this->session
[$name];
415 * Set a session variable.
417 public function setSession( $name, $value ) {
418 $this->session
[$name] = $value;
422 * Get the next tabindex attribute value.
424 public function nextTabIndex() {
425 return $this->tabIndex++
;
429 * Initializes language-related variables.
431 public function setupLanguage() {
432 global $wgLang, $wgContLang, $wgLanguageCode;
434 if ( $this->getSession( 'test' ) === null && !$this->request
->wasPosted() ) {
435 $wgLanguageCode = $this->getAcceptLanguage();
436 $wgLang = $wgContLang = Language
::factory( $wgLanguageCode );
437 $this->setVar( 'wgLanguageCode', $wgLanguageCode );
438 $this->setVar( '_UserLang', $wgLanguageCode );
440 $wgLanguageCode = $this->getVar( 'wgLanguageCode' );
441 $wgLang = Language
::factory( $this->getVar( '_UserLang' ) );
442 $wgContLang = Language
::factory( $wgLanguageCode );
447 * Retrieves MediaWiki language from Accept-Language HTTP header.
451 public function getAcceptLanguage() {
452 global $wgLanguageCode, $wgRequest;
454 $mwLanguages = Language
::getLanguageNames();
455 $headerLanguages = array_keys( $wgRequest->getAcceptLang() );
457 foreach ( $headerLanguages as $lang ) {
458 if ( isset( $mwLanguages[$lang] ) ) {
463 return $wgLanguageCode;
467 * Called by execute() before page output starts, to show a page list.
469 * @param $currentPageName String
471 private function startPageWrapper( $currentPageName ) {
472 $s = "<div class=\"config-page-wrapper\">\n";
473 $s .= "<div class=\"config-page\">\n";
474 $s .= "<div class=\"config-page-list\"><ul>\n";
477 foreach ( $this->pageSequence
as $id => $pageName ) {
478 $happy = !empty( $this->happyPages
[$id] );
479 $s .= $this->getPageListItem(
481 $happy ||
$lastHappy == $id - 1,
490 $s .= "</ul><br/><ul>\n";
492 foreach ( $this->otherPages
as $pageName ) {
493 $s .= $this->getPageListItem( $pageName, true, $currentPageName );
496 $s .= "</ul></div>\n"; // end list pane
497 $s .= Html
::element( 'h2', array(),
498 wfMsg( 'config-page-' . strtolower( $currentPageName ) ) );
500 $this->output
->addHTMLNoFlush( $s );
504 * Get a list item for the page list.
506 * @param $pageName String
507 * @param $enabled Boolean
508 * @param $currentPageName String
512 private function getPageListItem( $pageName, $enabled, $currentPageName ) {
513 $s = "<li class=\"config-page-list-item\">";
514 $name = wfMsg( 'config-page-' . strtolower( $pageName ) );
517 $query = array( 'page' => $pageName );
519 if ( !in_array( $pageName, $this->pageSequence
) ) {
520 if ( in_array( $currentPageName, $this->pageSequence
) ) {
521 $query['lastPage'] = $currentPageName;
524 $link = Html
::element( 'a',
526 'href' => $this->getUrl( $query )
531 $link = htmlspecialchars( $name );
534 if ( $pageName == $currentPageName ) {
535 $s .= "<span class=\"config-page-current\">$link</span>";
540 $s .= Html
::element( 'span',
542 'class' => 'config-page-disabled'
554 * Output some stuff after a page is finished.
556 private function endPageWrapper() {
557 $this->output
->addHTMLNoFlush(
559 "<br style=\"clear:both\"/>\n" .
564 * Get HTML for an error box with an icon.
566 * @param $text String: wikitext, get this with wfMsgNoTrans()
568 public function getErrorBox( $text ) {
569 return $this->getInfoBox( $text, 'critical-32.png', 'config-error-box' );
573 * Get HTML for a warning box with an icon.
575 * @param $text String: wikitext, get this with wfMsgNoTrans()
577 public function getWarningBox( $text ) {
578 return $this->getInfoBox( $text, 'warning-32.png', 'config-warning-box' );
582 * Get HTML for an info box with an icon.
584 * @param $text String: wikitext, get this with wfMsgNoTrans()
585 * @param $icon String: icon name, file in skins/common/images
586 * @param $class String: additional class name to add to the wrapper div
588 public function getInfoBox( $text, $icon = 'info-32.png', $class = false ) {
590 "<div class=\"config-info $class\">\n" .
591 "<div class=\"config-info-left\">\n" .
592 Html
::element( 'img',
594 'src' => '../skins/common/images/' . $icon,
595 'alt' => wfMsg( 'config-information' ),
599 "<div class=\"config-info-right\">\n" .
600 $this->parse( $text ) . "\n" .
602 "<div style=\"clear: left;\"></div>\n" .
608 * Get small text indented help for a preceding form field.
609 * Parameters like wfMsg().
611 public function getHelpBox( $msg /*, ... */ ) {
612 $args = func_get_args();
613 array_shift( $args );
614 $args = array_map( 'htmlspecialchars', $args );
615 $text = wfMsgReal( $msg, $args, false, false, false );
616 $html = $this->parse( $text, true );
619 "<div class=\"config-help-wrapper\">\n" .
620 "<div class=\"config-help-message\">\n" .
623 "<div class=\"config-show-help\">\n" .
625 wfMsgHtml( 'config-show-help' ) .
627 "<div class=\"config-hide-help\">\n" .
629 wfMsgHtml( 'config-hide-help' ) .
630 "</a></div>\n</div>\n";
636 public function showHelpBox( $msg /*, ... */ ) {
637 $args = func_get_args();
638 $html = call_user_func_array( array( $this, 'getHelpBox' ), $args );
639 $this->output
->addHTML( $html );
643 * Show a short informational message.
644 * Output looks like a list.
648 public function showMessage( $msg /*, ... */ ) {
649 $args = func_get_args();
650 array_shift( $args );
651 $html = '<div class="config-message">' .
652 $this->parse( wfMsgReal( $msg, $args, false, false, false ) ) .
654 $this->output
->addHTML( $html );
658 * @param $status Status
660 public function showStatusMessage( Status
$status ) {
661 $text = $status->getWikiText();
662 $this->output
->addWikiText(
663 "<div class=\"config-message\">\n" .
670 * Label a control by wrapping a config-input div around it and putting a
673 public function label( $msg, $forId, $contents ) {
674 if ( strval( $msg ) == '' ) {
675 $labelText = ' ';
677 $labelText = wfMsgHtml( $msg );
680 $attributes = array( 'class' => 'config-label' );
683 $attributes['for'] = $forId;
687 "<div class=\"config-input\">\n" .
690 $labelText ) . "\n" .
696 * Get a labelled text box to configure a variable.
698 * @param $params Array
700 * var: The variable to be configured (required)
701 * label: The message name for the label (required)
702 * attribs: Additional attributes for the input element (optional)
703 * controlName: The name for the input element (optional)
704 * value: The current value of the variable (optional)
706 public function getTextBox( $params ) {
707 if ( !isset( $params['controlName'] ) ) {
708 $params['controlName'] = 'config_' . $params['var'];
711 if ( !isset( $params['value'] ) ) {
712 $params['value'] = $this->getVar( $params['var'] );
715 if ( !isset( $params['attribs'] ) ) {
716 $params['attribs'] = array();
722 $params['controlName'],
724 $params['controlName'],
725 30, // intended to be overridden by CSS
727 $params['attribs'] +
array(
728 'id' => $params['controlName'],
729 'class' => 'config-input-text',
730 'tabindex' => $this->nextTabIndex()
737 * Get a labelled password box to configure a variable.
739 * Implements password hiding
740 * @param $params Array
742 * var: The variable to be configured (required)
743 * label: The message name for the label (required)
744 * attribs: Additional attributes for the input element (optional)
745 * controlName: The name for the input element (optional)
746 * value: The current value of the variable (optional)
748 public function getPasswordBox( $params ) {
749 if ( !isset( $params['value'] ) ) {
750 $params['value'] = $this->getVar( $params['var'] );
753 if ( !isset( $params['attribs'] ) ) {
754 $params['attribs'] = array();
757 $params['value'] = $this->getFakePassword( $params['value'] );
758 $params['attribs']['type'] = 'password';
760 return $this->getTextBox( $params );
764 * Get a labelled checkbox to configure a boolean variable.
766 * @param $params Array
768 * var: The variable to be configured (required)
769 * label: The message name for the label (required)
770 * attribs: Additional attributes for the input element (optional)
771 * controlName: The name for the input element (optional)
772 * value: The current value of the variable (optional)
774 public function getCheckBox( $params ) {
775 if ( !isset( $params['controlName'] ) ) {
776 $params['controlName'] = 'config_' . $params['var'];
779 if ( !isset( $params['value'] ) ) {
780 $params['value'] = $this->getVar( $params['var'] );
783 if ( !isset( $params['attribs'] ) ) {
784 $params['attribs'] = array();
787 if( isset( $params['rawtext'] ) ) {
788 $labelText = $params['rawtext'];
790 $labelText = $this->parse( wfMsg( $params['label'] ) );
794 "<div class=\"config-input-check\">\n" .
797 $params['controlName'],
799 $params['attribs'] +
array(
800 'id' => $params['controlName'],
801 'tabindex' => $this->nextTabIndex(),
810 * Get a set of labelled radio buttons.
812 * @param $params Array
814 * var: The variable to be configured (required)
815 * label: The message name for the label (required)
816 * itemLabelPrefix: The message name prefix for the item labels (required)
817 * values: List of allowed values (required)
818 * itemAttribs Array of attribute arrays, outer key is the value name (optional)
819 * commonAttribs Attribute array applied to all items
820 * controlName: The name for the input element (optional)
821 * value: The current value of the variable (optional)
823 public function getRadioSet( $params ) {
824 if ( !isset( $params['controlName'] ) ) {
825 $params['controlName'] = 'config_' . $params['var'];
828 if ( !isset( $params['value'] ) ) {
829 $params['value'] = $this->getVar( $params['var'] );
832 if ( !isset( $params['label'] ) ) {
835 $label = $this->parse( wfMsgNoTrans( $params['label'] ) );
838 $s = "<label class=\"config-label\">\n" .
841 "<ul class=\"config-settings-block\">\n";
842 foreach ( $params['values'] as $value ) {
843 $itemAttribs = array();
845 if ( isset( $params['commonAttribs'] ) ) {
846 $itemAttribs = $params['commonAttribs'];
849 if ( isset( $params['itemAttribs'][$value] ) ) {
850 $itemAttribs = $params['itemAttribs'][$value] +
$itemAttribs;
853 $checked = $value == $params['value'];
854 $id = $params['controlName'] . '_' . $value;
855 $itemAttribs['id'] = $id;
856 $itemAttribs['tabindex'] = $this->nextTabIndex();
860 Xml
::radio( $params['controlName'], $value, $checked, $itemAttribs ) .
862 Xml
::tags( 'label', array( 'for' => $id ), $this->parse(
863 wfMsgNoTrans( $params['itemLabelPrefix'] . strtolower( $value ) )
873 * Output an error or warning box using a Status object.
875 public function showStatusBox( $status ) {
876 if( !$status->isGood() ) {
877 $text = $status->getWikiText();
879 if( $status->isOk() ) {
880 $box = $this->getWarningBox( $text );
882 $box = $this->getErrorBox( $text );
885 $this->output
->addHTML( $box );
890 * Convenience function to set variables based on form data.
891 * Assumes that variables containing "password" in the name are (potentially
894 * @param $varNames Array
895 * @param $prefix String: the prefix added to variables to obtain form names
897 public function setVarsFromRequest( $varNames, $prefix = 'config_' ) {
898 $newValues = array();
900 foreach ( $varNames as $name ) {
901 $value = trim( $this->request
->getVal( $prefix . $name ) );
902 $newValues[$name] = $value;
904 if ( $value === null ) {
906 $this->setVar( $name, false );
908 if ( stripos( $name, 'password' ) !== false ) {
909 $this->setPassword( $name, $value );
911 $this->setVar( $name, $value );
920 * Helper for Installer::docLink()
922 protected function getDocUrl( $page ) {
923 $url = "{$_SERVER['PHP_SELF']}?page=" . urlencode( $page );
925 if ( in_array( $this->currentPageName
, $this->pageSequence
) ) {
926 $url .= '&lastPage=' . urlencode( $this->currentPageName
);