Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / installer / WebInstaller.php
blobc08212e39035956b966f9408ff2808ed0e2cf760
1 <?php
2 /**
3 * Core installer web interface.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Deployment
24 /**
25 * Class for the core installer web interface.
27 * @ingroup Deployment
28 * @since 1.17
30 class WebInstaller extends Installer {
32 /**
33 * @var WebInstallerOutput
35 public $output;
37 /**
38 * WebRequest object.
40 * @var WebRequest
42 public $request;
44 /**
45 * Cached session array.
47 * @var array[]
49 protected $session;
51 /**
52 * Captured PHP error text. Temporary.
54 * @var string[]
56 protected $phpErrors;
58 /**
59 * The main sequence of page names. These will be displayed in turn.
61 * To add a new installer page:
62 * * Add it to this WebInstaller::$pageSequence property
63 * * Add a "config-page-<name>" message
64 * * Add a "WebInstaller<name>" class
66 * @var string[]
68 public $pageSequence = [
69 'Language',
70 'ExistingWiki',
71 'Welcome',
72 'DBConnect',
73 'Upgrade',
74 'DBSettings',
75 'Name',
76 'Options',
77 'Install',
78 'Complete',
81 /**
82 * Out of sequence pages, selectable by the user at any time.
84 * @var string[]
86 protected $otherPages = [
87 'Restart',
88 'Readme',
89 'ReleaseNotes',
90 'Copying',
91 'UpgradeDoc', // Can't use Upgrade due to Upgrade step
94 /**
95 * Array of pages which have declared that they have been submitted, have validated
96 * their input, and need no further processing.
98 * @var bool[]
100 protected $happyPages;
103 * List of "skipped" pages. These are pages that will automatically continue
104 * to the next page on any GET request. To avoid breaking the "back" button,
105 * they need to be skipped during a back operation.
107 * @var bool[]
109 protected $skippedPages;
112 * Flag indicating that session data may have been lost.
114 * @var bool
116 public $showSessionWarning = false;
119 * Numeric index of the page we're on
121 * @var int
123 protected $tabIndex = 1;
126 * Name of the page we're on
128 * @var string
130 protected $currentPageName;
133 * Constructor.
135 * @param WebRequest $request
137 public function __construct( WebRequest $request ) {
138 parent::__construct();
139 $this->output = new WebInstallerOutput( $this );
140 $this->request = $request;
142 // Add parser hooks
143 global $wgParser;
144 $wgParser->setHook( 'downloadlink', [ $this, 'downloadLinkHook' ] );
145 $wgParser->setHook( 'doclink', [ $this, 'docLink' ] );
149 * Main entry point.
151 * @param array[] $session Initial session array
153 * @return array[] New session array
155 public function execute( array $session ) {
156 $this->session = $session;
158 if ( isset( $session['settings'] ) ) {
159 $this->settings = $session['settings'] + $this->settings;
162 $this->setupLanguage();
164 if ( ( $this->getVar( '_InstallDone' ) || $this->getVar( '_UpgradeDone' ) )
165 && $this->request->getVal( 'localsettings' )
167 $this->request->response()->header( 'Content-type: application/x-httpd-php' );
168 $this->request->response()->header(
169 'Content-Disposition: attachment; filename="LocalSettings.php"'
172 $ls = InstallerOverrides::getLocalSettingsGenerator( $this );
173 $rightsProfile = $this->rightsProfiles[$this->getVar( '_RightsProfile' )];
174 foreach ( $rightsProfile as $group => $rightsArr ) {
175 $ls->setGroupRights( $group, $rightsArr );
177 echo $ls->getText();
179 return $this->session;
182 $isCSS = $this->request->getVal( 'css' );
183 if ( $isCSS ) {
184 $this->outputCss();
185 return $this->session;
188 if ( isset( $session['happyPages'] ) ) {
189 $this->happyPages = $session['happyPages'];
190 } else {
191 $this->happyPages = [];
194 if ( isset( $session['skippedPages'] ) ) {
195 $this->skippedPages = $session['skippedPages'];
196 } else {
197 $this->skippedPages = [];
200 $lowestUnhappy = $this->getLowestUnhappy();
202 # Special case for Creative Commons partner chooser box.
203 if ( $this->request->getVal( 'SubmitCC' ) ) {
204 $page = $this->getPageByName( 'Options' );
205 $this->output->useShortHeader();
206 $this->output->allowFrames();
207 $page->submitCC();
209 return $this->finish();
212 if ( $this->request->getVal( 'ShowCC' ) ) {
213 $page = $this->getPageByName( 'Options' );
214 $this->output->useShortHeader();
215 $this->output->allowFrames();
216 $this->output->addHTML( $page->getCCDoneBox() );
218 return $this->finish();
221 # Get the page name.
222 $pageName = $this->request->getVal( 'page' );
224 if ( in_array( $pageName, $this->otherPages ) ) {
225 # Out of sequence
226 $pageId = false;
227 $page = $this->getPageByName( $pageName );
228 } else {
229 # Main sequence
230 if ( !$pageName || !in_array( $pageName, $this->pageSequence ) ) {
231 $pageId = $lowestUnhappy;
232 } else {
233 $pageId = array_search( $pageName, $this->pageSequence );
236 # If necessary, move back to the lowest-numbered unhappy page
237 if ( $pageId > $lowestUnhappy ) {
238 $pageId = $lowestUnhappy;
239 if ( $lowestUnhappy == 0 ) {
240 # Knocked back to start, possible loss of session data.
241 $this->showSessionWarning = true;
245 $pageName = $this->pageSequence[$pageId];
246 $page = $this->getPageByName( $pageName );
249 # If a back button was submitted, go back without submitting the form data.
250 if ( $this->request->wasPosted() && $this->request->getBool( 'submit-back' ) ) {
251 if ( $this->request->getVal( 'lastPage' ) ) {
252 $nextPage = $this->request->getVal( 'lastPage' );
253 } elseif ( $pageId !== false ) {
254 # Main sequence page
255 # Skip the skipped pages
256 $nextPageId = $pageId;
258 do {
259 $nextPageId--;
260 $nextPage = $this->pageSequence[$nextPageId];
261 } while ( isset( $this->skippedPages[$nextPage] ) );
262 } else {
263 $nextPage = $this->pageSequence[$lowestUnhappy];
266 $this->output->redirect( $this->getUrl( [ 'page' => $nextPage ] ) );
268 return $this->finish();
271 # Execute the page.
272 $this->currentPageName = $page->getName();
273 $this->startPageWrapper( $pageName );
275 if ( $page->isSlow() ) {
276 $this->disableTimeLimit();
279 $result = $page->execute();
281 $this->endPageWrapper();
283 if ( $result == 'skip' ) {
284 # Page skipped without explicit submission.
285 # Skip it when we click "back" so that we don't just go forward again.
286 $this->skippedPages[$pageName] = true;
287 $result = 'continue';
288 } else {
289 unset( $this->skippedPages[$pageName] );
292 # If it was posted, the page can request a continue to the next page.
293 if ( $result === 'continue' && !$this->output->headerDone() ) {
294 if ( $pageId !== false ) {
295 $this->happyPages[$pageId] = true;
298 $lowestUnhappy = $this->getLowestUnhappy();
300 if ( $this->request->getVal( 'lastPage' ) ) {
301 $nextPage = $this->request->getVal( 'lastPage' );
302 } elseif ( $pageId !== false ) {
303 $nextPage = $this->pageSequence[$pageId + 1];
304 } else {
305 $nextPage = $this->pageSequence[$lowestUnhappy];
308 if ( array_search( $nextPage, $this->pageSequence ) > $lowestUnhappy ) {
309 $nextPage = $this->pageSequence[$lowestUnhappy];
312 $this->output->redirect( $this->getUrl( [ 'page' => $nextPage ] ) );
315 return $this->finish();
319 * Find the next page in sequence that hasn't been completed
320 * @return int
322 public function getLowestUnhappy() {
323 if ( count( $this->happyPages ) == 0 ) {
324 return 0;
325 } else {
326 return max( array_keys( $this->happyPages ) ) + 1;
331 * Start the PHP session. This may be called before execute() to start the PHP session.
333 * @throws Exception
334 * @return bool
336 public function startSession() {
337 if ( wfIniGetBool( 'session.auto_start' ) || session_id() ) {
338 // Done already
339 return true;
342 $this->phpErrors = [];
343 set_error_handler( [ $this, 'errorHandler' ] );
344 try {
345 session_name( 'mw_installer_session' );
346 session_start();
347 } catch ( Exception $e ) {
348 restore_error_handler();
349 throw $e;
351 restore_error_handler();
353 if ( $this->phpErrors ) {
354 return false;
357 return true;
361 * Get a hash of data identifying this MW installation.
363 * This is used by mw-config/index.php to prevent multiple installations of MW
364 * on the same cookie domain from interfering with each other.
366 * @return string
368 public function getFingerprint() {
369 // Get the base URL of the installation
370 $url = $this->request->getFullRequestURL();
371 if ( preg_match( '!^(.*\?)!', $url, $m ) ) {
372 // Trim query string
373 $url = $m[1];
375 if ( preg_match( '!^(.*)/[^/]*/[^/]*$!', $url, $m ) ) {
376 // This... seems to try to get the base path from
377 // the /mw-config/index.php. Kinda scary though?
378 $url = $m[1];
381 return md5( serialize( [
382 'local path' => dirname( __DIR__ ),
383 'url' => $url,
384 'version' => $GLOBALS['wgVersion']
385 ] ) );
389 * Show an error message in a box. Parameters are like wfMessage(), or
390 * alternatively, pass a Message object in.
391 * @param string|Message $msg
393 public function showError( $msg /*...*/ ) {
394 if ( !( $msg instanceof Message ) ) {
395 $args = func_get_args();
396 array_shift( $args );
397 $args = array_map( 'htmlspecialchars', $args );
398 $msg = wfMessage( $msg, $args );
400 $text = $msg->useDatabase( false )->plain();
401 $this->output->addHTML( $this->getErrorBox( $text ) );
405 * Temporary error handler for session start debugging.
407 * @param int $errno Unused
408 * @param string $errstr
410 public function errorHandler( $errno, $errstr ) {
411 $this->phpErrors[] = $errstr;
415 * Clean up from execute()
417 * @return array[]
419 public function finish() {
420 $this->output->output();
422 $this->session['happyPages'] = $this->happyPages;
423 $this->session['skippedPages'] = $this->skippedPages;
424 $this->session['settings'] = $this->settings;
426 return $this->session;
430 * We're restarting the installation, reset the session, happyPages, etc
432 public function reset() {
433 $this->session = [];
434 $this->happyPages = [];
435 $this->settings = [];
439 * Get a URL for submission back to the same script.
441 * @param string[] $query
443 * @return string
445 public function getUrl( $query = [] ) {
446 $url = $this->request->getRequestURL();
447 # Remove existing query
448 $url = preg_replace( '/\?.*$/', '', $url );
450 if ( $query ) {
451 $url .= '?' . wfArrayToCgi( $query );
454 return $url;
458 * Get a WebInstallerPage by name.
460 * @param string $pageName
461 * @return WebInstallerPage
463 public function getPageByName( $pageName ) {
464 $pageClass = 'WebInstaller' . $pageName;
466 return new $pageClass( $this );
470 * Get a session variable.
472 * @param string $name
473 * @param array $default
475 * @return array
477 public function getSession( $name, $default = null ) {
478 if ( !isset( $this->session[$name] ) ) {
479 return $default;
480 } else {
481 return $this->session[$name];
486 * Set a session variable.
488 * @param string $name Key for the variable
489 * @param mixed $value
491 public function setSession( $name, $value ) {
492 $this->session[$name] = $value;
496 * Get the next tabindex attribute value.
498 * @return int
500 public function nextTabIndex() {
501 return $this->tabIndex++;
505 * Initializes language-related variables.
507 public function setupLanguage() {
508 global $wgLang, $wgContLang, $wgLanguageCode;
510 if ( $this->getSession( 'test' ) === null && !$this->request->wasPosted() ) {
511 $wgLanguageCode = $this->getAcceptLanguage();
512 $wgLang = $wgContLang = Language::factory( $wgLanguageCode );
513 RequestContext::getMain()->setLanguage( $wgLang );
514 $this->setVar( 'wgLanguageCode', $wgLanguageCode );
515 $this->setVar( '_UserLang', $wgLanguageCode );
516 } else {
517 $wgLanguageCode = $this->getVar( 'wgLanguageCode' );
518 $wgContLang = Language::factory( $wgLanguageCode );
523 * Retrieves MediaWiki language from Accept-Language HTTP header.
525 * @return string
527 public function getAcceptLanguage() {
528 global $wgLanguageCode, $wgRequest;
530 $mwLanguages = Language::fetchLanguageNames();
531 $headerLanguages = array_keys( $wgRequest->getAcceptLang() );
533 foreach ( $headerLanguages as $lang ) {
534 if ( isset( $mwLanguages[$lang] ) ) {
535 return $lang;
539 return $wgLanguageCode;
543 * Called by execute() before page output starts, to show a page list.
545 * @param string $currentPageName
547 private function startPageWrapper( $currentPageName ) {
548 $s = "<div class=\"config-page-wrapper\">\n";
549 $s .= "<div class=\"config-page\">\n";
550 $s .= "<div class=\"config-page-list\"><ul>\n";
551 $lastHappy = -1;
553 foreach ( $this->pageSequence as $id => $pageName ) {
554 $happy = !empty( $this->happyPages[$id] );
555 $s .= $this->getPageListItem(
556 $pageName,
557 $happy || $lastHappy == $id - 1,
558 $currentPageName
561 if ( $happy ) {
562 $lastHappy = $id;
566 $s .= "</ul><br/><ul>\n";
567 $s .= $this->getPageListItem( 'Restart', true, $currentPageName );
568 // End list pane
569 $s .= "</ul></div>\n";
571 // Messages:
572 // config-page-language, config-page-welcome, config-page-dbconnect, config-page-upgrade,
573 // config-page-dbsettings, config-page-name, config-page-options, config-page-install,
574 // config-page-complete, config-page-restart, config-page-readme, config-page-releasenotes,
575 // config-page-copying, config-page-upgradedoc, config-page-existingwiki
576 $s .= Html::element( 'h2', [],
577 wfMessage( 'config-page-' . strtolower( $currentPageName ) )->text() );
579 $this->output->addHTMLNoFlush( $s );
583 * Get a list item for the page list.
585 * @param string $pageName
586 * @param bool $enabled
587 * @param string $currentPageName
589 * @return string
591 private function getPageListItem( $pageName, $enabled, $currentPageName ) {
592 $s = "<li class=\"config-page-list-item\">";
594 // Messages:
595 // config-page-language, config-page-welcome, config-page-dbconnect, config-page-upgrade,
596 // config-page-dbsettings, config-page-name, config-page-options, config-page-install,
597 // config-page-complete, config-page-restart, config-page-readme, config-page-releasenotes,
598 // config-page-copying, config-page-upgradedoc, config-page-existingwiki
599 $name = wfMessage( 'config-page-' . strtolower( $pageName ) )->text();
601 if ( $enabled ) {
602 $query = [ 'page' => $pageName ];
604 if ( !in_array( $pageName, $this->pageSequence ) ) {
605 if ( in_array( $currentPageName, $this->pageSequence ) ) {
606 $query['lastPage'] = $currentPageName;
609 $link = Html::element( 'a',
611 'href' => $this->getUrl( $query )
613 $name
615 } else {
616 $link = htmlspecialchars( $name );
619 if ( $pageName == $currentPageName ) {
620 $s .= "<span class=\"config-page-current\">$link</span>";
621 } else {
622 $s .= $link;
624 } else {
625 $s .= Html::element( 'span',
627 'class' => 'config-page-disabled'
629 $name
633 $s .= "</li>\n";
635 return $s;
639 * Output some stuff after a page is finished.
641 private function endPageWrapper() {
642 $this->output->addHTMLNoFlush(
643 "<div class=\"visualClear\"></div>\n" .
644 "</div>\n" .
645 "<div class=\"visualClear\"></div>\n" .
646 "</div>" );
650 * Get HTML for an error box with an icon.
652 * @param string $text Wikitext, get this with wfMessage()->plain()
654 * @return string
656 public function getErrorBox( $text ) {
657 return $this->getInfoBox( $text, 'critical-32.png', 'config-error-box' );
661 * Get HTML for a warning box with an icon.
663 * @param string $text Wikitext, get this with wfMessage()->plain()
665 * @return string
667 public function getWarningBox( $text ) {
668 return $this->getInfoBox( $text, 'warning-32.png', 'config-warning-box' );
672 * Get HTML for an info box with an icon.
674 * @param string $text Wikitext, get this with wfMessage()->plain()
675 * @param string|bool $icon Icon name, file in mw-config/images. Default: false
676 * @param string|bool $class Additional class name to add to the wrapper div. Default: false.
678 * @return string
680 public function getInfoBox( $text, $icon = false, $class = false ) {
681 $text = $this->parse( $text, true );
682 $icon = ( $icon == false ) ?
683 'images/info-32.png' :
684 'images/' . $icon;
685 $alt = wfMessage( 'config-information' )->text();
687 return Html::infoBox( $text, $icon, $alt, $class );
691 * Get small text indented help for a preceding form field.
692 * Parameters like wfMessage().
694 * @param string $msg
695 * @return string
697 public function getHelpBox( $msg /*, ... */ ) {
698 $args = func_get_args();
699 array_shift( $args );
700 $args = array_map( 'htmlspecialchars', $args );
701 $text = wfMessage( $msg, $args )->useDatabase( false )->plain();
702 $html = $this->parse( $text, true );
704 return "<div class=\"config-help-field-container\">\n" .
705 "<span class=\"config-help-field-hint\" title=\"" .
706 wfMessage( 'config-help-tooltip' )->escaped() . "\">" .
707 wfMessage( 'config-help' )->escaped() . "</span>\n" .
708 "<span class=\"config-help-field-data\">" . $html . "</span>\n" .
709 "</div>\n";
713 * Output a help box.
714 * @param string $msg Key for wfMessage()
716 public function showHelpBox( $msg /*, ... */ ) {
717 $args = func_get_args();
718 $html = call_user_func_array( [ $this, 'getHelpBox' ], $args );
719 $this->output->addHTML( $html );
723 * Show a short informational message.
724 * Output looks like a list.
726 * @param string $msg
728 public function showMessage( $msg /*, ... */ ) {
729 $args = func_get_args();
730 array_shift( $args );
731 $html = '<div class="config-message">' .
732 $this->parse( wfMessage( $msg, $args )->useDatabase( false )->plain() ) .
733 "</div>\n";
734 $this->output->addHTML( $html );
738 * @param Status $status
740 public function showStatusMessage( Status $status ) {
741 $errors = array_merge( $status->getErrorsArray(), $status->getWarningsArray() );
742 foreach ( $errors as $error ) {
743 call_user_func_array( [ $this, 'showMessage' ], $error );
748 * Label a control by wrapping a config-input div around it and putting a
749 * label before it.
751 * @param string $msg
752 * @param string $forId
753 * @param string $contents
754 * @param string $helpData
755 * @return string
757 public function label( $msg, $forId, $contents, $helpData = "" ) {
758 if ( strval( $msg ) == '' ) {
759 $labelText = '&#160;';
760 } else {
761 $labelText = wfMessage( $msg )->escaped();
764 $attributes = [ 'class' => 'config-label' ];
766 if ( $forId ) {
767 $attributes['for'] = $forId;
770 return "<div class=\"config-block\">\n" .
771 " <div class=\"config-block-label\">\n" .
772 Xml::tags( 'label',
773 $attributes,
774 $labelText
775 ) . "\n" .
776 $helpData .
777 " </div>\n" .
778 " <div class=\"config-block-elements\">\n" .
779 $contents .
780 " </div>\n" .
781 "</div>\n";
785 * Get a labelled text box to configure a variable.
787 * @param mixed[] $params
788 * Parameters are:
789 * var: The variable to be configured (required)
790 * label: The message name for the label (required)
791 * attribs: Additional attributes for the input element (optional)
792 * controlName: The name for the input element (optional)
793 * value: The current value of the variable (optional)
794 * help: The html for the help text (optional)
796 * @return string
798 public function getTextBox( $params ) {
799 if ( !isset( $params['controlName'] ) ) {
800 $params['controlName'] = 'config_' . $params['var'];
803 if ( !isset( $params['value'] ) ) {
804 $params['value'] = $this->getVar( $params['var'] );
807 if ( !isset( $params['attribs'] ) ) {
808 $params['attribs'] = [];
810 if ( !isset( $params['help'] ) ) {
811 $params['help'] = "";
814 return $this->label(
815 $params['label'],
816 $params['controlName'],
817 Xml::input(
818 $params['controlName'],
819 30, // intended to be overridden by CSS
820 $params['value'],
821 $params['attribs'] + [
822 'id' => $params['controlName'],
823 'class' => 'config-input-text',
824 'tabindex' => $this->nextTabIndex()
827 $params['help']
832 * Get a labelled textarea to configure a variable
834 * @param mixed[] $params
835 * Parameters are:
836 * var: The variable to be configured (required)
837 * label: The message name for the label (required)
838 * attribs: Additional attributes for the input element (optional)
839 * controlName: The name for the input element (optional)
840 * value: The current value of the variable (optional)
841 * help: The html for the help text (optional)
843 * @return string
845 public function getTextArea( $params ) {
846 if ( !isset( $params['controlName'] ) ) {
847 $params['controlName'] = 'config_' . $params['var'];
850 if ( !isset( $params['value'] ) ) {
851 $params['value'] = $this->getVar( $params['var'] );
854 if ( !isset( $params['attribs'] ) ) {
855 $params['attribs'] = [];
857 if ( !isset( $params['help'] ) ) {
858 $params['help'] = "";
861 return $this->label(
862 $params['label'],
863 $params['controlName'],
864 Xml::textarea(
865 $params['controlName'],
866 $params['value'],
869 $params['attribs'] + [
870 'id' => $params['controlName'],
871 'class' => 'config-input-text',
872 'tabindex' => $this->nextTabIndex()
875 $params['help']
880 * Get a labelled password box to configure a variable.
882 * Implements password hiding
883 * @param mixed[] $params
884 * Parameters are:
885 * var: The variable to be configured (required)
886 * label: The message name for the label (required)
887 * attribs: Additional attributes for the input element (optional)
888 * controlName: The name for the input element (optional)
889 * value: The current value of the variable (optional)
890 * help: The html for the help text (optional)
892 * @return string
894 public function getPasswordBox( $params ) {
895 if ( !isset( $params['value'] ) ) {
896 $params['value'] = $this->getVar( $params['var'] );
899 if ( !isset( $params['attribs'] ) ) {
900 $params['attribs'] = [];
903 $params['value'] = $this->getFakePassword( $params['value'] );
904 $params['attribs']['type'] = 'password';
906 return $this->getTextBox( $params );
910 * Get a labelled checkbox to configure a boolean variable.
912 * @param mixed[] $params
913 * Parameters are:
914 * var: The variable to be configured (required)
915 * label: The message name for the label (required)
916 * attribs: Additional attributes for the input element (optional)
917 * controlName: The name for the input element (optional)
918 * value: The current value of the variable (optional)
919 * help: The html for the help text (optional)
921 * @return string
923 public function getCheckBox( $params ) {
924 if ( !isset( $params['controlName'] ) ) {
925 $params['controlName'] = 'config_' . $params['var'];
928 if ( !isset( $params['value'] ) ) {
929 $params['value'] = $this->getVar( $params['var'] );
932 if ( !isset( $params['attribs'] ) ) {
933 $params['attribs'] = [];
935 if ( !isset( $params['help'] ) ) {
936 $params['help'] = "";
938 if ( isset( $params['rawtext'] ) ) {
939 $labelText = $params['rawtext'];
940 } else {
941 $labelText = $this->parse( wfMessage( $params['label'] )->text() );
944 return "<div class=\"config-input-check\">\n" .
945 $params['help'] .
946 "<label>\n" .
947 Xml::check(
948 $params['controlName'],
949 $params['value'],
950 $params['attribs'] + [
951 'id' => $params['controlName'],
952 'tabindex' => $this->nextTabIndex(),
955 $labelText . "\n" .
956 "</label>\n" .
957 "</div>\n";
961 * Get a set of labelled radio buttons.
963 * @param mixed[] $params
964 * Parameters are:
965 * var: The variable to be configured (required)
966 * label: The message name for the label (required)
967 * itemLabelPrefix: The message name prefix for the item labels (required)
968 * itemLabels: List of message names to use for the item labels instead
969 * of itemLabelPrefix, keyed by values
970 * values: List of allowed values (required)
971 * itemAttribs: Array of attribute arrays, outer key is the value name (optional)
972 * commonAttribs: Attribute array applied to all items
973 * controlName: The name for the input element (optional)
974 * value: The current value of the variable (optional)
975 * help: The html for the help text (optional)
977 * @return string
979 public function getRadioSet( $params ) {
980 $items = $this->getRadioElements( $params );
982 if ( !isset( $params['label'] ) ) {
983 $label = '';
984 } else {
985 $label = $params['label'];
988 if ( !isset( $params['controlName'] ) ) {
989 $params['controlName'] = 'config_' . $params['var'];
992 if ( !isset( $params['help'] ) ) {
993 $params['help'] = "";
996 $s = "<ul>\n";
997 foreach ( $items as $value => $item ) {
998 $s .= "<li>$item</li>\n";
1000 $s .= "</ul>\n";
1002 return $this->label( $label, $params['controlName'], $s, $params['help'] );
1006 * Get a set of labelled radio buttons. You probably want to use getRadioSet(), not this.
1008 * @see getRadioSet
1010 * @return array
1012 public function getRadioElements( $params ) {
1013 if ( !isset( $params['controlName'] ) ) {
1014 $params['controlName'] = 'config_' . $params['var'];
1017 if ( !isset( $params['value'] ) ) {
1018 $params['value'] = $this->getVar( $params['var'] );
1021 $items = [];
1023 foreach ( $params['values'] as $value ) {
1024 $itemAttribs = [];
1026 if ( isset( $params['commonAttribs'] ) ) {
1027 $itemAttribs = $params['commonAttribs'];
1030 if ( isset( $params['itemAttribs'][$value] ) ) {
1031 $itemAttribs = $params['itemAttribs'][$value] + $itemAttribs;
1034 $checked = $value == $params['value'];
1035 $id = $params['controlName'] . '_' . $value;
1036 $itemAttribs['id'] = $id;
1037 $itemAttribs['tabindex'] = $this->nextTabIndex();
1039 $items[$value] =
1040 Xml::radio( $params['controlName'], $value, $checked, $itemAttribs ) .
1041 '&#160;' .
1042 Xml::tags( 'label', [ 'for' => $id ], $this->parse(
1043 isset( $params['itemLabels'] ) ?
1044 wfMessage( $params['itemLabels'][$value] )->plain() :
1045 wfMessage( $params['itemLabelPrefix'] . strtolower( $value ) )->plain()
1046 ) );
1049 return $items;
1053 * Output an error or warning box using a Status object.
1055 * @param Status $status
1057 public function showStatusBox( $status ) {
1058 if ( !$status->isGood() ) {
1059 $text = $status->getWikiText();
1061 if ( $status->isOK() ) {
1062 $box = $this->getWarningBox( $text );
1063 } else {
1064 $box = $this->getErrorBox( $text );
1067 $this->output->addHTML( $box );
1072 * Convenience function to set variables based on form data.
1073 * Assumes that variables containing "password" in the name are (potentially
1074 * fake) passwords.
1076 * @param string[] $varNames
1077 * @param string $prefix The prefix added to variables to obtain form names
1079 * @return string[]
1081 public function setVarsFromRequest( $varNames, $prefix = 'config_' ) {
1082 $newValues = [];
1084 foreach ( $varNames as $name ) {
1085 $value = $this->request->getVal( $prefix . $name );
1086 // bug 30524, do not trim passwords
1087 if ( stripos( $name, 'password' ) === false ) {
1088 $value = trim( $value );
1090 $newValues[$name] = $value;
1092 if ( $value === null ) {
1093 // Checkbox?
1094 $this->setVar( $name, false );
1095 } else {
1096 if ( stripos( $name, 'password' ) !== false ) {
1097 $this->setPassword( $name, $value );
1098 } else {
1099 $this->setVar( $name, $value );
1104 return $newValues;
1108 * Helper for Installer::docLink()
1110 * @param string $page
1112 * @return string
1114 protected function getDocUrl( $page ) {
1115 $url = "{$_SERVER['PHP_SELF']}?page=" . urlencode( $page );
1117 if ( in_array( $this->currentPageName, $this->pageSequence ) ) {
1118 $url .= '&lastPage=' . urlencode( $this->currentPageName );
1121 return $url;
1125 * Extension tag hook for a documentation link.
1127 * @param string $linkText
1128 * @param string[] $attribs
1129 * @param Parser $parser Unused
1131 * @return string
1133 public function docLink( $linkText, $attribs, $parser ) {
1134 $url = $this->getDocUrl( $attribs['href'] );
1136 return '<a href="' . htmlspecialchars( $url ) . '">' .
1137 htmlspecialchars( $linkText ) .
1138 '</a>';
1142 * Helper for "Download LocalSettings" link on WebInstall_Complete
1144 * @param string $text Unused
1145 * @param string[] $attribs Unused
1146 * @param Parser $parser Unused
1148 * @return string Html for download link
1150 public function downloadLinkHook( $text, $attribs, $parser ) {
1151 $anchor = Html::rawElement( 'a',
1152 [ 'href' => $this->getUrl( [ 'localsettings' => 1 ] ) ],
1153 wfMessage( 'config-download-localsettings' )->parse()
1156 return Html::rawElement( 'div', [ 'class' => 'config-download-link' ], $anchor );
1160 * If the software package wants the LocalSettings.php file
1161 * to be placed in a specific location, override this function
1162 * (see mw-config/overrides/README) to return the path of
1163 * where the file should be saved, or false for a generic
1164 * "in the base of your install"
1166 * @since 1.27
1167 * @return string|bool
1169 public function getLocalSettingsLocation() {
1170 return false;
1174 * @return bool
1176 public function envCheckPath() {
1177 // PHP_SELF isn't available sometimes, such as when PHP is CGI but
1178 // cgi.fix_pathinfo is disabled. In that case, fall back to SCRIPT_NAME
1179 // to get the path to the current script... hopefully it's reliable. SIGH
1180 $path = false;
1181 if ( !empty( $_SERVER['PHP_SELF'] ) ) {
1182 $path = $_SERVER['PHP_SELF'];
1183 } elseif ( !empty( $_SERVER['SCRIPT_NAME'] ) ) {
1184 $path = $_SERVER['SCRIPT_NAME'];
1186 if ( $path === false ) {
1187 $this->showError( 'config-no-uri' );
1188 return false;
1191 return parent::envCheckPath();
1194 public function envPrepPath() {
1195 parent::envPrepPath();
1196 // PHP_SELF isn't available sometimes, such as when PHP is CGI but
1197 // cgi.fix_pathinfo is disabled. In that case, fall back to SCRIPT_NAME
1198 // to get the path to the current script... hopefully it's reliable. SIGH
1199 $path = false;
1200 if ( !empty( $_SERVER['PHP_SELF'] ) ) {
1201 $path = $_SERVER['PHP_SELF'];
1202 } elseif ( !empty( $_SERVER['SCRIPT_NAME'] ) ) {
1203 $path = $_SERVER['SCRIPT_NAME'];
1205 if ( $path !== false ) {
1206 $scriptPath = preg_replace( '{^(.*)/(mw-)?config.*$}', '$1', $path );
1208 $this->setVar( 'wgScriptPath', "$scriptPath" );
1209 // Update variables set from Setup.php that are derived from wgScriptPath
1210 $this->setVar( 'wgScript', "$scriptPath/index.php" );
1211 $this->setVar( 'wgLoadScript', "$scriptPath/load.php" );
1212 $this->setVar( 'wgStylePath', "$scriptPath/skins" );
1213 $this->setVar( 'wgLocalStylePath', "$scriptPath/skins" );
1214 $this->setVar( 'wgExtensionAssetsPath', "$scriptPath/extensions" );
1215 $this->setVar( 'wgUploadPath', "$scriptPath/images" );
1216 $this->setVar( 'wgResourceBasePath', "$scriptPath" );
1221 * @return string
1223 protected function envGetDefaultServer() {
1224 return WebRequest::detectServer();
1228 * Output stylesheet for web installer pages
1230 public function outputCss() {
1231 $this->request->response()->header( 'Content-type: text/css' );
1232 echo $this->output->getCSS();
1236 * @return string[]
1238 public function getPhpErrors() {
1239 return $this->phpErrors;