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