3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 class CRM_Upgrade_Form
extends CRM_Core_Form
{
20 const QUEUE_NAME
= 'CRM_Upgrade';
23 * Minimum size of MySQL's thread_stack option
25 * @see install/index.php MINIMUM_THREAD_STACK
27 const MINIMUM_THREAD_STACK
= 192;
30 * Minimum previous CiviCRM version we can directly upgrade from
32 const MINIMUM_UPGRADABLE_VERSION
= '4.2.9';
35 * Minimum php version required to run (equal to or lower than the minimum install version)
37 * As of Civi 5.16, using PHP 5.x will lead to a hard crash during bootstrap.
39 const MINIMUM_PHP_VERSION
= '7.0.0';
42 * @var \CRM_Core_Config
47 * Upgrade for multilingual.
51 public $multilingual = FALSE;
54 * Locales available for multilingual upgrade.
61 * Constructor for the basic form page.
63 * We should not use QuickForm directly. This class provides a lot
64 * of default convenient functions, rules and buttons
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).
72 * The name of the form if different from class name.
74 public function __construct(
76 $action = CRM_Core_Action
::NONE
,
80 $this->_config
= CRM_Core_Config
::singleton();
82 $domain = new CRM_Core_DAO_Domain();
85 $this->multilingual
= (bool) $domain->locales
;
86 $this->locales
= explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $domain->locales
);
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
);
93 // we didn't call CRM_Core_BAO_ConfigSetting::retrieve(), so we need to set $dbLocale by hand
94 if ($this->multilingual
) {
96 $dbLocale = "_{$this->_config->lcMessages}";
99 parent
::__construct($state, $action, $method, $name);
107 public static function &incrementalPhpObject($version) {
108 static $incrementalPhpObject = [];
110 $versionParts = explode('.', $version);
111 $versionName = CRM_Utils_EnglishNumber
::toCamelCase($versionParts[0]) . CRM_Utils_EnglishNumber
::toCamelCase($versionParts[1]);
113 if (!array_key_exists($versionName, $incrementalPhpObject)) {
114 $className = "CRM_Upgrade_Incremental_php_{$versionName}";
115 $incrementalPhpObject[$versionName] = new $className();
117 return $incrementalPhpObject[$versionName];
126 public function checkVersionRelease($version, $release) {
127 $versionParts = explode('.', $version);
128 return ($versionParts[2] == $release);
132 * @param $constraints
136 public function checkSQLConstraints(&$constraints) {
138 foreach ($constraints as $constraint) {
139 if ($this->checkSQLConstraint($constraint)) {
145 return [$pass, $fail];
154 public function checkSQLConstraint($constraint) {
155 // check constraint here
160 * @param string $fileName
161 * @param bool $isQueryString
163 public function source($fileName, $isQueryString = FALSE) {
164 if ($isQueryString) {
165 CRM_Utils_File
::runSqlQuery($this->_config
->dsn
,
170 CRM_Utils_File
::sourceSQLFile($this->_config
->dsn
,
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';
182 CRM_Core_Error
::fatal($errorMessage);
184 $this->assign('recentlyViewed', FALSE);
187 public function buildQuickForm() {
188 $this->addDefaultButtons($this->getButtonTitle(),
196 * Getter function for title. Should be over-ridden by derived class
204 public function getTitle() {
205 return ts('Title not Set');
211 public function getFieldsetTitle() {
218 public function getButtonTitle() {
219 return ts('Continue');
223 * Use the form name to create the tpl file name.
231 public function getTemplateFileName() {
232 $this->assign('title',
233 $this->getFieldsetTitle()
235 $this->assign('message',
236 $this->getTemplateMessage()
238 return 'CRM/Upgrade/Base.tpl';
241 public function postProcess() {
244 if (!$this->verifyPostDBState($errorMessage)) {
245 if (!isset($errorMessage)) {
246 $errorMessage = 'post-condition failed for current upgrade step';
248 CRM_Core_Error
::fatal($errorMessage);
257 public function runQuery($query) {
258 return CRM_Core_DAO
::executeQuery($query);
266 public function setVersion($version) {
267 $this->logVersion($version);
270 UPDATE civicrm_domain
271 SET version = '$version'
273 return $this->runQuery($query);
281 public function logVersion($newVersion) {
283 $oldVersion = CRM_Core_BAO_Domain
::version();
285 $session = CRM_Core_Session
::singleton();
287 'entity_table' => 'civicrm_domain',
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'),
294 CRM_Core_BAO_Log
::add($logParams);
306 public function checkVersion($version) {
307 $domainID = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Domain',
311 return $domainID ?
TRUE : FALSE;
318 public function getRevisionSequence() {
320 $sqlDir = implode(DIRECTORY_SEPARATOR
,
321 [dirname(__FILE__
), 'Incremental', 'sql']
323 $sqlFiles = scandir($sqlDir);
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];
334 usort($revList, 'version_compare');
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);
348 return array_key_exists($index, $matches) ?
$matches[$index] : NULL;
357 public function processLocales($tplFile, $rev) {
358 $smarty = CRM_Core_Smarty
::singleton();
359 $smarty->assign('domainID', CRM_Core_Config
::domainID());
361 $this->source($smarty->fetch($tplFile), TRUE);
363 if ($this->multilingual
) {
364 CRM_Core_I18n_Schema
::rebuildMultilingualSchema($this->locales
, $rev);
366 return $this->multilingual
;
372 public function setSchemaStructureTables($rev) {
373 if ($this->multilingual
) {
374 CRM_Core_I18n_Schema
::schemaStructureTables($rev, TRUE);
383 public function processSQL($rev) {
384 $sqlFile = implode(DIRECTORY_SEPARATOR
,
392 $tplFile = "$sqlFile.tpl";
394 if (file_exists($tplFile)) {
395 $this->processLocales($tplFile, $rev);
398 if (!file_exists($sqlFile)) {
399 CRM_Core_Error
::fatal("sqlfile - $rev.mysql not found.");
401 $this->source($sqlFile);
406 * Determine the start and end version of the upgrade process.
408 * @return array(0=>$currentVer, 1=>$latestVer)
410 public function getUpgradeVersions() {
411 $latestVer = CRM_Utils_System
::version();
412 $currentVer = CRM_Core_BAO_Domain
::version(TRUE);
414 CRM_Core_Error
::fatal(ts('Version information missing in civicrm database.'));
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.'));
420 CRM_Core_Error
::fatal(ts('Version information missing in civicrm codebase.'));
423 return [$currentVer, $latestVer];
427 * Determine if $currentVer can be upgraded to $latestVer
432 * @return mixed, a string error message or boolean 'false' if OK
434 public function checkUpgradeableVersion($currentVer, $latestVer) {
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.');
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]
447 elseif (version_compare($currentVer, $latestVer) == 0) {
448 $error = ts('Your database has already been upgraded to CiviCRM %1',
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]
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 ',
461 1 => self
::MINIMUM_PHP_VERSION
,
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.',
473 if (CRM_Core_DAO
::getGlobalSetting('thread_stack', 0) < (1024 * self
::MINIMUM_THREAD_STACK
)) {
474 $error = ts('CiviCRM %1 requires MySQL thread stack >= %2k', [
476 2 => self
::MINIMUM_THREAD_STACK
,
484 * Determine if $currentver already matches $latestVer
489 * @return mixed, a string error message or boolean 'false' if OK
491 public function checkCurrentVersion($currentVer, $latestVer) {
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.');
499 elseif (version_compare($currentVer, $latestVer) != 0) {
500 $error = ts('Your database is not configured for version %1',
508 * Fill the queue with upgrade tasks.
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.
517 * @return CRM_Queue_Service
519 public static function buildQueue($currentVer, $latestVer, $postUpgradeMessageFile) {
520 $upgrade = new CRM_Upgrade_Form();
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'));
526 $queue = CRM_Queue_Service
::singleton()->create([
527 'name' => self
::QUEUE_NAME
,
532 $task = new CRM_Queue_Task(
533 ['CRM_Upgrade_Form', 'doFileCleanup'],
534 [$postUpgradeMessageFile],
537 $queue->createItem($task);
539 $task = new CRM_Queue_Task(
540 ['CRM_Upgrade_Form', 'disableOldExtensions'],
541 [$postUpgradeMessageFile],
542 "Checking extensions"
544 $queue->createItem($task);
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(
552 ['CRM_Upgrade_Form', 'doIncrementalUpgradeStart'],
555 "Begin Upgrade to $rev"
557 $queue->createItem($beginTask);
559 $task = new CRM_Queue_Task(
561 ['CRM_Upgrade_Form', 'doIncrementalUpgradeStep'],
563 [$rev, $currentVer, $latestVer, $postUpgradeMessageFile],
566 $queue->createItem($task);
568 $task = new CRM_Queue_Task(
570 ['CRM_Upgrade_Form', 'doIncrementalUpgradeFinish'],
572 [$rev, $currentVer, $latestVer, $postUpgradeMessageFile],
573 "Finish Upgrade DB to $rev"
575 $queue->createItem($task);
583 * Find any old, orphaned files that should have been deleted.
585 * These files can get left behind, eg, if you use the Joomla
588 * The earlier we can do this, the better - don't want upgrade logic
589 * to inadvertently rely on old/relocated files.
591 * @param \CRM_Queue_TaskContext $ctx
592 * @param string $postUpgradeMessageFile
595 public static function doFileCleanup(CRM_Queue_TaskContext
$ctx, $postUpgradeMessageFile) {
596 $source = new CRM_Utils_Check_Component_Source();
597 $files = $source->findOrphanedFiles();
599 foreach ($files as $file) {
600 if (is_dir($file['path'])) {
601 @rmdir
($file['path']);
604 @unlink
($file['path']);
607 if (file_exists($file['path'])) {
608 $errors[] = sprintf("<li>%s</li>", htmlentities($file['path']));
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>',
624 * Disable/uninstall any extensions not compatible with this new version.
626 * @param \CRM_Queue_TaskContext $ctx
627 * @param string $postUpgradeMessageFile
630 public static function disableOldExtensions(CRM_Queue_TaskContext
$ctx, $postUpgradeMessageFile) {
632 $manager = CRM_Extension_System
::singleton()->getManager();
633 foreach ($manager->getStatuses() as $key => $status) {
634 $obsolete = $manager->isIncompatible($key);
636 if (!empty($obsolete['disable']) && in_array($status, [$manager::STATUS_INSTALLED
, $manager::STATUS_INSTALLED_MISSING
])) {
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]);
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]);
648 if (!empty($obsolete['uninstall']) && in_array($status, [$manager::STATUS_DISABLED
, $manager::STATUS_DISABLED_MISSING
])) {
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.');
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]);
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'],
668 file_put_contents($postUpgradeMessageFile,
669 '<br/><br/><ul><li>' . implode("</li>\n<li>", $messages) . '</li></ul>',
678 * Perform an incremental version update.
680 * @param CRM_Queue_TaskContext $ctx
682 * the target (intermediate) revision e.g '3.2.alpha1'.
686 public static function doIncrementalUpgradeStart(CRM_Queue_TaskContext
$ctx, $rev) {
687 $upgrade = new CRM_Upgrade_Form();
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');
697 * Perform an incremental version update.
699 * @param CRM_Queue_TaskContext $ctx
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.
711 public static function doIncrementalUpgradeStep(CRM_Queue_TaskContext
$ctx, $rev, $originalVer, $latestVer, $postUpgradeMessageFile) {
712 $upgrade = new CRM_Upgrade_Form();
714 $phpFunctionName = 'upgrade_' . str_replace('.', '_', $rev);
716 $versionObject = $upgrade->incrementalPhpObject($rev);
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");
725 if (!($versionObject->verifyPreDBstate($error))) {
726 if (!isset($error)) {
727 $error = "post-condition failed for current upgrade for $rev";
729 CRM_Core_Error
::fatal($error);
734 $upgrade->setSchemaStructureTables($rev);
736 if (is_callable([$versionObject, $phpFunctionName])) {
737 $versionObject->$phpFunctionName($rev, $originalVer, $latestVer);
740 $upgrade->processSQL($rev);
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);
754 * Perform an incremental version update.
756 * @param CRM_Queue_TaskContext $ctx
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.
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();
773 $config = CRM_Core_Config
::singleton();
774 $config->userSystem
->flush();
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);
784 // Clear cached metadata.
785 Civi
::service('settings_manager')->flush();
787 // cleanup caches CRM-8739
788 $config = CRM_Core_Config
::singleton();
789 $config->cleanupCaches(1);
791 $versionCheck = new CRM_Utils_VersionCheck();
792 $versionCheck->flushCache();
794 // Rebuild all triggers and re-enable logging if needed
795 $logging = new CRM_Logging_Schema();
796 $logging->fixSchemaDifferences();
798 CRM_Core_ManagedEntities
::singleton(TRUE)->reconcile(TRUE);
802 * Compute any messages which should be displayed before upgrade
803 * by calling the 'setPreUpgradeMessage' on each incremental upgrade
806 * @param string $preUpgradeMessage
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);
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);