3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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;
47 // note latestVersion is legacy code, and
48 // only used for 2.0 -> 2.1 upgrade
49 public $latestVersion;
52 * Upgrade for multilingual
57 public $multilingual = FALSE;
60 * locales available for multilingual upgrade
68 * number to string mapper
73 static $_numberMap = array(
86 function __construct($state = NULL,
87 $action = CRM_Core_Action
::NONE
,
91 $this->_config
= CRM_Core_Config
::singleton();
93 // this->latestVersion is legacy code, only used for 2.0 -> 2.1 upgrade
94 // latest ver in 2.1 series
95 $this->latestVersion
= '2.1.6';
97 $domain = new CRM_Core_DAO_Domain();
100 $this->multilingual
= (bool) $domain->locales
;
101 $this->locales
= explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $domain->locales
);
103 $smarty = CRM_Core_Smarty
::singleton();
104 $smarty->compile_dir
= $this->_config
->templateCompileDir
;
105 $smarty->assign('multilingual', $this->multilingual
);
106 $smarty->assign('locales', $this->locales
);
108 // we didn't call CRM_Core_BAO_ConfigSetting::retrieve(), so we need to set $dbLocale by hand
109 if ($this->multilingual
) {
111 $dbLocale = "_{$this->_config->lcMessages}";
114 parent
::__construct($state, $action, $method, $name);
117 static function &incrementalPhpObject($version) {
118 static $incrementalPhpObject = array();
120 $versionParts = explode('.', $version);
121 $versionName = self
::$_numberMap[$versionParts[0]] . self
::$_numberMap[$versionParts[1]];
123 if (!array_key_exists($versionName, $incrementalPhpObject)) {
124 eval("\$incrementalPhpObject['$versionName'] = new CRM_Upgrade_Incremental_php_{$versionName};");
126 return $incrementalPhpObject[$versionName];
129 function checkVersionRelease($version, $release) {
130 $versionParts = explode('.', $version);
131 if ($versionParts[2] == $release) {
137 function checkSQLConstraints(&$constraints) {
139 foreach ($constraints as $constraint) {
140 if ($this->checkSQLConstraint($constraint)) {
146 return array($pass, $fail);
150 function checkSQLConstraint($constraint) {
151 // check constraint here
155 function source($fileName, $isQueryString = FALSE) {
157 CRM_Utils_File
::sourceSQLFile($this->_config
->dsn
,
158 $fileName, NULL, $isQueryString
162 function preProcess() {
163 CRM_Utils_System
::setTitle($this->getTitle());
164 if (!$this->verifyPreDBState($errorMessage)) {
165 if (!isset($errorMessage)) {
166 $errorMessage = 'pre-condition failed for current upgrade step';
168 CRM_Core_Error
::fatal($errorMessage);
170 $this->assign('recentlyViewed', FALSE);
173 function buildQuickForm() {
174 $this->addDefaultButtons($this->getButtonTitle(),
181 function getTitle() {
182 return ts('Title not Set');
185 function getFieldsetTitle() {
189 function getButtonTitle() {
190 return ts('Continue');
193 function getTemplateFileName() {
194 $this->assign('title',
195 $this->getFieldsetTitle()
197 $this->assign('message',
198 $this->getTemplateMessage()
200 return 'CRM/Upgrade/Base.tpl';
203 function postProcess() {
206 if (!$this->verifyPostDBState($errorMessage)) {
207 if (!isset($errorMessage)) {
208 $errorMessage = 'post-condition failed for current upgrade step';
210 CRM_Core_Error
::fatal($errorMessage);
214 function runQuery($query) {
215 return CRM_Core_DAO
::executeQuery($query,
216 CRM_Core_DAO
::$_nullArray
220 function setVersion($version) {
221 $this->logVersion($version);
224 UPDATE civicrm_domain
225 SET version = '$version'
227 return $this->runQuery($query);
230 function logVersion($newVersion) {
232 $oldVersion = CRM_Core_BAO_Domain
::version();
234 $session = CRM_Core_Session
::singleton();
236 'entity_table' => 'civicrm_domain',
238 'data' => "upgrade:{$oldVersion}->{$newVersion}",
239 // lets skip 'modified_id' for now, as it causes FK issues And
240 // is not very important for now.
241 'modified_date' => date('YmdHis'),
243 CRM_Core_BAO_Log
::add($logParams);
250 function checkVersion($version) {
251 $domainID = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Domain',
255 return $domainID ?
TRUE : FALSE;
258 function getRevisionSequence() {
260 $sqlDir = implode(DIRECTORY_SEPARATOR
,
261 array(dirname(__FILE__
), 'Incremental', 'sql')
263 $sqlFiles = scandir($sqlDir);
265 $sqlFilePattern = '/^((\d{1,2}\.\d{1,2})\.(\d{1,2}\.)?(\d{1,2}|\w{4,7}))\.(my)?sql(\.tpl)?$/i';
266 foreach ($sqlFiles as $file) {
267 if (preg_match($sqlFilePattern, $file, $matches)) {
268 if ($matches[2] == '4.0') {
269 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."));
271 if (!in_array($matches[1], $revList)) {
272 $revList[] = $matches[1];
279 '2.1.0', '2.2.beta2', '2.2.beta1', '2.2.alpha1', */
281 /* '2.2.alpha3', '2.2.0', '2.2.2', '2.1.alpha1', '2.1.3'); */
284 usort($revList, 'version_compare');
288 static function getRevisionPart($rev, $index = 1) {
289 $revPattern = '/^((\d{1,2})\.\d{1,2})\.(\d{1,2}|\w{4,7})?$/i';
290 preg_match($revPattern, $rev, $matches);
292 return array_key_exists($index, $matches) ?
$matches[$index] : NULL;
295 function processLocales($tplFile, $rev) {
296 $smarty = CRM_Core_Smarty
::singleton();
297 $smarty->assign('domainID', CRM_Core_Config
::domainID());
299 $this->source($smarty->fetch($tplFile), TRUE);
301 if ($this->multilingual
) {
302 CRM_Core_I18n_Schema
::rebuildMultilingualSchema($this->locales
, $rev);
304 return $this->multilingual
;
307 function setSchemaStructureTables($rev) {
308 if ($this->multilingual
) {
309 CRM_Core_I18n_Schema
::schemaStructureTables($rev, TRUE);
313 function processSQL($rev) {
314 $sqlFile = implode(DIRECTORY_SEPARATOR
,
316 dirname(__FILE__
), 'Incremental',
317 'sql', $rev . '.mysql',
320 $tplFile = "$sqlFile.tpl";
322 if (file_exists($tplFile)) {
323 $this->processLocales($tplFile, $rev);
326 if (!file_exists($sqlFile)) {
327 CRM_Core_Error
::fatal("sqlfile - $rev.mysql not found.");
329 $this->source($sqlFile);
334 * Determine the start and end version of the upgrade process
336 * @return array(0=>$currentVer, 1=>$latestVer)
338 function getUpgradeVersions() {
339 $latestVer = CRM_Utils_System
::version();
340 $currentVer = CRM_Core_BAO_Domain
::version(true);
342 CRM_Core_Error
::fatal(ts('Version information missing in civicrm database.'));
344 elseif (stripos($currentVer, 'upgrade')) {
345 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.'));
348 CRM_Core_Error
::fatal(ts('Version information missing in civicrm codebase.'));
351 // hack to make past ver compatible /w new incremental upgrade process
354 '2.2' => '2.2.alpha1',
355 '2.2.alph' => '2.2.alpha3',
356 // since 3.1.1 had domain.version set as 3.1.0
359 if (isset($convertVer[$currentVer])) {
360 $currentVer = $convertVer[$currentVer];
363 return array($currentVer, $latestVer);
367 * Determine if $currentVer can be upgraded to $latestVer
369 * @return mixed, a string error message or boolean 'false' if OK
371 function checkUpgradeableVersion($currentVer, $latestVer) {
373 // since version is suppose to be in valid format at this point, especially after conversion ($convertVer),
374 // lets do a pattern check -
375 if (!CRM_Utils_System
::isVersionFormatValid($currentVer)) {
376 $error = ts('Database is marked with invalid version format. You may want to investigate this before you proceed further.');
378 elseif (version_compare($currentVer, $latestVer) > 0) {
379 // DB version number is higher than codebase being upgraded to. This is unexpected condition-fatal error.
380 $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.',
381 array(1 => $currentVer, 2 => $latestVer, 3 => $dbToolsLink)
384 elseif (version_compare($currentVer, $latestVer) == 0) {
385 $error = ts('Your database has already been upgraded to CiviCRM %1',
386 array(1 => $latestVer)
390 $phpVersion = phpversion();
391 $minPhpVersion = '5.3.3';
392 if (version_compare($phpVersion, $minPhpVersion) < 0) {
393 $error = ts('CiviCRM %3 requires PHP version %1 (or newer), but the current system uses %2 ',
401 // check for mysql trigger privileges
402 if (!CRM_Core_DAO
::checkTriggerViewPermission(FALSE, TRUE)) {
403 $error = ts('CiviCRM %1 requires MySQL trigger privileges.',
404 array(1 => $latestVer));
407 if (CRM_Core_DAO
::getGlobalSetting('thread_stack', 0) < (1024*self
::MINIMUM_THREAD_STACK
)) {
408 $error = ts('CiviCRM %1 requires MySQL thread stack >= %2k', array(
410 2 => self
::MINIMUM_THREAD_STACK
,
418 * Determine if $currentver already matches $latestVer
420 * @return mixed, a string error message or boolean 'false' if OK
422 function checkCurrentVersion($currentVer, $latestVer) {
425 // since version is suppose to be in valid format at this point, especially after conversion ($convertVer),
426 // lets do a pattern check -
427 if (!CRM_Utils_System
::isVersionFormatValid($currentVer)) {
428 $error = ts('Database is marked with invalid version format. You may want to investigate this before you proceed further.');
430 elseif (version_compare($currentVer, $latestVer) != 0) {
431 $error = ts('Your database is not configured for version %1',
432 array(1 => $latestVer)
439 * Fill the queue with upgrade tasks
441 * @param $currentVer string, the original revision
442 * @param $latestVer string, the target (final) revision
443 * @param $postUpgradeMessageFile string, path of a modifiable file which lists the post-upgrade messages
447 static function buildQueue($currentVer, $latestVer, $postUpgradeMessageFile) {
448 $upgrade = new CRM_Upgrade_Form();
450 // hack to make 4.0.x (D7,J1.6) codebase go through 3.4.x (d6, J1.5) upgrade files,
451 // since schema wise they are same
452 if (CRM_Upgrade_Form
::getRevisionPart($currentVer) == '4.0') {
453 $currentVer = str_replace('4.0.', '3.4.', $currentVer);
456 // Ensure that queue can be created
457 if (!CRM_Queue_BAO_QueueItem
::findCreateTable()) {
458 CRM_Core_Error
::fatal(ts('Failed to find or create queueing table'));
460 $queue = CRM_Queue_Service
::singleton()->create(array(
461 'name' => self
::QUEUE_NAME
,
466 $revisions = $upgrade->getRevisionSequence();
467 foreach ($revisions as $rev) {
468 // proceed only if $currentVer < $rev
469 if (version_compare($currentVer, $rev) < 0) {
470 $beginTask = new CRM_Queue_Task(
472 array('CRM_Upgrade_Form', 'doIncrementalUpgradeStart'),
475 "Begin Upgrade to $rev"
477 $queue->createItem($beginTask);
479 $task = new CRM_Queue_Task(
481 array('CRM_Upgrade_Form', 'doIncrementalUpgradeStep'),
483 array($rev, $currentVer, $latestVer, $postUpgradeMessageFile),
486 $queue->createItem($task);
488 $task = new CRM_Queue_Task(
490 array('CRM_Upgrade_Form', 'doIncrementalUpgradeFinish'),
493 "Finish Upgrade DB to $rev"
495 $queue->createItem($task);
503 * Perform an incremental version update
505 * @param $rev string, the target (intermediate) revision e.g '3.2.alpha1'
506 * @param $currentVer string, the original revision
507 * @param $latestVer string, the target (final) revision
508 * @param $postUpgradeMessageFile string, path of a modifiable file which lists the post-upgrade messages
510 static function doIncrementalUpgradeStart(CRM_Queue_TaskContext
$ctx, $rev) {
511 $upgrade = new CRM_Upgrade_Form();
513 // as soon as we start doing anything we append ".upgrade" to version.
514 // this also helps detect any partial upgrade issues
515 $upgrade->setVersion($rev . '.upgrade');
521 * Perform an incremental version update
523 * @param $rev string, the target (intermediate) revision e.g '3.2.alpha1'
524 * @param $currentVer string, the original revision
525 * @param $latestVer string, the target (final) revision
526 * @param $postUpgradeMessageFile string, path of a modifiable file which lists the post-upgrade messages
528 static function doIncrementalUpgradeStep(CRM_Queue_TaskContext
$ctx, $rev, $currentVer, $latestVer, $postUpgradeMessageFile) {
529 $upgrade = new CRM_Upgrade_Form();
531 $phpFunctionName = 'upgrade_' . str_replace('.', '_', $rev);
533 // follow old upgrade process for all version
535 if (version_compare($rev, '3.2.alpha1') < 0) {
536 if (is_callable(array(
537 'CRM_Upgrade_Incremental_Legacy', $phpFunctionName))) {
538 call_user_func(array('CRM_Upgrade_Incremental_Legacy', $phpFunctionName), $rev);
541 $upgrade->processSQL($rev);
545 // new upgrade process from version
547 $versionObject = $upgrade->incrementalPhpObject($rev);
549 // pre-db check for major release.
550 if ($upgrade->checkVersionRelease($rev, 'alpha1')) {
551 if (!(is_callable(array(
552 $versionObject, 'verifyPreDBstate')))) {
553 CRM_Core_Error
::fatal("verifyPreDBstate method was not found for $rev");
557 if (!($versionObject->verifyPreDBstate($error))) {
558 if (!isset($error)) {
559 $error = "post-condition failed for current upgrade for $rev";
561 CRM_Core_Error
::fatal($error);
566 $upgrade->setSchemaStructureTables($rev);
568 if (is_callable(array(
569 $versionObject, $phpFunctionName))) {
570 $versionObject->$phpFunctionName($rev);
573 $upgrade->processSQL($rev);
576 // set post-upgrade-message if any
577 if (is_callable(array(
578 $versionObject, 'setPostUpgradeMessage'))) {
579 $postUpgradeMessage = file_get_contents($postUpgradeMessageFile);
580 $versionObject->setPostUpgradeMessage($postUpgradeMessage, $rev);
581 file_put_contents($postUpgradeMessageFile, $postUpgradeMessage);
583 $postUpgradeMessage = file_get_contents($postUpgradeMessageFile);
584 CRM_Upgrade_Incremental_Legacy
::setPostUpgradeMessage($postUpgradeMessage, $rev);
585 file_put_contents($postUpgradeMessageFile, $postUpgradeMessage);
593 * Perform an incremental version update
595 * @param $rev string, the target (intermediate) revision e.g '3.2.alpha1'
596 * @param $currentVer string, the original revision
597 * @param $latestVer string, the target (final) revision
598 * @param $postUpgradeMessageFile string, path of a modifiable file which lists the post-upgrade messages
600 static function doIncrementalUpgradeFinish(CRM_Queue_TaskContext
$ctx, $rev) {
601 $upgrade = new CRM_Upgrade_Form();
602 $upgrade->setVersion($rev);
603 CRM_Utils_System
::flushCache();
605 $config = CRM_Core_Config
::singleton();
606 $config->userSystem
->flush();
608 if (version_compare($currentVer, '4.1.alpha1') >= 0) {
609 CRM_Core_BAO_Setting
::updateSettingsFromMetaData();
614 static function doFinish() {
615 $upgrade = new CRM_Upgrade_Form();
616 list($ignore, $latestVer) = $upgrade->getUpgradeVersions();
617 // Seems extraneous in context, but we'll preserve old behavior
618 $upgrade->setVersion($latestVer);
620 // lets rebuild the config array in case we've made a few changes in the
622 // this also helps us always store the latest version of civi in the DB
624 CRM_Core_BAO_ConfigSetting
::add($params);
626 // cleanup caches CRM-8739
627 $config = CRM_Core_Config
::singleton();
628 $config->cleanupCaches(1, FALSE);
630 // Rebuild all triggers and re-enable logging if needed
631 $logging = new CRM_Logging_Schema();
632 $logging->fixSchemaDifferences();
636 * Compute any messages which should be displayed before upgrade
637 * by calling the 'setPreUpgradeMessage' on each incremental upgrade
640 * @param $preUpgradeMessage string, alterable
642 function setPreUpgradeMessage(&$preUpgradeMessage, $currentVer, $latestVer) {
643 CRM_Upgrade_Incremental_Legacy
::setPreUpgradeMessage($preUpgradeMessage, $currentVer, $latestVer);
645 // Scan through all php files and see if any file is interested in setting pre-upgrade-message
646 // based on $currentVer, $latestVer.
647 // Please note, at this point upgrade hasn't started executing queries.
648 $revisions = $this->getRevisionSequence();
649 foreach ($revisions as $rev) {
650 if (version_compare($currentVer, $rev) < 0 &&
651 version_compare($rev, '3.2.alpha1') > 0
653 $versionObject = $this->incrementalPhpObject($rev);
654 if (is_callable(array(
655 $versionObject, 'setPreUpgradeMessage'))) {
656 $versionObject->setPreUpgradeMessage($preUpgradeMessage, $rev, $currentVer);