3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 class CRM_Upgrade_Form
extends CRM_Core_Form
{
36 const QUEUE_NAME
= 'CRM_Upgrade';
39 * Minimum size of MySQL's thread_stack option
41 * @see install/index.php MINIMUM_THREAD_STACK
43 const MINIMUM_THREAD_STACK
= 192;
46 * Minimum previous CiviCRM version we can directly upgrade from
48 const MINIMUM_UPGRADABLE_VERSION
= '4.2.9';
51 * Minimum php version required to run (equal to or lower than the minimum install version)
53 * As of Civi 5.16, using PHP 5.x will lead to a hard crash during bootstrap.
55 const MINIMUM_PHP_VERSION
= '7.0.0';
58 * @var \CRM_Core_Config
63 * Upgrade for multilingual.
67 public $multilingual = FALSE;
70 * Locales available for multilingual upgrade.
77 * Constructor for the basic form page.
79 * We should not use QuickForm directly. This class provides a lot
80 * of default convenient functions, rules and buttons
82 * @param object $state
83 * State associated with this form.
84 * @param const|\enum|int $action The mode the form is operating in (None/Create/View/Update/Delete)
85 * @param string $method
86 * The type of http method used (GET/POST).
88 * The name of the form if different from class name.
90 public function __construct(
92 $action = CRM_Core_Action
::NONE
,
96 $this->_config
= CRM_Core_Config
::singleton();
98 $domain = new CRM_Core_DAO_Domain();
101 $this->multilingual
= (bool) $domain->locales
;
102 $this->locales
= explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $domain->locales
);
104 $smarty = CRM_Core_Smarty
::singleton();
105 //$smarty->compile_dir = $this->_config->templateCompileDir;
106 $smarty->assign('multilingual', $this->multilingual
);
107 $smarty->assign('locales', $this->locales
);
109 // we didn't call CRM_Core_BAO_ConfigSetting::retrieve(), so we need to set $dbLocale by hand
110 if ($this->multilingual
) {
112 $dbLocale = "_{$this->_config->lcMessages}";
115 parent
::__construct($state, $action, $method, $name);
123 public static function &incrementalPhpObject($version) {
124 static $incrementalPhpObject = [];
126 $versionParts = explode('.', $version);
127 $versionName = CRM_Utils_EnglishNumber
::toCamelCase($versionParts[0]) . CRM_Utils_EnglishNumber
::toCamelCase($versionParts[1]);
129 if (!array_key_exists($versionName, $incrementalPhpObject)) {
130 $className = "CRM_Upgrade_Incremental_php_{$versionName}";
131 $incrementalPhpObject[$versionName] = new $className();
133 return $incrementalPhpObject[$versionName];
142 public function checkVersionRelease($version, $release) {
143 $versionParts = explode('.', $version);
144 return ($versionParts[2] == $release);
148 * @param $constraints
152 public function checkSQLConstraints(&$constraints) {
154 foreach ($constraints as $constraint) {
155 if ($this->checkSQLConstraint($constraint)) {
161 return [$pass, $fail];
170 public function checkSQLConstraint($constraint) {
171 // check constraint here
176 * @param string $fileName
177 * @param bool $isQueryString
179 public function source($fileName, $isQueryString = FALSE) {
180 if ($isQueryString) {
181 CRM_Utils_File
::runSqlQuery($this->_config
->dsn
,
186 CRM_Utils_File
::sourceSQLFile($this->_config
->dsn
,
192 public function preProcess() {
193 CRM_Utils_System
::setTitle($this->getTitle());
194 if (!$this->verifyPreDBState($errorMessage)) {
195 if (!isset($errorMessage)) {
196 $errorMessage = 'pre-condition failed for current upgrade step';
198 CRM_Core_Error
::fatal($errorMessage);
200 $this->assign('recentlyViewed', FALSE);
203 public function buildQuickForm() {
204 $this->addDefaultButtons($this->getButtonTitle(),
212 * Getter function for title. Should be over-ridden by derived class
220 public function getTitle() {
221 return ts('Title not Set');
227 public function getFieldsetTitle() {
234 public function getButtonTitle() {
235 return ts('Continue');
239 * Use the form name to create the tpl file name.
247 public function getTemplateFileName() {
248 $this->assign('title',
249 $this->getFieldsetTitle()
251 $this->assign('message',
252 $this->getTemplateMessage()
254 return 'CRM/Upgrade/Base.tpl';
257 public function postProcess() {
260 if (!$this->verifyPostDBState($errorMessage)) {
261 if (!isset($errorMessage)) {
262 $errorMessage = 'post-condition failed for current upgrade step';
264 CRM_Core_Error
::fatal($errorMessage);
273 public function runQuery($query) {
274 return CRM_Core_DAO
::executeQuery($query);
282 public function setVersion($version) {
283 $this->logVersion($version);
286 UPDATE civicrm_domain
287 SET version = '$version'
289 return $this->runQuery($query);
297 public function logVersion($newVersion) {
299 $oldVersion = CRM_Core_BAO_Domain
::version();
301 $session = CRM_Core_Session
::singleton();
303 'entity_table' => 'civicrm_domain',
305 'data' => "upgrade:{$oldVersion}->{$newVersion}",
306 // lets skip 'modified_id' for now, as it causes FK issues And
307 // is not very important for now.
308 'modified_date' => date('YmdHis'),
310 CRM_Core_BAO_Log
::add($logParams);
322 public function checkVersion($version) {
323 $domainID = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Domain',
327 return $domainID ?
TRUE : FALSE;
334 public function getRevisionSequence() {
336 $sqlDir = implode(DIRECTORY_SEPARATOR
,
337 [dirname(__FILE__
), 'Incremental', 'sql']
339 $sqlFiles = scandir($sqlDir);
341 $sqlFilePattern = '/^((\d{1,2}\.\d{1,2})\.(\d{1,2}\.)?(\d{1,2}|\w{4,7}))\.(my)?sql(\.tpl)?$/i';
342 foreach ($sqlFiles as $file) {
343 if (preg_match($sqlFilePattern, $file, $matches)) {
344 if (!in_array($matches[1], $revList)) {
345 $revList[] = $matches[1];
350 usort($revList, 'version_compare');
360 public static function getRevisionPart($rev, $index = 1) {
361 $revPattern = '/^((\d{1,2})\.\d{1,2})\.(\d{1,2}|\w{4,7})?$/i';
362 preg_match($revPattern, $rev, $matches);
364 return array_key_exists($index, $matches) ?
$matches[$index] : NULL;
373 public function processLocales($tplFile, $rev) {
374 $smarty = CRM_Core_Smarty
::singleton();
375 $smarty->assign('domainID', CRM_Core_Config
::domainID());
377 $this->source($smarty->fetch($tplFile), TRUE);
379 if ($this->multilingual
) {
380 CRM_Core_I18n_Schema
::rebuildMultilingualSchema($this->locales
, $rev);
382 return $this->multilingual
;
388 public function setSchemaStructureTables($rev) {
389 if ($this->multilingual
) {
390 CRM_Core_I18n_Schema
::schemaStructureTables($rev, TRUE);
399 public function processSQL($rev) {
400 $sqlFile = implode(DIRECTORY_SEPARATOR
,
408 $tplFile = "$sqlFile.tpl";
410 if (file_exists($tplFile)) {
411 $this->processLocales($tplFile, $rev);
414 if (!file_exists($sqlFile)) {
415 CRM_Core_Error
::fatal("sqlfile - $rev.mysql not found.");
417 $this->source($sqlFile);
422 * Determine the start and end version of the upgrade process.
424 * @return array(0=>$currentVer, 1=>$latestVer)
426 public function getUpgradeVersions() {
427 $latestVer = CRM_Utils_System
::version();
428 $currentVer = CRM_Core_BAO_Domain
::version(TRUE);
430 CRM_Core_Error
::fatal(ts('Version information missing in civicrm database.'));
432 elseif (stripos($currentVer, 'upgrade')) {
433 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.'));
436 CRM_Core_Error
::fatal(ts('Version information missing in civicrm codebase.'));
439 return [$currentVer, $latestVer];
443 * Determine if $currentVer can be upgraded to $latestVer
448 * @return mixed, a string error message or boolean 'false' if OK
450 public function checkUpgradeableVersion($currentVer, $latestVer) {
452 // since version is suppose to be in valid format at this point, especially after conversion ($convertVer),
453 // lets do a pattern check -
454 if (!CRM_Utils_System
::isVersionFormatValid($currentVer)) {
455 $error = ts('Database is marked with invalid version format. You may want to investigate this before you proceed further.');
457 elseif (version_compare($currentVer, $latestVer) > 0) {
458 // DB version number is higher than codebase being upgraded to. This is unexpected condition-fatal error.
459 $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.',
460 [1 => $currentVer, 2 => $latestVer]
463 elseif (version_compare($currentVer, $latestVer) == 0) {
464 $error = ts('Your database has already been upgraded to CiviCRM %1',
468 elseif (version_compare($currentVer, self
::MINIMUM_UPGRADABLE_VERSION
) < 0) {
469 $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.',
470 [1 => self
::MINIMUM_UPGRADABLE_VERSION
, 2 => $latestVer]
474 if (version_compare(phpversion(), self
::MINIMUM_PHP_VERSION
) < 0) {
475 $error = ts('CiviCRM %3 requires PHP version %1 (or newer), but the current system uses %2 ',
477 1 => self
::MINIMUM_PHP_VERSION
,
483 // check for mysql trigger privileges
484 if (!\Civi
::settings()->get('logging_no_trigger_permission') && !CRM_Core_DAO
::checkTriggerViewPermission(FALSE, TRUE)) {
485 $error = ts('CiviCRM %1 requires MySQL trigger privileges.',
489 if (CRM_Core_DAO
::getGlobalSetting('thread_stack', 0) < (1024 * self
::MINIMUM_THREAD_STACK
)) {
490 $error = ts('CiviCRM %1 requires MySQL thread stack >= %2k', [
492 2 => self
::MINIMUM_THREAD_STACK
,
500 * Determine if $currentver already matches $latestVer
505 * @return mixed, a string error message or boolean 'false' if OK
507 public function checkCurrentVersion($currentVer, $latestVer) {
510 // since version is suppose to be in valid format at this point, especially after conversion ($convertVer),
511 // lets do a pattern check -
512 if (!CRM_Utils_System
::isVersionFormatValid($currentVer)) {
513 $error = ts('Database is marked with invalid version format. You may want to investigate this before you proceed further.');
515 elseif (version_compare($currentVer, $latestVer) != 0) {
516 $error = ts('Your database is not configured for version %1',
524 * Fill the queue with upgrade tasks.
526 * @param string $currentVer
527 * the original revision.
528 * @param string $latestVer
529 * the target (final) revision.
530 * @param string $postUpgradeMessageFile
531 * path of a modifiable file which lists the post-upgrade messages.
533 * @return CRM_Queue_Service
535 public static function buildQueue($currentVer, $latestVer, $postUpgradeMessageFile) {
536 $upgrade = new CRM_Upgrade_Form();
538 // Ensure that queue can be created
539 if (!CRM_Queue_BAO_QueueItem
::findCreateTable()) {
540 CRM_Core_Error
::fatal(ts('Failed to find or create queueing table'));
542 $queue = CRM_Queue_Service
::singleton()->create([
543 'name' => self
::QUEUE_NAME
,
548 $task = new CRM_Queue_Task(
549 ['CRM_Upgrade_Form', 'doFileCleanup'],
550 [$postUpgradeMessageFile],
553 $queue->createItem($task);
555 $task = new CRM_Queue_Task(
556 ['CRM_Upgrade_Form', 'disableOldExtensions'],
557 [$postUpgradeMessageFile],
558 "Checking extensions"
560 $queue->createItem($task);
562 $revisions = $upgrade->getRevisionSequence();
563 foreach ($revisions as $rev) {
564 // proceed only if $currentVer < $rev
565 if (version_compare($currentVer, $rev) < 0) {
566 $beginTask = new CRM_Queue_Task(
568 ['CRM_Upgrade_Form', 'doIncrementalUpgradeStart'],
571 "Begin Upgrade to $rev"
573 $queue->createItem($beginTask);
575 $task = new CRM_Queue_Task(
577 ['CRM_Upgrade_Form', 'doIncrementalUpgradeStep'],
579 [$rev, $currentVer, $latestVer, $postUpgradeMessageFile],
582 $queue->createItem($task);
584 $task = new CRM_Queue_Task(
586 ['CRM_Upgrade_Form', 'doIncrementalUpgradeFinish'],
588 [$rev, $currentVer, $latestVer, $postUpgradeMessageFile],
589 "Finish Upgrade DB to $rev"
591 $queue->createItem($task);
599 * Find any old, orphaned files that should have been deleted.
601 * These files can get left behind, eg, if you use the Joomla
604 * The earlier we can do this, the better - don't want upgrade logic
605 * to inadvertently rely on old/relocated files.
607 * @param \CRM_Queue_TaskContext $ctx
608 * @param string $postUpgradeMessageFile
611 public static function doFileCleanup(CRM_Queue_TaskContext
$ctx, $postUpgradeMessageFile) {
612 $source = new CRM_Utils_Check_Component_Source();
613 $files = $source->findOrphanedFiles();
615 foreach ($files as $file) {
616 if (is_dir($file['path'])) {
617 @rmdir
($file['path']);
620 @unlink
($file['path']);
623 if (file_exists($file['path'])) {
624 $errors[] = sprintf("<li>%s</li>", htmlentities($file['path']));
628 if (!empty($errors)) {
629 file_put_contents($postUpgradeMessageFile,
630 '<br/><br/>' . ts('Some old files could not be removed. Please remove them.')
631 . '<ul>' . implode("\n", $errors) . '</ul>',
640 * Disable/uninstall any extensions not compatible with this new version.
642 * @param \CRM_Queue_TaskContext $ctx
643 * @param string $postUpgradeMessageFile
646 public static function disableOldExtensions(CRM_Queue_TaskContext
$ctx, $postUpgradeMessageFile) {
648 $manager = CRM_Extension_System
::singleton()->getManager();
649 foreach ($manager->getStatuses() as $key => $status) {
650 $obsolete = $manager->isIncompatible($key);
652 if (!empty($obsolete['disable']) && in_array($status, [$manager::STATUS_INSTALLED
, $manager::STATUS_INSTALLED_MISSING
])) {
654 $manager->disable($key);
655 // Update the status for the sake of uninstall below.
656 $status = $status == $manager::STATUS_INSTALLED ?
$manager::STATUS_DISABLED
: $manager::STATUS_DISABLED_MISSING
;
657 // This message is intentionally overwritten by uninstall below as it would be redundant
658 $messages[$key] = ts('The extension %1 is now obsolete and has been disabled.', [1 => $key]);
660 catch (CRM_Extension_Exception
$e) {
661 $messages[] = ts('The obsolete extension %1 could not be removed due to an error. It is recommended to remove this extension manually.', [1 => $key]);
664 if (!empty($obsolete['uninstall']) && in_array($status, [$manager::STATUS_DISABLED
, $manager::STATUS_DISABLED_MISSING
])) {
666 $manager->uninstall($key);
667 $messages[$key] = ts('The extension %1 is now obsolete and has been uninstalled.', [1 => $key]);
668 if ($status == $manager::STATUS_DISABLED
) {
669 $messages[$key] .= ' ' . ts('You can remove it from your extensions directory.');
672 catch (CRM_Extension_Exception
$e) {
673 $messages[] = ts('The obsolete extension %1 could not be removed due to an error. It is recommended to remove this extension manually.', [1 => $key]);
676 if (!empty($obsolete['force-uninstall'])) {
677 CRM_Core_DAO
::executeQuery('UPDATE civicrm_extension SET is_active = 0 WHERE full_name = %1', [
678 1 => [$key, 'String'],
684 file_put_contents($postUpgradeMessageFile,
685 '<br/><br/><ul><li>' . implode("</li>\n<li>", $messages) . '</li></ul>',
694 * Perform an incremental version update.
696 * @param CRM_Queue_TaskContext $ctx
698 * the target (intermediate) revision e.g '3.2.alpha1'.
702 public static function doIncrementalUpgradeStart(CRM_Queue_TaskContext
$ctx, $rev) {
703 $upgrade = new CRM_Upgrade_Form();
705 // as soon as we start doing anything we append ".upgrade" to version.
706 // this also helps detect any partial upgrade issues
707 $upgrade->setVersion($rev . '.upgrade');
713 * Perform an incremental version update.
715 * @param CRM_Queue_TaskContext $ctx
717 * the target (intermediate) revision e.g '3.2.alpha1'.
718 * @param string $originalVer
719 * the original revision.
720 * @param string $latestVer
721 * the target (final) revision.
722 * @param string $postUpgradeMessageFile
723 * path of a modifiable file which lists the post-upgrade messages.
727 public static function doIncrementalUpgradeStep(CRM_Queue_TaskContext
$ctx, $rev, $originalVer, $latestVer, $postUpgradeMessageFile) {
728 $upgrade = new CRM_Upgrade_Form();
730 $phpFunctionName = 'upgrade_' . str_replace('.', '_', $rev);
732 $versionObject = $upgrade->incrementalPhpObject($rev);
734 // pre-db check for major release.
735 if ($upgrade->checkVersionRelease($rev, 'alpha1')) {
736 if (!(is_callable([$versionObject, 'verifyPreDBstate']))) {
737 CRM_Core_Error
::fatal("verifyPreDBstate method was not found for $rev");
741 if (!($versionObject->verifyPreDBstate($error))) {
742 if (!isset($error)) {
743 $error = "post-condition failed for current upgrade for $rev";
745 CRM_Core_Error
::fatal($error);
750 $upgrade->setSchemaStructureTables($rev);
752 if (is_callable([$versionObject, $phpFunctionName])) {
753 $versionObject->$phpFunctionName($rev, $originalVer, $latestVer);
756 $upgrade->processSQL($rev);
759 // set post-upgrade-message if any
760 if (is_callable([$versionObject, 'setPostUpgradeMessage'])) {
761 $postUpgradeMessage = file_get_contents($postUpgradeMessageFile);
762 $versionObject->setPostUpgradeMessage($postUpgradeMessage, $rev);
763 file_put_contents($postUpgradeMessageFile, $postUpgradeMessage);
770 * Perform an incremental version update.
772 * @param CRM_Queue_TaskContext $ctx
774 * the target (intermediate) revision e.g '3.2.alpha1'.
775 * @param string $currentVer
776 * the original revision.
777 * @param string $latestVer
778 * the target (final) revision.
779 * @param string $postUpgradeMessageFile
780 * path of a modifiable file which lists the post-upgrade messages.
784 public static function doIncrementalUpgradeFinish(CRM_Queue_TaskContext
$ctx, $rev, $currentVer, $latestVer, $postUpgradeMessageFile) {
785 $upgrade = new CRM_Upgrade_Form();
786 $upgrade->setVersion($rev);
787 CRM_Utils_System
::flushCache();
789 $config = CRM_Core_Config
::singleton();
790 $config->userSystem
->flush();
794 public static function doFinish() {
795 $upgrade = new CRM_Upgrade_Form();
796 list($ignore, $latestVer) = $upgrade->getUpgradeVersions();
797 // Seems extraneous in context, but we'll preserve old behavior
798 $upgrade->setVersion($latestVer);
800 // Clear cached metadata.
801 Civi
::service('settings_manager')->flush();
803 // cleanup caches CRM-8739
804 $config = CRM_Core_Config
::singleton();
805 $config->cleanupCaches(1);
807 $versionCheck = new CRM_Utils_VersionCheck();
808 $versionCheck->flushCache();
810 // Rebuild all triggers and re-enable logging if needed
811 $logging = new CRM_Logging_Schema();
812 $logging->fixSchemaDifferences();
816 * Compute any messages which should be displayed before upgrade
817 * by calling the 'setPreUpgradeMessage' on each incremental upgrade
820 * @param string $preUpgradeMessage
825 public function setPreUpgradeMessage(&$preUpgradeMessage, $currentVer, $latestVer) {
826 // check for changed message templates
827 CRM_Upgrade_Incremental_General
::checkMessageTemplate($preUpgradeMessage, $latestVer, $currentVer);
828 // set global messages
829 CRM_Upgrade_Incremental_General
::setPreUpgradeMessage($preUpgradeMessage, $currentVer, $latestVer);
831 // Scan through all php files and see if any file is interested in setting pre-upgrade-message
832 // based on $currentVer, $latestVer.
833 // Please note, at this point upgrade hasn't started executing queries.
834 $revisions = $this->getRevisionSequence();
835 foreach ($revisions as $rev) {
836 if (version_compare($currentVer, $rev) < 0) {
837 $versionObject = $this->incrementalPhpObject($rev);
838 CRM_Upgrade_Incremental_General
::updateMessageTemplate($preUpgradeMessage, $rev);
839 if (is_callable([$versionObject, 'setPreUpgradeMessage'])) {
840 $versionObject->setPreUpgradeMessage($preUpgradeMessage, $rev, $currentVer);