Merge pull request #18640 from eileenmcnaughton/syn
[civicrm-core.git] / CRM / Upgrade / Incremental / Base.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 use Civi\Core\SettingsBag;
13
14 /**
15 * Base class for incremental upgrades
16 */
17 class CRM_Upgrade_Incremental_Base {
18 const BATCH_SIZE = 5000;
19
20 /**
21 * Verify DB state.
22 *
23 * @param $errors
24 *
25 * @return bool
26 */
27 public function verifyPreDBstate(&$errors) {
28 return TRUE;
29 }
30
31 /**
32 * Compute any messages which should be displayed before upgrade.
33 *
34 * Note: This function is called iteratively for each upcoming
35 * revision to the database.
36 *
37 * @param $preUpgradeMessage
38 * @param string $rev
39 * a version number, e.g. '4.8.alpha1', '4.8.beta3', '4.8.0'.
40 * @param null $currentVer
41 */
42 public function setPreUpgradeMessage(&$preUpgradeMessage, $rev, $currentVer = NULL) {
43 }
44
45 /**
46 * Compute any messages which should be displayed after upgrade.
47 *
48 * @param string $postUpgradeMessage
49 * alterable.
50 * @param string $rev
51 * an intermediate version; note that setPostUpgradeMessage is called repeatedly with different $revs.
52 */
53 public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
54 }
55
56 /**
57 * (Queue Task Callback)
58 *
59 * @param \CRM_Queue_TaskContext $ctx
60 * @param string $rev
61 *
62 * @return bool
63 */
64 public static function runSql(CRM_Queue_TaskContext $ctx, $rev) {
65 $upgrade = new CRM_Upgrade_Form();
66 $upgrade->processSQL($rev);
67
68 return TRUE;
69 }
70
71 /**
72 * Syntactic sugar for adding a task.
73 *
74 * Task is (a) in this class and (b) has a high priority.
75 *
76 * After passing the $funcName, you can also pass parameters that will go to
77 * the function. Note that all params must be serializable.
78 *
79 * @param string $title
80 * @param string $funcName
81 */
82 protected function addTask($title, $funcName) {
83 $queue = CRM_Queue_Service::singleton()->load([
84 'type' => 'Sql',
85 'name' => CRM_Upgrade_Form::QUEUE_NAME,
86 ]);
87
88 $args = func_get_args();
89 $title = array_shift($args);
90 $funcName = array_shift($args);
91 $task = new CRM_Queue_Task(
92 [get_class($this), $funcName],
93 $args,
94 $title
95 );
96 $queue->createItem($task, ['weight' => -1]);
97 }
98
99 /**
100 * Remove a payment processor if not in use
101 *
102 * @param CRM_Queue_TaskContext $ctx
103 * @param string $name
104 * @return bool
105 * @throws \CiviCRM_API3_Exception
106 */
107 public static function removePaymentProcessorType(CRM_Queue_TaskContext $ctx, $name) {
108 $processors = civicrm_api3('PaymentProcessor', 'getcount', ['payment_processor_type_id' => $name]);
109 if (empty($processors['result'])) {
110 $result = civicrm_api3('PaymentProcessorType', 'get', [
111 'name' => $name,
112 'return' => 'id',
113 ]);
114 if (!empty($result['id'])) {
115 civicrm_api3('PaymentProcessorType', 'delete', ['id' => $result['id']]);
116 }
117 }
118 return TRUE;
119 }
120
121 /**
122 * @param string $table_name
123 * @param string $constraint_name
124 * @return bool
125 */
126 public static function checkFKExists($table_name, $constraint_name) {
127 return CRM_Core_BAO_SchemaHandler::checkFKExists($table_name, $constraint_name);
128 }
129
130 /**
131 * Add a column to a table if it doesn't already exist
132 *
133 * @param CRM_Queue_TaskContext $ctx
134 * @param string $table
135 * @param string $column
136 * @param string $properties
137 * @param bool $localizable is this a field that should be localized
138 * @param string|null $version CiviCRM version to use if rebuilding multilingual schema
139 * @param bool $triggerRebuild should we trigger the rebuild of the multilingual schema
140 *
141 * @return bool
142 */
143 public static function addColumn($ctx, $table, $column, $properties, $localizable = FALSE, $version = NULL, $triggerRebuild = TRUE) {
144 $locales = CRM_Core_I18n::getMultilingual();
145 $queries = [];
146 if (!CRM_Core_BAO_SchemaHandler::checkIfFieldExists($table, $column, FALSE)) {
147 if ($locales) {
148 if ($localizable) {
149 foreach ($locales as $locale) {
150 if (!CRM_Core_BAO_SchemaHandler::checkIfFieldExists($table, "{$column}_{$locale}", FALSE)) {
151 $queries[] = "ALTER TABLE `$table` ADD COLUMN `{$column}_{$locale}` $properties";
152 }
153 }
154 }
155 else {
156 $queries[] = "ALTER TABLE `$table` ADD COLUMN `$column` $properties";
157 }
158 }
159 else {
160 $queries[] = "ALTER TABLE `$table` ADD COLUMN `$column` $properties";
161 }
162 foreach ($queries as $query) {
163 CRM_Core_DAO::executeQuery($query, [], TRUE, NULL, FALSE, FALSE);
164 }
165 }
166 if ($locales && $triggerRebuild) {
167 CRM_Core_I18n_Schema::rebuildMultilingualSchema($locales, $version, TRUE);
168 }
169 return TRUE;
170 }
171
172 /**
173 * Do any relevant message template updates.
174 *
175 * @param CRM_Queue_TaskContext $ctx
176 * @param string $version
177 */
178 public static function updateMessageTemplates($ctx, $version) {
179 $messageTemplateObject = new CRM_Upgrade_Incremental_MessageTemplates($version);
180 $messageTemplateObject->updateTemplates();
181
182 }
183
184 /**
185 * Re-save any valid values from contribute settings into the normal setting
186 * format.
187 *
188 * We render the array of contribution_invoice_settings and any that have
189 * metadata defined we add to the correct key. This is safe to run even if no
190 * settings are to be converted, per the test in
191 * testConvertUpgradeContributeSettings.
192 *
193 * @param $ctx
194 *
195 * @return bool
196 */
197 public static function updateContributeSettings($ctx) {
198 // Use a direct query as api now does some handling on this.
199 $settings = CRM_Core_DAO::executeQuery("SELECT value, domain_id FROM civicrm_setting WHERE name = 'contribution_invoice_settings'");
200
201 while ($settings->fetch()) {
202 $contributionSettings = (array) CRM_Utils_String::unserialize($settings->value);
203 foreach (array_merge(SettingsBag::getContributionInvoiceSettingKeys(), ['deferred_revenue_enabled' => 'deferred_revenue_enabled']) as $possibleKeyName => $settingName) {
204 if (!empty($contributionSettings[$possibleKeyName]) && empty(Civi::settings($settings->domain_id)->getExplicit($settingName))) {
205 Civi::settings($settings->domain_id)->set($settingName, $contributionSettings[$possibleKeyName]);
206 }
207 }
208 }
209 return TRUE;
210 }
211
212 /**
213 * Do any relevant smart group updates.
214 *
215 * @param CRM_Queue_TaskContext $ctx
216 * @param array $actions
217 *
218 * @return bool
219 */
220 public function updateSmartGroups($ctx, $actions) {
221 $groupUpdateObject = new CRM_Upgrade_Incremental_SmartGroups();
222 $groupUpdateObject->updateGroups($actions);
223 return TRUE;
224 }
225
226 /**
227 * Drop a column from a table if it exist.
228 *
229 * @param CRM_Queue_TaskContext $ctx
230 * @param string $table
231 * @param string $column
232 * @return bool
233 */
234 public static function dropColumn($ctx, $table, $column) {
235 if (CRM_Core_BAO_SchemaHandler::checkIfFieldExists($table, $column)) {
236 CRM_Core_DAO::executeQuery("ALTER TABLE `$table` DROP COLUMN `$column`",
237 [], TRUE, NULL, FALSE, FALSE);
238 }
239 return TRUE;
240 }
241
242 /**
243 * Add a index to a table column.
244 *
245 * @param CRM_Queue_TaskContext $ctx
246 * @param string $table
247 * @param string|array $column
248 * @return bool
249 */
250 public static function addIndex($ctx, $table, $column) {
251 $tables = [$table => (array) $column];
252 CRM_Core_BAO_SchemaHandler::createIndexes($tables);
253
254 return TRUE;
255 }
256
257 /**
258 * Drop a index from a table if it exist.
259 *
260 * @param CRM_Queue_TaskContext $ctx
261 * @param string $table
262 * @param string $indexName
263 * @return bool
264 */
265 public static function dropIndex($ctx, $table, $indexName) {
266 CRM_Core_BAO_SchemaHandler::dropIndexIfExists($table, $indexName);
267
268 return TRUE;
269 }
270
271 /**
272 * Drop a table... but only if it's empty.
273 *
274 * @param CRM_Queue_TaskContext $ctx
275 * @param string $table
276 * @return bool
277 */
278 public static function dropTableIfEmpty($ctx, $table) {
279 if (CRM_Core_DAO::checkTableExists($table)) {
280 if (!CRM_Core_DAO::checkTableHasData($table)) {
281 CRM_Core_BAO_SchemaHandler::dropTable($table);
282 }
283 else {
284 $ctx->log->warning("dropTableIfEmpty($table): Found data. Preserved table.");
285 }
286 }
287
288 return TRUE;
289 }
290
291 /**
292 * Rebuild Multilingual Schema.
293 * @param CRM_Queue_TaskContext $ctx
294 * @param string|null $version CiviCRM version to use if rebuilding multilingual schema
295 *
296 * @return bool
297 */
298 public static function rebuildMultilingalSchema($ctx, $version = NULL) {
299 $locales = CRM_Core_I18n::getMultilingual();
300 if ($locales) {
301 CRM_Core_I18n_Schema::rebuildMultilingualSchema($locales, $version);
302 }
303 return TRUE;
304 }
305
306 }