Merge pull request #17859 from civicrm/5.28
[civicrm-core.git] / CRM / Upgrade / Form.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Class CRM_Upgrade_Form
14 */
15 class CRM_Upgrade_Form extends CRM_Core_Form {
16 const QUEUE_NAME = 'CRM_Upgrade';
17
18 /**
19 * Minimum size of MySQL's thread_stack option
20 *
21 * @see install/index.php MINIMUM_THREAD_STACK
22 */
23 const MINIMUM_THREAD_STACK = 192;
24
25 /**
26 * Minimum previous CiviCRM version we can directly upgrade from
27 */
28 const MINIMUM_UPGRADABLE_VERSION = '4.4.7';
29
30 /**
31 * @var \CRM_Core_Config
32 */
33 protected $_config;
34
35 /**
36 * Upgrade for multilingual.
37 *
38 * @var bool
39 */
40 public $multilingual = FALSE;
41
42 /**
43 * Locales available for multilingual upgrade.
44 *
45 * @var array
46 */
47 public $locales;
48
49 /**
50 * Constructor for the basic form page.
51 *
52 * We should not use QuickForm directly. This class provides a lot
53 * of default convenient functions, rules and buttons
54 *
55 * @param object $state
56 * State associated with this form.
57 * @param const|\enum|int $action The mode the form is operating in (None/Create/View/Update/Delete)
58 * @param string $method
59 * The type of http method used (GET/POST).
60 * @param string $name
61 * The name of the form if different from class name.
62 */
63 public function __construct(
64 $state = NULL,
65 $action = CRM_Core_Action::NONE,
66 $method = 'post',
67 $name = NULL
68 ) {
69 $this->_config = CRM_Core_Config::singleton();
70
71 $domain = new CRM_Core_DAO_Domain();
72 $domain->find(TRUE);
73
74 $this->multilingual = (bool) $domain->locales;
75 $this->locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales);
76
77 $smarty = CRM_Core_Smarty::singleton();
78 //$smarty->compile_dir = $this->_config->templateCompileDir;
79 $smarty->assign('multilingual', $this->multilingual);
80 $smarty->assign('locales', $this->locales);
81
82 // we didn't call CRM_Core_BAO_ConfigSetting::retrieve(), so we need to set $dbLocale by hand
83 if ($this->multilingual) {
84 global $dbLocale;
85 $dbLocale = "_{$this->_config->lcMessages}";
86 }
87
88 parent::__construct($state, $action, $method, $name);
89 }
90
91 /**
92 * @param $version
93 *
94 * @return mixed
95 */
96 public static function &incrementalPhpObject($version) {
97 static $incrementalPhpObject = [];
98
99 $versionParts = explode('.', $version);
100 $versionName = CRM_Utils_EnglishNumber::toCamelCase($versionParts[0]) . CRM_Utils_EnglishNumber::toCamelCase($versionParts[1]);
101
102 if (!array_key_exists($versionName, $incrementalPhpObject)) {
103 $className = "CRM_Upgrade_Incremental_php_{$versionName}";
104 $incrementalPhpObject[$versionName] = new $className();
105 }
106 return $incrementalPhpObject[$versionName];
107 }
108
109 /**
110 * @param $version
111 * @param $release
112 *
113 * @return bool
114 */
115 public function checkVersionRelease($version, $release) {
116 $versionParts = explode('.', $version);
117 return ($versionParts[2] == $release);
118 }
119
120 /**
121 * @param $constraints
122 *
123 * @return array
124 */
125 public function checkSQLConstraints(&$constraints) {
126 $pass = $fail = 0;
127 foreach ($constraints as $constraint) {
128 if ($this->checkSQLConstraint($constraint)) {
129 $pass++;
130 }
131 else {
132 $fail++;
133 }
134 return [$pass, $fail];
135 }
136 }
137
138 /**
139 * @param $constraint
140 *
141 * @return bool
142 */
143 public function checkSQLConstraint($constraint) {
144 // check constraint here
145 return TRUE;
146 }
147
148 /**
149 * @param string $fileName
150 * @param bool $isQueryString
151 */
152 public function source($fileName, $isQueryString = FALSE) {
153 if ($isQueryString) {
154 CRM_Utils_File::runSqlQuery($this->_config->dsn,
155 $fileName, NULL
156 );
157 }
158 else {
159 CRM_Utils_File::sourceSQLFile($this->_config->dsn,
160 $fileName, NULL
161 );
162 }
163 }
164
165 public function preProcess() {
166 CRM_Utils_System::setTitle($this->getTitle());
167 if (!$this->verifyPreDBState($errorMessage)) {
168 if (!isset($errorMessage)) {
169 $errorMessage = 'pre-condition failed for current upgrade step';
170 }
171 throw new CRM_Core_Exception($errorMessage);
172 }
173 $this->assign('recentlyViewed', FALSE);
174 }
175
176 public function buildQuickForm() {
177 $this->addDefaultButtons($this->getButtonTitle(),
178 'next',
179 NULL,
180 TRUE
181 );
182 }
183
184 /**
185 * Getter function for title. Should be over-ridden by derived class
186 *
187 * @return string
188 */
189
190 /**
191 * @return string
192 */
193 public function getTitle() {
194 return ts('Title not Set');
195 }
196
197 /**
198 * @return string
199 */
200 public function getFieldsetTitle() {
201 return '';
202 }
203
204 /**
205 * @return string
206 */
207 public function getButtonTitle() {
208 return ts('Continue');
209 }
210
211 /**
212 * Use the form name to create the tpl file name.
213 *
214 * @return string
215 */
216
217 /**
218 * @return string
219 */
220 public function getTemplateFileName() {
221 $this->assign('title',
222 $this->getFieldsetTitle()
223 );
224 $this->assign('message',
225 $this->getTemplateMessage()
226 );
227 return 'CRM/Upgrade/Base.tpl';
228 }
229
230 public function postProcess() {
231 $this->upgrade();
232
233 if (!$this->verifyPostDBState($errorMessage)) {
234 if (!isset($errorMessage)) {
235 $errorMessage = 'post-condition failed for current upgrade step';
236 }
237 throw new CRM_Core_Exception($errorMessage);
238 }
239 }
240
241 /**
242 * @param $query
243 *
244 * @return Object
245 */
246 public function runQuery($query) {
247 return CRM_Core_DAO::executeQuery($query);
248 }
249
250 /**
251 * @param $version
252 *
253 * @return Object
254 */
255 public function setVersion($version) {
256 $this->logVersion($version);
257
258 $query = "
259 UPDATE civicrm_domain
260 SET version = '$version'
261 ";
262 return $this->runQuery($query);
263 }
264
265 /**
266 * @param $newVersion
267 *
268 * @return bool
269 */
270 public function logVersion($newVersion) {
271 if ($newVersion) {
272 $oldVersion = CRM_Core_BAO_Domain::version();
273
274 $session = CRM_Core_Session::singleton();
275 $logParams = [
276 'entity_table' => 'civicrm_domain',
277 'entity_id' => 1,
278 'data' => "upgrade:{$oldVersion}->{$newVersion}",
279 // lets skip 'modified_id' for now, as it causes FK issues And
280 // is not very important for now.
281 'modified_date' => date('YmdHis'),
282 ];
283 CRM_Core_BAO_Log::add($logParams);
284 return TRUE;
285 }
286
287 return FALSE;
288 }
289
290 /**
291 * @param $version
292 *
293 * @return bool
294 */
295 public function checkVersion($version) {
296 $domainID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain',
297 $version, 'id',
298 'version'
299 );
300 return (bool) $domainID;
301 }
302
303 /**
304 * @return array
305 * @throws Exception
306 */
307 public function getRevisionSequence() {
308 $revList = [];
309 $sqlDir = implode(DIRECTORY_SEPARATOR,
310 [dirname(__FILE__), 'Incremental', 'sql']
311 );
312 $sqlFiles = scandir($sqlDir);
313
314 $sqlFilePattern = '/^((\d{1,2}\.\d{1,2})\.(\d{1,2}\.)?(\d{1,2}|\w{4,7}))\.(my)?sql(\.tpl)?$/i';
315 foreach ($sqlFiles as $file) {
316 if (preg_match($sqlFilePattern, $file, $matches)) {
317 if (!in_array($matches[1], $revList)) {
318 $revList[] = $matches[1];
319 }
320 }
321 }
322
323 usort($revList, 'version_compare');
324 return $revList;
325 }
326
327 /**
328 * @param $rev
329 * @param int $index
330 *
331 * @return null
332 */
333 public static function getRevisionPart($rev, $index = 1) {
334 $revPattern = '/^((\d{1,2})\.\d{1,2})\.(\d{1,2}|\w{4,7})?$/i';
335 preg_match($revPattern, $rev, $matches);
336
337 return array_key_exists($index, $matches) ? $matches[$index] : NULL;
338 }
339
340 /**
341 * @param $tplFile
342 * @param $rev
343 *
344 * @return bool
345 */
346 public function processLocales($tplFile, $rev) {
347 $smarty = CRM_Core_Smarty::singleton();
348 $smarty->assign('domainID', CRM_Core_Config::domainID());
349
350 $this->source($smarty->fetch($tplFile), TRUE);
351
352 if ($this->multilingual) {
353 CRM_Core_I18n_Schema::rebuildMultilingualSchema($this->locales, $rev);
354 }
355 return $this->multilingual;
356 }
357
358 /**
359 * @param $rev
360 */
361 public function setSchemaStructureTables($rev) {
362 if ($this->multilingual) {
363 CRM_Core_I18n_Schema::schemaStructureTables($rev, TRUE);
364 }
365 }
366
367 /**
368 * @param $rev
369 *
370 * @throws Exception
371 */
372 public function processSQL($rev) {
373 $sqlFile = implode(DIRECTORY_SEPARATOR,
374 [
375 dirname(__FILE__),
376 'Incremental',
377 'sql',
378 $rev . '.mysql',
379 ]
380 );
381 $tplFile = "$sqlFile.tpl";
382
383 if (file_exists($tplFile)) {
384 $this->processLocales($tplFile, $rev);
385 }
386 else {
387 if (!file_exists($sqlFile)) {
388 throw new CRM_Core_Exception("sqlfile - $rev.mysql not found.");
389 }
390 $this->source($sqlFile);
391 }
392 }
393
394 /**
395 * Determine the start and end version of the upgrade process.
396 *
397 * @return array(0=>$currentVer, 1=>$latestVer)
398 */
399 public function getUpgradeVersions() {
400 $latestVer = CRM_Utils_System::version();
401 $currentVer = CRM_Core_BAO_Domain::version(TRUE);
402 if (!$currentVer) {
403 throw new CRM_Core_Exception(ts('Version information missing in civicrm database.'));
404 }
405 elseif (stripos($currentVer, 'upgrade')) {
406 throw new CRM_Core_Exception(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.'));
407 }
408 if (!$latestVer) {
409 throw new CRM_Core_Exception(ts('Version information missing in civicrm codebase.'));
410 }
411
412 return [$currentVer, $latestVer];
413 }
414
415 /**
416 * Determine if $currentVer can be upgraded to $latestVer
417 *
418 * @param $currentVer
419 * @param $latestVer
420 *
421 * @return mixed, a string error message or boolean 'false' if OK
422 */
423 public function checkUpgradeableVersion($currentVer, $latestVer) {
424 $error = FALSE;
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.');
429 }
430 elseif (version_compare($currentVer, $latestVer) > 0) {
431 // DB version number is higher than codebase being upgraded to. This is unexpected condition-fatal error.
432 $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.',
433 [1 => $currentVer, 2 => $latestVer]
434 );
435 }
436 elseif (version_compare($currentVer, $latestVer) == 0) {
437 $error = ts('Your database has already been upgraded to CiviCRM %1',
438 [1 => $latestVer]
439 );
440 }
441 elseif (version_compare($currentVer, self::MINIMUM_UPGRADABLE_VERSION) < 0) {
442 $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.',
443 [1 => self::MINIMUM_UPGRADABLE_VERSION, 2 => $latestVer]
444 );
445 }
446
447 if (version_compare(phpversion(), CRM_Upgrade_Incremental_General::MIN_INSTALL_PHP_VER) < 0) {
448 $error = ts('CiviCRM %3 requires PHP version %1 (or newer), but the current system uses %2 ',
449 [
450 1 => CRM_Upgrade_Incremental_General::MIN_INSTALL_PHP_VER,
451 2 => phpversion(),
452 3 => $latestVer,
453 ]);
454 }
455
456 if (version_compare(CRM_Utils_SQL::getDatabaseVersion(), CRM_Upgrade_Incremental_General::MIN_INSTALL_MYSQL_VER) < 0) {
457 $error = ts('CiviCRM %4 requires MySQL version v%1 or MariaDB v%3 (or newer), but the current system uses %2 ',
458 [
459 1 => CRM_Upgrade_Incremental_General::MIN_INSTALL_MYSQL_VER,
460 2 => CRM_Utils_SQL::getDatabaseVersion(),
461 3 => '10.1',
462 4 => $latestVer,
463 ]);
464 }
465
466 // check for mysql trigger privileges
467 if (!\Civi::settings()->get('logging_no_trigger_permission') && !CRM_Core_DAO::checkTriggerViewPermission(FALSE, TRUE)) {
468 $error = ts('CiviCRM %1 requires MySQL trigger privileges.',
469 [1 => $latestVer]);
470 }
471
472 if (CRM_Core_DAO::getGlobalSetting('thread_stack', 0) < (1024 * self::MINIMUM_THREAD_STACK)) {
473 $error = ts('CiviCRM %1 requires MySQL thread stack >= %2k', [
474 1 => $latestVer,
475 2 => self::MINIMUM_THREAD_STACK,
476 ]);
477 }
478
479 return $error;
480 }
481
482 /**
483 * Determine if $currentver already matches $latestVer
484 *
485 * @param $currentVer
486 * @param $latestVer
487 *
488 * @return mixed, a string error message or boolean 'false' if OK
489 */
490 public function checkCurrentVersion($currentVer, $latestVer) {
491 $error = FALSE;
492
493 // since version is suppose to be in valid format at this point, especially after conversion ($convertVer),
494 // lets do a pattern check -
495 if (!CRM_Utils_System::isVersionFormatValid($currentVer)) {
496 $error = ts('Database is marked with invalid version format. You may want to investigate this before you proceed further.');
497 }
498 elseif (version_compare($currentVer, $latestVer) != 0) {
499 $error = ts('Your database is not configured for version %1',
500 [1 => $latestVer]
501 );
502 }
503 return $error;
504 }
505
506 /**
507 * Fill the queue with upgrade tasks.
508 *
509 * @param string $currentVer
510 * the original revision.
511 * @param string $latestVer
512 * the target (final) revision.
513 * @param string $postUpgradeMessageFile
514 * path of a modifiable file which lists the post-upgrade messages.
515 *
516 * @return CRM_Queue_Service
517 */
518 public static function buildQueue($currentVer, $latestVer, $postUpgradeMessageFile) {
519 $upgrade = new CRM_Upgrade_Form();
520
521 // Ensure that queue can be created
522 if (!CRM_Queue_BAO_QueueItem::findCreateTable()) {
523 throw new CRM_Core_Exception(ts('Failed to find or create queueing table'));
524 }
525 $queue = CRM_Queue_Service::singleton()->create([
526 'name' => self::QUEUE_NAME,
527 'type' => 'Sql',
528 'reset' => TRUE,
529 ]);
530
531 $task = new CRM_Queue_Task(
532 ['CRM_Upgrade_Form', 'doFileCleanup'],
533 [$postUpgradeMessageFile],
534 "Cleanup old files"
535 );
536 $queue->createItem($task);
537
538 $task = new CRM_Queue_Task(
539 ['CRM_Upgrade_Form', 'disableOldExtensions'],
540 [$postUpgradeMessageFile],
541 "Checking extensions"
542 );
543 $queue->createItem($task);
544
545 $revisions = $upgrade->getRevisionSequence();
546 foreach ($revisions as $rev) {
547 // proceed only if $currentVer < $rev
548 if (version_compare($currentVer, $rev) < 0) {
549 $beginTask = new CRM_Queue_Task(
550 // callback
551 ['CRM_Upgrade_Form', 'doIncrementalUpgradeStart'],
552 // arguments
553 [$rev],
554 "Begin Upgrade to $rev"
555 );
556 $queue->createItem($beginTask);
557
558 $task = new CRM_Queue_Task(
559 // callback
560 ['CRM_Upgrade_Form', 'doIncrementalUpgradeStep'],
561 // arguments
562 [$rev, $currentVer, $latestVer, $postUpgradeMessageFile],
563 "Upgrade DB to $rev"
564 );
565 $queue->createItem($task);
566
567 $task = new CRM_Queue_Task(
568 // callback
569 ['CRM_Upgrade_Form', 'doIncrementalUpgradeFinish'],
570 // arguments
571 [$rev, $currentVer, $latestVer, $postUpgradeMessageFile],
572 "Finish Upgrade DB to $rev"
573 );
574 $queue->createItem($task);
575 }
576 }
577
578 return $queue;
579 }
580
581 /**
582 * Find any old, orphaned files that should have been deleted.
583 *
584 * These files can get left behind, eg, if you use the Joomla
585 * upgrade procedure.
586 *
587 * The earlier we can do this, the better - don't want upgrade logic
588 * to inadvertently rely on old/relocated files.
589 *
590 * @param \CRM_Queue_TaskContext $ctx
591 * @param string $postUpgradeMessageFile
592 * @return bool
593 */
594 public static function doFileCleanup(CRM_Queue_TaskContext $ctx, $postUpgradeMessageFile) {
595 $source = new CRM_Utils_Check_Component_Source();
596 $files = $source->findOrphanedFiles();
597 $errors = [];
598 foreach ($files as $file) {
599 if (is_dir($file['path'])) {
600 @rmdir($file['path']);
601 }
602 else {
603 @unlink($file['path']);
604 }
605
606 if (file_exists($file['path'])) {
607 $errors[] = sprintf("<li>%s</li>", htmlentities($file['path']));
608 }
609 }
610
611 if (!empty($errors)) {
612 file_put_contents($postUpgradeMessageFile,
613 '<br/><br/>' . ts('Some old files could not be removed. Please remove them.')
614 . '<ul>' . implode("\n", $errors) . '</ul>',
615 FILE_APPEND
616 );
617 }
618
619 return TRUE;
620 }
621
622 /**
623 * Disable/uninstall any extensions not compatible with this new version.
624 *
625 * @param \CRM_Queue_TaskContext $ctx
626 * @param string $postUpgradeMessageFile
627 * @return bool
628 */
629 public static function disableOldExtensions(CRM_Queue_TaskContext $ctx, $postUpgradeMessageFile) {
630 $messages = [];
631 $manager = CRM_Extension_System::singleton()->getManager();
632 foreach ($manager->getStatuses() as $key => $status) {
633 $obsolete = $manager->isIncompatible($key);
634 if ($obsolete) {
635 if (!empty($obsolete['disable']) && in_array($status, [$manager::STATUS_INSTALLED, $manager::STATUS_INSTALLED_MISSING])) {
636 try {
637 $manager->disable($key);
638 // Update the status for the sake of uninstall below.
639 $status = $status == $manager::STATUS_INSTALLED ? $manager::STATUS_DISABLED : $manager::STATUS_DISABLED_MISSING;
640 // This message is intentionally overwritten by uninstall below as it would be redundant
641 $messages[$key] = ts('The extension %1 is now obsolete and has been disabled.', [1 => $key]);
642 }
643 catch (CRM_Extension_Exception $e) {
644 $messages[] = ts('The obsolete extension %1 could not be removed due to an error. It is recommended to remove this extension manually.', [1 => $key]);
645 }
646 }
647 if (!empty($obsolete['uninstall']) && in_array($status, [$manager::STATUS_DISABLED, $manager::STATUS_DISABLED_MISSING])) {
648 try {
649 $manager->uninstall($key);
650 $messages[$key] = ts('The extension %1 is now obsolete and has been uninstalled.', [1 => $key]);
651 if ($status == $manager::STATUS_DISABLED) {
652 $messages[$key] .= ' ' . ts('You can remove it from your extensions directory.');
653 }
654 }
655 catch (CRM_Extension_Exception $e) {
656 $messages[] = ts('The obsolete extension %1 could not be removed due to an error. It is recommended to remove this extension manually.', [1 => $key]);
657 }
658 }
659 if (!empty($obsolete['force-uninstall'])) {
660 CRM_Core_DAO::executeQuery('UPDATE civicrm_extension SET is_active = 0 WHERE full_name = %1', [
661 1 => [$key, 'String'],
662 ]);
663 }
664 }
665 }
666 if ($messages) {
667 file_put_contents($postUpgradeMessageFile,
668 '<br/><br/><ul><li>' . implode("</li>\n<li>", $messages) . '</li></ul>',
669 FILE_APPEND
670 );
671 }
672
673 return TRUE;
674 }
675
676 /**
677 * Perform an incremental version update.
678 *
679 * @param CRM_Queue_TaskContext $ctx
680 * @param string $rev
681 * the target (intermediate) revision e.g '3.2.alpha1'.
682 *
683 * @return bool
684 */
685 public static function doIncrementalUpgradeStart(CRM_Queue_TaskContext $ctx, $rev) {
686 $upgrade = new CRM_Upgrade_Form();
687
688 // as soon as we start doing anything we append ".upgrade" to version.
689 // this also helps detect any partial upgrade issues
690 $upgrade->setVersion($rev . '.upgrade');
691
692 return TRUE;
693 }
694
695 /**
696 * Perform an incremental version update.
697 *
698 * @param CRM_Queue_TaskContext $ctx
699 * @param string $rev
700 * the target (intermediate) revision e.g '3.2.alpha1'.
701 * @param string $originalVer
702 * the original revision.
703 * @param string $latestVer
704 * the target (final) revision.
705 * @param string $postUpgradeMessageFile
706 * path of a modifiable file which lists the post-upgrade messages.
707 *
708 * @return bool
709 */
710 public static function doIncrementalUpgradeStep(CRM_Queue_TaskContext $ctx, $rev, $originalVer, $latestVer, $postUpgradeMessageFile) {
711 $upgrade = new CRM_Upgrade_Form();
712
713 $phpFunctionName = 'upgrade_' . str_replace('.', '_', $rev);
714
715 $versionObject = $upgrade->incrementalPhpObject($rev);
716
717 // pre-db check for major release.
718 if ($upgrade->checkVersionRelease($rev, 'alpha1')) {
719 if (!(is_callable([$versionObject, 'verifyPreDBstate']))) {
720 throw new CRM_Core_Exception("verifyPreDBstate method was not found for $rev");
721 }
722
723 $error = NULL;
724 if (!($versionObject->verifyPreDBstate($error))) {
725 if (!isset($error)) {
726 $error = "post-condition failed for current upgrade for $rev";
727 }
728 throw new CRM_Core_Exception($error);
729 }
730
731 }
732
733 $upgrade->setSchemaStructureTables($rev);
734
735 if (is_callable([$versionObject, $phpFunctionName])) {
736 $versionObject->$phpFunctionName($rev, $originalVer, $latestVer);
737 }
738 else {
739 $upgrade->processSQL($rev);
740 }
741
742 // set post-upgrade-message if any
743 if (is_callable([$versionObject, 'setPostUpgradeMessage'])) {
744 $postUpgradeMessage = file_get_contents($postUpgradeMessageFile);
745 $versionObject->setPostUpgradeMessage($postUpgradeMessage, $rev);
746 file_put_contents($postUpgradeMessageFile, $postUpgradeMessage);
747 }
748
749 return TRUE;
750 }
751
752 /**
753 * Perform an incremental version update.
754 *
755 * @param CRM_Queue_TaskContext $ctx
756 * @param string $rev
757 * the target (intermediate) revision e.g '3.2.alpha1'.
758 * @param string $currentVer
759 * the original revision.
760 * @param string $latestVer
761 * the target (final) revision.
762 * @param string $postUpgradeMessageFile
763 * path of a modifiable file which lists the post-upgrade messages.
764 *
765 * @return bool
766 */
767 public static function doIncrementalUpgradeFinish(CRM_Queue_TaskContext $ctx, $rev, $currentVer, $latestVer, $postUpgradeMessageFile) {
768 $upgrade = new CRM_Upgrade_Form();
769 $upgrade->setVersion($rev);
770 CRM_Utils_System::flushCache();
771
772 $config = CRM_Core_Config::singleton();
773 $config->userSystem->flush();
774 return TRUE;
775 }
776
777 public static function doFinish() {
778 Civi::dispatcher()->setDispatchPolicy(\CRM_Upgrade_DispatchPolicy::get('upgrade.finish'));
779 $restore = \CRM_Utils_AutoClean::with(function() {
780 Civi::dispatcher()->setDispatchPolicy(\CRM_Upgrade_DispatchPolicy::get('upgrade.main'));
781 });
782
783 $upgrade = new CRM_Upgrade_Form();
784 list($ignore, $latestVer) = $upgrade->getUpgradeVersions();
785 // Seems extraneous in context, but we'll preserve old behavior
786 $upgrade->setVersion($latestVer);
787
788 CRM_Core_Invoke::rebuildMenuAndCaches(FALSE, TRUE);
789 // NOTE: triggerRebuild is FALSE becaues it will run again in a moment (via fixSchemaDifferences).
790
791 $versionCheck = new CRM_Utils_VersionCheck();
792 $versionCheck->flushCache();
793
794 // Rebuild all triggers and re-enable logging if needed
795 $logging = new CRM_Logging_Schema();
796 $logging->fixSchemaDifferences();
797 }
798
799 /**
800 * Compute any messages which should be displayed before upgrade
801 * by calling the 'setPreUpgradeMessage' on each incremental upgrade
802 * object.
803 *
804 * @param string $preUpgradeMessage
805 * alterable.
806 * @param $currentVer
807 * @param $latestVer
808 */
809 public function setPreUpgradeMessage(&$preUpgradeMessage, $currentVer, $latestVer) {
810 // check for changed message templates
811 CRM_Upgrade_Incremental_General::checkMessageTemplate($preUpgradeMessage, $latestVer, $currentVer);
812 // set global messages
813 CRM_Upgrade_Incremental_General::setPreUpgradeMessage($preUpgradeMessage, $currentVer, $latestVer);
814
815 // Scan through all php files and see if any file is interested in setting pre-upgrade-message
816 // based on $currentVer, $latestVer.
817 // Please note, at this point upgrade hasn't started executing queries.
818 $revisions = $this->getRevisionSequence();
819 foreach ($revisions as $rev) {
820 if (version_compare($currentVer, $rev) < 0) {
821 $versionObject = $this->incrementalPhpObject($rev);
822 CRM_Upgrade_Incremental_General::updateMessageTemplate($preUpgradeMessage, $rev);
823 if (is_callable([$versionObject, 'setPreUpgradeMessage'])) {
824 $versionObject->setPreUpgradeMessage($preUpgradeMessage, $rev, $currentVer);
825 }
826 }
827 }
828 }
829
830 }