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