3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
12 use Civi\Core\SettingsBag
;
15 * Base class for incremental upgrades
17 class CRM_Upgrade_Incremental_Base
{
18 const BATCH_SIZE
= 5000;
23 protected $majorMinor;
26 * Get the major and minor version for this class (based on English-style class name).
31 public function getMajorMinor() {
32 if (!$this->majorMinor
) {
33 $className = explode('_', static::CLASS);
34 $numbers = preg_split("/([[:upper:]][[:lower:]]+)/", array_pop($className), -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
);
35 $major = CRM_Utils_EnglishNumber
::toInt(array_shift($numbers));
36 $minor = CRM_Utils_EnglishNumber
::toInt(implode('', $numbers));
37 $this->majorMinor
= $major . '.' . $minor;
39 return $this->majorMinor
;
43 * Get a list of revisions (PATCH releases) related to this class.
46 * Ex: ['4.5.6', '4.5.7']
47 * @throws \ReflectionException
49 public function getRevisionSequence() {
52 $sqlGlob = implode(DIRECTORY_SEPARATOR
, [dirname(__FILE__
), 'sql', $this->getMajorMinor() . '.*.mysql.tpl']);
53 $sqlFiles = glob($sqlGlob);;
54 foreach ($sqlFiles as $file) {
55 $revList[] = str_replace('.mysql.tpl', '', basename($file));
58 $c = new ReflectionClass(static::class);
59 foreach ($c->getMethods() as $method) {
60 /** @var \ReflectionMethod $method */
61 if (preg_match(';^upgrade_([0-9_alphabeta]+)$;', $method->getName(), $m)) {
62 $revList[] = str_replace('_', '.', $m[1]);
66 $revList = array_unique($revList);
67 usort($revList, 'version_compare');
72 * Compute any messages which should be displayed before upgrade.
74 * Downstream classes should implement this method to generate their messages.
76 * This method will be invoked multiple times. Implementations MUST consult the `$rev`
77 * before deciding what messages to add. See the examples linked below.
79 * @see \CRM_Upgrade_Incremental_php_FourSeven::setPreUpgradeMessage()
80 * @see \CRM_Upgrade_Incremental_php_FiveTwenty::setPreUpgradeMessage()
82 * @param string $preUpgradeMessage
83 * Accumulated list of messages. Alterable.
85 * The incremental version number. (Called repeatedly, once for each increment.)
87 * Ex: Suppose the system upgrades from 5.7.3 to 5.10.0. The method FiveEight::setPreUpgradeMessage()
88 * will be called for each increment of '5.8.*' ('5.8.alpha1' => '5.8.beta1' => '5.8.0').
89 * @param null $currentVer
90 * This is the penultimate version targeted by the upgrader.
91 * Equivalent to CRM_Utils_System::version().
93 public function setPreUpgradeMessage(&$preUpgradeMessage, $rev, $currentVer = NULL) {
97 * Compute any messages which should be displayed after upgrade.
99 * Downstream classes should implement this method to generate their messages.
101 * This method will be invoked multiple times. Implementations MUST consult the `$rev`
102 * before deciding what messages to add. See the examples linked below.
104 * @see \CRM_Upgrade_Incremental_php_FourSeven::setPostUpgradeMessage()
105 * @see \CRM_Upgrade_Incremental_php_FiveTwentyOne::setPostUpgradeMessage()
107 * @param string $postUpgradeMessage
108 * Accumulated list of messages. Alterable.
110 * The incremental version number. (Called repeatedly, once for each increment.)
112 * Ex: Suppose the system upgrades from 5.7.3 to 5.10.0. The method FiveEight::setPreUpgradeMessage()
113 * will be called for each increment of '5.8.*' ('5.8.alpha1' => '5.8.beta1' => '5.8.0').
115 public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
119 * (Queue Task Callback)
121 * @param \CRM_Queue_TaskContext $ctx
126 public static function runSql(CRM_Queue_TaskContext
$ctx, $rev) {
127 $upgrade = new CRM_Upgrade_Form();
128 $upgrade->processSQL($rev);
134 * Syntactic sugar for adding a task.
136 * Task is (a) in this class and (b) has a high priority.
138 * After passing the $funcName, you can also pass parameters that will go to
139 * the function. Note that all params must be serializable.
141 * @param string $title
142 * @param string $funcName
144 protected function addTask($title, $funcName) {
145 $queue = CRM_Queue_Service
::singleton()->load([
147 'name' => CRM_Upgrade_Form
::QUEUE_NAME
,
150 $args = func_get_args();
151 $title = array_shift($args);
152 $funcName = array_shift($args);
153 $task = new CRM_Queue_Task(
154 [get_class($this), $funcName],
158 $queue->createItem($task, ['weight' => -1]);
162 * Add a task to activate an extension. This task will run post-upgrade (after all
163 * changes to core DB are settled).
165 * @param string $title
166 * @param string[] $keys
167 * List of extensions to enable.
169 protected function addExtensionTask(string $title, array $keys): void
{
170 Civi
::queue(CRM_Upgrade_Form
::QUEUE_NAME
)->createItem(
171 new CRM_Queue_Task([static::CLASS, 'enableExtension'], [$keys], $title),
177 * @param \CRM_Queue_TaskContext $ctx
178 * @param string[] $keys
179 * List of extensions to enable.
182 public static function enableExtension(CRM_Queue_TaskContext
$ctx, array $keys): bool {
183 // The `enableExtension` has a very high value of `weight`, so this runs after all
184 // core DB schema updates have been resolved. We can use high-level services.
186 Civi
::dispatcher()->setDispatchPolicy(\CRM_Upgrade_DispatchPolicy
::get('upgrade.finish'));
187 $restore = \CRM_Utils_AutoClean
::with(function() {
188 Civi
::dispatcher()->setDispatchPolicy(\CRM_Upgrade_DispatchPolicy
::get('upgrade.main'));
191 $manager = CRM_Extension_System
::singleton()->getManager();
192 $manager->enable($manager->findInstallRequirements($keys));
194 // Hrm, `enable()` normally does these things... but not during upgrade...
195 // Note: A good test-scenario is to install 5.45; enable logging and CiviGrant; disable searchkit+afform; then upgrade to 5.47.
196 $schema = new CRM_Logging_Schema();
197 $schema->fixSchemaDifferences();
199 CRM_Core_Invoke
::rebuildMenuAndCaches(FALSE, FALSE);
200 // sessionReset is FALSE because upgrade status/postUpgradeMessages are needed by the page. We reset later in doFinish().
206 * Remove a payment processor if not in use
208 * @param CRM_Queue_TaskContext $ctx
209 * @param string $name
211 * @throws \CiviCRM_API3_Exception
213 public static function removePaymentProcessorType(CRM_Queue_TaskContext
$ctx, $name) {
214 $processors = civicrm_api3('PaymentProcessor', 'getcount', ['payment_processor_type_id' => $name]);
215 if (empty($processors['result'])) {
216 $result = civicrm_api3('PaymentProcessorType', 'get', [
220 if (!empty($result['id'])) {
221 civicrm_api3('PaymentProcessorType', 'delete', ['id' => $result['id']]);
228 * @param string $table_name
229 * @param string $constraint_name
232 public static function checkFKExists($table_name, $constraint_name) {
233 return CRM_Core_BAO_SchemaHandler
::checkFKExists($table_name, $constraint_name);
237 * Add a column to a table if it doesn't already exist
239 * @param CRM_Queue_TaskContext $ctx
240 * @param string $table
241 * @param string $column
242 * @param string $properties
243 * @param bool $localizable is this a field that should be localized
244 * @param string|null $version CiviCRM version to use if rebuilding multilingual schema
245 * @param bool $triggerRebuild should we trigger the rebuild of the multilingual schema
249 public static function addColumn($ctx, $table, $column, $properties, $localizable = FALSE, $version = NULL, $triggerRebuild = TRUE) {
250 $locales = CRM_Core_I18n
::getMultilingual();
252 if (!CRM_Core_BAO_SchemaHandler
::checkIfFieldExists($table, $column, FALSE)) {
255 foreach ($locales as $locale) {
256 if (!CRM_Core_BAO_SchemaHandler
::checkIfFieldExists($table, "{$column}_{$locale}", FALSE)) {
257 $queries[] = "ALTER TABLE `$table` ADD COLUMN `{$column}_{$locale}` $properties";
262 $queries[] = "ALTER TABLE `$table` ADD COLUMN `$column` $properties";
266 $queries[] = "ALTER TABLE `$table` ADD COLUMN `$column` $properties";
268 foreach ($queries as $query) {
269 CRM_Core_DAO
::executeQuery($query, [], TRUE, NULL, FALSE, FALSE);
272 if ($locales && $triggerRebuild) {
273 CRM_Core_I18n_Schema
::rebuildMultilingualSchema($locales, $version, TRUE);
279 * Add the specified option group, gracefully if it already exists.
281 * @param CRM_Queue_TaskContext $ctx
282 * @param array $params
283 * @param array $options
287 public static function addOptionGroup(CRM_Queue_TaskContext
$ctx, $params, $options): bool {
288 $defaults = ['is_active' => 1];
289 $optionDefaults = ['is_active' => 1];
290 $optionDefaults['option_group_id'] = \CRM_Core_BAO_OptionGroup
::ensureOptionGroupExists(array_merge($defaults, $params));
292 foreach ($options as $option) {
293 \CRM_Core_BAO_OptionValue
::ensureOptionValueExists(array_merge($optionDefaults, $option));
299 * Do any relevant message template updates.
301 * @param CRM_Queue_TaskContext $ctx
302 * @param string $version
304 public static function updateMessageTemplates($ctx, $version) {
305 $messageTemplateObject = new CRM_Upgrade_Incremental_MessageTemplates($version);
306 $messageTemplateObject->updateTemplates();
310 * Updated a message token within a template.
312 * @param CRM_Queue_TaskContext $ctx
313 * @param string $workflowName
320 public static function updateMessageToken($ctx, string $workflowName, string $old, string $new, $version):bool {
321 $messageObj = new CRM_Upgrade_Incremental_MessageTemplates($version);
322 if (!empty($workflowName)) {
323 $messageObj->replaceTokenInTemplate($workflowName, $old, $new);
326 $messageObj->replaceTokenInMessageTemplates($old, $new);
332 * Updated a message token within a scheduled reminder.
334 * @param CRM_Queue_TaskContext $ctx
341 public static function updateActionScheduleToken($ctx, string $old, string $new, $version):bool {
342 $messageObj = new CRM_Upgrade_Incremental_MessageTemplates($version);
343 $messageObj->replaceTokenInActionSchedule($old, $new);
348 * Updated a message token within a label.
350 * @param CRM_Queue_TaskContext $ctx
357 public static function updatePrintLabelToken($ctx, string $old, string $new, $version):bool {
358 $messageObj = new CRM_Upgrade_Incremental_MessageTemplates($version);
359 $messageObj->replaceTokenInPrintLabel($old, $new);
364 * Updated a message token within greeting options.
366 * @param CRM_Queue_TaskContext $ctx
373 public static function updateGreetingOptions($ctx, string $old, string $new, $version):bool {
374 $messageObj = new CRM_Upgrade_Incremental_MessageTemplates($version);
375 $messageObj->replaceTokenInGreetingOptions($old, $new);
380 * Updated a currency in civicrm_currency and related configurations
382 * @param CRM_Queue_TaskContext $ctx
383 * @param string $old_name
384 * @param string $new_name
388 public static function updateCurrencyName($ctx, string $old_name, string $new_name): bool {
389 CRM_Core_DAO
::executeQuery('UPDATE civicrm_currency SET name = %1 WHERE name = %2', [
390 1 => [$new_name, 'String'],
391 2 => [$old_name, 'String'],
394 $oid = CRM_Core_DAO
::singleValueQuery("SELECT id FROM civicrm_option_group WHERE name = 'currencies_enabled'");
396 CRM_Core_DAO
::executeQuery('UPDATE civicrm_option_value SET value = %1 WHERE value = %2 AND option_group_id = %3', [
397 1 => [$new_name, 'String'],
398 2 => [$old_name, 'String'],
399 3 => [$oid, 'String'],
403 CRM_Core_DAO
::executeQuery('UPDATE civicrm_participant SET fee_currency = %1 WHERE fee_currency = %2', [
404 1 => [$new_name, 'String'],
405 2 => [$old_name, 'String'],
409 'civicrm_contribution',
410 'civicrm_contribution_page',
411 'civicrm_contribution_recur',
412 'civicrm_contribution_soft',
414 'civicrm_financial_item',
415 'civicrm_financial_trxn',
418 'civicrm_pledge_payment',
423 foreach ($tables as $table) {
424 CRM_Core_DAO
::executeQuery('UPDATE %3 SET currency = %1 WHERE currency = %2', [
425 1 => [$new_name, 'String'],
426 2 => [$old_name, 'String'],
427 3 => [$table, 'MysqlColumnNameOrAlias'],
435 * Re-save any valid values from contribute settings into the normal setting
438 * We render the array of contribution_invoice_settings and any that have
439 * metadata defined we add to the correct key. This is safe to run even if no
440 * settings are to be converted, per the test in
441 * testConvertUpgradeContributeSettings.
447 public static function updateContributeSettings($ctx) {
448 // Use a direct query as api now does some handling on this.
449 $settings = CRM_Core_DAO
::executeQuery("SELECT value, domain_id FROM civicrm_setting WHERE name = 'contribution_invoice_settings'");
451 while ($settings->fetch()) {
452 $contributionSettings = (array) CRM_Utils_String
::unserialize($settings->value
);
453 foreach (array_merge(SettingsBag
::getContributionInvoiceSettingKeys(), ['deferred_revenue_enabled' => 'deferred_revenue_enabled']) as $possibleKeyName => $settingName) {
454 if (!empty($contributionSettings[$possibleKeyName]) && empty(Civi
::settings($settings->domain_id
)->getExplicit($settingName))) {
455 Civi
::settings($settings->domain_id
)->set($settingName, $contributionSettings[$possibleKeyName]);
463 * Do any relevant smart group updates.
465 * @param CRM_Queue_TaskContext $ctx
466 * @param array $actions
470 public static function updateSmartGroups($ctx, $actions) {
471 $groupUpdateObject = new CRM_Upgrade_Incremental_SmartGroups();
472 $groupUpdateObject->updateGroups($actions);
477 * Drop a column from a table if it exist.
479 * @param CRM_Queue_TaskContext $ctx
480 * @param string $table
481 * @param string $column
484 public static function dropColumn($ctx, $table, $column) {
485 if (CRM_Core_BAO_SchemaHandler
::checkIfFieldExists($table, $column)) {
486 CRM_Core_DAO
::executeQuery("ALTER TABLE `$table` DROP COLUMN `$column`",
487 [], TRUE, NULL, FALSE, FALSE);
493 * Add a index to a table column.
495 * @param CRM_Queue_TaskContext $ctx
496 * @param string $table
497 * @param string|array $columns
498 * @param string $prefix
501 public static function addIndex($ctx, $table, $columns, $prefix = 'index') {
502 $tables = [$table => (array) $columns];
503 CRM_Core_BAO_SchemaHandler
::createIndexes($tables, $prefix);
509 * Drop a index from a table if it exist.
511 * @param CRM_Queue_TaskContext $ctx
512 * @param string $table
513 * @param string $indexName
516 public static function dropIndex($ctx, $table, $indexName) {
517 CRM_Core_BAO_SchemaHandler
::dropIndexIfExists($table, $indexName);
523 * Drop a table... but only if it's empty.
525 * @param CRM_Queue_TaskContext $ctx
526 * @param string $table
529 public static function dropTableIfEmpty($ctx, $table) {
530 if (CRM_Core_DAO
::checkTableExists($table)) {
531 if (!CRM_Core_DAO
::checkTableHasData($table)) {
532 CRM_Core_BAO_SchemaHandler
::dropTable($table);
535 $ctx->log
->warning("dropTableIfEmpty($table): Found data. Preserved table.");
543 * Rebuild Multilingual Schema.
544 * @param CRM_Queue_TaskContext $ctx
545 * @param string|null $version CiviCRM version to use if rebuilding multilingual schema
549 public static function rebuildMultilingalSchema($ctx, $version = NULL) {
550 $locales = CRM_Core_I18n
::getMultilingual();
552 CRM_Core_I18n_Schema
::rebuildMultilingualSchema($locales, $version);