1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
|
<?php
/**
* Core installer web interface.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @ingroup Installer
*/
namespace MediaWiki\Installer;
use Exception;
use HtmlArmor;
use MediaWiki\Context\RequestContext;
use MediaWiki\Html\Html;
use MediaWiki\Languages\LanguageNameUtils;
use MediaWiki\MediaWikiServices;
use MediaWiki\Message\Message;
use MediaWiki\Request\WebRequest;
use MediaWiki\Status\Status;
use MediaWiki\Xml\Xml;
/**
* Class for the core installer web interface.
*
* @ingroup Installer
* @since 1.17
*/
class WebInstaller extends Installer {
/**
* @var WebInstallerOutput
*/
public $output;
/**
* WebRequest object.
*
* @var WebRequest
*/
public $request;
/**
* Cached session array.
*
* @var array[]
*/
protected $session;
/**
* Captured PHP error text. Temporary.
*
* @var string[]
*/
protected $phpErrors;
/**
* The main sequence of page names. These will be displayed in turn.
*
* To add a new installer page:
* * Add it to this WebInstaller::$pageSequence property
* * Add a "config-page-<name>" message
* * Add a "WebInstaller<name>" class
*
* @var string[]
*/
public $pageSequence = [
'Language',
'ExistingWiki',
'Welcome',
'DBConnect',
'Upgrade',
'DBSettings',
'Name',
'Options',
'Install',
'Complete',
];
/**
* Out of sequence pages, selectable by the user at any time.
*
* @var string[]
*/
protected $otherPages = [
'Restart',
'ReleaseNotes',
'Copying',
'UpgradeDoc', // Can't use Upgrade due to Upgrade step
];
/**
* Array of pages which have declared that they have been submitted, have validated
* their input, and need no further processing.
*
* @var bool[]
*/
protected $happyPages;
/**
* List of "skipped" pages. These are pages that will automatically continue
* to the next page on any GET request. To avoid breaking the "back" button,
* they need to be skipped during a back operation.
*
* @var bool[]
*/
protected $skippedPages;
/**
* Flag indicating that session data may have been lost.
*
* @var bool
*/
public $showSessionWarning = false;
/**
* Numeric index of the page we're on
*
* @var int
*/
protected $tabIndex = 1;
/**
* Numeric index of the help box
*
* @var int
*/
protected $helpBoxId = 1;
/**
* Name of the page we're on
*
* @var string
*/
protected $currentPageName;
/**
* @param WebRequest $request
*/
public function __construct( WebRequest $request ) {
parent::__construct();
$this->output = new WebInstallerOutput( $this );
$this->request = $request;
}
/**
* Main entry point.
*
* @param array[] $session Initial session array
*
* @return array[] New session array
*/
public function execute( array $session ) {
$this->session = $session;
if ( isset( $session['settings'] ) ) {
$this->settings = $session['settings'] + $this->settings;
// T187586 MediaWikiServices works with globals
foreach ( $this->settings as $key => $val ) {
$GLOBALS[$key] = $val;
}
}
$this->setupLanguage();
if ( ( $this->getVar( '_InstallDone' ) || $this->getVar( '_UpgradeDone' ) )
&& $this->request->getVal( 'localsettings' )
) {
$this->outputLS();
return $this->session;
}
$isCSS = $this->request->getCheck( 'css' );
if ( $isCSS ) {
$this->outputCss();
return $this->session;
}
$this->happyPages = $session['happyPages'] ?? [];
$this->skippedPages = $session['skippedPages'] ?? [];
$lowestUnhappy = $this->getLowestUnhappy();
# Get the page name.
$pageName = $this->request->getVal( 'page', '' );
if ( in_array( $pageName, $this->otherPages ) ) {
# Out of sequence
$pageId = false;
$page = $this->getPageByName( $pageName );
} else {
# Main sequence
if ( !$pageName || !in_array( $pageName, $this->pageSequence ) ) {
$pageId = $lowestUnhappy;
} else {
$pageId = array_search( $pageName, $this->pageSequence );
}
# If necessary, move back to the lowest-numbered unhappy page
if ( $pageId > $lowestUnhappy ) {
$pageId = $lowestUnhappy;
if ( $lowestUnhappy == 0 ) {
# Knocked back to start, possible loss of session data.
$this->showSessionWarning = true;
}
}
$pageName = $this->pageSequence[$pageId];
$page = $this->getPageByName( $pageName );
}
# If a back button was submitted, go back without submitting the form data.
if ( $this->request->wasPosted() && $this->request->getBool( 'submit-back' ) ) {
if ( $this->request->getVal( 'lastPage' ) ) {
$nextPage = $this->request->getVal( 'lastPage' );
} elseif ( $pageId !== false ) {
# Main sequence page
# Skip the skipped pages
$nextPageId = $pageId;
do {
$nextPageId--;
$nextPage = $this->pageSequence[$nextPageId];
} while ( isset( $this->skippedPages[$nextPage] ) );
} else {
$nextPage = $this->pageSequence[$lowestUnhappy];
}
$this->output->redirect( $this->getUrl( [ 'page' => $nextPage ] ) );
return $this->finish();
}
# Execute the page.
$this->currentPageName = $page->getName();
$this->startPageWrapper( $pageName );
if ( $page->isSlow() ) {
$this->disableTimeLimit();
}
$result = $page->execute();
$this->endPageWrapper();
if ( $result == 'skip' ) {
# Page skipped without explicit submission.
# Skip it when we click "back" so that we don't just go forward again.
$this->skippedPages[$pageName] = true;
$result = 'continue';
} else {
unset( $this->skippedPages[$pageName] );
}
# If it was posted, the page can request a continue to the next page.
if ( $result === 'continue' && !$this->output->headerDone() ) {
if ( $pageId !== false ) {
$this->happyPages[$pageId] = true;
}
$lowestUnhappy = $this->getLowestUnhappy();
if ( $this->request->getVal( 'lastPage' ) ) {
$nextPage = $this->request->getVal( 'lastPage' );
} elseif ( $pageId !== false ) {
$nextPage = $this->pageSequence[$pageId + 1];
} else {
$nextPage = $this->pageSequence[$lowestUnhappy];
}
if ( array_search( $nextPage, $this->pageSequence ) > $lowestUnhappy ) {
$nextPage = $this->pageSequence[$lowestUnhappy];
}
$this->output->redirect( $this->getUrl( [ 'page' => $nextPage ] ) );
}
return $this->finish();
}
/**
* Find the next page in sequence that hasn't been completed
* @return int
*/
public function getLowestUnhappy() {
if ( count( $this->happyPages ) == 0 ) {
return 0;
} else {
return max( array_keys( $this->happyPages ) ) + 1;
}
}
/**
* Start the PHP session. This may be called before execute() to start the PHP session.
*
* @throws Exception
* @return bool
*/
public function startSession() {
if ( wfIniGetBool( 'session.auto_start' ) || session_id() ) {
// Done already
return true;
}
// Use secure cookies if we are on HTTPS
$options = [];
if ( $this->request->getProtocol() === 'https' ) {
$options['cookie_secure'] = '1';
}
$this->phpErrors = [];
set_error_handler( [ $this, 'errorHandler' ] );
try {
session_name( 'mw_installer_session' );
session_start( $options );
} catch ( Exception $e ) {
restore_error_handler();
throw $e;
}
restore_error_handler();
if ( $this->phpErrors ) {
return false;
}
return true;
}
/**
* Get a hash of data identifying this MW installation.
*
* This is used by mw-config/index.php to prevent multiple installations of MW
* on the same cookie domain from interfering with each other.
*
* @return string
*/
public function getFingerprint() {
// Get the base URL of the installation
$url = $this->request->getFullRequestURL();
if ( preg_match( '!^(.*\?)!', $url, $m ) ) {
// Trim query string
$url = $m[1];
}
if ( preg_match( '!^(.*)/[^/]*/[^/]*$!', $url, $m ) ) {
// This... seems to try to get the base path from
// the /mw-config/index.php. Kinda scary though?
$url = $m[1];
}
return md5( serialize( [
'local path' => dirname( __DIR__ ),
'url' => $url,
'version' => MW_VERSION
] ) );
}
public function showError( $msg, ...$params ) {
if ( !( $msg instanceof Message ) ) {
$msg = wfMessage(
$msg,
array_map( 'htmlspecialchars', $params )
);
}
$text = $msg->useDatabase( false )->parse();
$box = Html::errorBox( $text, '', 'config-error-box' );
$this->output->addHTML( $box );
}
/**
* Temporary error handler for session start debugging.
*
* @param int $errno Unused
* @param string $errstr
*/
public function errorHandler( $errno, $errstr ) {
$this->phpErrors[] = $errstr;
}
/**
* Clean up from execute()
*
* @return array[]
*/
public function finish() {
$this->output->output();
$this->session['happyPages'] = $this->happyPages;
$this->session['skippedPages'] = $this->skippedPages;
$this->session['settings'] = $this->settings;
return $this->session;
}
/**
* We're restarting the installation, reset the session, happyPages, etc
*/
public function reset() {
$this->session = [];
$this->happyPages = [];
$this->settings = [];
}
/**
* Get a URL for submission back to the same script.
*
* @param string[] $query
*
* @return string
*/
public function getUrl( $query = [] ) {
$url = $this->request->getRequestURL();
# Remove existing query
$url = preg_replace( '/\?.*$/', '', $url );
if ( $query ) {
$url .= '?' . wfArrayToCgi( $query );
}
return $url;
}
/**
* Get a WebInstallerPage by name.
*
* @param string $pageName
* @return WebInstallerPage
*/
public function getPageByName( $pageName ) {
$pageClass = 'MediaWiki\\Installer\\WebInstaller' . $pageName;
return new $pageClass( $this );
}
/**
* Get a session variable.
*
* @param string $name
* @param array|null $default
*
* @return array|null
*/
public function getSession( $name, $default = null ) {
return $this->session[$name] ?? $default;
}
/**
* Set a session variable.
*
* @param string $name Key for the variable
* @param mixed $value
*/
public function setSession( $name, $value ) {
$this->session[$name] = $value;
}
/**
* Get the next tabindex attribute value.
*
* @return int
*/
public function nextTabIndex() {
return $this->tabIndex++;
}
/**
* Initializes language-related variables.
*/
public function setupLanguage() {
global $wgLang, $wgLanguageCode;
if ( $this->getSession( 'test' ) === null && !$this->request->wasPosted() ) {
$wgLanguageCode = $this->getAcceptLanguage();
$wgLang = MediaWikiServices::getInstance()->getLanguageFactory()
->getLanguage( $wgLanguageCode );
RequestContext::getMain()->setLanguage( $wgLang );
$this->setVar( 'wgLanguageCode', $wgLanguageCode );
$this->setVar( '_UserLang', $wgLanguageCode );
} else {
$wgLanguageCode = $this->getVar( 'wgLanguageCode' );
}
}
/**
* Retrieves MediaWiki language from Accept-Language HTTP header.
*
* @return string
* @return-taint none It can only return a known-good code.
*/
public function getAcceptLanguage() {
global $wgLanguageCode;
$mwLanguages = MediaWikiServices::getInstance()
->getLanguageNameUtils()
->getLanguageNames( LanguageNameUtils::AUTONYMS, LanguageNameUtils::SUPPORTED );
$headerLanguages = array_keys( $this->request->getAcceptLang() );
foreach ( $headerLanguages as $lang ) {
if ( isset( $mwLanguages[$lang] ) ) {
return $lang;
}
}
return $wgLanguageCode;
}
/**
* Called by execute() before page output starts, to show a page list.
*
* @param string $currentPageName
*/
private function startPageWrapper( $currentPageName ) {
$s = "<div class=\"config-page-wrapper\">\n";
$s .= "<div class=\"config-page\">\n";
$s .= "<div class=\"config-page-list cdx-card\"><span class=\"cdx-card__text\">";
$s .= "<span class=\"cdx-card__text__description\"><ul>\n";
$lastHappy = -1;
foreach ( $this->pageSequence as $id => $pageName ) {
$happy = !empty( $this->happyPages[$id] );
$s .= $this->getPageListItem(
$pageName,
$happy || $lastHappy == $id - 1,
$currentPageName
);
if ( $happy ) {
$lastHappy = $id;
}
}
$s .= "</ul><br/><ul>\n";
$s .= $this->getPageListItem( 'Restart', true, $currentPageName );
// End list pane
$s .= "</ul></span></span></div>\n";
// Messages:
// config-page-language, config-page-welcome, config-page-dbconnect, config-page-upgrade,
// config-page-dbsettings, config-page-name, config-page-options, config-page-install,
// config-page-complete, config-page-restart, config-page-releasenotes,
// config-page-copying, config-page-upgradedoc, config-page-existingwiki
$s .= Html::element( 'h2', [],
wfMessage( 'config-page-' . strtolower( $currentPageName ) )->text() );
$this->output->addHTMLNoFlush( $s );
}
/**
* Get a list item for the page list.
*
* @param string $pageName
* @param bool $enabled
* @param string $currentPageName
*
* @return string
*/
private function getPageListItem( $pageName, $enabled, $currentPageName ) {
$s = "<li class=\"config-page-list-item\">";
// Messages:
// config-page-language, config-page-welcome, config-page-dbconnect, config-page-upgrade,
// config-page-dbsettings, config-page-name, config-page-options, config-page-install,
// config-page-complete, config-page-restart, config-page-releasenotes,
// config-page-copying, config-page-upgradedoc, config-page-existingwiki
$name = wfMessage( 'config-page-' . strtolower( $pageName ) )->text();
if ( $enabled ) {
$query = [ 'page' => $pageName ];
if ( !in_array( $pageName, $this->pageSequence ) ) {
if ( in_array( $currentPageName, $this->pageSequence ) ) {
$query['lastPage'] = $currentPageName;
}
$link = Html::element( 'a',
[
'href' => $this->getUrl( $query )
],
$name
);
} else {
$link = htmlspecialchars( $name );
}
if ( $pageName == $currentPageName ) {
$s .= "<span class=\"config-page-current\">$link</span>";
} else {
$s .= $link;
}
} else {
$s .= Html::element( 'span',
[
'class' => 'config-page-disabled'
],
$name
);
}
$s .= "</li>\n";
return $s;
}
/**
* Output some stuff after a page is finished.
*/
private function endPageWrapper() {
$this->output->addHTMLNoFlush(
"<div class=\"visualClear\"></div>\n" .
"</div>\n" .
"<div class=\"visualClear\"></div>\n" .
"</div>" );
}
/**
* Get small text indented help for a preceding form field.
* Parameters like wfMessage().
*
* @param string $msg
* @param mixed ...$args
* @return string HTML
* @return-taint escaped
*/
public function getHelpBox( $msg, ...$args ) {
$args = array_map( 'htmlspecialchars', $args );
$text = wfMessage( $msg, $args )->useDatabase( false )->plain();
$html = $this->parse( $text, true );
return "<div class=\"config-help-field-container\">\n" .
"<a class=\"config-help-field-hint\" title=\"" .
wfMessage( 'config-help-tooltip' )->escaped() . "\">ℹ️ " .
wfMessage( 'config-help' )->escaped() . "</a>\n" .
"<div class=\"config-help-field-content config-help-field-content-hidden " .
"cdx-message cdx-message--block cdx-message--notice\" style=\"margin: 10px\">" .
"<div class=\"cdx-message__content\">" . $html . "</div></div>\n" .
"</div>\n";
}
/**
* Output a help box.
* @param string $msg Key for wfMessage()
* @param mixed ...$params
*/
public function showHelpBox( $msg, ...$params ) {
$html = $this->getHelpBox( $msg, ...$params );
$this->output->addHTML( $html );
}
/**
* Get HTML for an information message box.
*
* @param string|HtmlArmor $text Wikitext to be parsed (from Message::plain) or raw HTML.
* @return string HTML
*/
public function getInfoBox( $text ) {
$html = ( $text instanceof HtmlArmor ) ?
HtmlArmor::getHtml( $text ) :
$this->parse( $text, true );
return '<div class="cdx-message cdx-message--block cdx-message--notice">' .
'<span class="cdx-message__icon"></span><div class="cdx-message__content">' .
'<p><strong>' . wfMessage( 'config-information' )->escaped() . '</strong></p>' .
$html .
"</div></div>\n";
}
public function showSuccess( $msg, ...$params ) {
$html = '<div class="cdx-message cdx-message--block cdx-message--success">' .
'<span class="cdx-message__icon"></span><div class="cdx-message__content">' .
$this->parse( wfMessage( $msg, $params )->useDatabase( false )->plain() ) .
"</div></div>\n";
$this->output->addHTML( $html );
}
public function showMessage( $msg, ...$params ) {
$html = '<div class="cdx-message cdx-message--block cdx-message--notice">' .
'<span class="cdx-message__icon"></span><div class="cdx-message__content">' .
$this->parse( wfMessage( $msg, $params )->useDatabase( false )->plain() ) .
"</div></div>\n";
$this->output->addHTML( $html );
}
public function showWarning( $msg, ...$params ) {
$html = '<div class="cdx-message cdx-message--block cdx-message--warning">' .
'<span class="cdx-message__icon"></span><div class="cdx-message__content">' .
$this->parse( wfMessage( $msg, $params )->useDatabase( false )->plain() ) .
"</div></div>\n";
$this->output->addHTML( $html );
}
public function showStatusMessage( Status $status ) {
// Show errors at the top in web installer to make them easier to notice
foreach ( $status->getMessages( 'error' ) as $msg ) {
$this->showWarning( $msg );
}
foreach ( $status->getMessages( 'warning' ) as $msg ) {
$this->showWarning( $msg );
}
}
/**
* Label a control by wrapping a config-input div around it and putting a
* label before it.
*
* @param string $msg
* @param string|false $forId
* @param string $contents HTML
* @param string $helpData
* @return string HTML
* @return-taint escaped
*/
public function label( $msg, $forId, $contents, $helpData = "" ) {
if ( strval( $msg ) == '' ) {
$labelText = "\u{00A0}";
} else {
$labelText = wfMessage( $msg )->escaped();
}
$attributes = [ 'class' => 'config-label' ];
if ( $forId ) {
$attributes['for'] = $forId;
}
return "<div class=\"config-block\">\n" .
" <div class=\"config-block-label\">\n" .
Xml::tags( 'label',
$attributes,
$labelText
) . "\n" .
$helpData .
" </div>\n" .
" <div class=\"config-block-elements\">\n" .
$contents .
" </div>\n" .
"</div>\n";
}
/**
* Get a labelled text box to configure a variable.
*
* @param mixed[] $params
* Parameters are:
* var: The variable to be configured (required)
* label: The message name for the label (required)
* attribs: Additional attributes for the input element (optional)
* controlName: The name for the input element (optional)
* value: The current value of the variable (optional)
* help: The html for the help text (optional)
*
* @return string HTML
* @return-taint escaped
*/
public function getTextBox( $params ) {
if ( !isset( $params['controlName'] ) ) {
$params['controlName'] = 'config_' . $params['var'];
}
if ( !isset( $params['value'] ) ) {
$params['value'] = $this->getVar( $params['var'] );
}
if ( !isset( $params['attribs'] ) ) {
$params['attribs'] = [];
}
if ( !isset( $params['help'] ) ) {
$params['help'] = "";
}
return $this->label(
$params['label'],
$params['controlName'],
"<div class=\"cdx-text-input\">" .
Xml::input(
$params['controlName'],
30, // intended to be overridden by CSS
$params['value'],
$params['attribs'] + [
'id' => $params['controlName'],
'class' => 'cdx-text-input__input',
'tabindex' => $this->nextTabIndex()
]
) . "</div>",
$params['help']
);
}
/**
* Get a labelled textarea to configure a variable
*
* @param mixed[] $params
* Parameters are:
* var: The variable to be configured (required)
* label: The message name for the label (required)
* attribs: Additional attributes for the input element (optional)
* controlName: The name for the input element (optional)
* value: The current value of the variable (optional)
* help: The html for the help text (optional)
*
* @return string
*/
public function getTextArea( $params ) {
if ( !isset( $params['controlName'] ) ) {
$params['controlName'] = 'config_' . $params['var'];
}
if ( !isset( $params['value'] ) ) {
$params['value'] = $this->getVar( $params['var'] );
}
if ( !isset( $params['attribs'] ) ) {
$params['attribs'] = [];
}
if ( !isset( $params['help'] ) ) {
$params['help'] = "";
}
return $this->label(
$params['label'],
$params['controlName'],
Xml::textarea(
$params['controlName'],
$params['value'],
30,
5,
$params['attribs'] + [
'id' => $params['controlName'],
'class' => 'config-input-text',
'tabindex' => $this->nextTabIndex()
]
),
$params['help']
);
}
/**
* Get a labelled password box to configure a variable.
*
* Implements password hiding
* @param mixed[] $params
* Parameters are:
* var: The variable to be configured (required)
* label: The message name for the label (required)
* attribs: Additional attributes for the input element (optional)
* controlName: The name for the input element (optional)
* value: The current value of the variable (optional)
* help: The html for the help text (optional)
*
* @return string HTML
* @return-taint escaped
*/
public function getPasswordBox( $params ) {
if ( !isset( $params['value'] ) ) {
$params['value'] = $this->getVar( $params['var'] );
}
if ( !isset( $params['attribs'] ) ) {
$params['attribs'] = [];
}
$params['value'] = $this->getFakePassword( $params['value'] );
$params['attribs']['type'] = 'password';
return $this->getTextBox( $params );
}
/**
* Add a class to an array of attributes. If the array already has a class,
* append the new class to the list.
*
* @param array &$attribs
* @param string $class
*/
private static function addClassAttrib( &$attribs, $class ) {
if ( isset( $attribs['class'] ) ) {
$attribs['class'] .= ' ' . $class;
} else {
$attribs['class'] = $class;
}
}
/**
* Get a labelled checkbox to configure a boolean variable.
*
* @param mixed[] $params
* Parameters are:
* var: The variable to be configured (required)
* label: The message name for the label (required)
* labelAttribs:Additional attributes for the label element (optional)
* attribs: Additional attributes for the input element (optional)
* controlName: The name for the input element (optional)
* value: The current value of the variable (optional)
* help: The html for the help text (optional)
*
* @return string HTML
* @return-taint escaped
*/
public function getCheckBox( $params ) {
if ( !isset( $params['controlName'] ) ) {
$params['controlName'] = 'config_' . $params['var'];
}
if ( !isset( $params['value'] ) ) {
$params['value'] = $this->getVar( $params['var'] );
}
if ( !isset( $params['attribs'] ) ) {
$params['attribs'] = [];
}
if ( !isset( $params['help'] ) ) {
$params['help'] = "";
}
if ( !isset( $params['labelAttribs'] ) ) {
$params['labelAttribs'] = [];
}
$labelText = $params['rawtext'] ?? $this->parse( wfMessage( $params['label'] )->plain() );
self::addClassAttrib( $params['attribs'], 'cdx-checkbox__input' );
self::addClassAttrib( $params['labelAttribs'], 'cdx-checkbox__label' );
return "<div class=\"cdx-checkbox\" style=\"margin-top: 12px; margin-bottom: 2px;\">\n" .
Xml::check(
$params['controlName'],
$params['value'],
$params['attribs'] + [
'id' => $params['controlName'],
'tabindex' => $this->nextTabIndex()
]
) .
"<span class=\"cdx-checkbox__icon\"></span>" .
Html::rawElement(
'label',
$params['labelAttribs'] + [
'for' => $params['controlName']
],
$labelText
) .
"</div>\n" . $params['help'];
}
/**
* Get a set of labelled radio buttons.
*
* @param mixed[] $params
* Parameters are:
* var: The variable to be configured (required)
* label: The message name for the label (required)
* itemLabelPrefix: The message name prefix for the item labels (required)
* itemLabels: List of message names to use for the item labels instead
* of itemLabelPrefix, keyed by values
* values: List of allowed values (required)
* itemAttribs: Array of attribute arrays, outer key is the value name (optional)
* commonAttribs: Attribute array applied to all items
* controlName: The name for the input element (optional)
* value: The current value of the variable (optional)
* help: The html for the help text (optional)
*
* @return string HTML
* @return-taint escaped
*/
public function getRadioSet( $params ) {
$items = $this->getRadioElements( $params );
$label = $params['label'] ?? '';
if ( !isset( $params['controlName'] ) ) {
$params['controlName'] = 'config_' . $params['var'];
}
if ( !isset( $params['help'] ) ) {
$params['help'] = "";
}
$s = "";
foreach ( $items as $item ) {
$s .= "$item\n";
}
return $this->label( $label, $params['controlName'], $s, $params['help'] );
}
/**
* Get a set of labelled radio buttons. You probably want to use getRadioSet(), not this.
*
* @see getRadioSet
*
* @param mixed[] $params
* @return string[] HTML
* @return-taint escaped
*/
public function getRadioElements( $params ) {
if ( !isset( $params['controlName'] ) ) {
$params['controlName'] = 'config_' . $params['var'];
}
if ( !isset( $params['value'] ) ) {
$params['value'] = $this->getVar( $params['var'] );
}
$items = [];
foreach ( $params['values'] as $value ) {
$itemAttribs = [];
if ( isset( $params['commonAttribs'] ) ) {
$itemAttribs = $params['commonAttribs'];
}
if ( isset( $params['itemAttribs'][$value] ) ) {
$itemAttribs = $params['itemAttribs'][$value] + $itemAttribs;
}
$checked = $value == $params['value'];
$id = $params['controlName'] . '_' . $value;
$itemAttribs['id'] = $id;
$itemAttribs['tabindex'] = $this->nextTabIndex();
self::addClassAttrib( $itemAttribs, 'cdx-radio__input' );
$items[$value] =
'<span class="cdx-radio">' .
Xml::radio( $params['controlName'], $value, $checked, $itemAttribs ) .
"<span class=\"cdx-radio__icon\"></span>\u{00A0}" .
Xml::tags( 'label', [ 'for' => $id, 'class' => 'cdx-radio__label' ], $this->parse(
isset( $params['itemLabels'] ) ?
wfMessage( $params['itemLabels'][$value] )->plain() :
wfMessage( $params['itemLabelPrefix'] . strtolower( $value ) )->plain()
) ) . '</span>';
}
return $items;
}
/**
* Output an error or warning box using a Status object.
*
* @param Status $status
*/
public function showStatusBox( $status ) {
if ( !$status->isGood() ) {
$html = $status->getHTML();
if ( $status->isOK() ) {
$box = Html::warningBox( $html, 'config-warning-box' );
} else {
$box = Html::errorBox( $html, '', 'config-error-box' );
}
$this->output->addHTML( $box );
}
}
/**
* Convenience function to set variables based on form data.
* Assumes that variables containing "password" in the name are (potentially
* fake) passwords.
*
* @param string[] $varNames
* @param string $prefix The prefix added to variables to obtain form names
*
* @return string[]
*/
public function setVarsFromRequest( $varNames, $prefix = 'config_' ) {
$newValues = [];
foreach ( $varNames as $name ) {
$value = $this->request->getVal( $prefix . $name );
// T32524, do not trim passwords
if ( $value !== null && stripos( $name, 'password' ) === false ) {
$value = trim( $value );
}
$newValues[$name] = $value;
if ( $value === null ) {
// Checkbox?
$this->setVar( $name, false );
} elseif ( stripos( $name, 'password' ) !== false ) {
$this->setPassword( $name, $value );
} else {
$this->setVar( $name, $value );
}
}
return $newValues;
}
/**
* Helper for WebInstallerOutput
*
* @internal For use by WebInstallerOutput
* @param string $page
* @return string
*/
public function getDocUrl( $page ) {
$query = [ 'page' => $page ];
if ( in_array( $this->currentPageName, $this->pageSequence ) ) {
$query['lastPage'] = $this->currentPageName;
}
return $this->getUrl( $query );
}
/**
* Helper for sidebar links.
*
* @internal For use in WebInstallerOutput class
* @param string $url
* @param string $linkText
* @return string HTML
*/
public function makeLinkItem( $url, $linkText ) {
return Html::rawElement( 'li', [],
Html::element( 'a', [ 'href' => $url ], $linkText )
);
}
/**
* If the software package wants the LocalSettings.php file
* to be placed in a specific location, override this function
* (see mw-config/overrides/README) to return the path of
* where the file should be saved, or false for a generic
* "in the base of your install"
*
* @since 1.27
* @return string|bool
*/
public function getLocalSettingsLocation() {
return false;
}
/**
* @return bool
*/
public function envCheckPath() {
// PHP_SELF isn't available sometimes, such as when PHP is CGI but
// cgi.fix_pathinfo is disabled. In that case, fall back to SCRIPT_NAME
// to get the path to the current script... hopefully it's reliable. SIGH
$path = false;
if ( !empty( $_SERVER['PHP_SELF'] ) ) {
$path = $_SERVER['PHP_SELF'];
} elseif ( !empty( $_SERVER['SCRIPT_NAME'] ) ) {
$path = $_SERVER['SCRIPT_NAME'];
}
if ( $path === false ) {
$this->showError( 'config-no-uri' );
return false;
}
return parent::envCheckPath();
}
public function envPrepPath() {
parent::envPrepPath();
// PHP_SELF isn't available sometimes, such as when PHP is CGI but
// cgi.fix_pathinfo is disabled. In that case, fall back to SCRIPT_NAME
// to get the path to the current script... hopefully it's reliable. SIGH
$path = false;
if ( !empty( $_SERVER['PHP_SELF'] ) ) {
$path = $_SERVER['PHP_SELF'];
} elseif ( !empty( $_SERVER['SCRIPT_NAME'] ) ) {
$path = $_SERVER['SCRIPT_NAME'];
}
if ( $path !== false ) {
$scriptPath = preg_replace( '{^(.*)/(mw-)?config.*$}', '$1', $path );
$this->setVar( 'wgScriptPath', "$scriptPath" );
// Update variables set from Setup.php that are derived from wgScriptPath
$this->setVar( 'wgScript', "$scriptPath/index.php" );
$this->setVar( 'wgLoadScript', "$scriptPath/load.php" );
$this->setVar( 'wgStylePath', "$scriptPath/skins" );
$this->setVar( 'wgLocalStylePath', "$scriptPath/skins" );
$this->setVar( 'wgExtensionAssetsPath', "$scriptPath/extensions" );
$this->setVar( 'wgUploadPath', "$scriptPath/images" );
$this->setVar( 'wgResourceBasePath', "$scriptPath" );
}
}
/**
* @return string
*/
protected function envGetDefaultServer() {
$assumeProxiesUseDefaultProtocolPorts =
$this->getVar( 'wgAssumeProxiesUseDefaultProtocolPorts' );
return WebRequest::detectServer( $assumeProxiesUseDefaultProtocolPorts );
}
/**
* Actually output LocalSettings.php for download
*/
private function outputLS() {
$this->request->response()->header( 'Content-type: application/x-httpd-php' );
$this->request->response()->header(
'Content-Disposition: attachment; filename="LocalSettings.php"'
);
$ls = InstallerOverrides::getLocalSettingsGenerator( $this );
$rightsProfile = $this->rightsProfiles[$this->getVar( '_RightsProfile' )];
foreach ( $rightsProfile as $group => $rightsArr ) {
$ls->setGroupRights( $group, $rightsArr );
}
echo $ls->getText();
}
/**
* Output stylesheet for web installer pages
*/
public function outputCss() {
$this->request->response()->header( 'Content-type: text/css' );
echo $this->output->getCSS();
}
/**
* @return string[]
*/
public function getPhpErrors() {
return $this->phpErrors;
}
/**
* Determine whether the current database needs to be upgraded, i.e. whether
* it already has MediaWiki tables.
*
* @return bool
*/
public function needsUpgrade() {
return $this->getDBInstaller()->needsUpgrade();
}
/**
* Perform database upgrades
*
* @return bool
*/
public function doUpgrade() {
$dbInstaller = $this->getDBInstaller();
$dbInstaller->preUpgrade();
$this->restoreServices();
$ret = true;
ob_start( [ $this, 'outputHandler' ] );
$up = DatabaseUpdater::newForDB(
$dbInstaller->definitelyGetConnection( DatabaseInstaller::CONN_CREATE_TABLES ) );
try {
$up->doUpdates();
$up->purgeCache();
// If they're going to possibly regenerate LocalSettings, we
// need to create the upgrade/secret keys. T28481
if ( !$this->getVar( '_ExistingDBSettings' ) ) {
$this->generateKeys();
}
$this->setVar( '_UpgradeDone', true );
} catch ( Exception $e ) {
// TODO: Should this use MWExceptionRenderer?
echo "\nAn error occurred:\n";
echo $e->getMessage();
$ret = false;
}
ob_end_flush();
return $ret;
}
public function outputHandler( $string ) {
return htmlspecialchars( $string );
}
}
|