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