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 if ( in_array( $pageName, $this->otherPages
) ) {
169 $page = $this->getPageByName( $pageName );
172 if ( !$pageName ||
!in_array( $pageName, $this->pageSequence
) ) {
173 $pageId = $lowestUnhappy;
175 $pageId = array_search( $pageName, $this->pageSequence
);
178 # If necessary, move back to the lowest-numbered unhappy page
179 if ( $pageId > $lowestUnhappy ) {
180 $pageId = $lowestUnhappy;
181 if ( $lowestUnhappy == 0 ) {
182 # Knocked back to start, possible loss of session data.
183 $this->showSessionWarning
= true;
187 $pageName = $this->pageSequence
[$pageId];
188 $page = $this->getPageByName( $pageName );
191 # If a back button was submitted, go back without submitting the form data.
192 if ( $this->request
->wasPosted() && $this->request
->getBool( 'submit-back' ) ) {
193 if ( $this->request
->getVal( 'lastPage' ) ) {
194 $nextPage = $this->request
->getVal( 'lastPage' );
195 } elseif ( $pageId !== false ) {
197 # Skip the skipped pages
198 $nextPageId = $pageId;
202 $nextPage = $this->pageSequence
[$nextPageId];
203 } while( isset( $this->skippedPages
[$nextPage] ) );
205 $nextPage = $this->pageSequence
[$lowestUnhappy];
208 $this->output
->redirect( $this->getUrl( array( 'page' => $nextPage ) ) );
209 return $this->finish();
213 $this->currentPageName
= $page->getName();
214 $this->startPageWrapper( $pageName );
215 $localSettings = $this->getLocalSettingsStatus();
217 if( !$localSettings->isGood() ) {
218 $this->showStatusBox( $localSettings );
221 $result = $page->execute();
224 $this->endPageWrapper();
226 if ( $result == 'skip' ) {
227 # Page skipped without explicit submission.
228 # Skip it when we click "back" so that we don't just go forward again.
229 $this->skippedPages
[$pageName] = true;
230 $result = 'continue';
232 unset( $this->skippedPages
[$pageName] );
235 # If it was posted, the page can request a continue to the next page.
236 if ( $result === 'continue' && !$this->output
->headerDone() ) {
237 if ( $pageId !== false ) {
238 $this->happyPages
[$pageId] = true;
241 $lowestUnhappy = $this->getLowestUnhappy();
243 if ( $this->request
->getVal( 'lastPage' ) ) {
244 $nextPage = $this->request
->getVal( 'lastPage' );
245 } elseif ( $pageId !== false ) {
246 $nextPage = $this->pageSequence
[$pageId +
1];
248 $nextPage = $this->pageSequence
[$lowestUnhappy];
251 if ( array_search( $nextPage, $this->pageSequence
) > $lowestUnhappy ) {
252 $nextPage = $this->pageSequence
[$lowestUnhappy];
255 $this->output
->redirect( $this->getUrl( array( 'page' => $nextPage ) ) );
258 return $this->finish();
261 public function getLowestUnhappy() {
262 if ( count( $this->happyPages
) == 0 ) {
265 return max( array_keys( $this->happyPages
) ) +
1;
270 * Start the PHP session. This may be called before execute() to start the PHP session.
272 public function startSession() {
273 $sessPath = $this->getSessionSavePath();
275 if( $sessPath != '' ) {
276 if( strval( ini_get( 'open_basedir' ) ) != '' ) {
277 // we need to skip the following check when open_basedir is on.
278 // The session path probably *wont* be writable by the current
279 // user, and telling them to change it is bad. Bug 23021.
280 } elseif( !is_dir( $sessPath ) ||
!is_writeable( $sessPath ) ) {
281 $this->showError( 'config-session-path-bad', $sessPath );
285 // If the path is unset it'll default to some system bit, which *probably* is ok...
286 // not sure how to actually get what will be used.
289 if( wfIniGetBool( 'session.auto_start' ) ||
session_id() ) {
294 $this->phpErrors
= array();
295 set_error_handler( array( $this, 'errorHandler' ) );
297 restore_error_handler();
299 if ( $this->phpErrors
) {
300 $this->showError( 'config-session-error', $this->phpErrors
[0] );
308 * Get the value of session.save_path
310 * Per http://www.php.net/manual/en/session.configuration.php#ini.session.save-path,
311 * this may have an initial integer value to indicate the depth of session
312 * storage (eg /tmp/a/b/c). Explode on ; and check and see if this part is
313 * there or not. Should also allow paths with semicolons in them (if you
314 * really wanted your session files stored in /tmp/some;dir) which PHP
315 * supposedly supports.
319 private function getSessionSavePath() {
320 $parts = explode( ';', ini_get( 'session.save_path' ), 2 );
321 return count( $parts ) == 1 ?
$parts[0] : $parts[1];
325 * Show an error message in a box. Parameters are like wfMsg().
327 public function showError( $msg /*...*/ ) {
328 $args = func_get_args();
329 array_shift( $args );
330 $args = array_map( 'htmlspecialchars', $args );
331 $msg = wfMsgReal( $msg, $args, false, false, false );
332 $this->output
->addHTML( $this->getErrorBox( $msg ) );
336 * Temporary error handler for session start debugging.
338 public function errorHandler( $errno, $errstr ) {
339 $this->phpErrors
[] = $errstr;
343 * Clean up from execute()
347 public function finish() {
348 $this->output
->output();
350 $this->session
['happyPages'] = $this->happyPages
;
351 $this->session
['skippedPages'] = $this->skippedPages
;
352 $this->session
['settings'] = $this->settings
;
354 return $this->session
;
358 * Get a URL for submission back to the same script.
360 * @param $query: Array
362 public function getUrl( $query = array() ) {
363 $url = $this->request
->getRequestURL();
364 # Remove existing query
365 $url = preg_replace( '/\?.*$/', '', $url );
368 $url .= '?' . wfArrayToCGI( $query );
375 * Get a WebInstallerPage from the main sequence, by ID.
379 * @return WebInstallerPage
381 public function getPageById( $id ) {
382 return $this->getPageByName( $this->pageSequence
[$id] );
386 * Get a WebInstallerPage by name.
388 * @param $pageName String
390 * @return WebInstallerPage
392 public function getPageByName( $pageName ) {
393 // Totally lame way to force autoload of WebInstallerPage.php
394 class_exists( 'WebInstallerPage' );
396 $pageClass = 'WebInstaller_' . $pageName;
398 return new $pageClass( $this );
402 * Get a session variable.
404 * @param $name String
407 public function getSession( $name, $default = null ) {
408 if ( !isset( $this->session
[$name] ) ) {
411 return $this->session
[$name];
416 * Set a session variable.
418 public function setSession( $name, $value ) {
419 $this->session
[$name] = $value;
423 * Get the next tabindex attribute value.
425 public function nextTabIndex() {
426 return $this->tabIndex++
;
430 * Initializes language-related variables.
432 public function setupLanguage() {
433 global $wgLang, $wgContLang, $wgLanguageCode;
435 if ( $this->getSession( 'test' ) === null && !$this->request
->wasPosted() ) {
436 $wgLanguageCode = $this->getAcceptLanguage();
437 $wgLang = $wgContLang = Language
::factory( $wgLanguageCode );
438 $this->setVar( 'wgLanguageCode', $wgLanguageCode );
439 $this->setVar( '_UserLang', $wgLanguageCode );
441 $wgLanguageCode = $this->getVar( 'wgLanguageCode' );
442 $wgLang = Language
::factory( $this->getVar( '_UserLang' ) );
443 $wgContLang = Language
::factory( $wgLanguageCode );
448 * Retrieves MediaWiki language from Accept-Language HTTP header.
452 public function getAcceptLanguage() {
453 global $wgLanguageCode, $wgRequest;
455 $mwLanguages = Language
::getLanguageNames();
456 $headerLanguages = array_keys( $wgRequest->getAcceptLang() );
458 foreach ( $headerLanguages as $lang ) {
459 if ( isset( $mwLanguages[$lang] ) ) {
464 return $wgLanguageCode;
468 * Called by execute() before page output starts, to show a page list.
470 * @param $currentPageName String
472 public function startPageWrapper( $currentPageName ) {
473 $s = "<div class=\"config-page-wrapper\">\n" .
474 "<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 "<div class=\"config-page\">\n" .
498 Xml
::element( 'h2', array(),
499 wfMsg( 'config-page-' . strtolower( $currentPageName ) ) );
501 $this->output
->addHTMLNoFlush( $s );
505 * Get a list item for the page list.
507 * @param $pageName String
508 * @param $enabled Boolean
509 * @param $currentPageName String
513 public function getPageListItem( $pageName, $enabled, $currentPageName ) {
514 $s = "<li class=\"config-page-list-item\">";
515 $name = wfMsg( 'config-page-' . strtolower( $pageName ) );
518 $query = array( 'page' => $pageName );
520 if ( !in_array( $pageName, $this->pageSequence
) ) {
521 if ( in_array( $currentPageName, $this->pageSequence
) ) {
522 $query['lastPage'] = $currentPageName;
525 $link = Xml
::element( 'a',
527 'href' => $this->getUrl( $query )
532 $link = htmlspecialchars( $name );
535 if ( $pageName == $currentPageName ) {
536 $s .= "<span class=\"config-page-current\">$link</span>";
541 $s .= Xml
::element( 'span',
543 'class' => 'config-page-disabled'
555 * Output some stuff after a page is finished.
557 public function endPageWrapper() {
558 $this->output
->addHTMLNoFlush(
560 "<br style=\"clear:both\"/>\n" .
565 * Get HTML for an error box with an icon.
567 * @param $text String: wikitext, get this with wfMsgNoTrans()
569 public function getErrorBox( $text ) {
570 return $this->getInfoBox( $text, 'critical-32.png', 'config-error-box' );
574 * Get HTML for a warning box with an icon.
576 * @param $text String: wikitext, get this with wfMsgNoTrans()
578 public function getWarningBox( $text ) {
579 return $this->getInfoBox( $text, 'warning-32.png', 'config-warning-box' );
583 * Get HTML for an info box with an icon.
585 * @param $text String: wikitext, get this with wfMsgNoTrans()
586 * @param $icon String: icon name, file in skins/common/images
587 * @param $class String: additional class name to add to the wrapper div
589 public function getInfoBox( $text, $icon = 'info-32.png', $class = false ) {
591 "<div class=\"config-info $class\">\n" .
592 "<div class=\"config-info-left\">\n" .
595 'src' => '../skins/common/images/' . $icon,
596 'alt' => wfMsg( 'config-information' ),
600 "<div class=\"config-info-right\">\n" .
601 $this->parse( $text ) . "\n" .
603 "<div style=\"clear: left;\"></div>\n" .
609 * Get small text indented help for a preceding form field.
610 * Parameters like wfMsg().
612 public function getHelpBox( $msg /*, ... */ ) {
613 $args = func_get_args();
614 array_shift( $args );
615 $args = array_map( 'htmlspecialchars', $args );
616 $text = wfMsgReal( $msg, $args, false, false, false );
617 $html = $this->parse( $text, true );
620 "<div class=\"config-help-wrapper\">\n" .
621 "<div class=\"config-help-message\">\n" .
624 "<div class=\"config-show-help\">\n" .
626 wfMsgHtml( 'config-show-help' ) .
628 "<div class=\"config-hide-help\">\n" .
630 wfMsgHtml( 'config-hide-help' ) .
631 "</a></div>\n</div>\n";
637 public function showHelpBox( $msg /*, ... */ ) {
638 $args = func_get_args();
639 $html = call_user_func_array( array( $this, 'getHelpBox' ), $args );
640 $this->output
->addHTML( $html );
644 * Show a short informational message.
645 * Output looks like a list.
649 public function showMessage( $msg /*, ... */ ) {
650 $args = func_get_args();
651 array_shift( $args );
652 $html = '<div class="config-message">' .
653 $this->parse( wfMsgReal( $msg, $args, false, false, false ) ) .
655 $this->output
->addHTML( $html );
659 * @param $status Status
661 public function showStatusMessage( Status
$status ) {
662 $text = $status->getWikiText();
663 $this->output
->addWikiText(
664 "<div class=\"config-message\">\n" .
671 * Label a control by wrapping a config-input div around it and putting a
674 public function label( $msg, $forId, $contents ) {
675 if ( strval( $msg ) == '' ) {
676 $labelText = ' ';
678 $labelText = wfMsgHtml( $msg );
681 $attributes = array( 'class' => 'config-label' );
684 $attributes['for'] = $forId;
688 "<div class=\"config-input\">\n" .
691 $labelText ) . "\n" .
697 * Get a labelled text box to configure a variable.
699 * @param $params Array
701 * var: The variable to be configured (required)
702 * label: The message name for the label (required)
703 * attribs: Additional attributes for the input element (optional)
704 * controlName: The name for the input element (optional)
705 * value: The current value of the variable (optional)
707 public function getTextBox( $params ) {
708 if ( !isset( $params['controlName'] ) ) {
709 $params['controlName'] = 'config_' . $params['var'];
712 if ( !isset( $params['value'] ) ) {
713 $params['value'] = $this->getVar( $params['var'] );
716 if ( !isset( $params['attribs'] ) ) {
717 $params['attribs'] = array();
723 $params['controlName'],
725 $params['controlName'],
726 30, // intended to be overridden by CSS
728 $params['attribs'] +
array(
729 'id' => $params['controlName'],
730 'class' => 'config-input-text',
731 'tabindex' => $this->nextTabIndex()
738 * Get a labelled password box to configure a variable.
740 * Implements password hiding
741 * @param $params Array
743 * var: The variable to be configured (required)
744 * label: The message name for the label (required)
745 * attribs: Additional attributes for the input element (optional)
746 * controlName: The name for the input element (optional)
747 * value: The current value of the variable (optional)
749 public function getPasswordBox( $params ) {
750 if ( !isset( $params['value'] ) ) {
751 $params['value'] = $this->getVar( $params['var'] );
754 if ( !isset( $params['attribs'] ) ) {
755 $params['attribs'] = array();
758 $params['value'] = $this->getFakePassword( $params['value'] );
759 $params['attribs']['type'] = 'password';
761 return $this->getTextBox( $params );
765 * Get a labelled checkbox to configure a boolean variable.
767 * @param $params Array
769 * var: The variable to be configured (required)
770 * label: The message name for the label (required)
771 * attribs: Additional attributes for the input element (optional)
772 * controlName: The name for the input element (optional)
773 * value: The current value of the variable (optional)
775 public function getCheckBox( $params ) {
776 if ( !isset( $params['controlName'] ) ) {
777 $params['controlName'] = 'config_' . $params['var'];
780 if ( !isset( $params['value'] ) ) {
781 $params['value'] = $this->getVar( $params['var'] );
784 if ( !isset( $params['attribs'] ) ) {
785 $params['attribs'] = array();
788 if( isset( $params['rawtext'] ) ) {
789 $labelText = $params['rawtext'];
791 $labelText = $this->parse( wfMsg( $params['label'] ) );
795 "<div class=\"config-input-check\">\n" .
798 $params['controlName'],
800 $params['attribs'] +
array(
801 'id' => $params['controlName'],
802 'class' => 'config-input-text',
803 'tabindex' => $this->nextTabIndex(),
812 * Get a set of labelled radio buttons.
814 * @param $params Array
816 * var: The variable to be configured (required)
817 * label: The message name for the label (required)
818 * itemLabelPrefix: The message name prefix for the item labels (required)
819 * values: List of allowed values (required)
820 * itemAttribs Array of attribute arrays, outer key is the value name (optional)
821 * commonAttribs Attribute array applied to all items
822 * controlName: The name for the input element (optional)
823 * value: The current value of the variable (optional)
825 public function getRadioSet( $params ) {
826 if ( !isset( $params['controlName'] ) ) {
827 $params['controlName'] = 'config_' . $params['var'];
830 if ( !isset( $params['value'] ) ) {
831 $params['value'] = $this->getVar( $params['var'] );
834 if ( !isset( $params['label'] ) ) {
837 $label = $this->parse( wfMsgNoTrans( $params['label'] ) );
840 $s = "<label class=\"config-label\">\n" .
843 "<ul class=\"config-settings-block\">\n";
844 foreach ( $params['values'] as $value ) {
845 $itemAttribs = array();
847 if ( isset( $params['commonAttribs'] ) ) {
848 $itemAttribs = $params['commonAttribs'];
851 if ( isset( $params['itemAttribs'][$value] ) ) {
852 $itemAttribs = $params['itemAttribs'][$value] +
$itemAttribs;
855 $checked = $value == $params['value'];
856 $id = $params['controlName'] . '_' . $value;
857 $itemAttribs['id'] = $id;
858 $itemAttribs['tabindex'] = $this->nextTabIndex();
862 Xml
::radio( $params['controlName'], $value, $checked, $itemAttribs ) .
864 Xml
::tags( 'label', array( 'for' => $id ), $this->parse(
865 wfMsgNoTrans( $params['itemLabelPrefix'] . strtolower( $value ) )
875 * Output an error or warning box using a Status object.
877 public function showStatusBox( $status ) {
878 if( !$status->isGood() ) {
879 $text = $status->getWikiText();
881 if( $status->isOk() ) {
882 $box = $this->getWarningBox( $text );
884 $box = $this->getErrorBox( $text );
887 $this->output
->addHTML( $box );
892 * Convenience function to set variables based on form data.
893 * Assumes that variables containing "password" in the name are (potentially
896 * @param $varNames Array
897 * @param $prefix String: the prefix added to variables to obtain form names
899 public function setVarsFromRequest( $varNames, $prefix = 'config_' ) {
900 $newValues = array();
902 foreach ( $varNames as $name ) {
903 $value = trim( $this->request
->getVal( $prefix . $name ) );
904 $newValues[$name] = $value;
906 if ( $value === null ) {
908 $this->setVar( $name, false );
910 if ( stripos( $name, 'password' ) !== false ) {
911 $this->setPassword( $name, $value );
913 $this->setVar( $name, $value );
922 * Get the starting tags of a fieldset.
924 * @param $legend String: message name
926 public function getFieldsetStart( $legend ) {
927 return "\n<fieldset><legend>" . wfMsgHtml( $legend ) . "</legend>\n";
931 * Get the end tag of a fieldset.
933 public function getFieldsetEnd() {
934 return "</fieldset>\n";
938 * Helper for Installer::docLink()
940 public function getDocUrl( $page ) {
941 $url = "{$_SERVER['PHP_SELF']}?page=" . urlencode( $page );
943 if ( in_array( $this->currentPageName
, $this->pageSequence
) ) {
944 $url .= '&lastPage=' . urlencode( $this->currentPageName
);