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