installer: Fix Html::infoBox param docs and mark as internal
[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 // Add parser hooks
150 $parser = MediaWikiServices::getInstance()->getParser();
151 $parser->setHook( 'downloadlink', [ $this, 'downloadLinkHook' ] );
152 $parser->setHook( 'doclink', [ $this, 'docLink' ] );
153 }
154
155 /**
156 * Main entry point.
157 *
158 * @param array[] $session Initial session array
159 *
160 * @return array[] New session array
161 */
162 public function execute( array $session ) {
163 $this->session = $session;
164
165 if ( isset( $session['settings'] ) ) {
166 $this->settings = $session['settings'] + $this->settings;
167 // T187586 MediaWikiServices works with globals
168 foreach ( $this->settings as $key => $val ) {
169 $GLOBALS[$key] = $val;
170 }
171 }
172
173 $this->setupLanguage();
174
175 if ( ( $this->getVar( '_InstallDone' ) || $this->getVar( '_UpgradeDone' ) )
176 && $this->request->getVal( 'localsettings' )
177 ) {
178 $this->outputLS();
179 return $this->session;
180 }
181
182 $isCSS = $this->request->getVal( 'css' );
183 if ( $isCSS ) {
184 $this->outputCss();
185 return $this->session;
186 }
187
188 $this->happyPages = $session['happyPages'] ?? [];
189
190 $this->skippedPages = $session['skippedPages'] ?? [];
191
192 $lowestUnhappy = $this->getLowestUnhappy();
193
194 # Special case for Creative Commons partner chooser box.
195 if ( $this->request->getVal( 'SubmitCC' ) ) {
196 $page = $this->getPageByName( 'Options' );
197 $this->output->useShortHeader();
198 $this->output->allowFrames();
199 $page->submitCC();
200
201 return $this->finish();
202 }
203
204 if ( $this->request->getVal( 'ShowCC' ) ) {
205 $page = $this->getPageByName( 'Options' );
206 $this->output->useShortHeader();
207 $this->output->allowFrames();
208 $this->output->addHTML( $page->getCCDoneBox() );
209
210 return $this->finish();
211 }
212
213 # Get the page name.
214 $pageName = $this->request->getVal( 'page' );
215
216 if ( in_array( $pageName, $this->otherPages ) ) {
217 # Out of sequence
218 $pageId = false;
219 $page = $this->getPageByName( $pageName );
220 } else {
221 # Main sequence
222 if ( !$pageName || !in_array( $pageName, $this->pageSequence ) ) {
223 $pageId = $lowestUnhappy;
224 } else {
225 $pageId = array_search( $pageName, $this->pageSequence );
226 }
227
228 # If necessary, move back to the lowest-numbered unhappy page
229 if ( $pageId > $lowestUnhappy ) {
230 $pageId = $lowestUnhappy;
231 if ( $lowestUnhappy == 0 ) {
232 # Knocked back to start, possible loss of session data.
233 $this->showSessionWarning = true;
234 }
235 }
236
237 $pageName = $this->pageSequence[$pageId];
238 $page = $this->getPageByName( $pageName );
239 }
240
241 # If a back button was submitted, go back without submitting the form data.
242 if ( $this->request->wasPosted() && $this->request->getBool( 'submit-back' ) ) {
243 if ( $this->request->getVal( 'lastPage' ) ) {
244 $nextPage = $this->request->getVal( 'lastPage' );
245 } elseif ( $pageId !== false ) {
246 # Main sequence page
247 # Skip the skipped pages
248 $nextPageId = $pageId;
249
250 do {
251 $nextPageId--;
252 $nextPage = $this->pageSequence[$nextPageId];
253 } while ( isset( $this->skippedPages[$nextPage] ) );
254 } else {
255 $nextPage = $this->pageSequence[$lowestUnhappy];
256 }
257
258 $this->output->redirect( $this->getUrl( [ 'page' => $nextPage ] ) );
259
260 return $this->finish();
261 }
262
263 # Execute the page.
264 $this->currentPageName = $page->getName();
265 $this->startPageWrapper( $pageName );
266
267 if ( $page->isSlow() ) {
268 $this->disableTimeLimit();
269 }
270
271 $result = $page->execute();
272
273 $this->endPageWrapper();
274
275 if ( $result == 'skip' ) {
276 # Page skipped without explicit submission.
277 # Skip it when we click "back" so that we don't just go forward again.
278 $this->skippedPages[$pageName] = true;
279 $result = 'continue';
280 } else {
281 unset( $this->skippedPages[$pageName] );
282 }
283
284 # If it was posted, the page can request a continue to the next page.
285 if ( $result === 'continue' && !$this->output->headerDone() ) {
286 if ( $pageId !== false ) {
287 $this->happyPages[$pageId] = true;
288 }
289
290 $lowestUnhappy = $this->getLowestUnhappy();
291
292 if ( $this->request->getVal( 'lastPage' ) ) {
293 $nextPage = $this->request->getVal( 'lastPage' );
294 } elseif ( $pageId !== false ) {
295 $nextPage = $this->pageSequence[$pageId + 1];
296 } else {
297 $nextPage = $this->pageSequence[$lowestUnhappy];
298 }
299
300 if ( array_search( $nextPage, $this->pageSequence ) > $lowestUnhappy ) {
301 $nextPage = $this->pageSequence[$lowestUnhappy];
302 }
303
304 $this->output->redirect( $this->getUrl( [ 'page' => $nextPage ] ) );
305 }
306
307 return $this->finish();
308 }
309
310 /**
311 * Find the next page in sequence that hasn't been completed
312 * @return int
313 */
314 public function getLowestUnhappy() {
315 if ( count( $this->happyPages ) == 0 ) {
316 return 0;
317 } else {
318 return max( array_keys( $this->happyPages ) ) + 1;
319 }
320 }
321
322 /**
323 * Start the PHP session. This may be called before execute() to start the PHP session.
324 *
325 * @throws Exception
326 * @return bool
327 */
328 public function startSession() {
329 if ( wfIniGetBool( 'session.auto_start' ) || session_id() ) {
330 // Done already
331 return true;
332 }
333
334 $this->phpErrors = [];
335 set_error_handler( [ $this, 'errorHandler' ] );
336 try {
337 session_name( 'mw_installer_session' );
338 session_start();
339 } catch ( Exception $e ) {
340 restore_error_handler();
341 throw $e;
342 }
343 restore_error_handler();
344
345 if ( $this->phpErrors ) {
346 return false;
347 }
348
349 return true;
350 }
351
352 /**
353 * Get a hash of data identifying this MW installation.
354 *
355 * This is used by mw-config/index.php to prevent multiple installations of MW
356 * on the same cookie domain from interfering with each other.
357 *
358 * @return string
359 */
360 public function getFingerprint() {
361 // Get the base URL of the installation
362 $url = $this->request->getFullRequestURL();
363 if ( preg_match( '!^(.*\?)!', $url, $m ) ) {
364 // Trim query string
365 $url = $m[1];
366 }
367 if ( preg_match( '!^(.*)/[^/]*/[^/]*$!', $url, $m ) ) {
368 // This... seems to try to get the base path from
369 // the /mw-config/index.php. Kinda scary though?
370 $url = $m[1];
371 }
372
373 return md5( serialize( [
374 'local path' => dirname( __DIR__ ),
375 'url' => $url,
376 'version' => $GLOBALS['wgVersion']
377 ] ) );
378 }
379
380 /**
381 * Show an error message in a box. Parameters are like wfMessage(), or
382 * alternatively, pass a Message object in.
383 * @param string|Message $msg
384 * @param mixed ...$params
385 */
386 public function showError( $msg, ...$params ) {
387 if ( !( $msg instanceof Message ) ) {
388 $msg = wfMessage(
389 $msg,
390 array_map( 'htmlspecialchars', $params )
391 );
392 }
393 $text = $msg->useDatabase( false )->plain();
394 $this->output->addHTML( $this->getErrorBox( $text ) );
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 * @param string $text Wikitext, get this with wfMessage()->plain()
642 *
643 * @return string
644 */
645 public function getErrorBox( $text ) {
646 return $this->getInfoBox( $text, 'critical-32.png', 'config-error-box' );
647 }
648
649 /**
650 * Get HTML for a warning box with an icon.
651 *
652 * @param string $text Wikitext, get this with wfMessage()->plain()
653 *
654 * @return string
655 */
656 public function getWarningBox( $text ) {
657 return $this->getInfoBox( $text, 'warning-32.png', 'config-warning-box' );
658 }
659
660 /**
661 * Get HTML for an information message box with an icon.
662 *
663 * @param string $text Wikitext to be parsed (from Message::plain).
664 * @param string|bool $icon Icon name, file in mw-config/images. Default: false
665 * @param string|bool $class Additional class name to add to the wrapper div. Default: false.
666 * @return string HTML
667 */
668 public function getInfoBox( $text, $icon = false, $class = false ) {
669 $html = $this->parse( $text, true );
670 $icon = ( $icon == false ) ?
671 'images/info-32.png' :
672 'images/' . $icon;
673 $alt = wfMessage( 'config-information' )->text();
674
675 return Html::infoBox( $html, $icon, $alt, $class );
676 }
677
678 /**
679 * Get small text indented help for a preceding form field.
680 * Parameters like wfMessage().
681 *
682 * @param string $msg
683 * @return string
684 */
685 public function getHelpBox( $msg, ...$args ) {
686 $args = array_map( 'htmlspecialchars', $args );
687 $text = wfMessage( $msg, $args )->useDatabase( false )->plain();
688 $html = $this->parse( $text, true );
689 $id = 'helpBox-' . $this->helpBoxId++;
690
691 return "<div class=\"config-help-field-container\">\n" .
692 "<input type=\"checkbox\" class=\"config-help-field-checkbox\" id=\"$id\" />" .
693 "<label class=\"config-help-field-hint\" for=\"$id\" title=\"" .
694 wfMessage( 'config-help-tooltip' )->escaped() . "\">" .
695 wfMessage( 'config-help' )->escaped() . "</label>\n" .
696 "<div class=\"config-help-field-data\">" . $html . "</div>\n" .
697 "</div>\n";
698 }
699
700 /**
701 * Output a help box.
702 * @param string $msg Key for wfMessage()
703 * @param mixed ...$params
704 */
705 public function showHelpBox( $msg, ...$params ) {
706 $html = $this->getHelpBox( $msg, ...$params );
707 $this->output->addHTML( $html );
708 }
709
710 /**
711 * Show a short informational message.
712 * Output looks like a list.
713 *
714 * @param string $msg
715 * @param mixed ...$params
716 */
717 public function showMessage( $msg, ...$params ) {
718 $html = '<div class="config-message">' .
719 $this->parse( wfMessage( $msg, $params )->useDatabase( false )->plain() ) .
720 "</div>\n";
721 $this->output->addHTML( $html );
722 }
723
724 /**
725 * @param Status $status
726 */
727 public function showStatusMessage( Status $status ) {
728 $errors = array_merge( $status->getErrorsArray(), $status->getWarningsArray() );
729 foreach ( $errors as $error ) {
730 $this->showMessage( ...$error );
731 }
732 }
733
734 /**
735 * Label a control by wrapping a config-input div around it and putting a
736 * label before it.
737 *
738 * @param string $msg
739 * @param string $forId
740 * @param string $contents
741 * @param string $helpData
742 * @return string
743 */
744 public function label( $msg, $forId, $contents, $helpData = "" ) {
745 if ( strval( $msg ) == '' ) {
746 $labelText = "\u{00A0}";
747 } else {
748 $labelText = wfMessage( $msg )->escaped();
749 }
750
751 $attributes = [ 'class' => 'config-label' ];
752
753 if ( $forId ) {
754 $attributes['for'] = $forId;
755 }
756
757 return "<div class=\"config-block\">\n" .
758 " <div class=\"config-block-label\">\n" .
759 Xml::tags( 'label',
760 $attributes,
761 $labelText
762 ) . "\n" .
763 $helpData .
764 " </div>\n" .
765 " <div class=\"config-block-elements\">\n" .
766 $contents .
767 " </div>\n" .
768 "</div>\n";
769 }
770
771 /**
772 * Get a labelled text box to configure a variable.
773 *
774 * @param mixed[] $params
775 * Parameters are:
776 * var: The variable to be configured (required)
777 * label: The message name for the label (required)
778 * attribs: Additional attributes for the input element (optional)
779 * controlName: The name for the input element (optional)
780 * value: The current value of the variable (optional)
781 * help: The html for the help text (optional)
782 *
783 * @return string
784 */
785 public function getTextBox( $params ) {
786 if ( !isset( $params['controlName'] ) ) {
787 $params['controlName'] = 'config_' . $params['var'];
788 }
789
790 if ( !isset( $params['value'] ) ) {
791 $params['value'] = $this->getVar( $params['var'] );
792 }
793
794 if ( !isset( $params['attribs'] ) ) {
795 $params['attribs'] = [];
796 }
797 if ( !isset( $params['help'] ) ) {
798 $params['help'] = "";
799 }
800
801 return $this->label(
802 $params['label'],
803 $params['controlName'],
804 Xml::input(
805 $params['controlName'],
806 30, // intended to be overridden by CSS
807 $params['value'],
808 $params['attribs'] + [
809 'id' => $params['controlName'],
810 'class' => 'config-input-text',
811 'tabindex' => $this->nextTabIndex()
812 ]
813 ),
814 $params['help']
815 );
816 }
817
818 /**
819 * Get a labelled textarea to configure a variable
820 *
821 * @param mixed[] $params
822 * Parameters are:
823 * var: The variable to be configured (required)
824 * label: The message name for the label (required)
825 * attribs: Additional attributes for the input element (optional)
826 * controlName: The name for the input element (optional)
827 * value: The current value of the variable (optional)
828 * help: The html for the help text (optional)
829 *
830 * @return string
831 */
832 public function getTextArea( $params ) {
833 if ( !isset( $params['controlName'] ) ) {
834 $params['controlName'] = 'config_' . $params['var'];
835 }
836
837 if ( !isset( $params['value'] ) ) {
838 $params['value'] = $this->getVar( $params['var'] );
839 }
840
841 if ( !isset( $params['attribs'] ) ) {
842 $params['attribs'] = [];
843 }
844 if ( !isset( $params['help'] ) ) {
845 $params['help'] = "";
846 }
847
848 return $this->label(
849 $params['label'],
850 $params['controlName'],
851 Xml::textarea(
852 $params['controlName'],
853 $params['value'],
854 30,
855 5,
856 $params['attribs'] + [
857 'id' => $params['controlName'],
858 'class' => 'config-input-text',
859 'tabindex' => $this->nextTabIndex()
860 ]
861 ),
862 $params['help']
863 );
864 }
865
866 /**
867 * Get a labelled password box to configure a variable.
868 *
869 * Implements password hiding
870 * @param mixed[] $params
871 * Parameters are:
872 * var: The variable to be configured (required)
873 * label: The message name for the label (required)
874 * attribs: Additional attributes for the input element (optional)
875 * controlName: The name for the input element (optional)
876 * value: The current value of the variable (optional)
877 * help: The html for the help text (optional)
878 *
879 * @return string
880 */
881 public function getPasswordBox( $params ) {
882 if ( !isset( $params['value'] ) ) {
883 $params['value'] = $this->getVar( $params['var'] );
884 }
885
886 if ( !isset( $params['attribs'] ) ) {
887 $params['attribs'] = [];
888 }
889
890 $params['value'] = $this->getFakePassword( $params['value'] );
891 $params['attribs']['type'] = 'password';
892
893 return $this->getTextBox( $params );
894 }
895
896 /**
897 * Get a labelled checkbox to configure a boolean variable.
898 *
899 * @param mixed[] $params
900 * Parameters are:
901 * var: The variable to be configured (required)
902 * label: The message name for the label (required)
903 * labelAttribs:Additional attributes for the label element (optional)
904 * attribs: Additional attributes for the input element (optional)
905 * controlName: The name for the input element (optional)
906 * value: The current value of the variable (optional)
907 * help: The html for the help text (optional)
908 *
909 * @return string
910 */
911 public function getCheckBox( $params ) {
912 if ( !isset( $params['controlName'] ) ) {
913 $params['controlName'] = 'config_' . $params['var'];
914 }
915
916 if ( !isset( $params['value'] ) ) {
917 $params['value'] = $this->getVar( $params['var'] );
918 }
919
920 if ( !isset( $params['attribs'] ) ) {
921 $params['attribs'] = [];
922 }
923 if ( !isset( $params['help'] ) ) {
924 $params['help'] = "";
925 }
926 if ( !isset( $params['labelAttribs'] ) ) {
927 $params['labelAttribs'] = [];
928 }
929 $labelText = $params['rawtext'] ?? $this->parse( wfMessage( $params['label'] )->plain() );
930
931 return "<div class=\"config-input-check\">\n" .
932 $params['help'] .
933 Html::rawElement(
934 'label',
935 $params['labelAttribs'],
936 Xml::check(
937 $params['controlName'],
938 $params['value'],
939 $params['attribs'] + [
940 'id' => $params['controlName'],
941 'tabindex' => $this->nextTabIndex(),
942 ]
943 ) .
944 $labelText . "\n"
945 ) .
946 "</div>\n";
947 }
948
949 /**
950 * Get a set of labelled radio buttons.
951 *
952 * @param mixed[] $params
953 * Parameters are:
954 * var: The variable to be configured (required)
955 * label: The message name for the label (required)
956 * itemLabelPrefix: The message name prefix for the item labels (required)
957 * itemLabels: List of message names to use for the item labels instead
958 * of itemLabelPrefix, keyed by values
959 * values: List of allowed values (required)
960 * itemAttribs: Array of attribute arrays, outer key is the value name (optional)
961 * commonAttribs: Attribute array applied to all items
962 * controlName: The name for the input element (optional)
963 * value: The current value of the variable (optional)
964 * help: The html for the help text (optional)
965 *
966 * @return string
967 */
968 public function getRadioSet( $params ) {
969 $items = $this->getRadioElements( $params );
970
971 $label = $params['label'] ?? '';
972
973 if ( !isset( $params['controlName'] ) ) {
974 $params['controlName'] = 'config_' . $params['var'];
975 }
976
977 if ( !isset( $params['help'] ) ) {
978 $params['help'] = "";
979 }
980
981 $s = "<ul>\n";
982 foreach ( $items as $value => $item ) {
983 $s .= "<li>$item</li>\n";
984 }
985 $s .= "</ul>\n";
986
987 return $this->label( $label, $params['controlName'], $s, $params['help'] );
988 }
989
990 /**
991 * Get a set of labelled radio buttons. You probably want to use getRadioSet(), not this.
992 *
993 * @see getRadioSet
994 *
995 * @param mixed[] $params
996 * @return array
997 */
998 public function getRadioElements( $params ) {
999 if ( !isset( $params['controlName'] ) ) {
1000 $params['controlName'] = 'config_' . $params['var'];
1001 }
1002
1003 if ( !isset( $params['value'] ) ) {
1004 $params['value'] = $this->getVar( $params['var'] );
1005 }
1006
1007 $items = [];
1008
1009 foreach ( $params['values'] as $value ) {
1010 $itemAttribs = [];
1011
1012 if ( isset( $params['commonAttribs'] ) ) {
1013 $itemAttribs = $params['commonAttribs'];
1014 }
1015
1016 if ( isset( $params['itemAttribs'][$value] ) ) {
1017 $itemAttribs = $params['itemAttribs'][$value] + $itemAttribs;
1018 }
1019
1020 $checked = $value == $params['value'];
1021 $id = $params['controlName'] . '_' . $value;
1022 $itemAttribs['id'] = $id;
1023 $itemAttribs['tabindex'] = $this->nextTabIndex();
1024
1025 $items[$value] =
1026 Xml::radio( $params['controlName'], $value, $checked, $itemAttribs ) .
1027 "\u{00A0}" .
1028 Xml::tags( 'label', [ 'for' => $id ], $this->parse(
1029 isset( $params['itemLabels'] ) ?
1030 wfMessage( $params['itemLabels'][$value] )->plain() :
1031 wfMessage( $params['itemLabelPrefix'] . strtolower( $value ) )->plain()
1032 ) );
1033 }
1034
1035 return $items;
1036 }
1037
1038 /**
1039 * Output an error or warning box using a Status object.
1040 *
1041 * @param Status $status
1042 */
1043 public function showStatusBox( $status ) {
1044 if ( !$status->isGood() ) {
1045 $text = $status->getWikiText();
1046
1047 if ( $status->isOK() ) {
1048 $box = $this->getWarningBox( $text );
1049 } else {
1050 $box = $this->getErrorBox( $text );
1051 }
1052
1053 $this->output->addHTML( $box );
1054 }
1055 }
1056
1057 /**
1058 * Convenience function to set variables based on form data.
1059 * Assumes that variables containing "password" in the name are (potentially
1060 * fake) passwords.
1061 *
1062 * @param string[] $varNames
1063 * @param string $prefix The prefix added to variables to obtain form names
1064 *
1065 * @return string[]
1066 */
1067 public function setVarsFromRequest( $varNames, $prefix = 'config_' ) {
1068 $newValues = [];
1069
1070 foreach ( $varNames as $name ) {
1071 $value = $this->request->getVal( $prefix . $name );
1072 // T32524, do not trim passwords
1073 if ( stripos( $name, 'password' ) === false ) {
1074 $value = trim( $value );
1075 }
1076 $newValues[$name] = $value;
1077
1078 if ( $value === null ) {
1079 // Checkbox?
1080 $this->setVar( $name, false );
1081 } elseif ( stripos( $name, 'password' ) !== false ) {
1082 $this->setPassword( $name, $value );
1083 } else {
1084 $this->setVar( $name, $value );
1085 }
1086 }
1087
1088 return $newValues;
1089 }
1090
1091 /**
1092 * Helper for Installer::docLink()
1093 *
1094 * @param string $page
1095 *
1096 * @return string
1097 */
1098 protected function getDocUrl( $page ) {
1099 $query = [ 'page' => $page ];
1100
1101 if ( in_array( $this->currentPageName, $this->pageSequence ) ) {
1102 $query['lastPage'] = $this->currentPageName;
1103 }
1104
1105 return $this->getUrl( $query );
1106 }
1107
1108 /**
1109 * Extension tag hook for a documentation link.
1110 *
1111 * @param string $linkText
1112 * @param string[] $attribs
1113 * @param Parser $parser Unused
1114 *
1115 * @return string
1116 */
1117 public function docLink( $linkText, $attribs, $parser ) {
1118 $url = $this->getDocUrl( $attribs['href'] );
1119
1120 return Html::element( 'a', [ 'href' => $url ], $linkText );
1121 }
1122
1123 /**
1124 * Helper for "Download LocalSettings" link on WebInstall_Complete
1125 *
1126 * @param string $text Unused
1127 * @param string[] $attribs Unused
1128 * @param Parser $parser Unused
1129 *
1130 * @return string Html for download link
1131 */
1132 public function downloadLinkHook( $text, $attribs, $parser ) {
1133 $anchor = Html::rawElement( 'a',
1134 [ 'href' => $this->getUrl( [ 'localsettings' => 1 ] ) ],
1135 wfMessage( 'config-download-localsettings' )->parse()
1136 );
1137
1138 return Html::rawElement( 'div', [ 'class' => 'config-download-link' ], $anchor );
1139 }
1140
1141 /**
1142 * If the software package wants the LocalSettings.php file
1143 * to be placed in a specific location, override this function
1144 * (see mw-config/overrides/README) to return the path of
1145 * where the file should be saved, or false for a generic
1146 * "in the base of your install"
1147 *
1148 * @since 1.27
1149 * @return string|bool
1150 */
1151 public function getLocalSettingsLocation() {
1152 return false;
1153 }
1154
1155 /**
1156 * @return bool
1157 */
1158 public function envCheckPath() {
1159 // PHP_SELF isn't available sometimes, such as when PHP is CGI but
1160 // cgi.fix_pathinfo is disabled. In that case, fall back to SCRIPT_NAME
1161 // to get the path to the current script... hopefully it's reliable. SIGH
1162 $path = false;
1163 if ( !empty( $_SERVER['PHP_SELF'] ) ) {
1164 $path = $_SERVER['PHP_SELF'];
1165 } elseif ( !empty( $_SERVER['SCRIPT_NAME'] ) ) {
1166 $path = $_SERVER['SCRIPT_NAME'];
1167 }
1168 if ( $path === false ) {
1169 $this->showError( 'config-no-uri' );
1170 return false;
1171 }
1172
1173 return parent::envCheckPath();
1174 }
1175
1176 public function envPrepPath() {
1177 parent::envPrepPath();
1178 // PHP_SELF isn't available sometimes, such as when PHP is CGI but
1179 // cgi.fix_pathinfo is disabled. In that case, fall back to SCRIPT_NAME
1180 // to get the path to the current script... hopefully it's reliable. SIGH
1181 $path = false;
1182 if ( !empty( $_SERVER['PHP_SELF'] ) ) {
1183 $path = $_SERVER['PHP_SELF'];
1184 } elseif ( !empty( $_SERVER['SCRIPT_NAME'] ) ) {
1185 $path = $_SERVER['SCRIPT_NAME'];
1186 }
1187 if ( $path !== false ) {
1188 $scriptPath = preg_replace( '{^(.*)/(mw-)?config.*$}', '$1', $path );
1189
1190 $this->setVar( 'wgScriptPath', "$scriptPath" );
1191 // Update variables set from Setup.php that are derived from wgScriptPath
1192 $this->setVar( 'wgScript', "$scriptPath/index.php" );
1193 $this->setVar( 'wgLoadScript', "$scriptPath/load.php" );
1194 $this->setVar( 'wgStylePath', "$scriptPath/skins" );
1195 $this->setVar( 'wgLocalStylePath', "$scriptPath/skins" );
1196 $this->setVar( 'wgExtensionAssetsPath', "$scriptPath/extensions" );
1197 $this->setVar( 'wgUploadPath', "$scriptPath/images" );
1198 $this->setVar( 'wgResourceBasePath', "$scriptPath" );
1199 }
1200 }
1201
1202 /**
1203 * @return string
1204 */
1205 protected function envGetDefaultServer() {
1206 return WebRequest::detectServer();
1207 }
1208
1209 /**
1210 * Actually output LocalSettings.php for download
1211 *
1212 * @suppress SecurityCheck-XSS
1213 */
1214 private function outputLS() {
1215 $this->request->response()->header( 'Content-type: application/x-httpd-php' );
1216 $this->request->response()->header(
1217 'Content-Disposition: attachment; filename="LocalSettings.php"'
1218 );
1219
1220 $ls = InstallerOverrides::getLocalSettingsGenerator( $this );
1221 $rightsProfile = $this->rightsProfiles[$this->getVar( '_RightsProfile' )];
1222 foreach ( $rightsProfile as $group => $rightsArr ) {
1223 $ls->setGroupRights( $group, $rightsArr );
1224 }
1225 echo $ls->getText();
1226 }
1227
1228 /**
1229 * Output stylesheet for web installer pages
1230 */
1231 public function outputCss() {
1232 $this->request->response()->header( 'Content-type: text/css' );
1233 echo $this->output->getCSS();
1234 }
1235
1236 /**
1237 * @return string[]
1238 */
1239 public function getPhpErrors() {
1240 return $this->phpErrors;
1241 }
1242
1243 }