In the web installer, use secure session cookies
[lhc/web/wiklou.git] / includes / installer / WebInstaller.php
1 <?php
2 /**
3 * Core installer web interface.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Deployment
22 */
23
24 /**
25 * Class for the core installer web interface.
26 *
27 * @ingroup Deployment
28 * @since 1.17
29 */
30 class WebInstaller extends Installer {
31
32 /**
33 * @var WebInstallerOutput
34 */
35 public $output;
36
37 /**
38 * WebRequest object.
39 *
40 * @var WebRequest
41 */
42 public $request;
43
44 /**
45 * Cached session array.
46 *
47 * @var array[]
48 */
49 protected $session;
50
51 /**
52 * Captured PHP error text. Temporary.
53 *
54 * @var string[]
55 */
56 protected $phpErrors;
57
58 /**
59 * The main sequence of page names. These will be displayed in turn.
60 *
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
65 *
66 * @var string[]
67 */
68 public $pageSequence = [
69 'Language',
70 'ExistingWiki',
71 'Welcome',
72 'DBConnect',
73 'Upgrade',
74 'DBSettings',
75 'Name',
76 'Options',
77 'Install',
78 'Complete',
79 ];
80
81 /**
82 * Out of sequence pages, selectable by the user at any time.
83 *
84 * @var string[]
85 */
86 protected $otherPages = [
87 'Restart',
88 'Readme',
89 'ReleaseNotes',
90 'Copying',
91 'UpgradeDoc', // Can't use Upgrade due to Upgrade step
92 ];
93
94 /**
95 * Array of pages which have declared that they have been submitted, have validated
96 * their input, and need no further processing.
97 *
98 * @var bool[]
99 */
100 protected $happyPages;
101
102 /**
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.
106 *
107 * @var bool[]
108 */
109 protected $skippedPages;
110
111 /**
112 * Flag indicating that session data may have been lost.
113 *
114 * @var bool
115 */
116 public $showSessionWarning = false;
117
118 /**
119 * Numeric index of the page we're on
120 *
121 * @var int
122 */
123 protected $tabIndex = 1;
124
125 /**
126 * Name of the page we're on
127 *
128 * @var string
129 */
130 protected $currentPageName;
131
132 /**
133 * @param WebRequest $request
134 */
135 public function __construct( WebRequest $request ) {
136 parent::__construct();
137 $this->output = new WebInstallerOutput( $this );
138 $this->request = $request;
139
140 // Add parser hooks
141 global $wgParser;
142 $wgParser->setHook( 'downloadlink', [ $this, 'downloadLinkHook' ] );
143 $wgParser->setHook( 'doclink', [ $this, 'docLink' ] );
144 }
145
146 /**
147 * Main entry point.
148 *
149 * @param array[] $session Initial session array
150 *
151 * @return array[] New session array
152 */
153 public function execute( array $session ) {
154 $this->session = $session;
155
156 if ( isset( $session['settings'] ) ) {
157 $this->settings = $session['settings'] + $this->settings;
158 // T187586 MediaWikiServices works with globals
159 foreach ( $this->settings as $key => $val ) {
160 $GLOBALS[$key] = $val;
161 }
162 }
163
164 $this->setupLanguage();
165
166 if ( ( $this->getVar( '_InstallDone' ) || $this->getVar( '_UpgradeDone' ) )
167 && $this->request->getVal( 'localsettings' )
168 ) {
169 $this->request->response()->header( 'Content-type: application/x-httpd-php' );
170 $this->request->response()->header(
171 'Content-Disposition: attachment; filename="LocalSettings.php"'
172 );
173
174 $ls = InstallerOverrides::getLocalSettingsGenerator( $this );
175 $rightsProfile = $this->rightsProfiles[$this->getVar( '_RightsProfile' )];
176 foreach ( $rightsProfile as $group => $rightsArr ) {
177 $ls->setGroupRights( $group, $rightsArr );
178 }
179 echo $ls->getText();
180
181 return $this->session;
182 }
183
184 $isCSS = $this->request->getVal( 'css' );
185 if ( $isCSS ) {
186 $this->outputCss();
187 return $this->session;
188 }
189
190 if ( isset( $session['happyPages'] ) ) {
191 $this->happyPages = $session['happyPages'];
192 } else {
193 $this->happyPages = [];
194 }
195
196 if ( isset( $session['skippedPages'] ) ) {
197 $this->skippedPages = $session['skippedPages'];
198 } else {
199 $this->skippedPages = [];
200 }
201
202 $lowestUnhappy = $this->getLowestUnhappy();
203
204 # Special case for Creative Commons partner chooser box.
205 if ( $this->request->getVal( 'SubmitCC' ) ) {
206 $page = $this->getPageByName( 'Options' );
207 $this->output->useShortHeader();
208 $this->output->allowFrames();
209 $page->submitCC();
210
211 return $this->finish();
212 }
213
214 if ( $this->request->getVal( 'ShowCC' ) ) {
215 $page = $this->getPageByName( 'Options' );
216 $this->output->useShortHeader();
217 $this->output->allowFrames();
218 $this->output->addHTML( $page->getCCDoneBox() );
219
220 return $this->finish();
221 }
222
223 # Get the page name.
224 $pageName = $this->request->getVal( 'page' );
225
226 if ( in_array( $pageName, $this->otherPages ) ) {
227 # Out of sequence
228 $pageId = false;
229 $page = $this->getPageByName( $pageName );
230 } else {
231 # Main sequence
232 if ( !$pageName || !in_array( $pageName, $this->pageSequence ) ) {
233 $pageId = $lowestUnhappy;
234 } else {
235 $pageId = array_search( $pageName, $this->pageSequence );
236 }
237
238 # If necessary, move back to the lowest-numbered unhappy page
239 if ( $pageId > $lowestUnhappy ) {
240 $pageId = $lowestUnhappy;
241 if ( $lowestUnhappy == 0 ) {
242 # Knocked back to start, possible loss of session data.
243 $this->showSessionWarning = true;
244 }
245 }
246
247 $pageName = $this->pageSequence[$pageId];
248 $page = $this->getPageByName( $pageName );
249 }
250
251 # If a back button was submitted, go back without submitting the form data.
252 if ( $this->request->wasPosted() && $this->request->getBool( 'submit-back' ) ) {
253 if ( $this->request->getVal( 'lastPage' ) ) {
254 $nextPage = $this->request->getVal( 'lastPage' );
255 } elseif ( $pageId !== false ) {
256 # Main sequence page
257 # Skip the skipped pages
258 $nextPageId = $pageId;
259
260 do {
261 $nextPageId--;
262 $nextPage = $this->pageSequence[$nextPageId];
263 } while ( isset( $this->skippedPages[$nextPage] ) );
264 } else {
265 $nextPage = $this->pageSequence[$lowestUnhappy];
266 }
267
268 $this->output->redirect( $this->getUrl( [ 'page' => $nextPage ] ) );
269
270 return $this->finish();
271 }
272
273 # Execute the page.
274 $this->currentPageName = $page->getName();
275 $this->startPageWrapper( $pageName );
276
277 if ( $page->isSlow() ) {
278 $this->disableTimeLimit();
279 }
280
281 $result = $page->execute();
282
283 $this->endPageWrapper();
284
285 if ( $result == 'skip' ) {
286 # Page skipped without explicit submission.
287 # Skip it when we click "back" so that we don't just go forward again.
288 $this->skippedPages[$pageName] = true;
289 $result = 'continue';
290 } else {
291 unset( $this->skippedPages[$pageName] );
292 }
293
294 # If it was posted, the page can request a continue to the next page.
295 if ( $result === 'continue' && !$this->output->headerDone() ) {
296 if ( $pageId !== false ) {
297 $this->happyPages[$pageId] = true;
298 }
299
300 $lowestUnhappy = $this->getLowestUnhappy();
301
302 if ( $this->request->getVal( 'lastPage' ) ) {
303 $nextPage = $this->request->getVal( 'lastPage' );
304 } elseif ( $pageId !== false ) {
305 $nextPage = $this->pageSequence[$pageId + 1];
306 } else {
307 $nextPage = $this->pageSequence[$lowestUnhappy];
308 }
309
310 if ( array_search( $nextPage, $this->pageSequence ) > $lowestUnhappy ) {
311 $nextPage = $this->pageSequence[$lowestUnhappy];
312 }
313
314 $this->output->redirect( $this->getUrl( [ 'page' => $nextPage ] ) );
315 }
316
317 return $this->finish();
318 }
319
320 /**
321 * Find the next page in sequence that hasn't been completed
322 * @return int
323 */
324 public function getLowestUnhappy() {
325 if ( count( $this->happyPages ) == 0 ) {
326 return 0;
327 } else {
328 return max( array_keys( $this->happyPages ) ) + 1;
329 }
330 }
331
332 /**
333 * Start the PHP session. This may be called before execute() to start the PHP session.
334 *
335 * @throws Exception
336 * @return bool
337 */
338 public function startSession() {
339 if ( wfIniGetBool( 'session.auto_start' ) || session_id() ) {
340 // Done already
341 return true;
342 }
343
344 // Use secure cookies if we are on HTTPS
345 $options = [];
346 if ( $this->request->getProtocol() === 'https' ) {
347 $options['cookie_secure'] = '1';
348 }
349
350 $this->phpErrors = [];
351 set_error_handler( [ $this, 'errorHandler' ] );
352 try {
353 session_name( 'mw_installer_session' );
354 session_start( $options );
355 } catch ( Exception $e ) {
356 restore_error_handler();
357 throw $e;
358 }
359 restore_error_handler();
360
361 if ( $this->phpErrors ) {
362 return false;
363 }
364
365 return true;
366 }
367
368 /**
369 * Get a hash of data identifying this MW installation.
370 *
371 * This is used by mw-config/index.php to prevent multiple installations of MW
372 * on the same cookie domain from interfering with each other.
373 *
374 * @return string
375 */
376 public function getFingerprint() {
377 // Get the base URL of the installation
378 $url = $this->request->getFullRequestURL();
379 if ( preg_match( '!^(.*\?)!', $url, $m ) ) {
380 // Trim query string
381 $url = $m[1];
382 }
383 if ( preg_match( '!^(.*)/[^/]*/[^/]*$!', $url, $m ) ) {
384 // This... seems to try to get the base path from
385 // the /mw-config/index.php. Kinda scary though?
386 $url = $m[1];
387 }
388
389 return md5( serialize( [
390 'local path' => dirname( __DIR__ ),
391 'url' => $url,
392 'version' => $GLOBALS['wgVersion']
393 ] ) );
394 }
395
396 /**
397 * Show an error message in a box. Parameters are like wfMessage(), or
398 * alternatively, pass a Message object in.
399 * @param string|Message $msg
400 */
401 public function showError( $msg /*...*/ ) {
402 if ( !( $msg instanceof Message ) ) {
403 $args = func_get_args();
404 array_shift( $args );
405 $args = array_map( 'htmlspecialchars', $args );
406 $msg = wfMessage( $msg, $args );
407 }
408 $text = $msg->useDatabase( false )->plain();
409 $this->output->addHTML( $this->getErrorBox( $text ) );
410 }
411
412 /**
413 * Temporary error handler for session start debugging.
414 *
415 * @param int $errno Unused
416 * @param string $errstr
417 */
418 public function errorHandler( $errno, $errstr ) {
419 $this->phpErrors[] = $errstr;
420 }
421
422 /**
423 * Clean up from execute()
424 *
425 * @return array[]
426 */
427 public function finish() {
428 $this->output->output();
429
430 $this->session['happyPages'] = $this->happyPages;
431 $this->session['skippedPages'] = $this->skippedPages;
432 $this->session['settings'] = $this->settings;
433
434 return $this->session;
435 }
436
437 /**
438 * We're restarting the installation, reset the session, happyPages, etc
439 */
440 public function reset() {
441 $this->session = [];
442 $this->happyPages = [];
443 $this->settings = [];
444 }
445
446 /**
447 * Get a URL for submission back to the same script.
448 *
449 * @param string[] $query
450 *
451 * @return string
452 */
453 public function getUrl( $query = [] ) {
454 $url = $this->request->getRequestURL();
455 # Remove existing query
456 $url = preg_replace( '/\?.*$/', '', $url );
457
458 if ( $query ) {
459 $url .= '?' . wfArrayToCgi( $query );
460 }
461
462 return $url;
463 }
464
465 /**
466 * Get a WebInstallerPage by name.
467 *
468 * @param string $pageName
469 * @return WebInstallerPage
470 */
471 public function getPageByName( $pageName ) {
472 $pageClass = 'WebInstaller' . $pageName;
473
474 return new $pageClass( $this );
475 }
476
477 /**
478 * Get a session variable.
479 *
480 * @param string $name
481 * @param array $default
482 *
483 * @return array
484 */
485 public function getSession( $name, $default = null ) {
486 if ( !isset( $this->session[$name] ) ) {
487 return $default;
488 } else {
489 return $this->session[$name];
490 }
491 }
492
493 /**
494 * Set a session variable.
495 *
496 * @param string $name Key for the variable
497 * @param mixed $value
498 */
499 public function setSession( $name, $value ) {
500 $this->session[$name] = $value;
501 }
502
503 /**
504 * Get the next tabindex attribute value.
505 *
506 * @return int
507 */
508 public function nextTabIndex() {
509 return $this->tabIndex++;
510 }
511
512 /**
513 * Initializes language-related variables.
514 */
515 public function setupLanguage() {
516 global $wgLang, $wgContLang, $wgLanguageCode;
517
518 if ( $this->getSession( 'test' ) === null && !$this->request->wasPosted() ) {
519 $wgLanguageCode = $this->getAcceptLanguage();
520 $wgLang = $wgContLang = Language::factory( $wgLanguageCode );
521 RequestContext::getMain()->setLanguage( $wgLang );
522 $this->setVar( 'wgLanguageCode', $wgLanguageCode );
523 $this->setVar( '_UserLang', $wgLanguageCode );
524 } else {
525 $wgLanguageCode = $this->getVar( 'wgLanguageCode' );
526 $wgContLang = Language::factory( $wgLanguageCode );
527 }
528 }
529
530 /**
531 * Retrieves MediaWiki language from Accept-Language HTTP header.
532 *
533 * @return string
534 */
535 public function getAcceptLanguage() {
536 global $wgLanguageCode, $wgRequest;
537
538 $mwLanguages = Language::fetchLanguageNames();
539 $headerLanguages = array_keys( $wgRequest->getAcceptLang() );
540
541 foreach ( $headerLanguages as $lang ) {
542 if ( isset( $mwLanguages[$lang] ) ) {
543 return $lang;
544 }
545 }
546
547 return $wgLanguageCode;
548 }
549
550 /**
551 * Called by execute() before page output starts, to show a page list.
552 *
553 * @param string $currentPageName
554 */
555 private function startPageWrapper( $currentPageName ) {
556 $s = "<div class=\"config-page-wrapper\">\n";
557 $s .= "<div class=\"config-page\">\n";
558 $s .= "<div class=\"config-page-list\"><ul>\n";
559 $lastHappy = -1;
560
561 foreach ( $this->pageSequence as $id => $pageName ) {
562 $happy = !empty( $this->happyPages[$id] );
563 $s .= $this->getPageListItem(
564 $pageName,
565 $happy || $lastHappy == $id - 1,
566 $currentPageName
567 );
568
569 if ( $happy ) {
570 $lastHappy = $id;
571 }
572 }
573
574 $s .= "</ul><br/><ul>\n";
575 $s .= $this->getPageListItem( 'Restart', true, $currentPageName );
576 // End list pane
577 $s .= "</ul></div>\n";
578
579 // Messages:
580 // config-page-language, config-page-welcome, config-page-dbconnect, config-page-upgrade,
581 // config-page-dbsettings, config-page-name, config-page-options, config-page-install,
582 // config-page-complete, config-page-restart, config-page-readme, config-page-releasenotes,
583 // config-page-copying, config-page-upgradedoc, config-page-existingwiki
584 $s .= Html::element( 'h2', [],
585 wfMessage( 'config-page-' . strtolower( $currentPageName ) )->text() );
586
587 $this->output->addHTMLNoFlush( $s );
588 }
589
590 /**
591 * Get a list item for the page list.
592 *
593 * @param string $pageName
594 * @param bool $enabled
595 * @param string $currentPageName
596 *
597 * @return string
598 */
599 private function getPageListItem( $pageName, $enabled, $currentPageName ) {
600 $s = "<li class=\"config-page-list-item\">";
601
602 // Messages:
603 // config-page-language, config-page-welcome, config-page-dbconnect, config-page-upgrade,
604 // config-page-dbsettings, config-page-name, config-page-options, config-page-install,
605 // config-page-complete, config-page-restart, config-page-readme, config-page-releasenotes,
606 // config-page-copying, config-page-upgradedoc, config-page-existingwiki
607 $name = wfMessage( 'config-page-' . strtolower( $pageName ) )->text();
608
609 if ( $enabled ) {
610 $query = [ 'page' => $pageName ];
611
612 if ( !in_array( $pageName, $this->pageSequence ) ) {
613 if ( in_array( $currentPageName, $this->pageSequence ) ) {
614 $query['lastPage'] = $currentPageName;
615 }
616
617 $link = Html::element( 'a',
618 [
619 'href' => $this->getUrl( $query )
620 ],
621 $name
622 );
623 } else {
624 $link = htmlspecialchars( $name );
625 }
626
627 if ( $pageName == $currentPageName ) {
628 $s .= "<span class=\"config-page-current\">$link</span>";
629 } else {
630 $s .= $link;
631 }
632 } else {
633 $s .= Html::element( 'span',
634 [
635 'class' => 'config-page-disabled'
636 ],
637 $name
638 );
639 }
640
641 $s .= "</li>\n";
642
643 return $s;
644 }
645
646 /**
647 * Output some stuff after a page is finished.
648 */
649 private function endPageWrapper() {
650 $this->output->addHTMLNoFlush(
651 "<div class=\"visualClear\"></div>\n" .
652 "</div>\n" .
653 "<div class=\"visualClear\"></div>\n" .
654 "</div>" );
655 }
656
657 /**
658 * Get HTML for an error box with an icon.
659 *
660 * @param string $text Wikitext, get this with wfMessage()->plain()
661 *
662 * @return string
663 */
664 public function getErrorBox( $text ) {
665 return $this->getInfoBox( $text, 'critical-32.png', 'config-error-box' );
666 }
667
668 /**
669 * Get HTML for a warning box with an icon.
670 *
671 * @param string $text Wikitext, get this with wfMessage()->plain()
672 *
673 * @return string
674 */
675 public function getWarningBox( $text ) {
676 return $this->getInfoBox( $text, 'warning-32.png', 'config-warning-box' );
677 }
678
679 /**
680 * Get HTML for an info box with an icon.
681 *
682 * @param string $text Wikitext, get this with wfMessage()->plain()
683 * @param string|bool $icon Icon name, file in mw-config/images. Default: false
684 * @param string|bool $class Additional class name to add to the wrapper div. Default: false.
685 *
686 * @return string
687 */
688 public function getInfoBox( $text, $icon = false, $class = false ) {
689 $text = $this->parse( $text, true );
690 $icon = ( $icon == false ) ?
691 'images/info-32.png' :
692 'images/' . $icon;
693 $alt = wfMessage( 'config-information' )->text();
694
695 return Html::infoBox( $text, $icon, $alt, $class );
696 }
697
698 /**
699 * Get small text indented help for a preceding form field.
700 * Parameters like wfMessage().
701 *
702 * @param string $msg
703 * @return string
704 */
705 public function getHelpBox( $msg /*, ... */ ) {
706 $args = func_get_args();
707 array_shift( $args );
708 $args = array_map( 'htmlspecialchars', $args );
709 $text = wfMessage( $msg, $args )->useDatabase( false )->plain();
710 $html = $this->parse( $text, true );
711
712 return "<div class=\"config-help-field-container\">\n" .
713 "<span class=\"config-help-field-hint\" title=\"" .
714 wfMessage( 'config-help-tooltip' )->escaped() . "\">" .
715 wfMessage( 'config-help' )->escaped() . "</span>\n" .
716 "<div class=\"config-help-field-data\">" . $html . "</div>\n" .
717 "</div>\n";
718 }
719
720 /**
721 * Output a help box.
722 * @param string $msg Key for wfMessage()
723 */
724 public function showHelpBox( $msg /*, ... */ ) {
725 $args = func_get_args();
726 $html = call_user_func_array( [ $this, 'getHelpBox' ], $args );
727 $this->output->addHTML( $html );
728 }
729
730 /**
731 * Show a short informational message.
732 * Output looks like a list.
733 *
734 * @param string $msg
735 */
736 public function showMessage( $msg /*, ... */ ) {
737 $args = func_get_args();
738 array_shift( $args );
739 $html = '<div class="config-message">' .
740 $this->parse( wfMessage( $msg, $args )->useDatabase( false )->plain() ) .
741 "</div>\n";
742 $this->output->addHTML( $html );
743 }
744
745 /**
746 * @param Status $status
747 */
748 public function showStatusMessage( Status $status ) {
749 $errors = array_merge( $status->getErrorsArray(), $status->getWarningsArray() );
750 foreach ( $errors as $error ) {
751 call_user_func_array( [ $this, 'showMessage' ], $error );
752 }
753 }
754
755 /**
756 * Label a control by wrapping a config-input div around it and putting a
757 * label before it.
758 *
759 * @param string $msg
760 * @param string $forId
761 * @param string $contents
762 * @param string $helpData
763 * @return string
764 */
765 public function label( $msg, $forId, $contents, $helpData = "" ) {
766 if ( strval( $msg ) == '' ) {
767 $labelText = '&#160;';
768 } else {
769 $labelText = wfMessage( $msg )->escaped();
770 }
771
772 $attributes = [ 'class' => 'config-label' ];
773
774 if ( $forId ) {
775 $attributes['for'] = $forId;
776 }
777
778 return "<div class=\"config-block\">\n" .
779 " <div class=\"config-block-label\">\n" .
780 Xml::tags( 'label',
781 $attributes,
782 $labelText
783 ) . "\n" .
784 $helpData .
785 " </div>\n" .
786 " <div class=\"config-block-elements\">\n" .
787 $contents .
788 " </div>\n" .
789 "</div>\n";
790 }
791
792 /**
793 * Get a labelled text box to configure a variable.
794 *
795 * @param mixed[] $params
796 * Parameters are:
797 * var: The variable to be configured (required)
798 * label: The message name for the label (required)
799 * attribs: Additional attributes for the input element (optional)
800 * controlName: The name for the input element (optional)
801 * value: The current value of the variable (optional)
802 * help: The html for the help text (optional)
803 *
804 * @return string
805 */
806 public function getTextBox( $params ) {
807 if ( !isset( $params['controlName'] ) ) {
808 $params['controlName'] = 'config_' . $params['var'];
809 }
810
811 if ( !isset( $params['value'] ) ) {
812 $params['value'] = $this->getVar( $params['var'] );
813 }
814
815 if ( !isset( $params['attribs'] ) ) {
816 $params['attribs'] = [];
817 }
818 if ( !isset( $params['help'] ) ) {
819 $params['help'] = "";
820 }
821
822 return $this->label(
823 $params['label'],
824 $params['controlName'],
825 Xml::input(
826 $params['controlName'],
827 30, // intended to be overridden by CSS
828 $params['value'],
829 $params['attribs'] + [
830 'id' => $params['controlName'],
831 'class' => 'config-input-text',
832 'tabindex' => $this->nextTabIndex()
833 ]
834 ),
835 $params['help']
836 );
837 }
838
839 /**
840 * Get a labelled textarea to configure a variable
841 *
842 * @param mixed[] $params
843 * Parameters are:
844 * var: The variable to be configured (required)
845 * label: The message name for the label (required)
846 * attribs: Additional attributes for the input element (optional)
847 * controlName: The name for the input element (optional)
848 * value: The current value of the variable (optional)
849 * help: The html for the help text (optional)
850 *
851 * @return string
852 */
853 public function getTextArea( $params ) {
854 if ( !isset( $params['controlName'] ) ) {
855 $params['controlName'] = 'config_' . $params['var'];
856 }
857
858 if ( !isset( $params['value'] ) ) {
859 $params['value'] = $this->getVar( $params['var'] );
860 }
861
862 if ( !isset( $params['attribs'] ) ) {
863 $params['attribs'] = [];
864 }
865 if ( !isset( $params['help'] ) ) {
866 $params['help'] = "";
867 }
868
869 return $this->label(
870 $params['label'],
871 $params['controlName'],
872 Xml::textarea(
873 $params['controlName'],
874 $params['value'],
875 30,
876 5,
877 $params['attribs'] + [
878 'id' => $params['controlName'],
879 'class' => 'config-input-text',
880 'tabindex' => $this->nextTabIndex()
881 ]
882 ),
883 $params['help']
884 );
885 }
886
887 /**
888 * Get a labelled password box to configure a variable.
889 *
890 * Implements password hiding
891 * @param mixed[] $params
892 * Parameters are:
893 * var: The variable to be configured (required)
894 * label: The message name for the label (required)
895 * attribs: Additional attributes for the input element (optional)
896 * controlName: The name for the input element (optional)
897 * value: The current value of the variable (optional)
898 * help: The html for the help text (optional)
899 *
900 * @return string
901 */
902 public function getPasswordBox( $params ) {
903 if ( !isset( $params['value'] ) ) {
904 $params['value'] = $this->getVar( $params['var'] );
905 }
906
907 if ( !isset( $params['attribs'] ) ) {
908 $params['attribs'] = [];
909 }
910
911 $params['value'] = $this->getFakePassword( $params['value'] );
912 $params['attribs']['type'] = 'password';
913
914 return $this->getTextBox( $params );
915 }
916
917 /**
918 * Get a labelled checkbox to configure a boolean variable.
919 *
920 * @param mixed[] $params
921 * Parameters are:
922 * var: The variable to be configured (required)
923 * label: The message name for the label (required)
924 * labelAttribs:Additional attributes for the label element (optional)
925 * attribs: Additional attributes for the input element (optional)
926 * controlName: The name for the input element (optional)
927 * value: The current value of the variable (optional)
928 * help: The html for the help text (optional)
929 *
930 * @return string
931 */
932 public function getCheckBox( $params ) {
933 if ( !isset( $params['controlName'] ) ) {
934 $params['controlName'] = 'config_' . $params['var'];
935 }
936
937 if ( !isset( $params['value'] ) ) {
938 $params['value'] = $this->getVar( $params['var'] );
939 }
940
941 if ( !isset( $params['attribs'] ) ) {
942 $params['attribs'] = [];
943 }
944 if ( !isset( $params['help'] ) ) {
945 $params['help'] = "";
946 }
947 if ( !isset( $params['labelAttribs'] ) ) {
948 $params['labelAttribs'] = [];
949 }
950 if ( isset( $params['rawtext'] ) ) {
951 $labelText = $params['rawtext'];
952 } else {
953 $labelText = $this->parse( wfMessage( $params['label'] )->text() );
954 }
955
956 return "<div class=\"config-input-check\">\n" .
957 $params['help'] .
958 Html::rawElement(
959 'label',
960 $params['labelAttribs'],
961 Xml::check(
962 $params['controlName'],
963 $params['value'],
964 $params['attribs'] + [
965 'id' => $params['controlName'],
966 'tabindex' => $this->nextTabIndex(),
967 ]
968 ) .
969 $labelText . "\n"
970 ) .
971 "</div>\n";
972 }
973
974 /**
975 * Get a set of labelled radio buttons.
976 *
977 * @param mixed[] $params
978 * Parameters are:
979 * var: The variable to be configured (required)
980 * label: The message name for the label (required)
981 * itemLabelPrefix: The message name prefix for the item labels (required)
982 * itemLabels: List of message names to use for the item labels instead
983 * of itemLabelPrefix, keyed by values
984 * values: List of allowed values (required)
985 * itemAttribs: Array of attribute arrays, outer key is the value name (optional)
986 * commonAttribs: Attribute array applied to all items
987 * controlName: The name for the input element (optional)
988 * value: The current value of the variable (optional)
989 * help: The html for the help text (optional)
990 *
991 * @return string
992 */
993 public function getRadioSet( $params ) {
994 $items = $this->getRadioElements( $params );
995
996 if ( !isset( $params['label'] ) ) {
997 $label = '';
998 } else {
999 $label = $params['label'];
1000 }
1001
1002 if ( !isset( $params['controlName'] ) ) {
1003 $params['controlName'] = 'config_' . $params['var'];
1004 }
1005
1006 if ( !isset( $params['help'] ) ) {
1007 $params['help'] = "";
1008 }
1009
1010 $s = "<ul>\n";
1011 foreach ( $items as $value => $item ) {
1012 $s .= "<li>$item</li>\n";
1013 }
1014 $s .= "</ul>\n";
1015
1016 return $this->label( $label, $params['controlName'], $s, $params['help'] );
1017 }
1018
1019 /**
1020 * Get a set of labelled radio buttons. You probably want to use getRadioSet(), not this.
1021 *
1022 * @see getRadioSet
1023 *
1024 * @param mixed[] $params
1025 * @return array
1026 */
1027 public function getRadioElements( $params ) {
1028 if ( !isset( $params['controlName'] ) ) {
1029 $params['controlName'] = 'config_' . $params['var'];
1030 }
1031
1032 if ( !isset( $params['value'] ) ) {
1033 $params['value'] = $this->getVar( $params['var'] );
1034 }
1035
1036 $items = [];
1037
1038 foreach ( $params['values'] as $value ) {
1039 $itemAttribs = [];
1040
1041 if ( isset( $params['commonAttribs'] ) ) {
1042 $itemAttribs = $params['commonAttribs'];
1043 }
1044
1045 if ( isset( $params['itemAttribs'][$value] ) ) {
1046 $itemAttribs = $params['itemAttribs'][$value] + $itemAttribs;
1047 }
1048
1049 $checked = $value == $params['value'];
1050 $id = $params['controlName'] . '_' . $value;
1051 $itemAttribs['id'] = $id;
1052 $itemAttribs['tabindex'] = $this->nextTabIndex();
1053
1054 $items[$value] =
1055 Xml::radio( $params['controlName'], $value, $checked, $itemAttribs ) .
1056 '&#160;' .
1057 Xml::tags( 'label', [ 'for' => $id ], $this->parse(
1058 isset( $params['itemLabels'] ) ?
1059 wfMessage( $params['itemLabels'][$value] )->plain() :
1060 wfMessage( $params['itemLabelPrefix'] . strtolower( $value ) )->plain()
1061 ) );
1062 }
1063
1064 return $items;
1065 }
1066
1067 /**
1068 * Output an error or warning box using a Status object.
1069 *
1070 * @param Status $status
1071 */
1072 public function showStatusBox( $status ) {
1073 if ( !$status->isGood() ) {
1074 $text = $status->getWikiText();
1075
1076 if ( $status->isOK() ) {
1077 $box = $this->getWarningBox( $text );
1078 } else {
1079 $box = $this->getErrorBox( $text );
1080 }
1081
1082 $this->output->addHTML( $box );
1083 }
1084 }
1085
1086 /**
1087 * Convenience function to set variables based on form data.
1088 * Assumes that variables containing "password" in the name are (potentially
1089 * fake) passwords.
1090 *
1091 * @param string[] $varNames
1092 * @param string $prefix The prefix added to variables to obtain form names
1093 *
1094 * @return string[]
1095 */
1096 public function setVarsFromRequest( $varNames, $prefix = 'config_' ) {
1097 $newValues = [];
1098
1099 foreach ( $varNames as $name ) {
1100 $value = $this->request->getVal( $prefix . $name );
1101 // T32524, do not trim passwords
1102 if ( stripos( $name, 'password' ) === false ) {
1103 $value = trim( $value );
1104 }
1105 $newValues[$name] = $value;
1106
1107 if ( $value === null ) {
1108 // Checkbox?
1109 $this->setVar( $name, false );
1110 } else {
1111 if ( stripos( $name, 'password' ) !== false ) {
1112 $this->setPassword( $name, $value );
1113 } else {
1114 $this->setVar( $name, $value );
1115 }
1116 }
1117 }
1118
1119 return $newValues;
1120 }
1121
1122 /**
1123 * Helper for Installer::docLink()
1124 *
1125 * @param string $page
1126 *
1127 * @return string
1128 */
1129 protected function getDocUrl( $page ) {
1130 $url = "{$_SERVER['PHP_SELF']}?page=" . urlencode( $page );
1131
1132 if ( in_array( $this->currentPageName, $this->pageSequence ) ) {
1133 $url .= '&lastPage=' . urlencode( $this->currentPageName );
1134 }
1135
1136 return $url;
1137 }
1138
1139 /**
1140 * Extension tag hook for a documentation link.
1141 *
1142 * @param string $linkText
1143 * @param string[] $attribs
1144 * @param Parser $parser Unused
1145 *
1146 * @return string
1147 */
1148 public function docLink( $linkText, $attribs, $parser ) {
1149 $url = $this->getDocUrl( $attribs['href'] );
1150
1151 return '<a href="' . htmlspecialchars( $url ) . '">' .
1152 htmlspecialchars( $linkText ) .
1153 '</a>';
1154 }
1155
1156 /**
1157 * Helper for "Download LocalSettings" link on WebInstall_Complete
1158 *
1159 * @param string $text Unused
1160 * @param string[] $attribs Unused
1161 * @param Parser $parser Unused
1162 *
1163 * @return string Html for download link
1164 */
1165 public function downloadLinkHook( $text, $attribs, $parser ) {
1166 $anchor = Html::rawElement( 'a',
1167 [ 'href' => $this->getUrl( [ 'localsettings' => 1 ] ) ],
1168 wfMessage( 'config-download-localsettings' )->parse()
1169 );
1170
1171 return Html::rawElement( 'div', [ 'class' => 'config-download-link' ], $anchor );
1172 }
1173
1174 /**
1175 * If the software package wants the LocalSettings.php file
1176 * to be placed in a specific location, override this function
1177 * (see mw-config/overrides/README) to return the path of
1178 * where the file should be saved, or false for a generic
1179 * "in the base of your install"
1180 *
1181 * @since 1.27
1182 * @return string|bool
1183 */
1184 public function getLocalSettingsLocation() {
1185 return false;
1186 }
1187
1188 /**
1189 * @return bool
1190 */
1191 public function envCheckPath() {
1192 // PHP_SELF isn't available sometimes, such as when PHP is CGI but
1193 // cgi.fix_pathinfo is disabled. In that case, fall back to SCRIPT_NAME
1194 // to get the path to the current script... hopefully it's reliable. SIGH
1195 $path = false;
1196 if ( !empty( $_SERVER['PHP_SELF'] ) ) {
1197 $path = $_SERVER['PHP_SELF'];
1198 } elseif ( !empty( $_SERVER['SCRIPT_NAME'] ) ) {
1199 $path = $_SERVER['SCRIPT_NAME'];
1200 }
1201 if ( $path === false ) {
1202 $this->showError( 'config-no-uri' );
1203 return false;
1204 }
1205
1206 return parent::envCheckPath();
1207 }
1208
1209 public function envPrepPath() {
1210 parent::envPrepPath();
1211 // PHP_SELF isn't available sometimes, such as when PHP is CGI but
1212 // cgi.fix_pathinfo is disabled. In that case, fall back to SCRIPT_NAME
1213 // to get the path to the current script... hopefully it's reliable. SIGH
1214 $path = false;
1215 if ( !empty( $_SERVER['PHP_SELF'] ) ) {
1216 $path = $_SERVER['PHP_SELF'];
1217 } elseif ( !empty( $_SERVER['SCRIPT_NAME'] ) ) {
1218 $path = $_SERVER['SCRIPT_NAME'];
1219 }
1220 if ( $path !== false ) {
1221 $scriptPath = preg_replace( '{^(.*)/(mw-)?config.*$}', '$1', $path );
1222
1223 $this->setVar( 'wgScriptPath', "$scriptPath" );
1224 // Update variables set from Setup.php that are derived from wgScriptPath
1225 $this->setVar( 'wgScript', "$scriptPath/index.php" );
1226 $this->setVar( 'wgLoadScript', "$scriptPath/load.php" );
1227 $this->setVar( 'wgStylePath', "$scriptPath/skins" );
1228 $this->setVar( 'wgLocalStylePath', "$scriptPath/skins" );
1229 $this->setVar( 'wgExtensionAssetsPath', "$scriptPath/extensions" );
1230 $this->setVar( 'wgUploadPath', "$scriptPath/images" );
1231 $this->setVar( 'wgResourceBasePath', "$scriptPath" );
1232 }
1233 }
1234
1235 /**
1236 * @return string
1237 */
1238 protected function envGetDefaultServer() {
1239 return WebRequest::detectServer();
1240 }
1241
1242 /**
1243 * Output stylesheet for web installer pages
1244 */
1245 public function outputCss() {
1246 $this->request->response()->header( 'Content-type: text/css' );
1247 echo $this->output->getCSS();
1248 }
1249
1250 /**
1251 * @return string[]
1252 */
1253 public function getPhpErrors() {
1254 return $this->phpErrors;
1255 }
1256
1257 }