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