Merge pull request #6397 from colemanw/CRM-14078
[civicrm-core.git] / CRM / Upgrade / Form.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Upgrade_Form extends CRM_Core_Form {
7da04cde 36 const QUEUE_NAME = 'CRM_Upgrade';
6a488035
TO
37
38 /**
39 * Minimum size of MySQL's thread_stack option
40 *
41 * @see install/index.php MINIMUM_THREAD_STACK
42 */
43 const MINIMUM_THREAD_STACK = 192;
44
304c1a5a
CW
45 /**
46 * Minimum previous CiviCRM version we can directly upgrade from
47 */
48 const MINIMUM_UPGRADABLE_VERSION = '4.0.8';
49
50 /**
51 * Minimum php version we support
52 */
53 const MINIMUM_PHP_VERSION = '5.3.3';
54
6a488035
TO
55 protected $_config;
56
6a488035 57 /**
fe482240 58 * Upgrade for multilingual.
6a488035
TO
59 *
60 * @var boolean
6a488035
TO
61 */
62 public $multilingual = FALSE;
63
64 /**
fe482240 65 * Locales available for multilingual upgrade.
6a488035
TO
66 *
67 * @var array
6a488035
TO
68 */
69 public $locales;
70
71 /**
fe482240 72 * Number to string mapper.
6a488035
TO
73 *
74 * @var array
6a488035
TO
75 */
76 static $_numberMap = array(
77 0 => 'Zero',
78 1 => 'One',
79 2 => 'Two',
80 3 => 'Three',
81 4 => 'Four',
82 5 => 'Five',
79e3ddee 83 6 => 'Six',
6a488035
TO
84 7 => 'Seven',
85 8 => 'Eight',
86 9 => 'Nine',
87 );
88
624e56fa 89 /**
fe482240 90 * Constructor for the basic form page.
624e56fa
EM
91 *
92 * We should not use QuickForm directly. This class provides a lot
93 * of default convenient functions, rules and buttons
94 *
c68f8bfa
TO
95 * @param object $state
96 * State associated with this form.
624e56fa 97 * @param \const|\enum $action The mode the form is operating in (None/Create/View/Update/Delete)
c68f8bfa
TO
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.
624e56fa
EM
102 *
103 * @return \CRM_Core_Form
624e56fa
EM
104 */
105 /**
106 * @param null|object $state
107 * @param const|enum|int $action
108 * @param string $method
109 * @param null|string $name
110 */
ae5ffbb7 111 public function __construct(
4e66d748 112 $state = NULL,
6a488035
TO
113 $action = CRM_Core_Action::NONE,
114 $method = 'post',
e418776c 115 $name = NULL
6a488035
TO
116 ) {
117 $this->_config = CRM_Core_Config::singleton();
118
6a488035
TO
119 $domain = new CRM_Core_DAO_Domain();
120 $domain->find(TRUE);
121
122 $this->multilingual = (bool) $domain->locales;
123 $this->locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales);
124
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);
129
130 // we didn't call CRM_Core_BAO_ConfigSetting::retrieve(), so we need to set $dbLocale by hand
131 if ($this->multilingual) {
132 global $dbLocale;
133 $dbLocale = "_{$this->_config->lcMessages}";
134 }
135
136 parent::__construct($state, $action, $method, $name);
137 }
138
624e56fa
EM
139 /**
140 * @param $version
141 *
142 * @return mixed
143 */
00be9182 144 public static function &incrementalPhpObject($version) {
6a488035
TO
145 static $incrementalPhpObject = array();
146
147 $versionParts = explode('.', $version);
148 $versionName = self::$_numberMap[$versionParts[0]] . self::$_numberMap[$versionParts[1]];
149
150 if (!array_key_exists($versionName, $incrementalPhpObject)) {
0e6e8724 151 $className = "CRM_Upgrade_Incremental_php_{$versionName}";
e8cb3963 152 $incrementalPhpObject[$versionName] = new $className();
6a488035
TO
153 }
154 return $incrementalPhpObject[$versionName];
155 }
156
624e56fa
EM
157 /**
158 * @param $version
159 * @param $release
160 *
161 * @return bool
162 */
00be9182 163 public function checkVersionRelease($version, $release) {
6a488035 164 $versionParts = explode('.', $version);
bf6a5362 165 return ($versionParts[2] == $release);
6a488035
TO
166 }
167
624e56fa
EM
168 /**
169 * @param $constraints
170 *
171 * @return array
172 */
00be9182 173 public function checkSQLConstraints(&$constraints) {
6a488035
TO
174 $pass = $fail = 0;
175 foreach ($constraints as $constraint) {
176 if ($this->checkSQLConstraint($constraint)) {
177 $pass++;
178 }
179 else {
180 $fail++;
181 }
182 return array($pass, $fail);
183 }
184 }
185
624e56fa
EM
186 /**
187 * @param $constraint
188 *
189 * @return bool
190 */
00be9182 191 public function checkSQLConstraint($constraint) {
6a488035
TO
192 // check constraint here
193 return TRUE;
194 }
195
624e56fa 196 /**
100fef9d 197 * @param string $fileName
624e56fa
EM
198 * @param bool $isQueryString
199 */
00be9182 200 public function source($fileName, $isQueryString = FALSE) {
6a488035
TO
201
202 CRM_Utils_File::sourceSQLFile($this->_config->dsn,
203 $fileName, NULL, $isQueryString
204 );
205 }
206
00be9182 207 public function preProcess() {
6a488035
TO
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';
212 }
213 CRM_Core_Error::fatal($errorMessage);
214 }
215 $this->assign('recentlyViewed', FALSE);
216 }
217
00be9182 218 public function buildQuickForm() {
6a488035
TO
219 $this->addDefaultButtons($this->getButtonTitle(),
220 'next',
221 NULL,
222 TRUE
223 );
224 }
225
624e56fa 226 /**
100fef9d 227 * Getter function for title. Should be over-ridden by derived class
624e56fa
EM
228 *
229 * @return string
624e56fa
EM
230 */
231 /**
232 * @return string
233 */
00be9182 234 public function getTitle() {
6a488035
TO
235 return ts('Title not Set');
236 }
237
624e56fa
EM
238 /**
239 * @return string
240 */
00be9182 241 public function getFieldsetTitle() {
ba8f6a69 242 return '';
6a488035
TO
243 }
244
624e56fa
EM
245 /**
246 * @return string
247 */
00be9182 248 public function getButtonTitle() {
6a488035
TO
249 return ts('Continue');
250 }
251
624e56fa 252 /**
fe482240 253 * Use the form name to create the tpl file name.
624e56fa
EM
254 *
255 * @return string
624e56fa
EM
256 */
257 /**
258 * @return string
259 */
00be9182 260 public function getTemplateFileName() {
6a488035
TO
261 $this->assign('title',
262 $this->getFieldsetTitle()
263 );
264 $this->assign('message',
265 $this->getTemplateMessage()
266 );
267 return 'CRM/Upgrade/Base.tpl';
268 }
269
00be9182 270 public function postProcess() {
6a488035
TO
271 $this->upgrade();
272
273 if (!$this->verifyPostDBState($errorMessage)) {
274 if (!isset($errorMessage)) {
275 $errorMessage = 'post-condition failed for current upgrade step';
276 }
277 CRM_Core_Error::fatal($errorMessage);
278 }
279 }
280
624e56fa
EM
281 /**
282 * @param $query
283 *
284 * @return Object
285 */
00be9182 286 public function runQuery($query) {
6a488035
TO
287 return CRM_Core_DAO::executeQuery($query,
288 CRM_Core_DAO::$_nullArray
289 );
290 }
291
624e56fa
EM
292 /**
293 * @param $version
294 *
295 * @return Object
296 */
00be9182 297 public function setVersion($version) {
6a488035
TO
298 $this->logVersion($version);
299
300 $query = "
301UPDATE civicrm_domain
302SET version = '$version'
303";
304 return $this->runQuery($query);
305 }
306
624e56fa
EM
307 /**
308 * @param $newVersion
309 *
310 * @return bool
311 */
00be9182 312 public function logVersion($newVersion) {
6a488035
TO
313 if ($newVersion) {
314 $oldVersion = CRM_Core_BAO_Domain::version();
315
316 $session = CRM_Core_Session::singleton();
317 $logParams = array(
318 'entity_table' => 'civicrm_domain',
319 'entity_id' => 1,
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'),
324 );
325 CRM_Core_BAO_Log::add($logParams);
326 return TRUE;
327 }
328
329 return FALSE;
330 }
331
624e56fa
EM
332 /**
333 * @param $version
334 *
335 * @return bool
336 */
00be9182 337 public function checkVersion($version) {
6a488035
TO
338 $domainID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain',
339 $version, 'id',
340 'version'
341 );
342 return $domainID ? TRUE : FALSE;
343 }
344
624e56fa
EM
345 /**
346 * @return array
347 * @throws Exception
348 */
00be9182 349 public function getRevisionSequence() {
6a488035
TO
350 $revList = array();
351 $sqlDir = implode(DIRECTORY_SEPARATOR,
352 array(dirname(__FILE__), 'Incremental', 'sql')
353 );
354 $sqlFiles = scandir($sqlDir);
355
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."));
361 }
362 if (!in_array($matches[1], $revList)) {
363 $revList[] = $matches[1];
364 }
365 }
366 }
367
6a488035
TO
368 usort($revList, 'version_compare');
369 return $revList;
370 }
371
624e56fa
EM
372 /**
373 * @param $rev
374 * @param int $index
375 *
376 * @return null
377 */
00be9182 378 public static function getRevisionPart($rev, $index = 1) {
6a488035
TO
379 $revPattern = '/^((\d{1,2})\.\d{1,2})\.(\d{1,2}|\w{4,7})?$/i';
380 preg_match($revPattern, $rev, $matches);
381
382 return array_key_exists($index, $matches) ? $matches[$index] : NULL;
383 }
384
624e56fa
EM
385 /**
386 * @param $tplFile
387 * @param $rev
388 *
389 * @return bool
390 */
00be9182 391 public function processLocales($tplFile, $rev) {
6a488035
TO
392 $smarty = CRM_Core_Smarty::singleton();
393 $smarty->assign('domainID', CRM_Core_Config::domainID());
394
395 $this->source($smarty->fetch($tplFile), TRUE);
396
397 if ($this->multilingual) {
398 CRM_Core_I18n_Schema::rebuildMultilingualSchema($this->locales, $rev);
399 }
400 return $this->multilingual;
401 }
402
624e56fa
EM
403 /**
404 * @param $rev
405 */
00be9182 406 public function setSchemaStructureTables($rev) {
6a488035
TO
407 if ($this->multilingual) {
408 CRM_Core_I18n_Schema::schemaStructureTables($rev, TRUE);
409 }
410 }
411
624e56fa
EM
412 /**
413 * @param $rev
414 *
415 * @throws Exception
416 */
00be9182 417 public function processSQL($rev) {
6a488035
TO
418 $sqlFile = implode(DIRECTORY_SEPARATOR,
419 array(
353ffa53
TO
420 dirname(__FILE__),
421 'Incremental',
422 'sql',
423 $rev . '.mysql',
6a488035
TO
424 )
425 );
426 $tplFile = "$sqlFile.tpl";
427
428 if (file_exists($tplFile)) {
429 $this->processLocales($tplFile, $rev);
430 }
431 else {
432 if (!file_exists($sqlFile)) {
433 CRM_Core_Error::fatal("sqlfile - $rev.mysql not found.");
434 }
435 $this->source($sqlFile);
436 }
437 }
438
439 /**
fe482240 440 * Determine the start and end version of the upgrade process.
6a488035
TO
441 *
442 * @return array(0=>$currentVer, 1=>$latestVer)
443 */
00be9182 444 public function getUpgradeVersions() {
6a488035 445 $latestVer = CRM_Utils_System::version();
e418776c 446 $currentVer = CRM_Core_BAO_Domain::version(TRUE);
6a488035
TO
447 if (!$currentVer) {
448 CRM_Core_Error::fatal(ts('Version information missing in civicrm database.'));
449 }
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.'));
452 }
453 if (!$latestVer) {
454 CRM_Core_Error::fatal(ts('Version information missing in civicrm codebase.'));
455 }
456
6a488035
TO
457 return array($currentVer, $latestVer);
458 }
459
460 /**
461 * Determine if $currentVer can be upgraded to $latestVer
462 *
77b97be7
EM
463 * @param $currentVer
464 * @param $latestVer
465 *
6a488035
TO
466 * @return mixed, a string error message or boolean 'false' if OK
467 */
00be9182 468 public function checkUpgradeableVersion($currentVer, $latestVer) {
6a488035
TO
469 $error = FALSE;
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.');
474 }
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.',
304c1a5a 478 array(1 => $currentVer, 2 => $latestVer)
6a488035
TO
479 );
480 }
481 elseif (version_compare($currentVer, $latestVer) == 0) {
482 $error = ts('Your database has already been upgraded to CiviCRM %1',
483 array(1 => $latestVer)
484 );
485 }
304c1a5a
CW
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)
489 );
490 }
6a488035 491
304c1a5a 492 if (version_compare(phpversion(), self::MINIMUM_PHP_VERSION) < 0) {
6a488035 493 $error = ts('CiviCRM %3 requires PHP version %1 (or newer), but the current system uses %2 ',
353ffa53 494 array(
304c1a5a
CW
495 1 => self::MINIMUM_PHP_VERSION,
496 2 => phpversion(),
353ffa53
TO
497 3 => $latestVer,
498 ));
6a488035
TO
499 }
500
501 // check for mysql trigger privileges
502 if (!CRM_Core_DAO::checkTriggerViewPermission(FALSE, TRUE)) {
503 $error = ts('CiviCRM %1 requires MySQL trigger privileges.',
353ffa53 504 array(1 => $latestVer));
6a488035 505 }
032c9d10 506
e418776c 507 if (CRM_Core_DAO::getGlobalSetting('thread_stack', 0) < (1024 * self::MINIMUM_THREAD_STACK)) {
6a488035
TO
508 $error = ts('CiviCRM %1 requires MySQL thread stack >= %2k', array(
509 1 => $latestVer,
21dfd5f5 510 2 => self::MINIMUM_THREAD_STACK,
6a488035
TO
511 ));
512 }
513
514 return $error;
515 }
516
517 /**
518 * Determine if $currentver already matches $latestVer
519 *
77b97be7
EM
520 * @param $currentVer
521 * @param $latestVer
522 *
6a488035
TO
523 * @return mixed, a string error message or boolean 'false' if OK
524 */
00be9182 525 public function checkCurrentVersion($currentVer, $latestVer) {
6a488035
TO
526 $error = FALSE;
527
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.');
532 }
533 elseif (version_compare($currentVer, $latestVer) != 0) {
534 $error = ts('Your database is not configured for version %1',
535 array(1 => $latestVer)
536 );
537 }
538 return $error;
539 }
540
541 /**
fe482240 542 * Fill the queue with upgrade tasks.
6a488035 543 *
5a4f6742
CW
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.
6a488035 550 *
bf6a5362 551 * @return CRM_Queue_Service
6a488035 552 */
00be9182 553 public static function buildQueue($currentVer, $latestVer, $postUpgradeMessageFile) {
6a488035
TO
554 $upgrade = new CRM_Upgrade_Form();
555
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);
560 }
561
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'));
565 }
566 $queue = CRM_Queue_Service::singleton()->create(array(
353ffa53
TO
567 'name' => self::QUEUE_NAME,
568 'type' => 'Sql',
569 'reset' => TRUE,
570 ));
6a488035
TO
571
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(
353ffa53 577 // callback
6a488035
TO
578 array('CRM_Upgrade_Form', 'doIncrementalUpgradeStart'),
579 // arguments
580 array($rev),
581 "Begin Upgrade to $rev"
582 );
583 $queue->createItem($beginTask);
584
585 $task = new CRM_Queue_Task(
353ffa53 586 // callback
6a488035
TO
587 array('CRM_Upgrade_Form', 'doIncrementalUpgradeStep'),
588 // arguments
589 array($rev, $currentVer, $latestVer, $postUpgradeMessageFile),
590 "Upgrade DB to $rev"
591 );
592 $queue->createItem($task);
593
594 $task = new CRM_Queue_Task(
353ffa53 595 // callback
6a488035
TO
596 array('CRM_Upgrade_Form', 'doIncrementalUpgradeFinish'),
597 // arguments
3373fe5e 598 array($rev, $currentVer, $latestVer, $postUpgradeMessageFile),
6a488035
TO
599 "Finish Upgrade DB to $rev"
600 );
601 $queue->createItem($task);
602 }
603 }
604
605 return $queue;
606 }
607
608 /**
fe482240 609 * Perform an incremental version update.
6a488035 610 *
77b97be7 611 * @param CRM_Queue_TaskContext $ctx
5a4f6742
CW
612 * @param string $rev
613 * the target (intermediate) revision e.g '3.2.alpha1'.
77b97be7
EM
614 *
615 * @return bool
6a488035 616 */
00be9182 617 public static function doIncrementalUpgradeStart(CRM_Queue_TaskContext $ctx, $rev) {
6a488035
TO
618 $upgrade = new CRM_Upgrade_Form();
619
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');
623
624 return TRUE;
625 }
626
627 /**
fe482240 628 * Perform an incremental version update.
6a488035 629 *
77b97be7 630 * @param CRM_Queue_TaskContext $ctx
5a4f6742
CW
631 * @param string $rev
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.
77b97be7
EM
639 *
640 * @return bool
6a488035 641 */
e418776c 642 public static function doIncrementalUpgradeStep(CRM_Queue_TaskContext $ctx, $rev, $originalVer, $latestVer, $postUpgradeMessageFile) {
6a488035
TO
643 $upgrade = new CRM_Upgrade_Form();
644
645 $phpFunctionName = 'upgrade_' . str_replace('.', '_', $rev);
646
bd00780f
CW
647 $versionObject = $upgrade->incrementalPhpObject($rev);
648
649 // pre-db check for major release.
650 if ($upgrade->checkVersionRelease($rev, 'alpha1')) {
651 if (!(is_callable(array(
353ffa53 652 $versionObject,
ae5ffbb7 653 'verifyPreDBstate',
353ffa53
TO
654 )))
655 ) {
bd00780f 656 CRM_Core_Error::fatal("verifyPreDBstate method was not found for $rev");
6a488035 657 }
6a488035 658
bd00780f
CW
659 $error = NULL;
660 if (!($versionObject->verifyPreDBstate($error))) {
661 if (!isset($error)) {
662 $error = "post-condition failed for current upgrade for $rev";
6a488035 663 }
bd00780f 664 CRM_Core_Error::fatal($error);
6a488035
TO
665 }
666
bd00780f 667 }
6a488035 668
bd00780f 669 $upgrade->setSchemaStructureTables($rev);
6a488035 670
bd00780f 671 if (is_callable(array(
353ffa53 672 $versionObject,
ae5ffbb7 673 $phpFunctionName,
353ffa53 674 ))) {
bd00780f
CW
675 $versionObject->$phpFunctionName($rev, $originalVer, $latestVer);
676 }
677 else {
678 $upgrade->processSQL($rev);
679 }
680
681 // set post-upgrade-message if any
682 if (is_callable(array(
353ffa53 683 $versionObject,
ae5ffbb7 684 'setPostUpgradeMessage',
353ffa53 685 ))) {
bd00780f
CW
686 $postUpgradeMessage = file_get_contents($postUpgradeMessageFile);
687 $versionObject->setPostUpgradeMessage($postUpgradeMessage, $rev);
bd00780f 688 file_put_contents($postUpgradeMessageFile, $postUpgradeMessage);
6a488035
TO
689 }
690
691 return TRUE;
692 }
693
694 /**
fe482240 695 * Perform an incremental version update.
6a488035 696 *
77b97be7 697 * @param CRM_Queue_TaskContext $ctx
5a4f6742
CW
698 * @param string $rev
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.
77b97be7
EM
706 *
707 * @return bool
6a488035 708 */
00be9182 709 public static function doIncrementalUpgradeFinish(CRM_Queue_TaskContext $ctx, $rev, $currentVer, $latestVer, $postUpgradeMessageFile) {
6a488035
TO
710 $upgrade = new CRM_Upgrade_Form();
711 $upgrade->setVersion($rev);
712 CRM_Utils_System::flushCache();
ac05cde3 713
d8a4acc0
C
714 $config = CRM_Core_Config::singleton();
715 $config->userSystem->flush();
6a488035
TO
716 return TRUE;
717 }
718
00be9182 719 public static function doFinish() {
6a488035
TO
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);
724
b597d0b1
DL
725 // lets rebuild the config array in case we've made a few changes in the
726 // code base
727 // this also helps us always store the latest version of civi in the DB
728 $params = array();
729 CRM_Core_BAO_ConfigSetting::add($params);
730
b6386d8c
PJ
731 // CRM-12804 comment-51411 : add any missing settings
732 // at the end of upgrade
733 CRM_Core_BAO_Setting::updateSettingsFromMetaData();
734
6a488035
TO
735 // cleanup caches CRM-8739
736 $config = CRM_Core_Config::singleton();
1fcf16cc 737 $config->cleanupCaches(1);
6a488035
TO
738
739 // Rebuild all triggers and re-enable logging if needed
740 $logging = new CRM_Logging_Schema();
741 $logging->fixSchemaDifferences();
70b85f7c 742
743 //CRM-16257 update Config.IDS.ini might be an old copy
744 CRM_Core_IDS::createConfigFile(TRUE);
6a488035
TO
745 }
746
747 /**
748 * Compute any messages which should be displayed before upgrade
749 * by calling the 'setPreUpgradeMessage' on each incremental upgrade
750 * object.
751 *
5a4f6742
CW
752 * @param string $preUpgradeMessage
753 * alterable.
77b97be7
EM
754 * @param $currentVer
755 * @param $latestVer
6a488035 756 */
00be9182 757 public function setPreUpgradeMessage(&$preUpgradeMessage, $currentVer, $latestVer) {
49368097
CW
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);
6a488035
TO
762
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) {
49368097 768 if (version_compare($currentVer, $rev) < 0) {
6a488035 769 $versionObject = $this->incrementalPhpObject($rev);
49368097 770 if (is_callable(array($versionObject, 'setPreUpgradeMessage'))) {
e418776c
TO
771 $versionObject->setPreUpgradeMessage($preUpgradeMessage, $rev, $currentVer);
772 }
6a488035
TO
773 }
774 }
775 }
96025800 776
6a488035 777}