3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
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.0.8';
51 * Minimum php version we support
53 const MINIMUM_PHP_VERSION
= '5.3.4';
58 * Upgrade for multilingual.
62 public $multilingual = FALSE;
65 * Locales available for multilingual upgrade.
72 * Number to string mapper.
76 static $_numberMap = array(
90 * Constructor for the basic form page.
92 * We should not use QuickForm directly. This class provides a lot
93 * of default convenient functions, rules and buttons
95 * @param object $state
96 * State associated with this form.
97 * @param const|\enum|int $action The mode the form is operating in (None/Create/View/Update/Delete)
98 * @param string $method
99 * The type of http method used (GET/POST).
100 * @param string $name
101 * The name of the form if different from class name.
103 public function __construct(
105 $action = CRM_Core_Action
::NONE
,
109 $this->_config
= CRM_Core_Config
::singleton();
111 $domain = new CRM_Core_DAO_Domain();
114 $this->multilingual
= (bool) $domain->locales
;
115 $this->locales
= explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $domain->locales
);
117 $smarty = CRM_Core_Smarty
::singleton();
118 //$smarty->compile_dir = $this->_config->templateCompileDir;
119 $smarty->assign('multilingual', $this->multilingual
);
120 $smarty->assign('locales', $this->locales
);
122 // we didn't call CRM_Core_BAO_ConfigSetting::retrieve(), so we need to set $dbLocale by hand
123 if ($this->multilingual
) {
125 $dbLocale = "_{$this->_config->lcMessages}";
128 parent
::__construct($state, $action, $method, $name);
136 public static function &incrementalPhpObject($version) {
137 static $incrementalPhpObject = array();
139 $versionParts = explode('.', $version);
140 $versionName = self
::$_numberMap[$versionParts[0]] . self
::$_numberMap[$versionParts[1]];
142 if (!array_key_exists($versionName, $incrementalPhpObject)) {
143 $className = "CRM_Upgrade_Incremental_php_{$versionName}";
144 $incrementalPhpObject[$versionName] = new $className();
146 return $incrementalPhpObject[$versionName];
155 public function checkVersionRelease($version, $release) {
156 $versionParts = explode('.', $version);
157 return ($versionParts[2] == $release);
161 * @param $constraints
165 public function checkSQLConstraints(&$constraints) {
167 foreach ($constraints as $constraint) {
168 if ($this->checkSQLConstraint($constraint)) {
174 return array($pass, $fail);
183 public function checkSQLConstraint($constraint) {
184 // check constraint here
189 * @param string $fileName
190 * @param bool $isQueryString
192 public function source($fileName, $isQueryString = FALSE) {
193 if ($isQueryString) {
194 CRM_Utils_File
::runSqlQuery($this->_config
->dsn
,
199 CRM_Utils_File
::sourceSQLFile($this->_config
->dsn
,
205 public function preProcess() {
206 CRM_Utils_System
::setTitle($this->getTitle());
207 if (!$this->verifyPreDBState($errorMessage)) {
208 if (!isset($errorMessage)) {
209 $errorMessage = 'pre-condition failed for current upgrade step';
211 CRM_Core_Error
::fatal($errorMessage);
213 $this->assign('recentlyViewed', FALSE);
216 public function buildQuickForm() {
217 $this->addDefaultButtons($this->getButtonTitle(),
225 * Getter function for title. Should be over-ridden by derived class
232 public function getTitle() {
233 return ts('Title not Set');
239 public function getFieldsetTitle() {
246 public function getButtonTitle() {
247 return ts('Continue');
251 * Use the form name to create the tpl file name.
258 public function getTemplateFileName() {
259 $this->assign('title',
260 $this->getFieldsetTitle()
262 $this->assign('message',
263 $this->getTemplateMessage()
265 return 'CRM/Upgrade/Base.tpl';
268 public function postProcess() {
271 if (!$this->verifyPostDBState($errorMessage)) {
272 if (!isset($errorMessage)) {
273 $errorMessage = 'post-condition failed for current upgrade step';
275 CRM_Core_Error
::fatal($errorMessage);
284 public function runQuery($query) {
285 return CRM_Core_DAO
::executeQuery($query,
286 CRM_Core_DAO
::$_nullArray
295 public function setVersion($version) {
296 $this->logVersion($version);
299 UPDATE civicrm_domain
300 SET version = '$version'
302 return $this->runQuery($query);
310 public function logVersion($newVersion) {
312 $oldVersion = CRM_Core_BAO_Domain
::version();
314 $session = CRM_Core_Session
::singleton();
316 'entity_table' => 'civicrm_domain',
318 'data' => "upgrade:{$oldVersion}->{$newVersion}",
319 // lets skip 'modified_id' for now, as it causes FK issues And
320 // is not very important for now.
321 'modified_date' => date('YmdHis'),
323 CRM_Core_BAO_Log
::add($logParams);
335 public function checkVersion($version) {
336 $domainID = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Domain',
340 return $domainID ?
TRUE : FALSE;
347 public function getRevisionSequence() {
349 $sqlDir = implode(DIRECTORY_SEPARATOR
,
350 array(dirname(__FILE__
), 'Incremental', 'sql')
352 $sqlFiles = scandir($sqlDir);
354 $sqlFilePattern = '/^((\d{1,2}\.\d{1,2})\.(\d{1,2}\.)?(\d{1,2}|\w{4,7}))\.(my)?sql(\.tpl)?$/i';
355 foreach ($sqlFiles as $file) {
356 if (preg_match($sqlFilePattern, $file, $matches)) {
357 if ($matches[2] == '4.0') {
358 CRM_Core_Error
::fatal("4.0.x upgrade files shouldn't exist. Contact Lobo to discuss this. This is related to the issue CRM-7731.");
360 if (!in_array($matches[1], $revList)) {
361 $revList[] = $matches[1];
366 usort($revList, 'version_compare');
376 public static function getRevisionPart($rev, $index = 1) {
377 $revPattern = '/^((\d{1,2})\.\d{1,2})\.(\d{1,2}|\w{4,7})?$/i';
378 preg_match($revPattern, $rev, $matches);
380 return array_key_exists($index, $matches) ?
$matches[$index] : NULL;
389 public function processLocales($tplFile, $rev) {
390 $smarty = CRM_Core_Smarty
::singleton();
391 $smarty->assign('domainID', CRM_Core_Config
::domainID());
393 $this->source($smarty->fetch($tplFile), TRUE);
395 if ($this->multilingual
) {
396 CRM_Core_I18n_Schema
::rebuildMultilingualSchema($this->locales
, $rev);
398 return $this->multilingual
;
404 public function setSchemaStructureTables($rev) {
405 if ($this->multilingual
) {
406 CRM_Core_I18n_Schema
::schemaStructureTables($rev, TRUE);
415 public function processSQL($rev) {
416 $sqlFile = implode(DIRECTORY_SEPARATOR
,
424 $tplFile = "$sqlFile.tpl";
426 if (file_exists($tplFile)) {
427 $this->processLocales($tplFile, $rev);
430 if (!file_exists($sqlFile)) {
431 CRM_Core_Error
::fatal("sqlfile - $rev.mysql not found.");
433 $this->source($sqlFile);
438 * Determine the start and end version of the upgrade process.
440 * @return array(0=>$currentVer, 1=>$latestVer)
442 public function getUpgradeVersions() {
443 $latestVer = CRM_Utils_System
::version();
444 $currentVer = CRM_Core_BAO_Domain
::version(TRUE);
446 CRM_Core_Error
::fatal(ts('Version information missing in civicrm database.'));
448 elseif (stripos($currentVer, 'upgrade')) {
449 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.'));
452 CRM_Core_Error
::fatal(ts('Version information missing in civicrm codebase.'));
455 return array($currentVer, $latestVer);
459 * Determine if $currentVer can be upgraded to $latestVer
464 * @return mixed, a string error message or boolean 'false' if OK
466 public function checkUpgradeableVersion($currentVer, $latestVer) {
468 // since version is suppose to be in valid format at this point, especially after conversion ($convertVer),
469 // lets do a pattern check -
470 if (!CRM_Utils_System
::isVersionFormatValid($currentVer)) {
471 $error = ts('Database is marked with invalid version format. You may want to investigate this before you proceed further.');
473 elseif (version_compare($currentVer, $latestVer) > 0) {
474 // DB version number is higher than codebase being upgraded to. This is unexpected condition-fatal error.
475 $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.',
476 array(1 => $currentVer, 2 => $latestVer)
479 elseif (version_compare($currentVer, $latestVer) == 0) {
480 $error = ts('Your database has already been upgraded to CiviCRM %1',
481 array(1 => $latestVer)
484 elseif (version_compare($currentVer, self
::MINIMUM_UPGRADABLE_VERSION
) < 0) {
485 $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.',
486 array(1 => self
::MINIMUM_UPGRADABLE_VERSION
, 2 => $latestVer)
490 if (version_compare(phpversion(), self
::MINIMUM_PHP_VERSION
) < 0) {
491 $error = ts('CiviCRM %3 requires PHP version %1 (or newer), but the current system uses %2 ',
493 1 => self
::MINIMUM_PHP_VERSION
,
499 // check for mysql trigger privileges
500 if (!\Civi
::settings()->get('logging_no_trigger_permission') && !CRM_Core_DAO
::checkTriggerViewPermission(FALSE, TRUE)) {
501 $error = ts('CiviCRM %1 requires MySQL trigger privileges.',
502 array(1 => $latestVer));
505 if (CRM_Core_DAO
::getGlobalSetting('thread_stack', 0) < (1024 * self
::MINIMUM_THREAD_STACK
)) {
506 $error = ts('CiviCRM %1 requires MySQL thread stack >= %2k', array(
508 2 => self
::MINIMUM_THREAD_STACK
,
516 * Determine if $currentver already matches $latestVer
521 * @return mixed, a string error message or boolean 'false' if OK
523 public function checkCurrentVersion($currentVer, $latestVer) {
526 // since version is suppose to be in valid format at this point, especially after conversion ($convertVer),
527 // lets do a pattern check -
528 if (!CRM_Utils_System
::isVersionFormatValid($currentVer)) {
529 $error = ts('Database is marked with invalid version format. You may want to investigate this before you proceed further.');
531 elseif (version_compare($currentVer, $latestVer) != 0) {
532 $error = ts('Your database is not configured for version %1',
533 array(1 => $latestVer)
540 * Fill the queue with upgrade tasks.
542 * @param string $currentVer
543 * the original revision.
544 * @param string $latestVer
545 * the target (final) revision.
546 * @param string $postUpgradeMessageFile
547 * path of a modifiable file which lists the post-upgrade messages.
549 * @return CRM_Queue_Service
551 public static function buildQueue($currentVer, $latestVer, $postUpgradeMessageFile) {
552 $upgrade = new CRM_Upgrade_Form();
554 // hack to make 4.0.x (D7,J1.6) codebase go through 3.4.x (d6, J1.5) upgrade files,
555 // since schema wise they are same
556 if (CRM_Upgrade_Form
::getRevisionPart($currentVer) == '4.0') {
557 $currentVer = str_replace('4.0.', '3.4.', $currentVer);
560 // Ensure that queue can be created
561 if (!CRM_Queue_BAO_QueueItem
::findCreateTable()) {
562 CRM_Core_Error
::fatal(ts('Failed to find or create queueing table'));
564 $queue = CRM_Queue_Service
::singleton()->create(array(
565 'name' => self
::QUEUE_NAME
,
570 $task = new CRM_Queue_Task(
571 array('CRM_Upgrade_Form', 'doFileCleanup'),
572 array($postUpgradeMessageFile),
575 $queue->createItem($task);
577 $revisions = $upgrade->getRevisionSequence();
578 foreach ($revisions as $rev) {
579 // proceed only if $currentVer < $rev
580 if (version_compare($currentVer, $rev) < 0) {
581 $beginTask = new CRM_Queue_Task(
583 array('CRM_Upgrade_Form', 'doIncrementalUpgradeStart'),
586 "Begin Upgrade to $rev"
588 $queue->createItem($beginTask);
590 $task = new CRM_Queue_Task(
592 array('CRM_Upgrade_Form', 'doIncrementalUpgradeStep'),
594 array($rev, $currentVer, $latestVer, $postUpgradeMessageFile),
597 $queue->createItem($task);
599 $task = new CRM_Queue_Task(
601 array('CRM_Upgrade_Form', 'doIncrementalUpgradeFinish'),
603 array($rev, $currentVer, $latestVer, $postUpgradeMessageFile),
604 "Finish Upgrade DB to $rev"
606 $queue->createItem($task);
614 * Find any old, orphaned files that should have been deleted.
616 * These files can get left behind, eg, if you use the Joomla
619 * The earlier we can do this, the better - don't want upgrade logic
620 * to inadvertently rely on old/relocated files.
622 * @param \CRM_Queue_TaskContext $ctx
623 * @param string $postUpgradeMessageFile
626 public static function doFileCleanup(CRM_Queue_TaskContext
$ctx, $postUpgradeMessageFile) {
627 $source = new CRM_Utils_Check_Component_Source();
628 $files = $source->findOrphanedFiles();
630 foreach ($files as $file) {
631 if (is_dir($file['path'])) {
632 @rmdir
($file['path']);
635 @unlink
($file['path']);
638 if (file_exists($file['path'])) {
639 $errors[] = sprintf("<li>%s</li>", htmlentities($file['path']));
643 if (!empty($errors)) {
644 file_put_contents($postUpgradeMessageFile,
645 '<br/><br/>' . ts('Some old files could not be removed. Please remove them.')
646 . '<ul>' . implode("\n", $errors) . '</ul>',
655 * Perform an incremental version update.
657 * @param CRM_Queue_TaskContext $ctx
659 * the target (intermediate) revision e.g '3.2.alpha1'.
663 public static function doIncrementalUpgradeStart(CRM_Queue_TaskContext
$ctx, $rev) {
664 $upgrade = new CRM_Upgrade_Form();
666 // as soon as we start doing anything we append ".upgrade" to version.
667 // this also helps detect any partial upgrade issues
668 $upgrade->setVersion($rev . '.upgrade');
674 * Perform an incremental version update.
676 * @param CRM_Queue_TaskContext $ctx
678 * the target (intermediate) revision e.g '3.2.alpha1'.
679 * @param string $originalVer
680 * the original revision.
681 * @param string $latestVer
682 * the target (final) revision.
683 * @param string $postUpgradeMessageFile
684 * path of a modifiable file which lists the post-upgrade messages.
688 public static function doIncrementalUpgradeStep(CRM_Queue_TaskContext
$ctx, $rev, $originalVer, $latestVer, $postUpgradeMessageFile) {
689 $upgrade = new CRM_Upgrade_Form();
691 $phpFunctionName = 'upgrade_' . str_replace('.', '_', $rev);
693 $versionObject = $upgrade->incrementalPhpObject($rev);
695 // pre-db check for major release.
696 if ($upgrade->checkVersionRelease($rev, 'alpha1')) {
697 if (!(is_callable(array($versionObject, 'verifyPreDBstate')))) {
698 CRM_Core_Error
::fatal("verifyPreDBstate method was not found for $rev");
702 if (!($versionObject->verifyPreDBstate($error))) {
703 if (!isset($error)) {
704 $error = "post-condition failed for current upgrade for $rev";
706 CRM_Core_Error
::fatal($error);
711 $upgrade->setSchemaStructureTables($rev);
713 if (is_callable(array($versionObject, $phpFunctionName))) {
714 $versionObject->$phpFunctionName($rev, $originalVer, $latestVer);
717 $upgrade->processSQL($rev);
720 // set post-upgrade-message if any
721 if (is_callable(array($versionObject, 'setPostUpgradeMessage'))) {
722 $postUpgradeMessage = file_get_contents($postUpgradeMessageFile);
723 $versionObject->setPostUpgradeMessage($postUpgradeMessage, $rev);
724 file_put_contents($postUpgradeMessageFile, $postUpgradeMessage);
731 * Perform an incremental version update.
733 * @param CRM_Queue_TaskContext $ctx
735 * the target (intermediate) revision e.g '3.2.alpha1'.
736 * @param string $currentVer
737 * the original revision.
738 * @param string $latestVer
739 * the target (final) revision.
740 * @param string $postUpgradeMessageFile
741 * path of a modifiable file which lists the post-upgrade messages.
745 public static function doIncrementalUpgradeFinish(CRM_Queue_TaskContext
$ctx, $rev, $currentVer, $latestVer, $postUpgradeMessageFile) {
746 $upgrade = new CRM_Upgrade_Form();
747 $upgrade->setVersion($rev);
748 CRM_Utils_System
::flushCache();
750 $config = CRM_Core_Config
::singleton();
751 $config->userSystem
->flush();
755 public static function doFinish() {
756 $upgrade = new CRM_Upgrade_Form();
757 list($ignore, $latestVer) = $upgrade->getUpgradeVersions();
758 // Seems extraneous in context, but we'll preserve old behavior
759 $upgrade->setVersion($latestVer);
761 // Clear cached metadata.
762 Civi
::service('settings_manager')->flush();
764 // cleanup caches CRM-8739
765 $config = CRM_Core_Config
::singleton();
766 $config->cleanupCaches(1);
768 // Rebuild all triggers and re-enable logging if needed
769 $logging = new CRM_Logging_Schema();
770 $logging->fixSchemaDifferences();
774 * Compute any messages which should be displayed before upgrade
775 * by calling the 'setPreUpgradeMessage' on each incremental upgrade
778 * @param string $preUpgradeMessage
783 public function setPreUpgradeMessage(&$preUpgradeMessage, $currentVer, $latestVer) {
784 // check for changed message templates
785 CRM_Upgrade_Incremental_General
::checkMessageTemplate($preUpgradeMessage, $latestVer, $currentVer);
786 // set global messages
787 CRM_Upgrade_Incremental_General
::setPreUpgradeMessage($preUpgradeMessage, $currentVer, $latestVer);
789 // Scan through all php files and see if any file is interested in setting pre-upgrade-message
790 // based on $currentVer, $latestVer.
791 // Please note, at this point upgrade hasn't started executing queries.
792 $revisions = $this->getRevisionSequence();
793 foreach ($revisions as $rev) {
794 if (version_compare($currentVer, $rev) < 0) {
795 $versionObject = $this->incrementalPhpObject($rev);
796 if (is_callable(array($versionObject, 'setPreUpgradeMessage'))) {
797 $versionObject->setPreUpgradeMessage($preUpgradeMessage, $rev, $currentVer);