| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 6 | | This work is published under the GNU AGPLv3 license with some | |
| 7 | | permitted exceptions and without any warranty. For full license | |
| 8 | | and copyright information, see https://civicrm.org/licensing | |
| 9 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * |
| 14 | * @package CRM |
| 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
| 16 | * $Id$ |
| 17 | * |
| 18 | */ |
| 19 | class CRM_Upgrade_Form extends CRM_Core_Form { |
| 20 | const QUEUE_NAME = 'CRM_Upgrade'; |
| 21 | |
| 22 | /** |
| 23 | * Minimum size of MySQL's thread_stack option |
| 24 | * |
| 25 | * @see install/index.php MINIMUM_THREAD_STACK |
| 26 | */ |
| 27 | const MINIMUM_THREAD_STACK = 192; |
| 28 | |
| 29 | /** |
| 30 | * Minimum previous CiviCRM version we can directly upgrade from |
| 31 | */ |
| 32 | const MINIMUM_UPGRADABLE_VERSION = '4.2.9'; |
| 33 | |
| 34 | /** |
| 35 | * Minimum php version required to run (equal to or lower than the minimum install version) |
| 36 | * |
| 37 | * As of Civi 5.16, using PHP 5.x will lead to a hard crash during bootstrap. |
| 38 | */ |
| 39 | const MINIMUM_PHP_VERSION = '7.0.0'; |
| 40 | |
| 41 | /** |
| 42 | * @var \CRM_Core_Config |
| 43 | */ |
| 44 | protected $_config; |
| 45 | |
| 46 | /** |
| 47 | * Upgrade for multilingual. |
| 48 | * |
| 49 | * @var bool |
| 50 | */ |
| 51 | public $multilingual = FALSE; |
| 52 | |
| 53 | /** |
| 54 | * Locales available for multilingual upgrade. |
| 55 | * |
| 56 | * @var array |
| 57 | */ |
| 58 | public $locales; |
| 59 | |
| 60 | /** |
| 61 | * Constructor for the basic form page. |
| 62 | * |
| 63 | * We should not use QuickForm directly. This class provides a lot |
| 64 | * of default convenient functions, rules and buttons |
| 65 | * |
| 66 | * @param object $state |
| 67 | * State associated with this form. |
| 68 | * @param const|\enum|int $action The mode the form is operating in (None/Create/View/Update/Delete) |
| 69 | * @param string $method |
| 70 | * The type of http method used (GET/POST). |
| 71 | * @param string $name |
| 72 | * The name of the form if different from class name. |
| 73 | */ |
| 74 | public function __construct( |
| 75 | $state = NULL, |
| 76 | $action = CRM_Core_Action::NONE, |
| 77 | $method = 'post', |
| 78 | $name = NULL |
| 79 | ) { |
| 80 | $this->_config = CRM_Core_Config::singleton(); |
| 81 | |
| 82 | $domain = new CRM_Core_DAO_Domain(); |
| 83 | $domain->find(TRUE); |
| 84 | |
| 85 | $this->multilingual = (bool) $domain->locales; |
| 86 | $this->locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales); |
| 87 | |
| 88 | $smarty = CRM_Core_Smarty::singleton(); |
| 89 | //$smarty->compile_dir = $this->_config->templateCompileDir; |
| 90 | $smarty->assign('multilingual', $this->multilingual); |
| 91 | $smarty->assign('locales', $this->locales); |
| 92 | |
| 93 | // we didn't call CRM_Core_BAO_ConfigSetting::retrieve(), so we need to set $dbLocale by hand |
| 94 | if ($this->multilingual) { |
| 95 | global $dbLocale; |
| 96 | $dbLocale = "_{$this->_config->lcMessages}"; |
| 97 | } |
| 98 | |
| 99 | parent::__construct($state, $action, $method, $name); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * @param $version |
| 104 | * |
| 105 | * @return mixed |
| 106 | */ |
| 107 | public static function &incrementalPhpObject($version) { |
| 108 | static $incrementalPhpObject = []; |
| 109 | |
| 110 | $versionParts = explode('.', $version); |
| 111 | $versionName = CRM_Utils_EnglishNumber::toCamelCase($versionParts[0]) . CRM_Utils_EnglishNumber::toCamelCase($versionParts[1]); |
| 112 | |
| 113 | if (!array_key_exists($versionName, $incrementalPhpObject)) { |
| 114 | $className = "CRM_Upgrade_Incremental_php_{$versionName}"; |
| 115 | $incrementalPhpObject[$versionName] = new $className(); |
| 116 | } |
| 117 | return $incrementalPhpObject[$versionName]; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * @param $version |
| 122 | * @param $release |
| 123 | * |
| 124 | * @return bool |
| 125 | */ |
| 126 | public function checkVersionRelease($version, $release) { |
| 127 | $versionParts = explode('.', $version); |
| 128 | return ($versionParts[2] == $release); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * @param $constraints |
| 133 | * |
| 134 | * @return array |
| 135 | */ |
| 136 | public function checkSQLConstraints(&$constraints) { |
| 137 | $pass = $fail = 0; |
| 138 | foreach ($constraints as $constraint) { |
| 139 | if ($this->checkSQLConstraint($constraint)) { |
| 140 | $pass++; |
| 141 | } |
| 142 | else { |
| 143 | $fail++; |
| 144 | } |
| 145 | return [$pass, $fail]; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * @param $constraint |
| 151 | * |
| 152 | * @return bool |
| 153 | */ |
| 154 | public function checkSQLConstraint($constraint) { |
| 155 | // check constraint here |
| 156 | return TRUE; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * @param string $fileName |
| 161 | * @param bool $isQueryString |
| 162 | */ |
| 163 | public function source($fileName, $isQueryString = FALSE) { |
| 164 | if ($isQueryString) { |
| 165 | CRM_Utils_File::runSqlQuery($this->_config->dsn, |
| 166 | $fileName, NULL |
| 167 | ); |
| 168 | } |
| 169 | else { |
| 170 | CRM_Utils_File::sourceSQLFile($this->_config->dsn, |
| 171 | $fileName, NULL |
| 172 | ); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | public function preProcess() { |
| 177 | CRM_Utils_System::setTitle($this->getTitle()); |
| 178 | if (!$this->verifyPreDBState($errorMessage)) { |
| 179 | if (!isset($errorMessage)) { |
| 180 | $errorMessage = 'pre-condition failed for current upgrade step'; |
| 181 | } |
| 182 | CRM_Core_Error::fatal($errorMessage); |
| 183 | } |
| 184 | $this->assign('recentlyViewed', FALSE); |
| 185 | } |
| 186 | |
| 187 | public function buildQuickForm() { |
| 188 | $this->addDefaultButtons($this->getButtonTitle(), |
| 189 | 'next', |
| 190 | NULL, |
| 191 | TRUE |
| 192 | ); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Getter function for title. Should be over-ridden by derived class |
| 197 | * |
| 198 | * @return string |
| 199 | */ |
| 200 | |
| 201 | /** |
| 202 | * @return string |
| 203 | */ |
| 204 | public function getTitle() { |
| 205 | return ts('Title not Set'); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * @return string |
| 210 | */ |
| 211 | public function getFieldsetTitle() { |
| 212 | return ''; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * @return string |
| 217 | */ |
| 218 | public function getButtonTitle() { |
| 219 | return ts('Continue'); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Use the form name to create the tpl file name. |
| 224 | * |
| 225 | * @return string |
| 226 | */ |
| 227 | |
| 228 | /** |
| 229 | * @return string |
| 230 | */ |
| 231 | public function getTemplateFileName() { |
| 232 | $this->assign('title', |
| 233 | $this->getFieldsetTitle() |
| 234 | ); |
| 235 | $this->assign('message', |
| 236 | $this->getTemplateMessage() |
| 237 | ); |
| 238 | return 'CRM/Upgrade/Base.tpl'; |
| 239 | } |
| 240 | |
| 241 | public function postProcess() { |
| 242 | $this->upgrade(); |
| 243 | |
| 244 | if (!$this->verifyPostDBState($errorMessage)) { |
| 245 | if (!isset($errorMessage)) { |
| 246 | $errorMessage = 'post-condition failed for current upgrade step'; |
| 247 | } |
| 248 | CRM_Core_Error::fatal($errorMessage); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * @param $query |
| 254 | * |
| 255 | * @return Object |
| 256 | */ |
| 257 | public function runQuery($query) { |
| 258 | return CRM_Core_DAO::executeQuery($query); |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * @param $version |
| 263 | * |
| 264 | * @return Object |
| 265 | */ |
| 266 | public function setVersion($version) { |
| 267 | $this->logVersion($version); |
| 268 | |
| 269 | $query = " |
| 270 | UPDATE civicrm_domain |
| 271 | SET version = '$version' |
| 272 | "; |
| 273 | return $this->runQuery($query); |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * @param $newVersion |
| 278 | * |
| 279 | * @return bool |
| 280 | */ |
| 281 | public function logVersion($newVersion) { |
| 282 | if ($newVersion) { |
| 283 | $oldVersion = CRM_Core_BAO_Domain::version(); |
| 284 | |
| 285 | $session = CRM_Core_Session::singleton(); |
| 286 | $logParams = [ |
| 287 | 'entity_table' => 'civicrm_domain', |
| 288 | 'entity_id' => 1, |
| 289 | 'data' => "upgrade:{$oldVersion}->{$newVersion}", |
| 290 | // lets skip 'modified_id' for now, as it causes FK issues And |
| 291 | // is not very important for now. |
| 292 | 'modified_date' => date('YmdHis'), |
| 293 | ]; |
| 294 | CRM_Core_BAO_Log::add($logParams); |
| 295 | return TRUE; |
| 296 | } |
| 297 | |
| 298 | return FALSE; |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * @param $version |
| 303 | * |
| 304 | * @return bool |
| 305 | */ |
| 306 | public function checkVersion($version) { |
| 307 | $domainID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', |
| 308 | $version, 'id', |
| 309 | 'version' |
| 310 | ); |
| 311 | return $domainID ? TRUE : FALSE; |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * @return array |
| 316 | * @throws Exception |
| 317 | */ |
| 318 | public function getRevisionSequence() { |
| 319 | $revList = []; |
| 320 | $sqlDir = implode(DIRECTORY_SEPARATOR, |
| 321 | [dirname(__FILE__), 'Incremental', 'sql'] |
| 322 | ); |
| 323 | $sqlFiles = scandir($sqlDir); |
| 324 | |
| 325 | $sqlFilePattern = '/^((\d{1,2}\.\d{1,2})\.(\d{1,2}\.)?(\d{1,2}|\w{4,7}))\.(my)?sql(\.tpl)?$/i'; |
| 326 | foreach ($sqlFiles as $file) { |
| 327 | if (preg_match($sqlFilePattern, $file, $matches)) { |
| 328 | if (!in_array($matches[1], $revList)) { |
| 329 | $revList[] = $matches[1]; |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | usort($revList, 'version_compare'); |
| 335 | return $revList; |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * @param $rev |
| 340 | * @param int $index |
| 341 | * |
| 342 | * @return null |
| 343 | */ |
| 344 | public static function getRevisionPart($rev, $index = 1) { |
| 345 | $revPattern = '/^((\d{1,2})\.\d{1,2})\.(\d{1,2}|\w{4,7})?$/i'; |
| 346 | preg_match($revPattern, $rev, $matches); |
| 347 | |
| 348 | return array_key_exists($index, $matches) ? $matches[$index] : NULL; |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * @param $tplFile |
| 353 | * @param $rev |
| 354 | * |
| 355 | * @return bool |
| 356 | */ |
| 357 | public function processLocales($tplFile, $rev) { |
| 358 | $smarty = CRM_Core_Smarty::singleton(); |
| 359 | $smarty->assign('domainID', CRM_Core_Config::domainID()); |
| 360 | |
| 361 | $this->source($smarty->fetch($tplFile), TRUE); |
| 362 | |
| 363 | if ($this->multilingual) { |
| 364 | CRM_Core_I18n_Schema::rebuildMultilingualSchema($this->locales, $rev); |
| 365 | } |
| 366 | return $this->multilingual; |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * @param $rev |
| 371 | */ |
| 372 | public function setSchemaStructureTables($rev) { |
| 373 | if ($this->multilingual) { |
| 374 | CRM_Core_I18n_Schema::schemaStructureTables($rev, TRUE); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * @param $rev |
| 380 | * |
| 381 | * @throws Exception |
| 382 | */ |
| 383 | public function processSQL($rev) { |
| 384 | $sqlFile = implode(DIRECTORY_SEPARATOR, |
| 385 | [ |
| 386 | dirname(__FILE__), |
| 387 | 'Incremental', |
| 388 | 'sql', |
| 389 | $rev . '.mysql', |
| 390 | ] |
| 391 | ); |
| 392 | $tplFile = "$sqlFile.tpl"; |
| 393 | |
| 394 | if (file_exists($tplFile)) { |
| 395 | $this->processLocales($tplFile, $rev); |
| 396 | } |
| 397 | else { |
| 398 | if (!file_exists($sqlFile)) { |
| 399 | CRM_Core_Error::fatal("sqlfile - $rev.mysql not found."); |
| 400 | } |
| 401 | $this->source($sqlFile); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | /** |
| 406 | * Determine the start and end version of the upgrade process. |
| 407 | * |
| 408 | * @return array(0=>$currentVer, 1=>$latestVer) |
| 409 | */ |
| 410 | public function getUpgradeVersions() { |
| 411 | $latestVer = CRM_Utils_System::version(); |
| 412 | $currentVer = CRM_Core_BAO_Domain::version(TRUE); |
| 413 | if (!$currentVer) { |
| 414 | CRM_Core_Error::fatal(ts('Version information missing in civicrm database.')); |
| 415 | } |
| 416 | elseif (stripos($currentVer, 'upgrade')) { |
| 417 | CRM_Core_Error::fatal(ts('Database check failed - the database looks to have been partially upgraded. You may want to reload the database with the backup and try the upgrade process again.')); |
| 418 | } |
| 419 | if (!$latestVer) { |
| 420 | CRM_Core_Error::fatal(ts('Version information missing in civicrm codebase.')); |
| 421 | } |
| 422 | |
| 423 | return [$currentVer, $latestVer]; |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * Determine if $currentVer can be upgraded to $latestVer |
| 428 | * |
| 429 | * @param $currentVer |
| 430 | * @param $latestVer |
| 431 | * |
| 432 | * @return mixed, a string error message or boolean 'false' if OK |
| 433 | */ |
| 434 | public function checkUpgradeableVersion($currentVer, $latestVer) { |
| 435 | $error = FALSE; |
| 436 | // since version is suppose to be in valid format at this point, especially after conversion ($convertVer), |
| 437 | // lets do a pattern check - |
| 438 | if (!CRM_Utils_System::isVersionFormatValid($currentVer)) { |
| 439 | $error = ts('Database is marked with invalid version format. You may want to investigate this before you proceed further.'); |
| 440 | } |
| 441 | elseif (version_compare($currentVer, $latestVer) > 0) { |
| 442 | // DB version number is higher than codebase being upgraded to. This is unexpected condition-fatal error. |
| 443 | $error = ts('Your database is marked with an unexpected version number: %1. The automated upgrade to version %2 can not be run - and the %2 codebase may not be compatible with your database state. You will need to determine the correct version corresponding to your current database state. You may want to revert to the codebase you were using prior to beginning this upgrade until you resolve this problem.', |
| 444 | [1 => $currentVer, 2 => $latestVer] |
| 445 | ); |
| 446 | } |
| 447 | elseif (version_compare($currentVer, $latestVer) == 0) { |
| 448 | $error = ts('Your database has already been upgraded to CiviCRM %1', |
| 449 | [1 => $latestVer] |
| 450 | ); |
| 451 | } |
| 452 | elseif (version_compare($currentVer, self::MINIMUM_UPGRADABLE_VERSION) < 0) { |
| 453 | $error = ts('CiviCRM versions prior to %1 cannot be upgraded directly to %2. This upgrade will need to be done in stages. First download an intermediate version (the LTS may be a good choice) and upgrade to that before proceeding to this version.', |
| 454 | [1 => self::MINIMUM_UPGRADABLE_VERSION, 2 => $latestVer] |
| 455 | ); |
| 456 | } |
| 457 | |
| 458 | if (version_compare(phpversion(), self::MINIMUM_PHP_VERSION) < 0) { |
| 459 | $error = ts('CiviCRM %3 requires PHP version %1 (or newer), but the current system uses %2 ', |
| 460 | [ |
| 461 | 1 => self::MINIMUM_PHP_VERSION, |
| 462 | 2 => phpversion(), |
| 463 | 3 => $latestVer, |
| 464 | ]); |
| 465 | } |
| 466 | |
| 467 | // check for mysql trigger privileges |
| 468 | if (!\Civi::settings()->get('logging_no_trigger_permission') && !CRM_Core_DAO::checkTriggerViewPermission(FALSE, TRUE)) { |
| 469 | $error = ts('CiviCRM %1 requires MySQL trigger privileges.', |
| 470 | [1 => $latestVer]); |
| 471 | } |
| 472 | |
| 473 | if (CRM_Core_DAO::getGlobalSetting('thread_stack', 0) < (1024 * self::MINIMUM_THREAD_STACK)) { |
| 474 | $error = ts('CiviCRM %1 requires MySQL thread stack >= %2k', [ |
| 475 | 1 => $latestVer, |
| 476 | 2 => self::MINIMUM_THREAD_STACK, |
| 477 | ]); |
| 478 | } |
| 479 | |
| 480 | return $error; |
| 481 | } |
| 482 | |
| 483 | /** |
| 484 | * Determine if $currentver already matches $latestVer |
| 485 | * |
| 486 | * @param $currentVer |
| 487 | * @param $latestVer |
| 488 | * |
| 489 | * @return mixed, a string error message or boolean 'false' if OK |
| 490 | */ |
| 491 | public function checkCurrentVersion($currentVer, $latestVer) { |
| 492 | $error = FALSE; |
| 493 | |
| 494 | // since version is suppose to be in valid format at this point, especially after conversion ($convertVer), |
| 495 | // lets do a pattern check - |
| 496 | if (!CRM_Utils_System::isVersionFormatValid($currentVer)) { |
| 497 | $error = ts('Database is marked with invalid version format. You may want to investigate this before you proceed further.'); |
| 498 | } |
| 499 | elseif (version_compare($currentVer, $latestVer) != 0) { |
| 500 | $error = ts('Your database is not configured for version %1', |
| 501 | [1 => $latestVer] |
| 502 | ); |
| 503 | } |
| 504 | return $error; |
| 505 | } |
| 506 | |
| 507 | /** |
| 508 | * Fill the queue with upgrade tasks. |
| 509 | * |
| 510 | * @param string $currentVer |
| 511 | * the original revision. |
| 512 | * @param string $latestVer |
| 513 | * the target (final) revision. |
| 514 | * @param string $postUpgradeMessageFile |
| 515 | * path of a modifiable file which lists the post-upgrade messages. |
| 516 | * |
| 517 | * @return CRM_Queue_Service |
| 518 | */ |
| 519 | public static function buildQueue($currentVer, $latestVer, $postUpgradeMessageFile) { |
| 520 | $upgrade = new CRM_Upgrade_Form(); |
| 521 | |
| 522 | // Ensure that queue can be created |
| 523 | if (!CRM_Queue_BAO_QueueItem::findCreateTable()) { |
| 524 | CRM_Core_Error::fatal(ts('Failed to find or create queueing table')); |
| 525 | } |
| 526 | $queue = CRM_Queue_Service::singleton()->create([ |
| 527 | 'name' => self::QUEUE_NAME, |
| 528 | 'type' => 'Sql', |
| 529 | 'reset' => TRUE, |
| 530 | ]); |
| 531 | |
| 532 | $task = new CRM_Queue_Task( |
| 533 | ['CRM_Upgrade_Form', 'doFileCleanup'], |
| 534 | [$postUpgradeMessageFile], |
| 535 | "Cleanup old files" |
| 536 | ); |
| 537 | $queue->createItem($task); |
| 538 | |
| 539 | $task = new CRM_Queue_Task( |
| 540 | ['CRM_Upgrade_Form', 'disableOldExtensions'], |
| 541 | [$postUpgradeMessageFile], |
| 542 | "Checking extensions" |
| 543 | ); |
| 544 | $queue->createItem($task); |
| 545 | |
| 546 | $revisions = $upgrade->getRevisionSequence(); |
| 547 | foreach ($revisions as $rev) { |
| 548 | // proceed only if $currentVer < $rev |
| 549 | if (version_compare($currentVer, $rev) < 0) { |
| 550 | $beginTask = new CRM_Queue_Task( |
| 551 | // callback |
| 552 | ['CRM_Upgrade_Form', 'doIncrementalUpgradeStart'], |
| 553 | // arguments |
| 554 | [$rev], |
| 555 | "Begin Upgrade to $rev" |
| 556 | ); |
| 557 | $queue->createItem($beginTask); |
| 558 | |
| 559 | $task = new CRM_Queue_Task( |
| 560 | // callback |
| 561 | ['CRM_Upgrade_Form', 'doIncrementalUpgradeStep'], |
| 562 | // arguments |
| 563 | [$rev, $currentVer, $latestVer, $postUpgradeMessageFile], |
| 564 | "Upgrade DB to $rev" |
| 565 | ); |
| 566 | $queue->createItem($task); |
| 567 | |
| 568 | $task = new CRM_Queue_Task( |
| 569 | // callback |
| 570 | ['CRM_Upgrade_Form', 'doIncrementalUpgradeFinish'], |
| 571 | // arguments |
| 572 | [$rev, $currentVer, $latestVer, $postUpgradeMessageFile], |
| 573 | "Finish Upgrade DB to $rev" |
| 574 | ); |
| 575 | $queue->createItem($task); |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | return $queue; |
| 580 | } |
| 581 | |
| 582 | /** |
| 583 | * Find any old, orphaned files that should have been deleted. |
| 584 | * |
| 585 | * These files can get left behind, eg, if you use the Joomla |
| 586 | * upgrade procedure. |
| 587 | * |
| 588 | * The earlier we can do this, the better - don't want upgrade logic |
| 589 | * to inadvertently rely on old/relocated files. |
| 590 | * |
| 591 | * @param \CRM_Queue_TaskContext $ctx |
| 592 | * @param string $postUpgradeMessageFile |
| 593 | * @return bool |
| 594 | */ |
| 595 | public static function doFileCleanup(CRM_Queue_TaskContext $ctx, $postUpgradeMessageFile) { |
| 596 | $source = new CRM_Utils_Check_Component_Source(); |
| 597 | $files = $source->findOrphanedFiles(); |
| 598 | $errors = []; |
| 599 | foreach ($files as $file) { |
| 600 | if (is_dir($file['path'])) { |
| 601 | @rmdir($file['path']); |
| 602 | } |
| 603 | else { |
| 604 | @unlink($file['path']); |
| 605 | } |
| 606 | |
| 607 | if (file_exists($file['path'])) { |
| 608 | $errors[] = sprintf("<li>%s</li>", htmlentities($file['path'])); |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | if (!empty($errors)) { |
| 613 | file_put_contents($postUpgradeMessageFile, |
| 614 | '<br/><br/>' . ts('Some old files could not be removed. Please remove them.') |
| 615 | . '<ul>' . implode("\n", $errors) . '</ul>', |
| 616 | FILE_APPEND |
| 617 | ); |
| 618 | } |
| 619 | |
| 620 | return TRUE; |
| 621 | } |
| 622 | |
| 623 | /** |
| 624 | * Disable/uninstall any extensions not compatible with this new version. |
| 625 | * |
| 626 | * @param \CRM_Queue_TaskContext $ctx |
| 627 | * @param string $postUpgradeMessageFile |
| 628 | * @return bool |
| 629 | */ |
| 630 | public static function disableOldExtensions(CRM_Queue_TaskContext $ctx, $postUpgradeMessageFile) { |
| 631 | $messages = []; |
| 632 | $manager = CRM_Extension_System::singleton()->getManager(); |
| 633 | foreach ($manager->getStatuses() as $key => $status) { |
| 634 | $obsolete = $manager->isIncompatible($key); |
| 635 | if ($obsolete) { |
| 636 | if (!empty($obsolete['disable']) && in_array($status, [$manager::STATUS_INSTALLED, $manager::STATUS_INSTALLED_MISSING])) { |
| 637 | try { |
| 638 | $manager->disable($key); |
| 639 | // Update the status for the sake of uninstall below. |
| 640 | $status = $status == $manager::STATUS_INSTALLED ? $manager::STATUS_DISABLED : $manager::STATUS_DISABLED_MISSING; |
| 641 | // This message is intentionally overwritten by uninstall below as it would be redundant |
| 642 | $messages[$key] = ts('The extension %1 is now obsolete and has been disabled.', [1 => $key]); |
| 643 | } |
| 644 | catch (CRM_Extension_Exception $e) { |
| 645 | $messages[] = ts('The obsolete extension %1 could not be removed due to an error. It is recommended to remove this extension manually.', [1 => $key]); |
| 646 | } |
| 647 | } |
| 648 | if (!empty($obsolete['uninstall']) && in_array($status, [$manager::STATUS_DISABLED, $manager::STATUS_DISABLED_MISSING])) { |
| 649 | try { |
| 650 | $manager->uninstall($key); |
| 651 | $messages[$key] = ts('The extension %1 is now obsolete and has been uninstalled.', [1 => $key]); |
| 652 | if ($status == $manager::STATUS_DISABLED) { |
| 653 | $messages[$key] .= ' ' . ts('You can remove it from your extensions directory.'); |
| 654 | } |
| 655 | } |
| 656 | catch (CRM_Extension_Exception $e) { |
| 657 | $messages[] = ts('The obsolete extension %1 could not be removed due to an error. It is recommended to remove this extension manually.', [1 => $key]); |
| 658 | } |
| 659 | } |
| 660 | if (!empty($obsolete['force-uninstall'])) { |
| 661 | CRM_Core_DAO::executeQuery('UPDATE civicrm_extension SET is_active = 0 WHERE full_name = %1', [ |
| 662 | 1 => [$key, 'String'], |
| 663 | ]); |
| 664 | } |
| 665 | } |
| 666 | } |
| 667 | if ($messages) { |
| 668 | file_put_contents($postUpgradeMessageFile, |
| 669 | '<br/><br/><ul><li>' . implode("</li>\n<li>", $messages) . '</li></ul>', |
| 670 | FILE_APPEND |
| 671 | ); |
| 672 | } |
| 673 | |
| 674 | return TRUE; |
| 675 | } |
| 676 | |
| 677 | /** |
| 678 | * Perform an incremental version update. |
| 679 | * |
| 680 | * @param CRM_Queue_TaskContext $ctx |
| 681 | * @param string $rev |
| 682 | * the target (intermediate) revision e.g '3.2.alpha1'. |
| 683 | * |
| 684 | * @return bool |
| 685 | */ |
| 686 | public static function doIncrementalUpgradeStart(CRM_Queue_TaskContext $ctx, $rev) { |
| 687 | $upgrade = new CRM_Upgrade_Form(); |
| 688 | |
| 689 | // as soon as we start doing anything we append ".upgrade" to version. |
| 690 | // this also helps detect any partial upgrade issues |
| 691 | $upgrade->setVersion($rev . '.upgrade'); |
| 692 | |
| 693 | return TRUE; |
| 694 | } |
| 695 | |
| 696 | /** |
| 697 | * Perform an incremental version update. |
| 698 | * |
| 699 | * @param CRM_Queue_TaskContext $ctx |
| 700 | * @param string $rev |
| 701 | * the target (intermediate) revision e.g '3.2.alpha1'. |
| 702 | * @param string $originalVer |
| 703 | * the original revision. |
| 704 | * @param string $latestVer |
| 705 | * the target (final) revision. |
| 706 | * @param string $postUpgradeMessageFile |
| 707 | * path of a modifiable file which lists the post-upgrade messages. |
| 708 | * |
| 709 | * @return bool |
| 710 | */ |
| 711 | public static function doIncrementalUpgradeStep(CRM_Queue_TaskContext $ctx, $rev, $originalVer, $latestVer, $postUpgradeMessageFile) { |
| 712 | $upgrade = new CRM_Upgrade_Form(); |
| 713 | |
| 714 | $phpFunctionName = 'upgrade_' . str_replace('.', '_', $rev); |
| 715 | |
| 716 | $versionObject = $upgrade->incrementalPhpObject($rev); |
| 717 | |
| 718 | // pre-db check for major release. |
| 719 | if ($upgrade->checkVersionRelease($rev, 'alpha1')) { |
| 720 | if (!(is_callable([$versionObject, 'verifyPreDBstate']))) { |
| 721 | CRM_Core_Error::fatal("verifyPreDBstate method was not found for $rev"); |
| 722 | } |
| 723 | |
| 724 | $error = NULL; |
| 725 | if (!($versionObject->verifyPreDBstate($error))) { |
| 726 | if (!isset($error)) { |
| 727 | $error = "post-condition failed for current upgrade for $rev"; |
| 728 | } |
| 729 | CRM_Core_Error::fatal($error); |
| 730 | } |
| 731 | |
| 732 | } |
| 733 | |
| 734 | $upgrade->setSchemaStructureTables($rev); |
| 735 | |
| 736 | if (is_callable([$versionObject, $phpFunctionName])) { |
| 737 | $versionObject->$phpFunctionName($rev, $originalVer, $latestVer); |
| 738 | } |
| 739 | else { |
| 740 | $upgrade->processSQL($rev); |
| 741 | } |
| 742 | |
| 743 | // set post-upgrade-message if any |
| 744 | if (is_callable([$versionObject, 'setPostUpgradeMessage'])) { |
| 745 | $postUpgradeMessage = file_get_contents($postUpgradeMessageFile); |
| 746 | $versionObject->setPostUpgradeMessage($postUpgradeMessage, $rev); |
| 747 | file_put_contents($postUpgradeMessageFile, $postUpgradeMessage); |
| 748 | } |
| 749 | |
| 750 | return TRUE; |
| 751 | } |
| 752 | |
| 753 | /** |
| 754 | * Perform an incremental version update. |
| 755 | * |
| 756 | * @param CRM_Queue_TaskContext $ctx |
| 757 | * @param string $rev |
| 758 | * the target (intermediate) revision e.g '3.2.alpha1'. |
| 759 | * @param string $currentVer |
| 760 | * the original revision. |
| 761 | * @param string $latestVer |
| 762 | * the target (final) revision. |
| 763 | * @param string $postUpgradeMessageFile |
| 764 | * path of a modifiable file which lists the post-upgrade messages. |
| 765 | * |
| 766 | * @return bool |
| 767 | */ |
| 768 | public static function doIncrementalUpgradeFinish(CRM_Queue_TaskContext $ctx, $rev, $currentVer, $latestVer, $postUpgradeMessageFile) { |
| 769 | $upgrade = new CRM_Upgrade_Form(); |
| 770 | $upgrade->setVersion($rev); |
| 771 | CRM_Utils_System::flushCache(); |
| 772 | |
| 773 | $config = CRM_Core_Config::singleton(); |
| 774 | $config->userSystem->flush(); |
| 775 | return TRUE; |
| 776 | } |
| 777 | |
| 778 | public static function doFinish() { |
| 779 | $upgrade = new CRM_Upgrade_Form(); |
| 780 | list($ignore, $latestVer) = $upgrade->getUpgradeVersions(); |
| 781 | // Seems extraneous in context, but we'll preserve old behavior |
| 782 | $upgrade->setVersion($latestVer); |
| 783 | |
| 784 | // Clear cached metadata. |
| 785 | Civi::service('settings_manager')->flush(); |
| 786 | |
| 787 | // cleanup caches CRM-8739 |
| 788 | $config = CRM_Core_Config::singleton(); |
| 789 | $config->cleanupCaches(1); |
| 790 | |
| 791 | $versionCheck = new CRM_Utils_VersionCheck(); |
| 792 | $versionCheck->flushCache(); |
| 793 | |
| 794 | // Rebuild all triggers and re-enable logging if needed |
| 795 | $logging = new CRM_Logging_Schema(); |
| 796 | $logging->fixSchemaDifferences(); |
| 797 | |
| 798 | CRM_Core_ManagedEntities::singleton(TRUE)->reconcile(TRUE); |
| 799 | } |
| 800 | |
| 801 | /** |
| 802 | * Compute any messages which should be displayed before upgrade |
| 803 | * by calling the 'setPreUpgradeMessage' on each incremental upgrade |
| 804 | * object. |
| 805 | * |
| 806 | * @param string $preUpgradeMessage |
| 807 | * alterable. |
| 808 | * @param $currentVer |
| 809 | * @param $latestVer |
| 810 | */ |
| 811 | public function setPreUpgradeMessage(&$preUpgradeMessage, $currentVer, $latestVer) { |
| 812 | // check for changed message templates |
| 813 | CRM_Upgrade_Incremental_General::checkMessageTemplate($preUpgradeMessage, $latestVer, $currentVer); |
| 814 | // set global messages |
| 815 | CRM_Upgrade_Incremental_General::setPreUpgradeMessage($preUpgradeMessage, $currentVer, $latestVer); |
| 816 | |
| 817 | // Scan through all php files and see if any file is interested in setting pre-upgrade-message |
| 818 | // based on $currentVer, $latestVer. |
| 819 | // Please note, at this point upgrade hasn't started executing queries. |
| 820 | $revisions = $this->getRevisionSequence(); |
| 821 | foreach ($revisions as $rev) { |
| 822 | if (version_compare($currentVer, $rev) < 0) { |
| 823 | $versionObject = $this->incrementalPhpObject($rev); |
| 824 | CRM_Upgrade_Incremental_General::updateMessageTemplate($preUpgradeMessage, $rev); |
| 825 | if (is_callable([$versionObject, 'setPreUpgradeMessage'])) { |
| 826 | $versionObject->setPreUpgradeMessage($preUpgradeMessage, $rev, $currentVer); |
| 827 | } |
| 828 | } |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | } |