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;
449 $mwLanguages = Language
::getLanguageNames();
450 $langs = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
452 foreach ( explode( ';', $langs ) as $splitted ) {
453 foreach ( explode( ',', $splitted ) as $lang ) {
454 $lang = trim( strtolower( $lang ) );
456 if ( $lang == '' ||
$lang[0] == 'q' ) {
460 if ( isset( $mwLanguages[$lang] ) ) {
464 $lang = preg_replace( '/^(.*?)(?=-[^-]*)$/', '\\1', $lang );
466 if ( $lang != '' && isset( $mwLanguages[$lang] ) ) {
472 return $wgLanguageCode;
476 * Called by execute() before page output starts, to show a page list.
478 * @param $currentPageName String
480 public function startPageWrapper( $currentPageName ) {
481 $s = "<div class=\"config-page-wrapper\">\n" .
482 "<div class=\"config-page-list\"><ul>\n";
485 foreach ( $this->pageSequence
as $id => $pageName ) {
486 $happy = !empty( $this->happyPages
[$id] );
487 $s .= $this->getPageListItem(
489 $happy ||
$lastHappy == $id - 1,
498 $s .= "</ul><br/><ul>\n";
500 foreach ( $this->otherPages
as $pageName ) {
501 $s .= $this->getPageListItem( $pageName, true, $currentPageName );
504 $s .= "</ul></div>\n". // end list pane
505 "<div class=\"config-page\">\n" .
506 Xml
::element( 'h2', array(),
507 wfMsg( 'config-page-' . strtolower( $currentPageName ) ) );
509 $this->output
->addHTMLNoFlush( $s );
513 * Get a list item for the page list.
515 * @param $pageName String
516 * @param $enabled Boolean
517 * @param $currentPageName String
521 public function getPageListItem( $pageName, $enabled, $currentPageName ) {
522 $s = "<li class=\"config-page-list-item\">";
523 $name = wfMsg( 'config-page-' . strtolower( $pageName ) );
526 $query = array( 'page' => $pageName );
528 if ( !in_array( $pageName, $this->pageSequence
) ) {
529 if ( in_array( $currentPageName, $this->pageSequence
) ) {
530 $query['lastPage'] = $currentPageName;
533 $link = Xml
::element( 'a',
535 'href' => $this->getUrl( $query )
540 $link = htmlspecialchars( $name );
543 if ( $pageName == $currentPageName ) {
544 $s .= "<span class=\"config-page-current\">$link</span>";
549 $s .= Xml
::element( 'span',
551 'class' => 'config-page-disabled'
563 * Output some stuff after a page is finished.
565 public function endPageWrapper() {
566 $this->output
->addHTMLNoFlush(
568 "<br style=\"clear:both\"/>\n" .
573 * Get HTML for an error box with an icon.
575 * @param $text String: wikitext, get this with wfMsgNoTrans()
577 public function getErrorBox( $text ) {
578 return $this->getInfoBox( $text, 'critical-32.png', 'config-error-box' );
582 * Get HTML for a warning box with an icon.
584 * @param $text String: wikitext, get this with wfMsgNoTrans()
586 public function getWarningBox( $text ) {
587 return $this->getInfoBox( $text, 'warning-32.png', 'config-warning-box' );
591 * Get HTML for an info box with an icon.
593 * @param $text String: wikitext, get this with wfMsgNoTrans()
594 * @param $icon String: icon name, file in skins/common/images
595 * @param $class String: additional class name to add to the wrapper div
597 public function getInfoBox( $text, $icon = 'info-32.png', $class = false ) {
599 "<div class=\"config-info $class\">\n" .
600 "<div class=\"config-info-left\">\n" .
603 'src' => '../skins/common/images/' . $icon,
604 'alt' => wfMsg( 'config-information' ),
608 "<div class=\"config-info-right\">\n" .
609 $this->parse( $text ) . "\n" .
611 "<div style=\"clear: left;\"></div>\n" .
617 * Get small text indented help for a preceding form field.
618 * Parameters like wfMsg().
620 public function getHelpBox( $msg /*, ... */ ) {
621 $args = func_get_args();
622 array_shift( $args );
623 $args = array_map( 'htmlspecialchars', $args );
625 $text = wfMsgReal( $msg, $args, false, false, false );
626 $html = $this->parse( $text, true );
627 $id = $this->helpId++
;
628 $alt = wfMsg( 'help' );
631 "<div class=\"config-help-wrapper\">\n" .
632 "<div class=\"config-help-message\">\n" .
635 "<div class=\"config-show-help\">\n" .
637 wfMsgHtml( 'config-show-help' ) .
639 "<div class=\"config-hide-help\">\n" .
641 wfMsgHtml( 'config-hide-help' ) .
642 "</a></div>\n</div>\n";
648 public function showHelpBox( $msg /*, ... */ ) {
649 $args = func_get_args();
650 $html = call_user_func_array( array( $this, 'getHelpBox' ), $args );
651 $this->output
->addHTML( $html );
655 * Show a short informational message.
656 * Output looks like a list.
660 public function showMessage( $msg /*, ... */ ) {
661 $args = func_get_args();
662 array_shift( $args );
663 $html = '<div class="config-message">' .
664 $this->parse( wfMsgReal( $msg, $args, false, false, false ) ) .
666 $this->output
->addHTML( $html );
670 * @param Status $status
672 public function showStatusMessage( Status
$status ) {
673 $text = $status->getWikiText();
674 $this->output
->addWikiText(
675 "<div class=\"config-message\">\n" .
682 * Label a control by wrapping a config-input div around it and putting a
685 public function label( $msg, $forId, $contents ) {
686 if ( strval( $msg ) == '' ) {
687 $labelText = ' ';
689 $labelText = wfMsgHtml( $msg );
692 $attributes = array( 'class' => 'config-label' );
695 $attributes['for'] = $forId;
699 "<div class=\"config-input\">\n" .
702 $labelText ) . "\n" .
708 * Get a labelled text box to configure a variable.
710 * @param $params Array
712 * var: The variable to be configured (required)
713 * label: The message name for the label (required)
714 * attribs: Additional attributes for the input element (optional)
715 * controlName: The name for the input element (optional)
716 * value: The current value of the variable (optional)
718 public function getTextBox( $params ) {
719 if ( !isset( $params['controlName'] ) ) {
720 $params['controlName'] = 'config_' . $params['var'];
723 if ( !isset( $params['value'] ) ) {
724 $params['value'] = $this->getVar( $params['var'] );
727 if ( !isset( $params['attribs'] ) ) {
728 $params['attribs'] = array();
734 $params['controlName'],
736 $params['controlName'],
737 30, // intended to be overridden by CSS
739 $params['attribs'] +
array(
740 'id' => $params['controlName'],
741 'class' => 'config-input-text',
742 'tabindex' => $this->nextTabIndex()
749 * Get a labelled password box to configure a variable.
751 * Implements password hiding
752 * @param $params Array
754 * var: The variable to be configured (required)
755 * label: The message name for the label (required)
756 * attribs: Additional attributes for the input element (optional)
757 * controlName: The name for the input element (optional)
758 * value: The current value of the variable (optional)
760 public function getPasswordBox( $params ) {
761 if ( !isset( $params['value'] ) ) {
762 $params['value'] = $this->getVar( $params['var'] );
765 if ( !isset( $params['attribs'] ) ) {
766 $params['attribs'] = array();
769 $params['value'] = $this->getFakePassword( $params['value'] );
770 $params['attribs']['type'] = 'password';
772 return $this->getTextBox( $params );
776 * Get a labelled checkbox to configure a boolean variable.
778 * @param $params Array
780 * var: The variable to be configured (required)
781 * label: The message name for the label (required)
782 * attribs: Additional attributes for the input element (optional)
783 * controlName: The name for the input element (optional)
784 * value: The current value of the variable (optional)
786 public function getCheckBox( $params ) {
787 if ( !isset( $params['controlName'] ) ) {
788 $params['controlName'] = 'config_' . $params['var'];
791 if ( !isset( $params['value'] ) ) {
792 $params['value'] = $this->getVar( $params['var'] );
795 if ( !isset( $params['attribs'] ) ) {
796 $params['attribs'] = array();
799 if( isset( $params['rawtext'] ) ) {
800 $labelText = $params['rawtext'];
802 $labelText = $this->parse( wfMsg( $params['label'] ) );
806 "<div class=\"config-input-check\">\n" .
809 $params['controlName'],
811 $params['attribs'] +
array(
812 'id' => $params['controlName'],
813 'class' => 'config-input-text',
814 'tabindex' => $this->nextTabIndex(),
823 * Get a set of labelled radio buttons.
825 * @param $params Array
827 * var: The variable to be configured (required)
828 * label: The message name for the label (required)
829 * itemLabelPrefix: The message name prefix for the item labels (required)
830 * values: List of allowed values (required)
831 * itemAttribs Array of attribute arrays, outer key is the value name (optional)
832 * commonAttribs Attribute array applied to all items
833 * controlName: The name for the input element (optional)
834 * value: The current value of the variable (optional)
836 public function getRadioSet( $params ) {
837 if ( !isset( $params['controlName'] ) ) {
838 $params['controlName'] = 'config_' . $params['var'];
841 if ( !isset( $params['value'] ) ) {
842 $params['value'] = $this->getVar( $params['var'] );
845 if ( !isset( $params['label'] ) ) {
848 $label = $this->parse( wfMsgNoTrans( $params['label'] ) );
851 $s = "<label class=\"config-label\">\n" .
854 "<ul class=\"config-settings-block\">\n";
855 foreach ( $params['values'] as $value ) {
856 $itemAttribs = array();
858 if ( isset( $params['commonAttribs'] ) ) {
859 $itemAttribs = $params['commonAttribs'];
862 if ( isset( $params['itemAttribs'][$value] ) ) {
863 $itemAttribs = $params['itemAttribs'][$value] +
$itemAttribs;
866 $checked = $value == $params['value'];
867 $id = $params['controlName'] . '_' . $value;
868 $itemAttribs['id'] = $id;
869 $itemAttribs['tabindex'] = $this->nextTabIndex();
873 Xml
::radio( $params['controlName'], $value, $checked, $itemAttribs ) .
875 Xml
::tags( 'label', array( 'for' => $id ), $this->parse(
876 wfMsgNoTrans( $params['itemLabelPrefix'] . strtolower( $value ) )
886 * Output an error or warning box using a Status object.
888 public function showStatusBox( $status ) {
889 if( !$status->isGood() ) {
890 $text = $status->getWikiText();
892 if( $status->isOk() ) {
893 $box = $this->getWarningBox( $text );
895 $box = $this->getErrorBox( $text );
898 $this->output
->addHTML( $box );
903 * Convenience function to set variables based on form data.
904 * Assumes that variables containing "password" in the name are (potentially
907 * @param $varNames Array
908 * @param $prefix String: the prefix added to variables to obtain form names
910 public function setVarsFromRequest( $varNames, $prefix = 'config_' ) {
911 $newValues = array();
913 foreach ( $varNames as $name ) {
914 $value = trim( $this->request
->getVal( $prefix . $name ) );
915 $newValues[$name] = $value;
917 if ( $value === null ) {
919 $this->setVar( $name, false );
921 if ( stripos( $name, 'password' ) !== false ) {
922 $this->setPassword( $name, $value );
924 $this->setVar( $name, $value );
933 * Get the starting tags of a fieldset.
935 * @param $legend String: message name
937 public function getFieldsetStart( $legend ) {
938 return "\n<fieldset><legend>" . wfMsgHtml( $legend ) . "</legend>\n";
942 * Get the end tag of a fieldset.
944 public function getFieldsetEnd() {
945 return "</fieldset>\n";
949 * Helper for Installer::docLink()
951 public function getDocUrl( $page ) {
952 $url = "{$_SERVER['PHP_SELF']}?page=" . urlencode( $page );
954 if ( in_array( $this->currentPageName
, $this->pageSequence
) ) {
955 $url .= '&lastPage=' . urlencode( $this->currentPageName
);