3 * Core installer web interface.
10 * Class for the core installer web interface.
15 class WebInstaller
extends Installer
{
18 * @var WebInstallerOutput
30 * Cached session array.
37 * Captured PHP error text. Temporary.
43 * The main sequence of page names. These will be displayed in turn.
46 * * Add a config-page-<name> message
47 * * Add a WebInstaller_<name> class
50 public $pageSequence = array(
64 * Out of sequence pages, selectable by the user at any time.
67 protected $otherPages = array(
72 'UpgradeDoc', // Can't use Upgrade due to Upgrade step
76 * Array of pages which have declared that they have been submitted, have validated
77 * their input, and need no further processing.
80 protected $happyPages;
83 * List of "skipped" pages. These are pages that will automatically continue
84 * to the next page on any GET request. To avoid breaking the "back" button,
85 * they need to be skipped during a back operation.
88 protected $skippedPages;
91 * Flag indicating that session data may have been lost.
94 public $showSessionWarning = false;
97 * Numeric index of the page we're on
100 protected $tabIndex = 1;
103 * Name of the page we're on
106 protected $currentPageName;
111 * @param $request WebRequest
113 public function __construct( WebRequest
$request ) {
114 parent
::__construct();
115 $this->output
= new WebInstallerOutput( $this );
116 $this->request
= $request;
120 $wgParser->setHook( 'downloadlink', array( $this, 'downloadLinkHook' ) );
121 $wgParser->setHook( 'doclink', array( $this, 'docLink' ) );
127 * @param $session Array: initial session array
129 * @return Array: new session array
131 public function execute( array $session ) {
132 $this->session
= $session;
134 if ( isset( $session['settings'] ) ) {
135 $this->settings
= $session['settings'] +
$this->settings
;
139 $this->setupLanguage();
141 if( ( $this->getVar( '_InstallDone' ) ||
$this->getVar( '_UpgradeDone' ) )
142 && $this->request
->getVal( 'localsettings' ) )
144 $this->request
->response()->header( 'Content-type: application/x-httpd-php' );
145 $this->request
->response()->header(
146 'Content-Disposition: attachment; filename="LocalSettings.php"'
149 $ls = new LocalSettingsGenerator( $this );
150 $rightsProfile = $this->rightsProfiles
[$this->getVar( '_RightsProfile' )];
151 foreach( $rightsProfile as $group => $rightsArr ) {
152 $ls->setGroupRights( $group, $rightsArr );
155 return $this->session
;
158 $cssDir = $this->request
->getVal( 'css' );
160 $cssDir = ( $cssDir == 'rtl' ?
'rtl' : 'ltr' );
161 $this->request
->response()->header( 'Content-type: text/css' );
162 echo $this->output
->getCSS( $cssDir );
163 return $this->session
;
166 if ( isset( $session['happyPages'] ) ) {
167 $this->happyPages
= $session['happyPages'];
169 $this->happyPages
= array();
172 if ( isset( $session['skippedPages'] ) ) {
173 $this->skippedPages
= $session['skippedPages'];
175 $this->skippedPages
= array();
178 $lowestUnhappy = $this->getLowestUnhappy();
180 # Special case for Creative Commons partner chooser box.
181 if ( $this->request
->getVal( 'SubmitCC' ) ) {
182 $page = $this->getPageByName( 'Options' );
183 $this->output
->useShortHeader();
184 $this->output
->allowFrames();
186 return $this->finish();
189 if ( $this->request
->getVal( 'ShowCC' ) ) {
190 $page = $this->getPageByName( 'Options' );
191 $this->output
->useShortHeader();
192 $this->output
->allowFrames();
193 $this->output
->addHTML( $page->getCCDoneBox() );
194 return $this->finish();
198 $pageName = $this->request
->getVal( 'page' );
200 if ( in_array( $pageName, $this->otherPages
) ) {
203 $page = $this->getPageByName( $pageName );
206 if ( !$pageName ||
!in_array( $pageName, $this->pageSequence
) ) {
207 $pageId = $lowestUnhappy;
209 $pageId = array_search( $pageName, $this->pageSequence
);
212 # If necessary, move back to the lowest-numbered unhappy page
213 if ( $pageId > $lowestUnhappy ) {
214 $pageId = $lowestUnhappy;
215 if ( $lowestUnhappy == 0 ) {
216 # Knocked back to start, possible loss of session data.
217 $this->showSessionWarning
= true;
221 $pageName = $this->pageSequence
[$pageId];
222 $page = $this->getPageByName( $pageName );
225 # If a back button was submitted, go back without submitting the form data.
226 if ( $this->request
->wasPosted() && $this->request
->getBool( 'submit-back' ) ) {
227 if ( $this->request
->getVal( 'lastPage' ) ) {
228 $nextPage = $this->request
->getVal( 'lastPage' );
229 } elseif ( $pageId !== false ) {
231 # Skip the skipped pages
232 $nextPageId = $pageId;
236 $nextPage = $this->pageSequence
[$nextPageId];
237 } while( isset( $this->skippedPages
[$nextPage] ) );
239 $nextPage = $this->pageSequence
[$lowestUnhappy];
242 $this->output
->redirect( $this->getUrl( array( 'page' => $nextPage ) ) );
243 return $this->finish();
247 $this->currentPageName
= $page->getName();
248 $this->startPageWrapper( $pageName );
250 $result = $page->execute();
252 $this->endPageWrapper();
254 if ( $result == 'skip' ) {
255 # Page skipped without explicit submission.
256 # Skip it when we click "back" so that we don't just go forward again.
257 $this->skippedPages
[$pageName] = true;
258 $result = 'continue';
260 unset( $this->skippedPages
[$pageName] );
263 # If it was posted, the page can request a continue to the next page.
264 if ( $result === 'continue' && !$this->output
->headerDone() ) {
265 if ( $pageId !== false ) {
266 $this->happyPages
[$pageId] = true;
269 $lowestUnhappy = $this->getLowestUnhappy();
271 if ( $this->request
->getVal( 'lastPage' ) ) {
272 $nextPage = $this->request
->getVal( 'lastPage' );
273 } elseif ( $pageId !== false ) {
274 $nextPage = $this->pageSequence
[$pageId +
1];
276 $nextPage = $this->pageSequence
[$lowestUnhappy];
279 if ( array_search( $nextPage, $this->pageSequence
) > $lowestUnhappy ) {
280 $nextPage = $this->pageSequence
[$lowestUnhappy];
283 $this->output
->redirect( $this->getUrl( array( 'page' => $nextPage ) ) );
286 return $this->finish();
290 * Find the next page in sequence that hasn't been completed
293 public function getLowestUnhappy() {
294 if ( count( $this->happyPages
) == 0 ) {
297 return max( array_keys( $this->happyPages
) ) +
1;
302 * Start the PHP session. This may be called before execute() to start the PHP session.
306 public function startSession() {
307 if( wfIniGetBool( 'session.auto_start' ) ||
session_id() ) {
312 $this->phpErrors
= array();
313 set_error_handler( array( $this, 'errorHandler' ) );
315 restore_error_handler();
317 if ( $this->phpErrors
) {
318 $this->showError( 'config-session-error', $this->phpErrors
[0] );
326 * Get a hash of data identifying this MW installation.
328 * This is used by mw-config/index.php to prevent multiple installations of MW
329 * on the same cookie domain from interfering with each other.
333 public function getFingerprint() {
334 // Get the base URL of the installation
335 $url = $this->request
->getFullRequestURL();
336 if ( preg_match( '!^(.*\?)!', $url, $m) ) {
340 if ( preg_match( '!^(.*)/[^/]*/[^/]*$!', $url, $m ) ) {
341 // This... seems to try to get the base path from
342 // the /mw-config/index.php. Kinda scary though?
345 return md5( serialize( array(
346 'local path' => dirname( dirname( __FILE__
) ),
348 'version' => $GLOBALS['wgVersion']
353 * Show an error message in a box. Parameters are like wfMsg().
355 public function showError( $msg /*...*/ ) {
356 $args = func_get_args();
357 array_shift( $args );
358 $args = array_map( 'htmlspecialchars', $args );
359 $msg = wfMsgReal( $msg, $args, false, false, false );
360 $this->output
->addHTML( $this->getErrorBox( $msg ) );
364 * Temporary error handler for session start debugging.
366 public function errorHandler( $errno, $errstr ) {
367 $this->phpErrors
[] = $errstr;
371 * Clean up from execute()
375 public function finish() {
376 $this->output
->output();
378 $this->session
['happyPages'] = $this->happyPages
;
379 $this->session
['skippedPages'] = $this->skippedPages
;
380 $this->session
['settings'] = $this->settings
;
382 return $this->session
;
386 * We're restarting the installation, reset the session, happyPages, etc
388 public function reset() {
389 $this->session
= array();
390 $this->happyPages
= array();
391 $this->settings
= array();
395 * Get a URL for submission back to the same script.
397 * @param $query array
400 public function getUrl( $query = array() ) {
401 $url = $this->request
->getRequestURL();
402 # Remove existing query
403 $url = preg_replace( '/\?.*$/', '', $url );
406 $url .= '?' . wfArrayToCGI( $query );
413 * Get a WebInstallerPage by name.
415 * @param $pageName String
416 * @return WebInstallerPage
418 public function getPageByName( $pageName ) {
419 $pageClass = 'WebInstaller_' . $pageName;
421 return new $pageClass( $this );
425 * Get a session variable.
427 * @param $name String
430 public function getSession( $name, $default = null ) {
431 if ( !isset( $this->session
[$name] ) ) {
434 return $this->session
[$name];
439 * Set a session variable.
440 * @param $name String key for the variable
441 * @param $value Mixed
443 public function setSession( $name, $value ) {
444 $this->session
[$name] = $value;
448 * Get the next tabindex attribute value.
451 public function nextTabIndex() {
452 return $this->tabIndex++
;
456 * Initializes language-related variables.
458 public function setupLanguage() {
459 global $wgLang, $wgContLang, $wgLanguageCode;
461 if ( $this->getSession( 'test' ) === null && !$this->request
->wasPosted() ) {
462 $wgLanguageCode = $this->getAcceptLanguage();
463 $wgLang = $wgContLang = Language
::factory( $wgLanguageCode );
464 $this->setVar( 'wgLanguageCode', $wgLanguageCode );
465 $this->setVar( '_UserLang', $wgLanguageCode );
467 $wgLanguageCode = $this->getVar( 'wgLanguageCode' );
468 $wgLang = Language
::factory( $this->getVar( '_UserLang' ) );
469 $wgContLang = Language
::factory( $wgLanguageCode );
474 * Retrieves MediaWiki language from Accept-Language HTTP header.
478 public function getAcceptLanguage() {
479 global $wgLanguageCode, $wgRequest;
481 $mwLanguages = Language
::getLanguageNames();
482 $headerLanguages = array_keys( $wgRequest->getAcceptLang() );
484 foreach ( $headerLanguages as $lang ) {
485 if ( isset( $mwLanguages[$lang] ) ) {
490 return $wgLanguageCode;
494 * Called by execute() before page output starts, to show a page list.
496 * @param $currentPageName String
498 private function startPageWrapper( $currentPageName ) {
499 $s = "<div class=\"config-page-wrapper\">\n";
500 $s .= "<div class=\"config-page\">\n";
501 $s .= "<div class=\"config-page-list\"><ul>\n";
504 foreach ( $this->pageSequence
as $id => $pageName ) {
505 $happy = !empty( $this->happyPages
[$id] );
506 $s .= $this->getPageListItem(
508 $happy ||
$lastHappy == $id - 1,
517 $s .= "</ul><br/><ul>\n";
518 $s .= $this->getPageListItem( 'Restart', true, $currentPageName );
519 $s .= "</ul></div>\n"; // end list pane
520 $s .= Html
::element( 'h2', array(),
521 wfMsg( 'config-page-' . strtolower( $currentPageName ) ) );
523 $this->output
->addHTMLNoFlush( $s );
527 * Get a list item for the page list.
529 * @param $pageName String
530 * @param $enabled Boolean
531 * @param $currentPageName String
535 private function getPageListItem( $pageName, $enabled, $currentPageName ) {
536 $s = "<li class=\"config-page-list-item\">";
537 $name = wfMsg( 'config-page-' . strtolower( $pageName ) );
540 $query = array( 'page' => $pageName );
542 if ( !in_array( $pageName, $this->pageSequence
) ) {
543 if ( in_array( $currentPageName, $this->pageSequence
) ) {
544 $query['lastPage'] = $currentPageName;
547 $link = Html
::element( 'a',
549 'href' => $this->getUrl( $query )
554 $link = htmlspecialchars( $name );
557 if ( $pageName == $currentPageName ) {
558 $s .= "<span class=\"config-page-current\">$link</span>";
563 $s .= Html
::element( 'span',
565 'class' => 'config-page-disabled'
577 * Output some stuff after a page is finished.
579 private function endPageWrapper() {
580 $this->output
->addHTMLNoFlush(
581 "<div class=\"visualClear\"></div>\n" .
583 "<div class=\"visualClear\"></div>\n" .
588 * Get HTML for an error box with an icon.
590 * @param $text String: wikitext, get this with wfMsgNoTrans()
594 public function getErrorBox( $text ) {
595 return $this->getInfoBox( $text, 'critical-32.png', 'config-error-box' );
599 * Get HTML for a warning box with an icon.
601 * @param $text String: wikitext, get this with wfMsgNoTrans()
605 public function getWarningBox( $text ) {
606 return $this->getInfoBox( $text, 'warning-32.png', 'config-warning-box' );
610 * Get HTML for an info box with an icon.
612 * @param $text String: wikitext, get this with wfMsgNoTrans()
613 * @param $icon String: icon name, file in skins/common/images
614 * @param $class String: additional class name to add to the wrapper div
618 public function getInfoBox( $text, $icon = false, $class = false ) {
619 $text = $this->parse( $text, true );
620 $icon = ( $icon == false ) ?
'../skins/common/images/info-32.png' : '../skins/common/images/'.$icon;
621 $alt = wfMsg( 'config-information' );
622 return Html
::infoBox( $text, $icon, $alt, $class, false );
626 * Get small text indented help for a preceding form field.
627 * Parameters like wfMsg().
631 public function getHelpBox( $msg /*, ... */ ) {
632 $args = func_get_args();
633 array_shift( $args );
634 $args = array_map( 'htmlspecialchars', $args );
635 $text = wfMsgReal( $msg, $args, false, false, false );
636 $html = htmlspecialchars( $text );
637 $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" .
647 * @param $msg String key for wfMsg()
649 public function showHelpBox( $msg /*, ... */ ) {
650 $args = func_get_args();
651 $html = call_user_func_array( array( $this, 'getHelpBox' ), $args );
652 $this->output
->addHTML( $html );
656 * Show a short informational message.
657 * Output looks like a list.
661 public function showMessage( $msg /*, ... */ ) {
662 $args = func_get_args();
663 array_shift( $args );
664 $html = '<div class="config-message">' .
665 $this->parse( wfMsgReal( $msg, $args, false, false, false ) ) .
667 $this->output
->addHTML( $html );
671 * @param $status Status
673 public function showStatusMessage( Status
$status ) {
674 $text = $status->getWikiText();
675 $this->output
->addWikiText(
676 "<div class=\"config-message\">\n" .
683 * Label a control by wrapping a config-input div around it and putting a
688 public function label( $msg, $forId, $contents, $helpData = "" ) {
689 if ( strval( $msg ) == '' ) {
690 $labelText = ' ';
692 $labelText = wfMsgHtml( $msg );
695 $attributes = array( 'class' => 'config-label' );
698 $attributes['for'] = $forId;
702 "<div class=\"config-block\">\n" .
703 " <div class=\"config-block-label\">\n" .
706 $labelText ) . "\n" .
709 " <div class=\"config-block-elements\">\n" .
716 * Get a labelled text box to configure a variable.
718 * @param $params Array
720 * var: The variable to be configured (required)
721 * label: The message name for the label (required)
722 * attribs: Additional attributes for the input element (optional)
723 * controlName: The name for the input element (optional)
724 * value: The current value of the variable (optional)
725 * help: The html for the help text (optional)
729 public function getTextBox( $params ) {
730 if ( !isset( $params['controlName'] ) ) {
731 $params['controlName'] = 'config_' . $params['var'];
734 if ( !isset( $params['value'] ) ) {
735 $params['value'] = $this->getVar( $params['var'] );
738 if ( !isset( $params['attribs'] ) ) {
739 $params['attribs'] = array();
741 if ( !isset( $params['help'] ) ) {
742 $params['help'] = "";
747 $params['controlName'],
749 $params['controlName'],
750 30, // intended to be overridden by CSS
752 $params['attribs'] +
array(
753 'id' => $params['controlName'],
754 'class' => 'config-input-text',
755 'tabindex' => $this->nextTabIndex()
763 * Get a labelled textarea to configure a variable
765 * @param $params Array
767 * var: The variable to be configured (required)
768 * label: The message name for the label (required)
769 * attribs: Additional attributes for the input element (optional)
770 * controlName: The name for the input element (optional)
771 * value: The current value of the variable (optional)
772 * help: The html for the help text (optional)
776 public function getTextArea( $params ) {
777 if ( !isset( $params['controlName'] ) ) {
778 $params['controlName'] = 'config_' . $params['var'];
781 if ( !isset( $params['value'] ) ) {
782 $params['value'] = $this->getVar( $params['var'] );
785 if ( !isset( $params['attribs'] ) ) {
786 $params['attribs'] = array();
788 if ( !isset( $params['help'] ) ) {
789 $params['help'] = "";
794 $params['controlName'],
796 $params['controlName'],
800 $params['attribs'] +
array(
801 'id' => $params['controlName'],
802 'class' => 'config-input-text',
803 'tabindex' => $this->nextTabIndex()
811 * Get a labelled password box to configure a variable.
813 * Implements password hiding
814 * @param $params Array
816 * var: The variable to be configured (required)
817 * label: The message name for the label (required)
818 * attribs: Additional attributes for the input element (optional)
819 * controlName: The name for the input element (optional)
820 * value: The current value of the variable (optional)
821 * help: The html for the help text (optional)
825 public function getPasswordBox( $params ) {
826 if ( !isset( $params['value'] ) ) {
827 $params['value'] = $this->getVar( $params['var'] );
830 if ( !isset( $params['attribs'] ) ) {
831 $params['attribs'] = array();
834 $params['value'] = $this->getFakePassword( $params['value'] );
835 $params['attribs']['type'] = 'password';
837 return $this->getTextBox( $params );
841 * Get a labelled checkbox to configure a boolean variable.
843 * @param $params Array
845 * var: The variable to be configured (required)
846 * label: The message name for the label (required)
847 * attribs: Additional attributes for the input element (optional)
848 * controlName: The name for the input element (optional)
849 * value: The current value of the variable (optional)
850 * help: The html for the help text (optional)
854 public function getCheckBox( $params ) {
855 if ( !isset( $params['controlName'] ) ) {
856 $params['controlName'] = 'config_' . $params['var'];
859 if ( !isset( $params['value'] ) ) {
860 $params['value'] = $this->getVar( $params['var'] );
863 if ( !isset( $params['attribs'] ) ) {
864 $params['attribs'] = array();
866 if ( !isset( $params['help'] ) ) {
867 $params['help'] = "";
869 if( isset( $params['rawtext'] ) ) {
870 $labelText = $params['rawtext'];
872 $labelText = $this->parse( wfMsg( $params['label'] ) );
876 "<div class=\"config-input-check\">\n" .
880 $params['controlName'],
882 $params['attribs'] +
array(
883 'id' => $params['controlName'],
884 'tabindex' => $this->nextTabIndex(),
893 * Get a set of labelled radio buttons.
895 * @param $params Array
897 * var: The variable to be configured (required)
898 * label: The message name for the label (required)
899 * itemLabelPrefix: The message name prefix for the item labels (required)
900 * values: List of allowed values (required)
901 * itemAttribs Array of attribute arrays, outer key is the value name (optional)
902 * commonAttribs Attribute array applied to all items
903 * controlName: The name for the input element (optional)
904 * value: The current value of the variable (optional)
905 * help: The html for the help text (optional)
909 public function getRadioSet( $params ) {
910 if ( !isset( $params['controlName'] ) ) {
911 $params['controlName'] = 'config_' . $params['var'];
914 if ( !isset( $params['value'] ) ) {
915 $params['value'] = $this->getVar( $params['var'] );
918 if ( !isset( $params['label'] ) ) {
921 $label = $params['label'];
923 if ( !isset( $params['help'] ) ) {
924 $params['help'] = "";
927 foreach ( $params['values'] as $value ) {
928 $itemAttribs = array();
930 if ( isset( $params['commonAttribs'] ) ) {
931 $itemAttribs = $params['commonAttribs'];
934 if ( isset( $params['itemAttribs'][$value] ) ) {
935 $itemAttribs = $params['itemAttribs'][$value] +
$itemAttribs;
938 $checked = $value == $params['value'];
939 $id = $params['controlName'] . '_' . $value;
940 $itemAttribs['id'] = $id;
941 $itemAttribs['tabindex'] = $this->nextTabIndex();
945 Xml
::radio( $params['controlName'], $value, $checked, $itemAttribs ) .
947 Xml
::tags( 'label', array( 'for' => $id ), $this->parse(
948 wfMsgNoTrans( $params['itemLabelPrefix'] . strtolower( $value ) )
955 return $this->label( $label, $params['controlName'], $s, $params['help'] );
959 * Output an error or warning box using a Status object.
961 * @param $status Status
963 public function showStatusBox( $status ) {
964 if( !$status->isGood() ) {
965 $text = $status->getWikiText();
967 if( $status->isOk() ) {
968 $box = $this->getWarningBox( $text );
970 $box = $this->getErrorBox( $text );
973 $this->output
->addHTML( $box );
978 * Convenience function to set variables based on form data.
979 * Assumes that variables containing "password" in the name are (potentially
982 * @param $varNames Array
983 * @param $prefix String: the prefix added to variables to obtain form names
987 public function setVarsFromRequest( $varNames, $prefix = 'config_' ) {
988 $newValues = array();
990 foreach ( $varNames as $name ) {
991 $value = trim( $this->request
->getVal( $prefix . $name ) );
992 $newValues[$name] = $value;
994 if ( $value === null ) {
996 $this->setVar( $name, false );
998 if ( stripos( $name, 'password' ) !== false ) {
999 $this->setPassword( $name, $value );
1001 $this->setVar( $name, $value );
1010 * Helper for Installer::docLink()
1014 protected function getDocUrl( $page ) {
1015 $url = "{$_SERVER['PHP_SELF']}?page=" . urlencode( $page );
1017 if ( in_array( $this->currentPageName
, $this->pageSequence
) ) {
1018 $url .= '&lastPage=' . urlencode( $this->currentPageName
);
1025 * Extension tag hook for a documentation link.
1029 public function docLink( $linkText, $attribs, $parser ) {
1030 $url = $this->getDocUrl( $attribs['href'] );
1031 return '<a href="' . htmlspecialchars( $url ) . '">' .
1032 htmlspecialchars( $linkText ) .
1037 * Helper for "Download LocalSettings" link on WebInstall_Complete
1039 * @return String Html for download link
1041 public function downloadLinkHook( $text, $attribs, $parser ) {
1042 $img = Html
::element( 'img', array(
1043 'src' => '../skins/common/images/download-32.png',
1047 $anchor = Html
::rawElement( 'a',
1048 array( 'href' => $this->getURL( array( 'localsettings' => 1 ) ) ),
1049 $img . ' ' . wfMsgHtml( 'config-download-localsettings' ) );
1050 return Html
::rawElement( 'div', array( 'class' => 'config-download-link' ), $anchor );