Commit | Line | Data |
---|---|---|
bf6a5362 CW |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
bf6a5362 | 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 | | |
bf6a5362 CW |
9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
ff6f993e | 12 | use Civi\Core\SettingsBag; |
13 | ||
bf6a5362 CW |
14 | /** |
15 | * Base class for incremental upgrades | |
16 | */ | |
17 | class CRM_Upgrade_Incremental_Base { | |
18 | const BATCH_SIZE = 5000; | |
19 | ||
d7e4d371 TO |
20 | /** |
21 | * @var string|null | |
22 | */ | |
23 | protected $majorMinor; | |
24 | ||
25 | /** | |
26 | * Get the major and minor version for this class (based on English-style class name). | |
27 | * | |
28 | * @return string | |
29 | * Ex: '5.34' or '4.7' | |
30 | */ | |
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; | |
38 | } | |
39 | return $this->majorMinor; | |
40 | } | |
41 | ||
42 | /** | |
43 | * Get a list of revisions (PATCH releases) related to this class. | |
44 | * | |
45 | * @return array | |
46 | * Ex: ['4.5.6', '4.5.7'] | |
47 | * @throws \ReflectionException | |
48 | */ | |
49 | public function getRevisionSequence() { | |
50 | $revList = []; | |
51 | ||
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)); | |
56 | } | |
57 | ||
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]); | |
63 | } | |
64 | } | |
65 | ||
66 | $revList = array_unique($revList); | |
67 | usort($revList, 'version_compare'); | |
68 | return $revList; | |
69 | } | |
70 | ||
bf6a5362 CW |
71 | /** |
72 | * Verify DB state. | |
73 | * | |
74 | * @param $errors | |
75 | * | |
76 | * @return bool | |
77 | */ | |
78 | public function verifyPreDBstate(&$errors) { | |
79 | return TRUE; | |
80 | } | |
81 | ||
82 | /** | |
83 | * Compute any messages which should be displayed before upgrade. | |
84 | * | |
85 | * Note: This function is called iteratively for each upcoming | |
86 | * revision to the database. | |
87 | * | |
88 | * @param $preUpgradeMessage | |
89 | * @param string $rev | |
90 | * a version number, e.g. '4.8.alpha1', '4.8.beta3', '4.8.0'. | |
91 | * @param null $currentVer | |
92 | */ | |
93 | public function setPreUpgradeMessage(&$preUpgradeMessage, $rev, $currentVer = NULL) { | |
94 | } | |
95 | ||
96 | /** | |
97 | * Compute any messages which should be displayed after upgrade. | |
98 | * | |
99 | * @param string $postUpgradeMessage | |
100 | * alterable. | |
101 | * @param string $rev | |
102 | * an intermediate version; note that setPostUpgradeMessage is called repeatedly with different $revs. | |
bf6a5362 CW |
103 | */ |
104 | public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) { | |
105 | } | |
106 | ||
bf6a5362 CW |
107 | /** |
108 | * (Queue Task Callback) | |
54957108 | 109 | * |
110 | * @param \CRM_Queue_TaskContext $ctx | |
111 | * @param string $rev | |
112 | * | |
113 | * @return bool | |
bf6a5362 CW |
114 | */ |
115 | public static function runSql(CRM_Queue_TaskContext $ctx, $rev) { | |
116 | $upgrade = new CRM_Upgrade_Form(); | |
117 | $upgrade->processSQL($rev); | |
118 | ||
119 | return TRUE; | |
120 | } | |
121 | ||
122 | /** | |
54957108 | 123 | * Syntactic sugar for adding a task. |
124 | * | |
125 | * Task is (a) in this class and (b) has a high priority. | |
bf6a5362 CW |
126 | * |
127 | * After passing the $funcName, you can also pass parameters that will go to | |
128 | * the function. Note that all params must be serializable. | |
54957108 | 129 | * |
130 | * @param string $title | |
131 | * @param string $funcName | |
bf6a5362 CW |
132 | */ |
133 | protected function addTask($title, $funcName) { | |
be2fb01f | 134 | $queue = CRM_Queue_Service::singleton()->load([ |
bf6a5362 CW |
135 | 'type' => 'Sql', |
136 | 'name' => CRM_Upgrade_Form::QUEUE_NAME, | |
be2fb01f | 137 | ]); |
bf6a5362 CW |
138 | |
139 | $args = func_get_args(); | |
140 | $title = array_shift($args); | |
141 | $funcName = array_shift($args); | |
142 | $task = new CRM_Queue_Task( | |
be2fb01f | 143 | [get_class($this), $funcName], |
bf6a5362 CW |
144 | $args, |
145 | $title | |
146 | ); | |
be2fb01f | 147 | $queue->createItem($task, ['weight' => -1]); |
bf6a5362 CW |
148 | } |
149 | ||
058b8a5e CW |
150 | /** |
151 | * Remove a payment processor if not in use | |
152 | * | |
87a33a95 CW |
153 | * @param CRM_Queue_TaskContext $ctx |
154 | * @param string $name | |
155 | * @return bool | |
058b8a5e CW |
156 | * @throws \CiviCRM_API3_Exception |
157 | */ | |
87a33a95 | 158 | public static function removePaymentProcessorType(CRM_Queue_TaskContext $ctx, $name) { |
be2fb01f | 159 | $processors = civicrm_api3('PaymentProcessor', 'getcount', ['payment_processor_type_id' => $name]); |
058b8a5e | 160 | if (empty($processors['result'])) { |
be2fb01f | 161 | $result = civicrm_api3('PaymentProcessorType', 'get', [ |
058b8a5e CW |
162 | 'name' => $name, |
163 | 'return' => 'id', | |
be2fb01f | 164 | ]); |
058b8a5e | 165 | if (!empty($result['id'])) { |
be2fb01f | 166 | civicrm_api3('PaymentProcessorType', 'delete', ['id' => $result['id']]); |
058b8a5e CW |
167 | } |
168 | } | |
87a33a95 | 169 | return TRUE; |
058b8a5e CW |
170 | } |
171 | ||
27e82c24 SL |
172 | /** |
173 | * @param string $table_name | |
174 | * @param string $constraint_name | |
175 | * @return bool | |
176 | */ | |
177 | public static function checkFKExists($table_name, $constraint_name) { | |
178 | return CRM_Core_BAO_SchemaHandler::checkFKExists($table_name, $constraint_name); | |
179 | } | |
180 | ||
b412f764 CW |
181 | /** |
182 | * Add a column to a table if it doesn't already exist | |
183 | * | |
184 | * @param CRM_Queue_TaskContext $ctx | |
185 | * @param string $table | |
186 | * @param string $column | |
187 | * @param string $properties | |
61612722 | 188 | * @param bool $localizable is this a field that should be localized |
9f266042 | 189 | * @param string|null $version CiviCRM version to use if rebuilding multilingual schema |
35fd9d21 | 190 | * @param bool $triggerRebuild should we trigger the rebuild of the multilingual schema |
9f266042 | 191 | * |
b412f764 CW |
192 | * @return bool |
193 | */ | |
35fd9d21 | 194 | public static function addColumn($ctx, $table, $column, $properties, $localizable = FALSE, $version = NULL, $triggerRebuild = TRUE) { |
394d18d3 | 195 | $locales = CRM_Core_I18n::getMultilingual(); |
be2fb01f | 196 | $queries = []; |
ce33da5a | 197 | if (!CRM_Core_BAO_SchemaHandler::checkIfFieldExists($table, $column, FALSE)) { |
394d18d3 | 198 | if ($locales) { |
61612722 | 199 | if ($localizable) { |
61612722 | 200 | foreach ($locales as $locale) { |
ce33da5a | 201 | if (!CRM_Core_BAO_SchemaHandler::checkIfFieldExists($table, "{$column}_{$locale}", FALSE)) { |
41ace555 SL |
202 | $queries[] = "ALTER TABLE `$table` ADD COLUMN `{$column}_{$locale}` $properties"; |
203 | } | |
61612722 SL |
204 | } |
205 | } | |
206 | else { | |
207 | $queries[] = "ALTER TABLE `$table` ADD COLUMN `$column` $properties"; | |
208 | } | |
209 | } | |
210 | else { | |
211 | $queries[] = "ALTER TABLE `$table` ADD COLUMN `$column` $properties"; | |
212 | } | |
213 | foreach ($queries as $query) { | |
be2fb01f | 214 | CRM_Core_DAO::executeQuery($query, [], TRUE, NULL, FALSE, FALSE); |
61612722 SL |
215 | } |
216 | } | |
35fd9d21 | 217 | if ($locales && $triggerRebuild) { |
76ee148d | 218 | CRM_Core_I18n_Schema::rebuildMultilingualSchema($locales, $version, TRUE); |
150676ad | 219 | } |
b412f764 CW |
220 | return TRUE; |
221 | } | |
222 | ||
c87e11eb EM |
223 | /** |
224 | * Add the specified option group, gracefully if it already exists. | |
225 | * | |
226 | * @param CRM_Queue_TaskContext $ctx | |
227 | * @param array $params | |
228 | * @param array $options | |
229 | * | |
230 | * @return bool | |
231 | */ | |
232 | public static function addOptionGroup(CRM_Queue_TaskContext $ctx, $params, $options): bool { | |
233 | $defaults = ['is_active' => 1]; | |
234 | $optionDefaults = ['is_active' => 1]; | |
235 | $optionDefaults['option_group_id'] = \CRM_Core_BAO_OptionGroup::ensureOptionGroupExists(array_merge($defaults, $params)); | |
236 | ||
237 | foreach ($options as $option) { | |
238 | \CRM_Core_BAO_OptionValue::ensureOptionValueExists(array_merge($optionDefaults, $option)); | |
239 | } | |
240 | return TRUE; | |
241 | } | |
242 | ||
fe83c251 | 243 | /** |
244 | * Do any relevant message template updates. | |
245 | * | |
246 | * @param CRM_Queue_TaskContext $ctx | |
247 | * @param string $version | |
248 | */ | |
249 | public static function updateMessageTemplates($ctx, $version) { | |
250 | $messageTemplateObject = new CRM_Upgrade_Incremental_MessageTemplates($version); | |
251 | $messageTemplateObject->updateTemplates(); | |
8515e10c | 252 | } |
fe83c251 | 253 | |
8515e10c EM |
254 | /** |
255 | * Updated a message token within a template. | |
256 | * | |
257 | * @param CRM_Queue_TaskContext $ctx | |
258 | * @param string $workflowName | |
259 | * @param string $old | |
260 | * @param string $new | |
261 | * @param $version | |
262 | * | |
263 | * @return bool | |
264 | */ | |
265 | public static function updateMessageToken($ctx, string $workflowName, string $old, string $new, $version):bool { | |
266 | $messageObj = new CRM_Upgrade_Incremental_MessageTemplates($version); | |
fa547918 EM |
267 | if (!empty($workflowName)) { |
268 | $messageObj->replaceTokenInTemplate($workflowName, $old, $new); | |
269 | } | |
270 | else { | |
271 | $messageObj->replaceTokenInMessageTemplates($old, $new); | |
272 | } | |
8515e10c | 273 | return TRUE; |
fe83c251 | 274 | } |
275 | ||
b2148ce1 | 276 | /** |
8ff591cb | 277 | * Updated a message token within a scheduled reminder. |
b2148ce1 EM |
278 | * |
279 | * @param CRM_Queue_TaskContext $ctx | |
280 | * @param string $old | |
281 | * @param string $new | |
282 | * @param $version | |
283 | * | |
284 | * @return bool | |
285 | */ | |
286 | public static function updateActionScheduleToken($ctx, string $old, string $new, $version):bool { | |
287 | $messageObj = new CRM_Upgrade_Incremental_MessageTemplates($version); | |
288 | $messageObj->replaceTokenInActionSchedule($old, $new); | |
289 | return TRUE; | |
290 | } | |
291 | ||
8ff591cb | 292 | /** |
2f7db718 | 293 | * Updated a message token within a label. |
8ff591cb EM |
294 | * |
295 | * @param CRM_Queue_TaskContext $ctx | |
296 | * @param string $old | |
297 | * @param string $new | |
298 | * @param $version | |
299 | * | |
300 | * @return bool | |
301 | */ | |
302 | public static function updatePrintLabelToken($ctx, string $old, string $new, $version):bool { | |
303 | $messageObj = new CRM_Upgrade_Incremental_MessageTemplates($version); | |
304 | $messageObj->replaceTokenInPrintLabel($old, $new); | |
305 | return TRUE; | |
306 | } | |
307 | ||
2f7db718 EM |
308 | /** |
309 | * Updated a message token within greeting options. | |
310 | * | |
311 | * @param CRM_Queue_TaskContext $ctx | |
312 | * @param string $old | |
313 | * @param string $new | |
314 | * @param $version | |
315 | * | |
316 | * @return bool | |
317 | */ | |
318 | public static function updateGreetingOptions($ctx, string $old, string $new, $version):bool { | |
319 | $messageObj = new CRM_Upgrade_Incremental_MessageTemplates($version); | |
320 | $messageObj->replaceTokenInGreetingOptions($old, $new); | |
321 | return TRUE; | |
322 | } | |
323 | ||
28da6cc0 ML |
324 | /** |
325 | * Updated a currency in civicrm_currency and related configurations | |
326 | * | |
327 | * @param CRM_Queue_TaskContext $ctx | |
328 | * @param string $old_name | |
329 | * @param string $new_name | |
330 | * | |
331 | * @return bool | |
332 | */ | |
333 | public static function updateCurrencyName($ctx, string $old_name, string $new_name): bool { | |
334 | CRM_Core_DAO::executeQuery('UPDATE civicrm_currency SET name = %1 WHERE name = %2', [ | |
335 | 1 => [$new_name, 'String'], | |
336 | 2 => [$old_name, 'String'], | |
337 | ]); | |
338 | ||
339 | $oid = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_option_group WHERE name = 'currencies_enabled'"); | |
340 | if ($oid) { | |
341 | CRM_Core_DAO::executeQuery('UPDATE civicrm_option_value SET value = %1 WHERE value = %2 AND option_group_id = %3', [ | |
342 | 1 => [$new_name, 'String'], | |
343 | 2 => [$old_name, 'String'], | |
344 | 3 => [$oid, 'String'], | |
345 | ]); | |
346 | } | |
347 | ||
348 | CRM_Core_DAO::executeQuery('UPDATE civicrm_participant SET fee_currency = %1 WHERE fee_currency = %2', [ | |
349 | 1 => [$new_name, 'String'], | |
350 | 2 => [$old_name, 'String'], | |
351 | ]); | |
352 | ||
353 | $tables = [ | |
354 | 'civicrm_contribution', | |
355 | 'civicrm_contribution_page', | |
356 | 'civicrm_contribution_recur', | |
357 | 'civicrm_contribution_soft', | |
358 | 'civicrm_event', | |
359 | 'civicrm_financial_item', | |
360 | 'civicrm_financial_trxn', | |
361 | 'civicrm_grant', | |
362 | 'civicrm_pcp', | |
363 | 'civicrm_pledge_payment', | |
364 | 'civicrm_pledge', | |
365 | 'civicrm_product', | |
366 | ]; | |
367 | ||
368 | foreach ($tables as $table) { | |
369 | CRM_Core_DAO::executeQuery('UPDATE %3 SET currency = %1 WHERE currency = %2', [ | |
370 | 1 => [$new_name, 'String'], | |
371 | 2 => [$old_name, 'String'], | |
372 | 3 => [$table, 'MysqlColumnNameOrAlias'], | |
373 | ]); | |
374 | } | |
375 | ||
376 | return TRUE; | |
377 | } | |
378 | ||
504770b4 | 379 | /** |
380 | * Re-save any valid values from contribute settings into the normal setting | |
381 | * format. | |
382 | * | |
383 | * We render the array of contribution_invoice_settings and any that have | |
384 | * metadata defined we add to the correct key. This is safe to run even if no | |
385 | * settings are to be converted, per the test in | |
386 | * testConvertUpgradeContributeSettings. | |
387 | * | |
388 | * @param $ctx | |
389 | * | |
390 | * @return bool | |
391 | */ | |
392 | public static function updateContributeSettings($ctx) { | |
ff6f993e | 393 | // Use a direct query as api now does some handling on this. |
394 | $settings = CRM_Core_DAO::executeQuery("SELECT value, domain_id FROM civicrm_setting WHERE name = 'contribution_invoice_settings'"); | |
395 | ||
396 | while ($settings->fetch()) { | |
397 | $contributionSettings = (array) CRM_Utils_String::unserialize($settings->value); | |
398 | foreach (array_merge(SettingsBag::getContributionInvoiceSettingKeys(), ['deferred_revenue_enabled' => 'deferred_revenue_enabled']) as $possibleKeyName => $settingName) { | |
399 | if (!empty($contributionSettings[$possibleKeyName]) && empty(Civi::settings($settings->domain_id)->getExplicit($settingName))) { | |
400 | Civi::settings($settings->domain_id)->set($settingName, $contributionSettings[$possibleKeyName]); | |
401 | } | |
402 | } | |
504770b4 | 403 | } |
404 | return TRUE; | |
405 | } | |
406 | ||
7015248a | 407 | /** |
408 | * Do any relevant smart group updates. | |
409 | * | |
410 | * @param CRM_Queue_TaskContext $ctx | |
ac241c34 | 411 | * @param array $actions |
7015248a | 412 | * |
413 | * @return bool | |
414 | */ | |
adcd5156 | 415 | public static function updateSmartGroups($ctx, $actions) { |
ac241c34 CW |
416 | $groupUpdateObject = new CRM_Upgrade_Incremental_SmartGroups(); |
417 | $groupUpdateObject->updateGroups($actions); | |
7015248a | 418 | return TRUE; |
419 | } | |
420 | ||
21ca2cb6 | 421 | /** |
422 | * Drop a column from a table if it exist. | |
423 | * | |
424 | * @param CRM_Queue_TaskContext $ctx | |
425 | * @param string $table | |
426 | * @param string $column | |
427 | * @return bool | |
428 | */ | |
429 | public static function dropColumn($ctx, $table, $column) { | |
430 | if (CRM_Core_BAO_SchemaHandler::checkIfFieldExists($table, $column)) { | |
431 | CRM_Core_DAO::executeQuery("ALTER TABLE `$table` DROP COLUMN `$column`", | |
be2fb01f | 432 | [], TRUE, NULL, FALSE, FALSE); |
21ca2cb6 | 433 | } |
434 | return TRUE; | |
435 | } | |
436 | ||
19415e64 | 437 | /** |
438 | * Add a index to a table column. | |
439 | * | |
440 | * @param CRM_Queue_TaskContext $ctx | |
441 | * @param string $table | |
8a493ab9 CW |
442 | * @param string|array $columns |
443 | * @param string $prefix | |
19415e64 | 444 | * @return bool |
445 | */ | |
8a493ab9 CW |
446 | public static function addIndex($ctx, $table, $columns, $prefix = 'index') { |
447 | $tables = [$table => (array) $columns]; | |
448 | CRM_Core_BAO_SchemaHandler::createIndexes($tables, $prefix); | |
19415e64 | 449 | |
450 | return TRUE; | |
451 | } | |
452 | ||
453 | /** | |
454 | * Drop a index from a table if it exist. | |
455 | * | |
456 | * @param CRM_Queue_TaskContext $ctx | |
457 | * @param string $table | |
458 | * @param string $indexName | |
459 | * @return bool | |
460 | */ | |
461 | public static function dropIndex($ctx, $table, $indexName) { | |
462 | CRM_Core_BAO_SchemaHandler::dropIndexIfExists($table, $indexName); | |
463 | ||
464 | return TRUE; | |
465 | } | |
466 | ||
e3ed5029 TO |
467 | /** |
468 | * Drop a table... but only if it's empty. | |
469 | * | |
470 | * @param CRM_Queue_TaskContext $ctx | |
471 | * @param string $table | |
472 | * @return bool | |
473 | */ | |
474 | public static function dropTableIfEmpty($ctx, $table) { | |
475 | if (CRM_Core_DAO::checkTableExists($table)) { | |
476 | if (!CRM_Core_DAO::checkTableHasData($table)) { | |
477 | CRM_Core_BAO_SchemaHandler::dropTable($table); | |
478 | } | |
479 | else { | |
480 | $ctx->log->warning("dropTableIfEmpty($table): Found data. Preserved table."); | |
481 | } | |
482 | } | |
483 | ||
484 | return TRUE; | |
485 | } | |
486 | ||
81e30d0a SL |
487 | /** |
488 | * Rebuild Multilingual Schema. | |
489 | * @param CRM_Queue_TaskContext $ctx | |
9f266042 | 490 | * @param string|null $version CiviCRM version to use if rebuilding multilingual schema |
491 | * | |
81e30d0a SL |
492 | * @return bool |
493 | */ | |
76ee148d | 494 | public static function rebuildMultilingalSchema($ctx, $version = NULL) { |
394d18d3 CW |
495 | $locales = CRM_Core_I18n::getMultilingual(); |
496 | if ($locales) { | |
76ee148d | 497 | CRM_Core_I18n_Schema::rebuildMultilingualSchema($locales, $version); |
81e30d0a SL |
498 | } |
499 | return TRUE; | |
500 | } | |
501 | ||
bf6a5362 | 502 | } |