3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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.3';
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 $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 * @return \CRM_Core_Form
106 * @param null|object $state
107 * @param const|enum|int $action
108 * @param string $method
109 * @param null|string $name
111 public function __construct(
113 $action = CRM_Core_Action
::NONE
,
117 $this->_config
= CRM_Core_Config
::singleton();
119 $domain = new CRM_Core_DAO_Domain();
122 $this->multilingual
= (bool) $domain->locales
;
123 $this->locales
= explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $domain->locales
);
125 $smarty = CRM_Core_Smarty
::singleton();
126 $smarty->compile_dir
= $this->_config
->templateCompileDir
;
127 $smarty->assign('multilingual', $this->multilingual
);
128 $smarty->assign('locales', $this->locales
);
130 // we didn't call CRM_Core_BAO_ConfigSetting::retrieve(), so we need to set $dbLocale by hand
131 if ($this->multilingual
) {
133 $dbLocale = "_{$this->_config->lcMessages}";
136 parent
::__construct($state, $action, $method, $name);
144 public static function &incrementalPhpObject($version) {
145 static $incrementalPhpObject = array();
147 $versionParts = explode('.', $version);
148 $versionName = self
::$_numberMap[$versionParts[0]] . self
::$_numberMap[$versionParts[1]];
150 if (!array_key_exists($versionName, $incrementalPhpObject)) {
151 $className = "CRM_Upgrade_Incremental_php_{$versionName}";
152 $incrementalPhpObject[$versionName] = new $className();
154 return $incrementalPhpObject[$versionName];
163 public function checkVersionRelease($version, $release) {
164 $versionParts = explode('.', $version);
165 return ($versionParts[2] == $release);
169 * @param $constraints
173 public function checkSQLConstraints(&$constraints) {
175 foreach ($constraints as $constraint) {
176 if ($this->checkSQLConstraint($constraint)) {
182 return array($pass, $fail);
191 public function checkSQLConstraint($constraint) {
192 // check constraint here
197 * @param string $fileName
198 * @param bool $isQueryString
200 public function source($fileName, $isQueryString = FALSE) {
202 CRM_Utils_File
::sourceSQLFile($this->_config
->dsn
,
203 $fileName, NULL, $isQueryString
207 public function preProcess() {
208 CRM_Utils_System
::setTitle($this->getTitle());
209 if (!$this->verifyPreDBState($errorMessage)) {
210 if (!isset($errorMessage)) {
211 $errorMessage = 'pre-condition failed for current upgrade step';
213 CRM_Core_Error
::fatal($errorMessage);
215 $this->assign('recentlyViewed', FALSE);
218 public function buildQuickForm() {
219 $this->addDefaultButtons($this->getButtonTitle(),
227 * Getter function for title. Should be over-ridden by derived class
234 public function getTitle() {
235 return ts('Title not Set');
241 public function getFieldsetTitle() {
248 public function getButtonTitle() {
249 return ts('Continue');
253 * Use the form name to create the tpl file name.
260 public function getTemplateFileName() {
261 $this->assign('title',
262 $this->getFieldsetTitle()
264 $this->assign('message',
265 $this->getTemplateMessage()
267 return 'CRM/Upgrade/Base.tpl';
270 public function postProcess() {
273 if (!$this->verifyPostDBState($errorMessage)) {
274 if (!isset($errorMessage)) {
275 $errorMessage = 'post-condition failed for current upgrade step';
277 CRM_Core_Error
::fatal($errorMessage);
286 public function runQuery($query) {
287 return CRM_Core_DAO
::executeQuery($query,
288 CRM_Core_DAO
::$_nullArray
297 public function setVersion($version) {
298 $this->logVersion($version);
301 UPDATE civicrm_domain
302 SET version = '$version'
304 return $this->runQuery($query);
312 public function logVersion($newVersion) {
314 $oldVersion = CRM_Core_BAO_Domain
::version();
316 $session = CRM_Core_Session
::singleton();
318 'entity_table' => 'civicrm_domain',
320 'data' => "upgrade:{$oldVersion}->{$newVersion}",
321 // lets skip 'modified_id' for now, as it causes FK issues And
322 // is not very important for now.
323 'modified_date' => date('YmdHis'),
325 CRM_Core_BAO_Log
::add($logParams);
337 public function checkVersion($version) {
338 $domainID = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Domain',
342 return $domainID ?
TRUE : FALSE;
349 public function getRevisionSequence() {
351 $sqlDir = implode(DIRECTORY_SEPARATOR
,
352 array(dirname(__FILE__
), 'Incremental', 'sql')
354 $sqlFiles = scandir($sqlDir);
356 $sqlFilePattern = '/^((\d{1,2}\.\d{1,2})\.(\d{1,2}\.)?(\d{1,2}|\w{4,7}))\.(my)?sql(\.tpl)?$/i';
357 foreach ($sqlFiles as $file) {
358 if (preg_match($sqlFilePattern, $file, $matches)) {
359 if ($matches[2] == '4.0') {
360 CRM_Core_Error
::fatal(ts("4.0.x upgrade files shouldn't exist. Contact Lobo to discuss this. This is related to the issue CRM-7731."));
362 if (!in_array($matches[1], $revList)) {
363 $revList[] = $matches[1];
368 usort($revList, 'version_compare');
378 public static function getRevisionPart($rev, $index = 1) {
379 $revPattern = '/^((\d{1,2})\.\d{1,2})\.(\d{1,2}|\w{4,7})?$/i';
380 preg_match($revPattern, $rev, $matches);
382 return array_key_exists($index, $matches) ?
$matches[$index] : NULL;
391 public function processLocales($tplFile, $rev) {
392 $smarty = CRM_Core_Smarty
::singleton();
393 $smarty->assign('domainID', CRM_Core_Config
::domainID());
395 $this->source($smarty->fetch($tplFile), TRUE);
397 if ($this->multilingual
) {
398 CRM_Core_I18n_Schema
::rebuildMultilingualSchema($this->locales
, $rev);
400 return $this->multilingual
;
406 public function setSchemaStructureTables($rev) {
407 if ($this->multilingual
) {
408 CRM_Core_I18n_Schema
::schemaStructureTables($rev, TRUE);
417 public function processSQL($rev) {
418 $sqlFile = implode(DIRECTORY_SEPARATOR
,
426 $tplFile = "$sqlFile.tpl";
428 if (file_exists($tplFile)) {
429 $this->processLocales($tplFile, $rev);
432 if (!file_exists($sqlFile)) {
433 CRM_Core_Error
::fatal("sqlfile - $rev.mysql not found.");
435 $this->source($sqlFile);
440 * Determine the start and end version of the upgrade process.
442 * @return array(0=>$currentVer, 1=>$latestVer)
444 public function getUpgradeVersions() {
445 $latestVer = CRM_Utils_System
::version();
446 $currentVer = CRM_Core_BAO_Domain
::version(TRUE);
448 CRM_Core_Error
::fatal(ts('Version information missing in civicrm database.'));
450 elseif (stripos($currentVer, 'upgrade')) {
451 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.'));
454 CRM_Core_Error
::fatal(ts('Version information missing in civicrm codebase.'));
457 return array($currentVer, $latestVer);
461 * Determine if $currentVer can be upgraded to $latestVer
466 * @return mixed, a string error message or boolean 'false' if OK
468 public function checkUpgradeableVersion($currentVer, $latestVer) {
470 // since version is suppose to be in valid format at this point, especially after conversion ($convertVer),
471 // lets do a pattern check -
472 if (!CRM_Utils_System
::isVersionFormatValid($currentVer)) {
473 $error = ts('Database is marked with invalid version format. You may want to investigate this before you proceed further.');
475 elseif (version_compare($currentVer, $latestVer) > 0) {
476 // DB version number is higher than codebase being upgraded to. This is unexpected condition-fatal error.
477 $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.',
478 array(1 => $currentVer, 2 => $latestVer)
481 elseif (version_compare($currentVer, $latestVer) == 0) {
482 $error = ts('Your database has already been upgraded to CiviCRM %1',
483 array(1 => $latestVer)
486 elseif (version_compare($currentVer, self
::MINIMUM_UPGRADABLE_VERSION
) < 0) {
487 $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.',
488 array(1 => self
::MINIMUM_UPGRADABLE_VERSION
, 2 => $latestVer)
492 if (version_compare(phpversion(), self
::MINIMUM_PHP_VERSION
) < 0) {
493 $error = ts('CiviCRM %3 requires PHP version %1 (or newer), but the current system uses %2 ',
495 1 => self
::MINIMUM_PHP_VERSION
,
501 // check for mysql trigger privileges
502 if (!CRM_Core_DAO
::checkTriggerViewPermission(FALSE, TRUE)) {
503 $error = ts('CiviCRM %1 requires MySQL trigger privileges.',
504 array(1 => $latestVer));
507 if (CRM_Core_DAO
::getGlobalSetting('thread_stack', 0) < (1024 * self
::MINIMUM_THREAD_STACK
)) {
508 $error = ts('CiviCRM %1 requires MySQL thread stack >= %2k', array(
510 2 => self
::MINIMUM_THREAD_STACK
,
518 * Determine if $currentver already matches $latestVer
523 * @return mixed, a string error message or boolean 'false' if OK
525 public function checkCurrentVersion($currentVer, $latestVer) {
528 // since version is suppose to be in valid format at this point, especially after conversion ($convertVer),
529 // lets do a pattern check -
530 if (!CRM_Utils_System
::isVersionFormatValid($currentVer)) {
531 $error = ts('Database is marked with invalid version format. You may want to investigate this before you proceed further.');
533 elseif (version_compare($currentVer, $latestVer) != 0) {
534 $error = ts('Your database is not configured for version %1',
535 array(1 => $latestVer)
542 * Fill the queue with upgrade tasks.
544 * @param string $currentVer
545 * the original revision.
546 * @param string $latestVer
547 * the target (final) revision.
548 * @param string $postUpgradeMessageFile
549 * path of a modifiable file which lists the post-upgrade messages.
551 * @return CRM_Queue_Service
553 public static function buildQueue($currentVer, $latestVer, $postUpgradeMessageFile) {
554 $upgrade = new CRM_Upgrade_Form();
556 // hack to make 4.0.x (D7,J1.6) codebase go through 3.4.x (d6, J1.5) upgrade files,
557 // since schema wise they are same
558 if (CRM_Upgrade_Form
::getRevisionPart($currentVer) == '4.0') {
559 $currentVer = str_replace('4.0.', '3.4.', $currentVer);
562 // Ensure that queue can be created
563 if (!CRM_Queue_BAO_QueueItem
::findCreateTable()) {
564 CRM_Core_Error
::fatal(ts('Failed to find or create queueing table'));
566 $queue = CRM_Queue_Service
::singleton()->create(array(
567 'name' => self
::QUEUE_NAME
,
572 $revisions = $upgrade->getRevisionSequence();
573 foreach ($revisions as $rev) {
574 // proceed only if $currentVer < $rev
575 if (version_compare($currentVer, $rev) < 0) {
576 $beginTask = new CRM_Queue_Task(
578 array('CRM_Upgrade_Form', 'doIncrementalUpgradeStart'),
581 "Begin Upgrade to $rev"
583 $queue->createItem($beginTask);
585 $task = new CRM_Queue_Task(
587 array('CRM_Upgrade_Form', 'doIncrementalUpgradeStep'),
589 array($rev, $currentVer, $latestVer, $postUpgradeMessageFile),
592 $queue->createItem($task);
594 $task = new CRM_Queue_Task(
596 array('CRM_Upgrade_Form', 'doIncrementalUpgradeFinish'),
598 array($rev, $currentVer, $latestVer, $postUpgradeMessageFile),
599 "Finish Upgrade DB to $rev"
601 $queue->createItem($task);
609 * Perform an incremental version update.
611 * @param CRM_Queue_TaskContext $ctx
613 * the target (intermediate) revision e.g '3.2.alpha1'.
617 public static function doIncrementalUpgradeStart(CRM_Queue_TaskContext
$ctx, $rev) {
618 $upgrade = new CRM_Upgrade_Form();
620 // as soon as we start doing anything we append ".upgrade" to version.
621 // this also helps detect any partial upgrade issues
622 $upgrade->setVersion($rev . '.upgrade');
628 * Perform an incremental version update.
630 * @param CRM_Queue_TaskContext $ctx
632 * the target (intermediate) revision e.g '3.2.alpha1'.
633 * @param string $originalVer
634 * the original revision.
635 * @param string $latestVer
636 * the target (final) revision.
637 * @param string $postUpgradeMessageFile
638 * path of a modifiable file which lists the post-upgrade messages.
642 public static function doIncrementalUpgradeStep(CRM_Queue_TaskContext
$ctx, $rev, $originalVer, $latestVer, $postUpgradeMessageFile) {
643 $upgrade = new CRM_Upgrade_Form();
645 $phpFunctionName = 'upgrade_' . str_replace('.', '_', $rev);
647 $versionObject = $upgrade->incrementalPhpObject($rev);
649 // pre-db check for major release.
650 if ($upgrade->checkVersionRelease($rev, 'alpha1')) {
651 if (!(is_callable(array(
656 CRM_Core_Error
::fatal("verifyPreDBstate method was not found for $rev");
660 if (!($versionObject->verifyPreDBstate($error))) {
661 if (!isset($error)) {
662 $error = "post-condition failed for current upgrade for $rev";
664 CRM_Core_Error
::fatal($error);
669 $upgrade->setSchemaStructureTables($rev);
671 if (is_callable(array(
675 $versionObject->$phpFunctionName($rev, $originalVer, $latestVer);
678 $upgrade->processSQL($rev);
681 // set post-upgrade-message if any
682 if (is_callable(array(
684 'setPostUpgradeMessage',
686 $postUpgradeMessage = file_get_contents($postUpgradeMessageFile);
687 $versionObject->setPostUpgradeMessage($postUpgradeMessage, $rev);
688 file_put_contents($postUpgradeMessageFile, $postUpgradeMessage);
695 * Perform an incremental version update.
697 * @param CRM_Queue_TaskContext $ctx
699 * the target (intermediate) revision e.g '3.2.alpha1'.
700 * @param string $currentVer
701 * the original revision.
702 * @param string $latestVer
703 * the target (final) revision.
704 * @param string $postUpgradeMessageFile
705 * path of a modifiable file which lists the post-upgrade messages.
709 public static function doIncrementalUpgradeFinish(CRM_Queue_TaskContext
$ctx, $rev, $currentVer, $latestVer, $postUpgradeMessageFile) {
710 $upgrade = new CRM_Upgrade_Form();
711 $upgrade->setVersion($rev);
712 CRM_Utils_System
::flushCache();
714 $config = CRM_Core_Config
::singleton();
715 $config->userSystem
->flush();
719 public static function doFinish() {
720 $upgrade = new CRM_Upgrade_Form();
721 list($ignore, $latestVer) = $upgrade->getUpgradeVersions();
722 // Seems extraneous in context, but we'll preserve old behavior
723 $upgrade->setVersion($latestVer);
725 // lets rebuild the config array in case we've made a few changes in the
727 // this also helps us always store the latest version of civi in the DB
729 CRM_Core_BAO_ConfigSetting
::add($params);
731 // CRM-12804 comment-51411 : add any missing settings
732 // at the end of upgrade
733 CRM_Core_BAO_Setting
::updateSettingsFromMetaData();
735 // cleanup caches CRM-8739
736 $config = CRM_Core_Config
::singleton();
737 $config->cleanupCaches(1);
739 // Rebuild all triggers and re-enable logging if needed
740 $logging = new CRM_Logging_Schema();
741 $logging->fixSchemaDifferences();
743 //CRM-16257 update Config.IDS.ini might be an old copy
744 CRM_Core_IDS
::createConfigFile(TRUE);
748 * Compute any messages which should be displayed before upgrade
749 * by calling the 'setPreUpgradeMessage' on each incremental upgrade
752 * @param string $preUpgradeMessage
757 public function setPreUpgradeMessage(&$preUpgradeMessage, $currentVer, $latestVer) {
758 // check for changed message templates
759 CRM_Upgrade_Incremental_General
::checkMessageTemplate($preUpgradeMessage, $latestVer, $currentVer);
760 // set global messages
761 CRM_Upgrade_Incremental_General
::setPreUpgradeMessage($preUpgradeMessage, $currentVer, $latestVer);
763 // Scan through all php files and see if any file is interested in setting pre-upgrade-message
764 // based on $currentVer, $latestVer.
765 // Please note, at this point upgrade hasn't started executing queries.
766 $revisions = $this->getRevisionSequence();
767 foreach ($revisions as $rev) {
768 if (version_compare($currentVer, $rev) < 0) {
769 $versionObject = $this->incrementalPhpObject($rev);
770 if (is_callable(array($versionObject, 'setPreUpgradeMessage'))) {
771 $versionObject->setPreUpgradeMessage($preUpgradeMessage, $rev, $currentVer);