9d223eb4836023a81e3b6307baa37a583d62ef98
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 * Get a hash of data identifying this MW installation.
337 * This is used by config/index.php to prevent multiple installations of MW
338 * on the same cookie domain from interfering with each other.
340 public function getFingerprint() {
341 // Get the base URL of the installation
342 $url = $this->request
->getFullRequestURL();
343 if ( preg_match( '!^(.*)/[^/]*/[^/]*$!', $url, $m ) ) {
346 return md5( serialize( array(
347 'local path' => dirname( dirname( __FILE__
) ),
349 'version' => $GLOBALS['wgVersion']
354 * Show an error message in a box. Parameters are like wfMsg().
356 public function showError( $msg /*...*/ ) {
357 $args = func_get_args();
358 array_shift( $args );
359 $args = array_map( 'htmlspecialchars', $args );
360 $msg = wfMsgReal( $msg, $args, false, false, false );
361 $this->output
->addHTML( $this->getErrorBox( $msg ) );
365 * Temporary error handler for session start debugging.
367 public function errorHandler( $errno, $errstr ) {
368 $this->phpErrors
[] = $errstr;
372 * Clean up from execute()
376 public function finish() {
377 $this->output
->output();
379 $this->session
['happyPages'] = $this->happyPages
;
380 $this->session
['skippedPages'] = $this->skippedPages
;
381 $this->session
['settings'] = $this->settings
;
383 return $this->session
;
387 * Get a URL for submission back to the same script.
389 * @param $query: Array
391 public function getUrl( $query = array() ) {
392 $url = $this->request
->getRequestURL();
393 # Remove existing query
394 $url = preg_replace( '/\?.*$/', '', $url );
397 $url .= '?' . wfArrayToCGI( $query );
404 * Get a WebInstallerPage by name.
406 * @param $pageName String
408 * @return WebInstallerPage
410 public function getPageByName( $pageName ) {
411 // Totally lame way to force autoload of WebInstallerPage.php
412 class_exists( 'WebInstallerPage' );
414 $pageClass = 'WebInstaller_' . $pageName;
416 return new $pageClass( $this );
420 * Get a session variable.
422 * @param $name String
425 public function getSession( $name, $default = null ) {
426 if ( !isset( $this->session
[$name] ) ) {
429 return $this->session
[$name];
434 * Set a session variable.
436 public function setSession( $name, $value ) {
437 $this->session
[$name] = $value;
441 * Get the next tabindex attribute value.
443 public function nextTabIndex() {
444 return $this->tabIndex++
;
448 * Initializes language-related variables.
450 public function setupLanguage() {
451 global $wgLang, $wgContLang, $wgLanguageCode;
453 if ( $this->getSession( 'test' ) === null && !$this->request
->wasPosted() ) {
454 $wgLanguageCode = $this->getAcceptLanguage();
455 $wgLang = $wgContLang = Language
::factory( $wgLanguageCode );
456 $this->setVar( 'wgLanguageCode', $wgLanguageCode );
457 $this->setVar( '_UserLang', $wgLanguageCode );
459 $wgLanguageCode = $this->getVar( 'wgLanguageCode' );
460 $wgLang = Language
::factory( $this->getVar( '_UserLang' ) );
461 $wgContLang = Language
::factory( $wgLanguageCode );
466 * Retrieves MediaWiki language from Accept-Language HTTP header.
470 public function getAcceptLanguage() {
471 global $wgLanguageCode, $wgRequest;
473 $mwLanguages = Language
::getLanguageNames();
474 $headerLanguages = array_keys( $wgRequest->getAcceptLang() );
476 foreach ( $headerLanguages as $lang ) {
477 if ( isset( $mwLanguages[$lang] ) ) {
482 return $wgLanguageCode;
486 * Called by execute() before page output starts, to show a page list.
488 * @param $currentPageName String
490 private function startPageWrapper( $currentPageName ) {
491 $s = "<div class=\"config-page-wrapper\">\n";
492 $s .= "<div class=\"config-page\">\n";
493 $s .= "<div class=\"config-page-list\"><ul>\n";
496 foreach ( $this->pageSequence
as $id => $pageName ) {
497 $happy = !empty( $this->happyPages
[$id] );
498 $s .= $this->getPageListItem(
500 $happy ||
$lastHappy == $id - 1,
509 $s .= "</ul><br/><ul>\n";
511 foreach ( $this->otherPages
as $pageName ) {
512 $s .= $this->getPageListItem( $pageName, true, $currentPageName );
515 $s .= "</ul></div>\n"; // end list pane
516 $s .= Html
::element( 'h2', array(),
517 wfMsg( 'config-page-' . strtolower( $currentPageName ) ) );
519 $this->output
->addHTMLNoFlush( $s );
523 * Get a list item for the page list.
525 * @param $pageName String
526 * @param $enabled Boolean
527 * @param $currentPageName String
531 private function getPageListItem( $pageName, $enabled, $currentPageName ) {
532 $s = "<li class=\"config-page-list-item\">";
533 $name = wfMsg( 'config-page-' . strtolower( $pageName ) );
536 $query = array( 'page' => $pageName );
538 if ( !in_array( $pageName, $this->pageSequence
) ) {
539 if ( in_array( $currentPageName, $this->pageSequence
) ) {
540 $query['lastPage'] = $currentPageName;
543 $link = Html
::element( 'a',
545 'href' => $this->getUrl( $query )
550 $link = htmlspecialchars( $name );
553 if ( $pageName == $currentPageName ) {
554 $s .= "<span class=\"config-page-current\">$link</span>";
559 $s .= Html
::element( 'span',
561 'class' => 'config-page-disabled'
573 * Output some stuff after a page is finished.
575 private function endPageWrapper() {
576 $this->output
->addHTMLNoFlush(
578 "<br style=\"clear:both\"/>\n" .
583 * Get HTML for an error box with an icon.
585 * @param $text String: wikitext, get this with wfMsgNoTrans()
587 public function getErrorBox( $text ) {
588 return $this->getInfoBox( $text, 'critical-32.png', 'config-error-box' );
592 * Get HTML for a warning box with an icon.
594 * @param $text String: wikitext, get this with wfMsgNoTrans()
596 public function getWarningBox( $text ) {
597 return $this->getInfoBox( $text, 'warning-32.png', 'config-warning-box' );
601 * Get HTML for an info box with an icon.
603 * @param $text String: wikitext, get this with wfMsgNoTrans()
604 * @param $icon String: icon name, file in skins/common/images
605 * @param $class String: additional class name to add to the wrapper div
607 public function getInfoBox( $text, $icon = 'info-32.png', $class = false ) {
609 "<div class=\"config-info $class\">\n" .
610 "<div class=\"config-info-left\">\n" .
611 Html
::element( 'img',
613 'src' => '../skins/common/images/' . $icon,
614 'alt' => wfMsg( 'config-information' ),
618 "<div class=\"config-info-right\">\n" .
619 $this->parse( $text ) . "\n" .
621 "<div style=\"clear: left;\"></div>\n" .
627 * Get small text indented help for a preceding form field.
628 * Parameters like wfMsg().
630 public function getHelpBox( $msg /*, ... */ ) {
631 $args = func_get_args();
632 array_shift( $args );
633 $args = array_map( 'htmlspecialchars', $args );
634 $text = wfMsgReal( $msg, $args, false, false, false );
635 $html = htmlspecialchars( $text );
636 $html = $this->parse( $text, true );
639 return "<div class=\"mw-help-field-container\">\n" .
640 "<span class=\"mw-help-field-hint\">" . wfMsgHtml( 'config-help' ) . "</span>\n" .
641 "<span class=\"mw-help-field-data\">" . $html . "</span>\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, $helpData = "" ) {
686 if ( strval( $msg ) == '' ) {
687 $labelText = ' ';
689 $labelText = wfMsgHtml( $msg );
692 $attributes = array( 'class' => 'config-label' );
695 $attributes['for'] = $forId;
699 "<div class=\"config-block\">\n" .
700 " <div class=\"config-block-label\">\n" .
703 $labelText ) . "\n" .
706 " <div class=\"config-block-elements\">\n" .
713 * Get a labelled text box to configure a variable.
715 * @param $params Array
717 * var: The variable to be configured (required)
718 * label: The message name for the label (required)
719 * attribs: Additional attributes for the input element (optional)
720 * controlName: The name for the input element (optional)
721 * value: The current value of the variable (optional)
722 * help: The html for the help text (optional)
724 public function getTextBox( $params ) {
725 if ( !isset( $params['controlName'] ) ) {
726 $params['controlName'] = 'config_' . $params['var'];
729 if ( !isset( $params['value'] ) ) {
730 $params['value'] = $this->getVar( $params['var'] );
733 if ( !isset( $params['attribs'] ) ) {
734 $params['attribs'] = array();
736 if ( !isset( $params['help'] ) ) {
737 $params['help'] = "";
742 $params['controlName'],
744 $params['controlName'],
745 30, // intended to be overridden by CSS
747 $params['attribs'] +
array(
748 'id' => $params['controlName'],
749 'class' => 'config-input-text',
750 'tabindex' => $this->nextTabIndex()
758 * Get a labelled password box to configure a variable.
760 * Implements password hiding
761 * @param $params Array
763 * var: The variable to be configured (required)
764 * label: The message name for the label (required)
765 * attribs: Additional attributes for the input element (optional)
766 * controlName: The name for the input element (optional)
767 * value: The current value of the variable (optional)
768 * help: The html for the help text (optional)
770 public function getPasswordBox( $params ) {
771 if ( !isset( $params['value'] ) ) {
772 $params['value'] = $this->getVar( $params['var'] );
775 if ( !isset( $params['attribs'] ) ) {
776 $params['attribs'] = array();
779 $params['value'] = $this->getFakePassword( $params['value'] );
780 $params['attribs']['type'] = 'password';
782 return $this->getTextBox( $params );
786 * Get a labelled checkbox to configure a boolean variable.
788 * @param $params Array
790 * var: The variable to be configured (required)
791 * label: The message name for the label (required)
792 * attribs: Additional attributes for the input element (optional)
793 * controlName: The name for the input element (optional)
794 * value: The current value of the variable (optional)
795 * help: The html for the help text (optional)
797 public function getCheckBox( $params ) {
798 if ( !isset( $params['controlName'] ) ) {
799 $params['controlName'] = 'config_' . $params['var'];
802 if ( !isset( $params['value'] ) ) {
803 $params['value'] = $this->getVar( $params['var'] );
806 if ( !isset( $params['attribs'] ) ) {
807 $params['attribs'] = array();
809 if ( !isset( $params['help'] ) ) {
810 $params['help'] = "";
812 if( isset( $params['rawtext'] ) ) {
813 $labelText = $params['rawtext'];
815 $labelText = $this->parse( wfMsg( $params['label'] ) );
819 "<div class=\"config-input-check\">\n" .
823 $params['controlName'],
825 $params['attribs'] +
array(
826 'id' => $params['controlName'],
827 'tabindex' => $this->nextTabIndex(),
836 * Get a set of labelled radio buttons.
838 * @param $params Array
840 * var: The variable to be configured (required)
841 * label: The message name for the label (required)
842 * itemLabelPrefix: The message name prefix for the item labels (required)
843 * values: List of allowed values (required)
844 * itemAttribs Array of attribute arrays, outer key is the value name (optional)
845 * commonAttribs Attribute array applied to all items
846 * controlName: The name for the input element (optional)
847 * value: The current value of the variable (optional)
848 * help: The html for the help text (optional)
850 public function getRadioSet( $params ) {
851 if ( !isset( $params['controlName'] ) ) {
852 $params['controlName'] = 'config_' . $params['var'];
855 if ( !isset( $params['value'] ) ) {
856 $params['value'] = $this->getVar( $params['var'] );
859 if ( !isset( $params['label'] ) ) {
862 $label = $params['label'];
864 if ( !isset( $params['help'] ) ) {
865 $params['help'] = "";
868 foreach ( $params['values'] as $value ) {
869 $itemAttribs = array();
871 if ( isset( $params['commonAttribs'] ) ) {
872 $itemAttribs = $params['commonAttribs'];
875 if ( isset( $params['itemAttribs'][$value] ) ) {
876 $itemAttribs = $params['itemAttribs'][$value] +
$itemAttribs;
879 $checked = $value == $params['value'];
880 $id = $params['controlName'] . '_' . $value;
881 $itemAttribs['id'] = $id;
882 $itemAttribs['tabindex'] = $this->nextTabIndex();
886 Xml
::radio( $params['controlName'], $value, $checked, $itemAttribs ) .
888 Xml
::tags( 'label', array( 'for' => $id ), $this->parse(
889 wfMsgNoTrans( $params['itemLabelPrefix'] . strtolower( $value ) )
896 return $this->label( $label, $params['controlName'], $s, $params['help'] );
900 * Output an error or warning box using a Status object.
902 public function showStatusBox( $status ) {
903 if( !$status->isGood() ) {
904 $text = $status->getWikiText();
906 if( $status->isOk() ) {
907 $box = $this->getWarningBox( $text );
909 $box = $this->getErrorBox( $text );
912 $this->output
->addHTML( $box );
917 * Convenience function to set variables based on form data.
918 * Assumes that variables containing "password" in the name are (potentially
921 * @param $varNames Array
922 * @param $prefix String: the prefix added to variables to obtain form names
924 public function setVarsFromRequest( $varNames, $prefix = 'config_' ) {
925 $newValues = array();
927 foreach ( $varNames as $name ) {
928 $value = trim( $this->request
->getVal( $prefix . $name ) );
929 $newValues[$name] = $value;
931 if ( $value === null ) {
933 $this->setVar( $name, false );
935 if ( stripos( $name, 'password' ) !== false ) {
936 $this->setPassword( $name, $value );
938 $this->setVar( $name, $value );
947 * Helper for Installer::docLink()
949 protected function getDocUrl( $page ) {
950 $url = "{$_SERVER['PHP_SELF']}?page=" . urlencode( $page );
952 if ( in_array( $this->currentPageName
, $this->pageSequence
) ) {
953 $url .= '&lastPage=' . urlencode( $this->currentPageName
);