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->request
->getVal( 'localsettings' ) )
123 $ls = new LocalSettingsGenerator( $this );
124 $this->request
->response()->header('Content-type: text/plain');
126 $this->request
->response()->header(
127 'Content-Disposition: attachment; filename="LocalSettings.php"'
131 return $this->session
;
134 if ( isset( $session['happyPages'] ) ) {
135 $this->happyPages
= $session['happyPages'];
137 $this->happyPages
= array();
140 if ( isset( $session['skippedPages'] ) ) {
141 $this->skippedPages
= $session['skippedPages'];
143 $this->skippedPages
= array();
146 $lowestUnhappy = $this->getLowestUnhappy();
148 # Special case for Creative Commons partner chooser box.
149 if ( $this->request
->getVal( 'SubmitCC' ) ) {
150 $page = $this->getPageByName( 'Options' );
151 $this->output
->useShortHeader();
153 return $this->finish();
156 if ( $this->request
->getVal( 'ShowCC' ) ) {
157 $page = $this->getPageByName( 'Options' );
158 $this->output
->useShortHeader();
159 $this->output
->addHTML( $page->getCCDoneBox() );
160 return $this->finish();
164 $pageName = $this->request
->getVal( 'page' );
166 # Check LocalSettings status
167 $localSettings = $this->getLocalSettingsStatus();
169 if( !$localSettings->isGood() && $this->getVar( '_LocalSettingsLocked' ) ) {
170 $pageName = 'Locked';
172 $page = $this->getPageByName( $pageName );
173 $page->setLocalSettingsStatus( $localSettings );
174 } elseif ( in_array( $pageName, $this->otherPages
) ) {
177 $page = $this->getPageByName( $pageName );
180 if ( !$pageName ||
!in_array( $pageName, $this->pageSequence
) ) {
181 $pageId = $lowestUnhappy;
183 $pageId = array_search( $pageName, $this->pageSequence
);
186 # If necessary, move back to the lowest-numbered unhappy page
187 if ( $pageId > $lowestUnhappy ) {
188 $pageId = $lowestUnhappy;
189 if ( $lowestUnhappy == 0 ) {
190 # Knocked back to start, possible loss of session data.
191 $this->showSessionWarning
= true;
195 $pageName = $this->pageSequence
[$pageId];
196 $page = $this->getPageByName( $pageName );
199 # If a back button was submitted, go back without submitting the form data.
200 if ( $this->request
->wasPosted() && $this->request
->getBool( 'submit-back' ) ) {
201 if ( $this->request
->getVal( 'lastPage' ) ) {
202 $nextPage = $this->request
->getVal( 'lastPage' );
203 } elseif ( $pageId !== false ) {
205 # Skip the skipped pages
206 $nextPageId = $pageId;
210 $nextPage = $this->pageSequence
[$nextPageId];
211 } while( isset( $this->skippedPages
[$nextPage] ) );
213 $nextPage = $this->pageSequence
[$lowestUnhappy];
216 $this->output
->redirect( $this->getUrl( array( 'page' => $nextPage ) ) );
217 return $this->finish();
221 $this->currentPageName
= $page->getName();
222 $this->startPageWrapper( $pageName );
224 $result = $page->execute();
226 $this->endPageWrapper();
228 if ( $result == 'skip' ) {
229 # Page skipped without explicit submission.
230 # Skip it when we click "back" so that we don't just go forward again.
231 $this->skippedPages
[$pageName] = true;
232 $result = 'continue';
234 unset( $this->skippedPages
[$pageName] );
237 # If it was posted, the page can request a continue to the next page.
238 if ( $result === 'continue' && !$this->output
->headerDone() ) {
239 if ( $pageId !== false ) {
240 $this->happyPages
[$pageId] = true;
243 $lowestUnhappy = $this->getLowestUnhappy();
245 if ( $this->request
->getVal( 'lastPage' ) ) {
246 $nextPage = $this->request
->getVal( 'lastPage' );
247 } elseif ( $pageId !== false ) {
248 $nextPage = $this->pageSequence
[$pageId +
1];
250 $nextPage = $this->pageSequence
[$lowestUnhappy];
253 if ( array_search( $nextPage, $this->pageSequence
) > $lowestUnhappy ) {
254 $nextPage = $this->pageSequence
[$lowestUnhappy];
257 $this->output
->redirect( $this->getUrl( array( 'page' => $nextPage ) ) );
260 return $this->finish();
263 public function getLowestUnhappy() {
264 if ( count( $this->happyPages
) == 0 ) {
267 return max( array_keys( $this->happyPages
) ) +
1;
272 * Start the PHP session. This may be called before execute() to start the PHP session.
274 public function startSession() {
275 $sessPath = $this->getSessionSavePath();
277 if( $sessPath != '' ) {
278 if( strval( ini_get( 'open_basedir' ) ) != '' ) {
279 // we need to skip the following check when open_basedir is on.
280 // The session path probably *wont* be writable by the current
281 // user, and telling them to change it is bad. Bug 23021.
282 } elseif( !is_dir( $sessPath ) ||
!is_writeable( $sessPath ) ) {
283 $this->showError( 'config-session-path-bad', $sessPath );
287 // If the path is unset it'll default to some system bit, which *probably* is ok...
288 // not sure how to actually get what will be used.
291 if( wfIniGetBool( 'session.auto_start' ) ||
session_id() ) {
296 $this->phpErrors
= array();
297 set_error_handler( array( $this, 'errorHandler' ) );
299 restore_error_handler();
301 if ( $this->phpErrors
) {
302 $this->showError( 'config-session-error', $this->phpErrors
[0] );
310 * Get the value of session.save_path
312 * Per http://www.php.net/manual/en/session.configuration.php#ini.session.save-path,
313 * this may have an initial integer value to indicate the depth of session
314 * storage (eg /tmp/a/b/c). Explode on ; and check and see if this part is
315 * there or not. Should also allow paths with semicolons in them (if you
316 * really wanted your session files stored in /tmp/some;dir) which PHP
317 * supposedly supports.
321 private function getSessionSavePath() {
322 $parts = explode( ';', ini_get( 'session.save_path' ), 2 );
323 return count( $parts ) == 1 ?
$parts[0] : $parts[1];
327 * Show an error message in a box. Parameters are like wfMsg().
329 public function showError( $msg /*...*/ ) {
330 $args = func_get_args();
331 array_shift( $args );
332 $args = array_map( 'htmlspecialchars', $args );
333 $msg = wfMsgReal( $msg, $args, false, false, false );
334 $this->output
->addHTML( $this->getErrorBox( $msg ) );
338 * Temporary error handler for session start debugging.
340 public function errorHandler( $errno, $errstr ) {
341 $this->phpErrors
[] = $errstr;
345 * Clean up from execute()
349 public function finish() {
350 $this->output
->output();
352 $this->session
['happyPages'] = $this->happyPages
;
353 $this->session
['skippedPages'] = $this->skippedPages
;
354 $this->session
['settings'] = $this->settings
;
356 return $this->session
;
360 * Get a URL for submission back to the same script.
362 * @param $query: Array
364 public function getUrl( $query = array() ) {
365 $url = $this->request
->getRequestURL();
366 # Remove existing query
367 $url = preg_replace( '/\?.*$/', '', $url );
370 $url .= '?' . wfArrayToCGI( $query );
377 * Get a WebInstallerPage by name.
379 * @param $pageName String
381 * @return WebInstallerPage
383 public function getPageByName( $pageName ) {
384 // Totally lame way to force autoload of WebInstallerPage.php
385 class_exists( 'WebInstallerPage' );
387 $pageClass = 'WebInstaller_' . $pageName;
389 return new $pageClass( $this );
393 * Get a session variable.
395 * @param $name String
398 public function getSession( $name, $default = null ) {
399 if ( !isset( $this->session
[$name] ) ) {
402 return $this->session
[$name];
407 * Set a session variable.
409 public function setSession( $name, $value ) {
410 $this->session
[$name] = $value;
414 * Get the next tabindex attribute value.
416 public function nextTabIndex() {
417 return $this->tabIndex++
;
421 * Initializes language-related variables.
423 public function setupLanguage() {
424 global $wgLang, $wgContLang, $wgLanguageCode;
426 if ( $this->getSession( 'test' ) === null && !$this->request
->wasPosted() ) {
427 $wgLanguageCode = $this->getAcceptLanguage();
428 $wgLang = $wgContLang = Language
::factory( $wgLanguageCode );
429 $this->setVar( 'wgLanguageCode', $wgLanguageCode );
430 $this->setVar( '_UserLang', $wgLanguageCode );
432 $wgLanguageCode = $this->getVar( 'wgLanguageCode' );
433 $wgLang = Language
::factory( $this->getVar( '_UserLang' ) );
434 $wgContLang = Language
::factory( $wgLanguageCode );
439 * Retrieves MediaWiki language from Accept-Language HTTP header.
443 public function getAcceptLanguage() {
444 global $wgLanguageCode, $wgRequest;
446 $mwLanguages = Language
::getLanguageNames();
447 $headerLanguages = array_keys( $wgRequest->getAcceptLang() );
449 foreach ( $headerLanguages as $lang ) {
450 if ( isset( $mwLanguages[$lang] ) ) {
455 return $wgLanguageCode;
459 * Called by execute() before page output starts, to show a page list.
461 * @param $currentPageName String
463 private function startPageWrapper( $currentPageName ) {
464 $s = "<div class=\"config-page-wrapper\">\n" .
465 "<div class=\"config-page-list\"><ul>\n";
468 foreach ( $this->pageSequence
as $id => $pageName ) {
469 $happy = !empty( $this->happyPages
[$id] );
470 $s .= $this->getPageListItem(
472 $happy ||
$lastHappy == $id - 1,
481 $s .= "</ul><br/><ul>\n";
483 foreach ( $this->otherPages
as $pageName ) {
484 $s .= $this->getPageListItem( $pageName, true, $currentPageName );
487 $s .= "</ul></div>\n". // end list pane
488 "<div class=\"config-page\">\n" .
489 Xml
::element( 'h2', array(),
490 wfMsg( 'config-page-' . strtolower( $currentPageName ) ) );
492 $this->output
->addHTMLNoFlush( $s );
496 * Get a list item for the page list.
498 * @param $pageName String
499 * @param $enabled Boolean
500 * @param $currentPageName String
504 private function getPageListItem( $pageName, $enabled, $currentPageName ) {
505 $s = "<li class=\"config-page-list-item\">";
506 $name = wfMsg( 'config-page-' . strtolower( $pageName ) );
509 $query = array( 'page' => $pageName );
511 if ( !in_array( $pageName, $this->pageSequence
) ) {
512 if ( in_array( $currentPageName, $this->pageSequence
) ) {
513 $query['lastPage'] = $currentPageName;
516 $link = Xml
::element( 'a',
518 'href' => $this->getUrl( $query )
523 $link = htmlspecialchars( $name );
526 if ( $pageName == $currentPageName ) {
527 $s .= "<span class=\"config-page-current\">$link</span>";
532 $s .= Xml
::element( 'span',
534 'class' => 'config-page-disabled'
546 * Output some stuff after a page is finished.
548 private function endPageWrapper() {
549 $this->output
->addHTMLNoFlush(
551 "<br style=\"clear:both\"/>\n" .
556 * Get HTML for an error box with an icon.
558 * @param $text String: wikitext, get this with wfMsgNoTrans()
560 public function getErrorBox( $text ) {
561 return $this->getInfoBox( $text, 'critical-32.png', 'config-error-box' );
565 * Get HTML for a warning box with an icon.
567 * @param $text String: wikitext, get this with wfMsgNoTrans()
569 public function getWarningBox( $text ) {
570 return $this->getInfoBox( $text, 'warning-32.png', 'config-warning-box' );
574 * Get HTML for an info box with an icon.
576 * @param $text String: wikitext, get this with wfMsgNoTrans()
577 * @param $icon String: icon name, file in skins/common/images
578 * @param $class String: additional class name to add to the wrapper div
580 public function getInfoBox( $text, $icon = 'info-32.png', $class = false ) {
582 "<div class=\"config-info $class\">\n" .
583 "<div class=\"config-info-left\">\n" .
586 'src' => '../skins/common/images/' . $icon,
587 'alt' => wfMsg( 'config-information' ),
591 "<div class=\"config-info-right\">\n" .
592 $this->parse( $text ) . "\n" .
594 "<div style=\"clear: left;\"></div>\n" .
600 * Get small text indented help for a preceding form field.
601 * Parameters like wfMsg().
603 public function getHelpBox( $msg /*, ... */ ) {
604 $args = func_get_args();
605 array_shift( $args );
606 $args = array_map( 'htmlspecialchars', $args );
607 $text = wfMsgReal( $msg, $args, false, false, false );
608 $html = $this->parse( $text, true );
611 "<div class=\"config-help-wrapper\">\n" .
612 "<div class=\"config-help-message\">\n" .
615 "<div class=\"config-show-help\">\n" .
617 wfMsgHtml( 'config-show-help' ) .
619 "<div class=\"config-hide-help\">\n" .
621 wfMsgHtml( 'config-hide-help' ) .
622 "</a></div>\n</div>\n";
628 public function showHelpBox( $msg /*, ... */ ) {
629 $args = func_get_args();
630 $html = call_user_func_array( array( $this, 'getHelpBox' ), $args );
631 $this->output
->addHTML( $html );
635 * Show a short informational message.
636 * Output looks like a list.
640 public function showMessage( $msg /*, ... */ ) {
641 $args = func_get_args();
642 array_shift( $args );
643 $html = '<div class="config-message">' .
644 $this->parse( wfMsgReal( $msg, $args, false, false, false ) ) .
646 $this->output
->addHTML( $html );
650 * @param $status Status
652 public function showStatusMessage( Status
$status ) {
653 $text = $status->getWikiText();
654 $this->output
->addWikiText(
655 "<div class=\"config-message\">\n" .
662 * Label a control by wrapping a config-input div around it and putting a
665 public function label( $msg, $forId, $contents ) {
666 if ( strval( $msg ) == '' ) {
667 $labelText = ' ';
669 $labelText = wfMsgHtml( $msg );
672 $attributes = array( 'class' => 'config-label' );
675 $attributes['for'] = $forId;
679 "<div class=\"config-input\">\n" .
682 $labelText ) . "\n" .
688 * Get a labelled text box to configure a variable.
690 * @param $params Array
692 * var: The variable to be configured (required)
693 * label: The message name for the label (required)
694 * attribs: Additional attributes for the input element (optional)
695 * controlName: The name for the input element (optional)
696 * value: The current value of the variable (optional)
698 public function getTextBox( $params ) {
699 if ( !isset( $params['controlName'] ) ) {
700 $params['controlName'] = 'config_' . $params['var'];
703 if ( !isset( $params['value'] ) ) {
704 $params['value'] = $this->getVar( $params['var'] );
707 if ( !isset( $params['attribs'] ) ) {
708 $params['attribs'] = array();
714 $params['controlName'],
716 $params['controlName'],
717 30, // intended to be overridden by CSS
719 $params['attribs'] +
array(
720 'id' => $params['controlName'],
721 'class' => 'config-input-text',
722 'tabindex' => $this->nextTabIndex()
729 * Get a labelled password box to configure a variable.
731 * Implements password hiding
732 * @param $params Array
734 * var: The variable to be configured (required)
735 * label: The message name for the label (required)
736 * attribs: Additional attributes for the input element (optional)
737 * controlName: The name for the input element (optional)
738 * value: The current value of the variable (optional)
740 public function getPasswordBox( $params ) {
741 if ( !isset( $params['value'] ) ) {
742 $params['value'] = $this->getVar( $params['var'] );
745 if ( !isset( $params['attribs'] ) ) {
746 $params['attribs'] = array();
749 $params['value'] = $this->getFakePassword( $params['value'] );
750 $params['attribs']['type'] = 'password';
752 return $this->getTextBox( $params );
756 * Get a labelled checkbox to configure a boolean variable.
758 * @param $params Array
760 * var: The variable to be configured (required)
761 * label: The message name for the label (required)
762 * attribs: Additional attributes for the input element (optional)
763 * controlName: The name for the input element (optional)
764 * value: The current value of the variable (optional)
766 public function getCheckBox( $params ) {
767 if ( !isset( $params['controlName'] ) ) {
768 $params['controlName'] = 'config_' . $params['var'];
771 if ( !isset( $params['value'] ) ) {
772 $params['value'] = $this->getVar( $params['var'] );
775 if ( !isset( $params['attribs'] ) ) {
776 $params['attribs'] = array();
779 if( isset( $params['rawtext'] ) ) {
780 $labelText = $params['rawtext'];
782 $labelText = $this->parse( wfMsg( $params['label'] ) );
786 "<div class=\"config-input-check\">\n" .
789 $params['controlName'],
791 $params['attribs'] +
array(
792 'id' => $params['controlName'],
793 'class' => 'config-input-text',
794 'tabindex' => $this->nextTabIndex(),
803 * Get a set of labelled radio buttons.
805 * @param $params Array
807 * var: The variable to be configured (required)
808 * label: The message name for the label (required)
809 * itemLabelPrefix: The message name prefix for the item labels (required)
810 * values: List of allowed values (required)
811 * itemAttribs Array of attribute arrays, outer key is the value name (optional)
812 * commonAttribs Attribute array applied to all items
813 * controlName: The name for the input element (optional)
814 * value: The current value of the variable (optional)
816 public function getRadioSet( $params ) {
817 if ( !isset( $params['controlName'] ) ) {
818 $params['controlName'] = 'config_' . $params['var'];
821 if ( !isset( $params['value'] ) ) {
822 $params['value'] = $this->getVar( $params['var'] );
825 if ( !isset( $params['label'] ) ) {
828 $label = $this->parse( wfMsgNoTrans( $params['label'] ) );
831 $s = "<label class=\"config-label\">\n" .
834 "<ul class=\"config-settings-block\">\n";
835 foreach ( $params['values'] as $value ) {
836 $itemAttribs = array();
838 if ( isset( $params['commonAttribs'] ) ) {
839 $itemAttribs = $params['commonAttribs'];
842 if ( isset( $params['itemAttribs'][$value] ) ) {
843 $itemAttribs = $params['itemAttribs'][$value] +
$itemAttribs;
846 $checked = $value == $params['value'];
847 $id = $params['controlName'] . '_' . $value;
848 $itemAttribs['id'] = $id;
849 $itemAttribs['tabindex'] = $this->nextTabIndex();
853 Xml
::radio( $params['controlName'], $value, $checked, $itemAttribs ) .
855 Xml
::tags( 'label', array( 'for' => $id ), $this->parse(
856 wfMsgNoTrans( $params['itemLabelPrefix'] . strtolower( $value ) )
866 * Output an error or warning box using a Status object.
868 public function showStatusBox( $status ) {
869 if( !$status->isGood() ) {
870 $text = $status->getWikiText();
872 if( $status->isOk() ) {
873 $box = $this->getWarningBox( $text );
875 $box = $this->getErrorBox( $text );
878 $this->output
->addHTML( $box );
883 * Convenience function to set variables based on form data.
884 * Assumes that variables containing "password" in the name are (potentially
887 * @param $varNames Array
888 * @param $prefix String: the prefix added to variables to obtain form names
890 public function setVarsFromRequest( $varNames, $prefix = 'config_' ) {
891 $newValues = array();
893 foreach ( $varNames as $name ) {
894 $value = trim( $this->request
->getVal( $prefix . $name ) );
895 $newValues[$name] = $value;
897 if ( $value === null ) {
899 $this->setVar( $name, false );
901 if ( stripos( $name, 'password' ) !== false ) {
902 $this->setPassword( $name, $value );
904 $this->setVar( $name, $value );
913 * Helper for Installer::docLink()
915 protected function getDocUrl( $page ) {
916 $url = "{$_SERVER['PHP_SELF']}?page=" . urlencode( $page );
918 if ( in_array( $this->currentPageName
, $this->pageSequence
) ) {
919 $url .= '&lastPage=' . urlencode( $this->currentPageName
);