Merge "Replace infobox usages and extend successbox, warningbox and errorbox"
[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 * @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|HtmlArmor $text Wikitext to be parsed (from Message::plain) or raw HTML.
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 = ( $text instanceof HtmlArmor ) ?
670 HtmlArmor::getHtml( $text ) :
671 $this->parse( $text, true );
672 $icon = ( $icon == false ) ?
673 'images/info-32.png' :
674 'images/' . $icon;
675 $alt = wfMessage( 'config-information' )->text();
676
677 return Html::infoBox( $html, $icon, $alt, $class );
678 }
679
680 /**
681 * Get small text indented help for a preceding form field.
682 * Parameters like wfMessage().
683 *
684 * @param string $msg
685 * @return string
686 */
687 public function getHelpBox( $msg, ...$args ) {
688 $args = array_map( 'htmlspecialchars', $args );
689 $text = wfMessage( $msg, $args )->useDatabase( false )->plain();
690 $html = $this->parse( $text, true );
691 $id = 'helpBox-' . $this->helpBoxId++;
692
693 return "<div class=\"config-help-field-container\">\n" .
694 "<input type=\"checkbox\" class=\"config-help-field-checkbox\" id=\"$id\" />" .
695 "<label class=\"config-help-field-hint\" for=\"$id\" title=\"" .
696 wfMessage( 'config-help-tooltip' )->escaped() . "\">" .
697 wfMessage( 'config-help' )->escaped() . "</label>\n" .
698 "<div class=\"config-help-field-data\">" . $html . "</div>\n" .
699 "</div>\n";
700 }
701
702 /**
703 * Output a help box.
704 * @param string $msg Key for wfMessage()
705 * @param mixed ...$params
706 */
707 public function showHelpBox( $msg, ...$params ) {
708 $html = $this->getHelpBox( $msg, ...$params );
709 $this->output->addHTML( $html );
710 }
711
712 /**
713 * Show a short informational message.
714 * Output looks like a list.
715 *
716 * @param string $msg
717 * @param mixed ...$params
718 */
719 public function showMessage( $msg, ...$params ) {
720 $html = '<div class="config-message">' .
721 $this->parse( wfMessage( $msg, $params )->useDatabase( false )->plain() ) .
722 "</div>\n";
723 $this->output->addHTML( $html );
724 }
725
726 /**
727 * @param Status $status
728 */
729 public function showStatusMessage( Status $status ) {
730 $errors = array_merge( $status->getErrorsArray(), $status->getWarningsArray() );
731 foreach ( $errors as $error ) {
732 $this->showMessage( ...$error );
733 }
734 }
735
736 /**
737 * Label a control by wrapping a config-input div around it and putting a
738 * label before it.
739 *
740 * @param string $msg
741 * @param string $forId
742 * @param string $contents
743 * @param string $helpData
744 * @return string
745 */
746 public function label( $msg, $forId, $contents, $helpData = "" ) {
747 if ( strval( $msg ) == '' ) {
748 $labelText = "\u{00A0}";
749 } else {
750 $labelText = wfMessage( $msg )->escaped();
751 }
752
753 $attributes = [ 'class' => 'config-label' ];
754
755 if ( $forId ) {
756 $attributes['for'] = $forId;
757 }
758
759 return "<div class=\"config-block\">\n" .
760 " <div class=\"config-block-label\">\n" .
761 Xml::tags( 'label',
762 $attributes,
763 $labelText
764 ) . "\n" .
765 $helpData .
766 " </div>\n" .
767 " <div class=\"config-block-elements\">\n" .
768 $contents .
769 " </div>\n" .
770 "</div>\n";
771 }
772
773 /**
774 * Get a labelled text box to configure a variable.
775 *
776 * @param mixed[] $params
777 * Parameters are:
778 * var: The variable to be configured (required)
779 * label: The message name for the label (required)
780 * attribs: Additional attributes for the input element (optional)
781 * controlName: The name for the input element (optional)
782 * value: The current value of the variable (optional)
783 * help: The html for the help text (optional)
784 *
785 * @return string
786 */
787 public function getTextBox( $params ) {
788 if ( !isset( $params['controlName'] ) ) {
789 $params['controlName'] = 'config_' . $params['var'];
790 }
791
792 if ( !isset( $params['value'] ) ) {
793 $params['value'] = $this->getVar( $params['var'] );
794 }
795
796 if ( !isset( $params['attribs'] ) ) {
797 $params['attribs'] = [];
798 }
799 if ( !isset( $params['help'] ) ) {
800 $params['help'] = "";
801 }
802
803 return $this->label(
804 $params['label'],
805 $params['controlName'],
806 Xml::input(
807 $params['controlName'],
808 30, // intended to be overridden by CSS
809 $params['value'],
810 $params['attribs'] + [
811 'id' => $params['controlName'],
812 'class' => 'config-input-text',
813 'tabindex' => $this->nextTabIndex()
814 ]
815 ),
816 $params['help']
817 );
818 }
819
820 /**
821 * Get a labelled textarea to configure a variable
822 *
823 * @param mixed[] $params
824 * Parameters are:
825 * var: The variable to be configured (required)
826 * label: The message name for the label (required)
827 * attribs: Additional attributes for the input element (optional)
828 * controlName: The name for the input element (optional)
829 * value: The current value of the variable (optional)
830 * help: The html for the help text (optional)
831 *
832 * @return string
833 */
834 public function getTextArea( $params ) {
835 if ( !isset( $params['controlName'] ) ) {
836 $params['controlName'] = 'config_' . $params['var'];
837 }
838
839 if ( !isset( $params['value'] ) ) {
840 $params['value'] = $this->getVar( $params['var'] );
841 }
842
843 if ( !isset( $params['attribs'] ) ) {
844 $params['attribs'] = [];
845 }
846 if ( !isset( $params['help'] ) ) {
847 $params['help'] = "";
848 }
849
850 return $this->label(
851 $params['label'],
852 $params['controlName'],
853 Xml::textarea(
854 $params['controlName'],
855 $params['value'],
856 30,
857 5,
858 $params['attribs'] + [
859 'id' => $params['controlName'],
860 'class' => 'config-input-text',
861 'tabindex' => $this->nextTabIndex()
862 ]
863 ),
864 $params['help']
865 );
866 }
867
868 /**
869 * Get a labelled password box to configure a variable.
870 *
871 * Implements password hiding
872 * @param mixed[] $params
873 * Parameters are:
874 * var: The variable to be configured (required)
875 * label: The message name for the label (required)
876 * attribs: Additional attributes for the input element (optional)
877 * controlName: The name for the input element (optional)
878 * value: The current value of the variable (optional)
879 * help: The html for the help text (optional)
880 *
881 * @return string
882 */
883 public function getPasswordBox( $params ) {
884 if ( !isset( $params['value'] ) ) {
885 $params['value'] = $this->getVar( $params['var'] );
886 }
887
888 if ( !isset( $params['attribs'] ) ) {
889 $params['attribs'] = [];
890 }
891
892 $params['value'] = $this->getFakePassword( $params['value'] );
893 $params['attribs']['type'] = 'password';
894
895 return $this->getTextBox( $params );
896 }
897
898 /**
899 * Get a labelled checkbox to configure a boolean variable.
900 *
901 * @param mixed[] $params
902 * Parameters are:
903 * var: The variable to be configured (required)
904 * label: The message name for the label (required)
905 * labelAttribs:Additional attributes for the label element (optional)
906 * attribs: Additional attributes for the input element (optional)
907 * controlName: The name for the input element (optional)
908 * value: The current value of the variable (optional)
909 * help: The html for the help text (optional)
910 *
911 * @return string
912 */
913 public function getCheckBox( $params ) {
914 if ( !isset( $params['controlName'] ) ) {
915 $params['controlName'] = 'config_' . $params['var'];
916 }
917
918 if ( !isset( $params['value'] ) ) {
919 $params['value'] = $this->getVar( $params['var'] );
920 }
921
922 if ( !isset( $params['attribs'] ) ) {
923 $params['attribs'] = [];
924 }
925 if ( !isset( $params['help'] ) ) {
926 $params['help'] = "";
927 }
928 if ( !isset( $params['labelAttribs'] ) ) {
929 $params['labelAttribs'] = [];
930 }
931 $labelText = $params['rawtext'] ?? $this->parse( wfMessage( $params['label'] )->plain() );
932
933 return "<div class=\"config-input-check\">\n" .
934 $params['help'] .
935 Html::rawElement(
936 'label',
937 $params['labelAttribs'],
938 Xml::check(
939 $params['controlName'],
940 $params['value'],
941 $params['attribs'] + [
942 'id' => $params['controlName'],
943 'tabindex' => $this->nextTabIndex(),
944 ]
945 ) .
946 $labelText . "\n"
947 ) .
948 "</div>\n";
949 }
950
951 /**
952 * Get a set of labelled radio buttons.
953 *
954 * @param mixed[] $params
955 * Parameters are:
956 * var: The variable to be configured (required)
957 * label: The message name for the label (required)
958 * itemLabelPrefix: The message name prefix for the item labels (required)
959 * itemLabels: List of message names to use for the item labels instead
960 * of itemLabelPrefix, keyed by values
961 * values: List of allowed values (required)
962 * itemAttribs: Array of attribute arrays, outer key is the value name (optional)
963 * commonAttribs: Attribute array applied to all items
964 * controlName: The name for the input element (optional)
965 * value: The current value of the variable (optional)
966 * help: The html for the help text (optional)
967 *
968 * @return string
969 */
970 public function getRadioSet( $params ) {
971 $items = $this->getRadioElements( $params );
972
973 $label = $params['label'] ?? '';
974
975 if ( !isset( $params['controlName'] ) ) {
976 $params['controlName'] = 'config_' . $params['var'];
977 }
978
979 if ( !isset( $params['help'] ) ) {
980 $params['help'] = "";
981 }
982
983 $s = "<ul>\n";
984 foreach ( $items as $value => $item ) {
985 $s .= "<li>$item</li>\n";
986 }
987 $s .= "</ul>\n";
988
989 return $this->label( $label, $params['controlName'], $s, $params['help'] );
990 }
991
992 /**
993 * Get a set of labelled radio buttons. You probably want to use getRadioSet(), not this.
994 *
995 * @see getRadioSet
996 *
997 * @param mixed[] $params
998 * @return array
999 */
1000 public function getRadioElements( $params ) {
1001 if ( !isset( $params['controlName'] ) ) {
1002 $params['controlName'] = 'config_' . $params['var'];
1003 }
1004
1005 if ( !isset( $params['value'] ) ) {
1006 $params['value'] = $this->getVar( $params['var'] );
1007 }
1008
1009 $items = [];
1010
1011 foreach ( $params['values'] as $value ) {
1012 $itemAttribs = [];
1013
1014 if ( isset( $params['commonAttribs'] ) ) {
1015 $itemAttribs = $params['commonAttribs'];
1016 }
1017
1018 if ( isset( $params['itemAttribs'][$value] ) ) {
1019 $itemAttribs = $params['itemAttribs'][$value] + $itemAttribs;
1020 }
1021
1022 $checked = $value == $params['value'];
1023 $id = $params['controlName'] . '_' . $value;
1024 $itemAttribs['id'] = $id;
1025 $itemAttribs['tabindex'] = $this->nextTabIndex();
1026
1027 $items[$value] =
1028 Xml::radio( $params['controlName'], $value, $checked, $itemAttribs ) .
1029 "\u{00A0}" .
1030 Xml::tags( 'label', [ 'for' => $id ], $this->parse(
1031 isset( $params['itemLabels'] ) ?
1032 wfMessage( $params['itemLabels'][$value] )->plain() :
1033 wfMessage( $params['itemLabelPrefix'] . strtolower( $value ) )->plain()
1034 ) );
1035 }
1036
1037 return $items;
1038 }
1039
1040 /**
1041 * Output an error or warning box using a Status object.
1042 *
1043 * @param Status $status
1044 */
1045 public function showStatusBox( $status ) {
1046 if ( !$status->isGood() ) {
1047 $text = $status->getWikiText();
1048
1049 if ( $status->isOK() ) {
1050 $box = Html::warningBox( $text, 'config-warning-box' );
1051 } else {
1052 $box = Html::errorBox( $text, '', 'config-error-box' );
1053 }
1054
1055 $this->output->addHTML( $box );
1056 }
1057 }
1058
1059 /**
1060 * Convenience function to set variables based on form data.
1061 * Assumes that variables containing "password" in the name are (potentially
1062 * fake) passwords.
1063 *
1064 * @param string[] $varNames
1065 * @param string $prefix The prefix added to variables to obtain form names
1066 *
1067 * @return string[]
1068 */
1069 public function setVarsFromRequest( $varNames, $prefix = 'config_' ) {
1070 $newValues = [];
1071
1072 foreach ( $varNames as $name ) {
1073 $value = $this->request->getVal( $prefix . $name );
1074 // T32524, do not trim passwords
1075 if ( stripos( $name, 'password' ) === false ) {
1076 $value = trim( $value );
1077 }
1078 $newValues[$name] = $value;
1079
1080 if ( $value === null ) {
1081 // Checkbox?
1082 $this->setVar( $name, false );
1083 } elseif ( stripos( $name, 'password' ) !== false ) {
1084 $this->setPassword( $name, $value );
1085 } else {
1086 $this->setVar( $name, $value );
1087 }
1088 }
1089
1090 return $newValues;
1091 }
1092
1093 /**
1094 * Helper for WebInstallerOutput
1095 *
1096 * @internal For use by WebInstallerOutput
1097 * @param string $page
1098 * @return string
1099 */
1100 public function getDocUrl( $page ) {
1101 $query = [ 'page' => $page ];
1102
1103 if ( in_array( $this->currentPageName, $this->pageSequence ) ) {
1104 $query['lastPage'] = $this->currentPageName;
1105 }
1106
1107 return $this->getUrl( $query );
1108 }
1109
1110 /**
1111 * Helper for sidebar links.
1112 *
1113 * @internal For use in WebInstallerOutput class
1114 * @param string $url
1115 * @param string $linkText
1116 * @return string HTML
1117 */
1118 public function makeLinkItem( $url, $linkText ) {
1119 return Html::rawElement( 'li', [],
1120 Html::element( 'a', [ 'href' => $url ], $linkText )
1121 );
1122 }
1123
1124 /**
1125 * Helper for "Download LocalSettings" link.
1126 *
1127 * @internal For use in WebInstallerComplete class
1128 * @return string Html for download link
1129 */
1130 public function makeDownloadLinkHtml() {
1131 $anchor = Html::rawElement( 'a',
1132 [ 'href' => $this->getUrl( [ 'localsettings' => 1 ] ) ],
1133 wfMessage( 'config-download-localsettings' )->parse()
1134 );
1135
1136 return Html::rawElement( 'div', [ 'class' => 'config-download-link' ], $anchor );
1137 }
1138
1139 /**
1140 * If the software package wants the LocalSettings.php file
1141 * to be placed in a specific location, override this function
1142 * (see mw-config/overrides/README) to return the path of
1143 * where the file should be saved, or false for a generic
1144 * "in the base of your install"
1145 *
1146 * @since 1.27
1147 * @return string|bool
1148 */
1149 public function getLocalSettingsLocation() {
1150 return false;
1151 }
1152
1153 /**
1154 * @return bool
1155 */
1156 public function envCheckPath() {
1157 // PHP_SELF isn't available sometimes, such as when PHP is CGI but
1158 // cgi.fix_pathinfo is disabled. In that case, fall back to SCRIPT_NAME
1159 // to get the path to the current script... hopefully it's reliable. SIGH
1160 $path = false;
1161 if ( !empty( $_SERVER['PHP_SELF'] ) ) {
1162 $path = $_SERVER['PHP_SELF'];
1163 } elseif ( !empty( $_SERVER['SCRIPT_NAME'] ) ) {
1164 $path = $_SERVER['SCRIPT_NAME'];
1165 }
1166 if ( $path === false ) {
1167 $this->showError( 'config-no-uri' );
1168 return false;
1169 }
1170
1171 return parent::envCheckPath();
1172 }
1173
1174 public function envPrepPath() {
1175 parent::envPrepPath();
1176 // PHP_SELF isn't available sometimes, such as when PHP is CGI but
1177 // cgi.fix_pathinfo is disabled. In that case, fall back to SCRIPT_NAME
1178 // to get the path to the current script... hopefully it's reliable. SIGH
1179 $path = false;
1180 if ( !empty( $_SERVER['PHP_SELF'] ) ) {
1181 $path = $_SERVER['PHP_SELF'];
1182 } elseif ( !empty( $_SERVER['SCRIPT_NAME'] ) ) {
1183 $path = $_SERVER['SCRIPT_NAME'];
1184 }
1185 if ( $path !== false ) {
1186 $scriptPath = preg_replace( '{^(.*)/(mw-)?config.*$}', '$1', $path );
1187
1188 $this->setVar( 'wgScriptPath', "$scriptPath" );
1189 // Update variables set from Setup.php that are derived from wgScriptPath
1190 $this->setVar( 'wgScript', "$scriptPath/index.php" );
1191 $this->setVar( 'wgLoadScript', "$scriptPath/load.php" );
1192 $this->setVar( 'wgStylePath', "$scriptPath/skins" );
1193 $this->setVar( 'wgLocalStylePath', "$scriptPath/skins" );
1194 $this->setVar( 'wgExtensionAssetsPath', "$scriptPath/extensions" );
1195 $this->setVar( 'wgUploadPath', "$scriptPath/images" );
1196 $this->setVar( 'wgResourceBasePath', "$scriptPath" );
1197 }
1198 }
1199
1200 /**
1201 * @return string
1202 */
1203 protected function envGetDefaultServer() {
1204 return WebRequest::detectServer();
1205 }
1206
1207 /**
1208 * Actually output LocalSettings.php for download
1209 *
1210 * @suppress SecurityCheck-XSS
1211 */
1212 private function outputLS() {
1213 $this->request->response()->header( 'Content-type: application/x-httpd-php' );
1214 $this->request->response()->header(
1215 'Content-Disposition: attachment; filename="LocalSettings.php"'
1216 );
1217
1218 $ls = InstallerOverrides::getLocalSettingsGenerator( $this );
1219 $rightsProfile = $this->rightsProfiles[$this->getVar( '_RightsProfile' )];
1220 foreach ( $rightsProfile as $group => $rightsArr ) {
1221 $ls->setGroupRights( $group, $rightsArr );
1222 }
1223 echo $ls->getText();
1224 }
1225
1226 /**
1227 * Output stylesheet for web installer pages
1228 */
1229 public function outputCss() {
1230 $this->request->response()->header( 'Content-type: text/css' );
1231 echo $this->output->getCSS();
1232 }
1233
1234 /**
1235 * @return string[]
1236 */
1237 public function getPhpErrors() {
1238 return $this->phpErrors;
1239 }
1240
1241 }