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