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 | ||
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. | |
bf6a5362 CW |
52 | */ |
53 | public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) { | |
54 | } | |
55 | ||
bf6a5362 CW |
56 | /** |
57 | * (Queue Task Callback) | |
54957108 | 58 | * |
59 | * @param \CRM_Queue_TaskContext $ctx | |
60 | * @param string $rev | |
61 | * | |
62 | * @return bool | |
bf6a5362 CW |
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 | /** | |
54957108 | 72 | * Syntactic sugar for adding a task. |
73 | * | |
74 | * Task is (a) in this class and (b) has a high priority. | |
bf6a5362 CW |
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. | |
54957108 | 78 | * |
79 | * @param string $title | |
80 | * @param string $funcName | |
bf6a5362 CW |
81 | */ |
82 | protected function addTask($title, $funcName) { | |
be2fb01f | 83 | $queue = CRM_Queue_Service::singleton()->load([ |
bf6a5362 CW |
84 | 'type' => 'Sql', |
85 | 'name' => CRM_Upgrade_Form::QUEUE_NAME, | |
be2fb01f | 86 | ]); |
bf6a5362 CW |
87 | |
88 | $args = func_get_args(); | |
89 | $title = array_shift($args); | |
90 | $funcName = array_shift($args); | |
91 | $task = new CRM_Queue_Task( | |
be2fb01f | 92 | [get_class($this), $funcName], |
bf6a5362 CW |
93 | $args, |
94 | $title | |
95 | ); | |
be2fb01f | 96 | $queue->createItem($task, ['weight' => -1]); |
bf6a5362 CW |
97 | } |
98 | ||
058b8a5e CW |
99 | /** |
100 | * Remove a payment processor if not in use | |
101 | * | |
87a33a95 CW |
102 | * @param CRM_Queue_TaskContext $ctx |
103 | * @param string $name | |
104 | * @return bool | |
058b8a5e CW |
105 | * @throws \CiviCRM_API3_Exception |
106 | */ | |
87a33a95 | 107 | public static function removePaymentProcessorType(CRM_Queue_TaskContext $ctx, $name) { |
be2fb01f | 108 | $processors = civicrm_api3('PaymentProcessor', 'getcount', ['payment_processor_type_id' => $name]); |
058b8a5e | 109 | if (empty($processors['result'])) { |
be2fb01f | 110 | $result = civicrm_api3('PaymentProcessorType', 'get', [ |
058b8a5e CW |
111 | 'name' => $name, |
112 | 'return' => 'id', | |
be2fb01f | 113 | ]); |
058b8a5e | 114 | if (!empty($result['id'])) { |
be2fb01f | 115 | civicrm_api3('PaymentProcessorType', 'delete', ['id' => $result['id']]); |
058b8a5e CW |
116 | } |
117 | } | |
87a33a95 | 118 | return TRUE; |
058b8a5e CW |
119 | } |
120 | ||
27e82c24 SL |
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 | ||
b412f764 CW |
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 | |
61612722 | 137 | * @param bool $localizable is this a field that should be localized |
9f266042 | 138 | * @param string|null $version CiviCRM version to use if rebuilding multilingual schema |
139 | * | |
b412f764 CW |
140 | * @return bool |
141 | */ | |
76ee148d | 142 | public static function addColumn($ctx, $table, $column, $properties, $localizable = FALSE, $version = NULL) { |
394d18d3 | 143 | $locales = CRM_Core_I18n::getMultilingual(); |
be2fb01f | 144 | $queries = []; |
ce33da5a | 145 | if (!CRM_Core_BAO_SchemaHandler::checkIfFieldExists($table, $column, FALSE)) { |
394d18d3 | 146 | if ($locales) { |
61612722 | 147 | if ($localizable) { |
61612722 | 148 | foreach ($locales as $locale) { |
ce33da5a | 149 | if (!CRM_Core_BAO_SchemaHandler::checkIfFieldExists($table, "{$column}_{$locale}", FALSE)) { |
41ace555 SL |
150 | $queries[] = "ALTER TABLE `$table` ADD COLUMN `{$column}_{$locale}` $properties"; |
151 | } | |
61612722 SL |
152 | } |
153 | } | |
154 | else { | |
155 | $queries[] = "ALTER TABLE `$table` ADD COLUMN `$column` $properties"; | |
156 | } | |
157 | } | |
158 | else { | |
159 | $queries[] = "ALTER TABLE `$table` ADD COLUMN `$column` $properties"; | |
160 | } | |
161 | foreach ($queries as $query) { | |
be2fb01f | 162 | CRM_Core_DAO::executeQuery($query, [], TRUE, NULL, FALSE, FALSE); |
61612722 SL |
163 | } |
164 | } | |
394d18d3 | 165 | if ($locales) { |
76ee148d | 166 | CRM_Core_I18n_Schema::rebuildMultilingualSchema($locales, $version, TRUE); |
150676ad | 167 | } |
b412f764 CW |
168 | return TRUE; |
169 | } | |
170 | ||
fe83c251 | 171 | /** |
172 | * Do any relevant message template updates. | |
173 | * | |
174 | * @param CRM_Queue_TaskContext $ctx | |
175 | * @param string $version | |
176 | */ | |
177 | public static function updateMessageTemplates($ctx, $version) { | |
178 | $messageTemplateObject = new CRM_Upgrade_Incremental_MessageTemplates($version); | |
179 | $messageTemplateObject->updateTemplates(); | |
180 | ||
181 | } | |
182 | ||
504770b4 | 183 | /** |
184 | * Re-save any valid values from contribute settings into the normal setting | |
185 | * format. | |
186 | * | |
187 | * We render the array of contribution_invoice_settings and any that have | |
188 | * metadata defined we add to the correct key. This is safe to run even if no | |
189 | * settings are to be converted, per the test in | |
190 | * testConvertUpgradeContributeSettings. | |
191 | * | |
192 | * @param $ctx | |
193 | * | |
194 | * @return bool | |
195 | */ | |
196 | public static function updateContributeSettings($ctx) { | |
ff6f993e | 197 | // Use a direct query as api now does some handling on this. |
198 | $settings = CRM_Core_DAO::executeQuery("SELECT value, domain_id FROM civicrm_setting WHERE name = 'contribution_invoice_settings'"); | |
199 | ||
200 | while ($settings->fetch()) { | |
201 | $contributionSettings = (array) CRM_Utils_String::unserialize($settings->value); | |
202 | foreach (array_merge(SettingsBag::getContributionInvoiceSettingKeys(), ['deferred_revenue_enabled' => 'deferred_revenue_enabled']) as $possibleKeyName => $settingName) { | |
203 | if (!empty($contributionSettings[$possibleKeyName]) && empty(Civi::settings($settings->domain_id)->getExplicit($settingName))) { | |
204 | Civi::settings($settings->domain_id)->set($settingName, $contributionSettings[$possibleKeyName]); | |
205 | } | |
206 | } | |
504770b4 | 207 | } |
208 | return TRUE; | |
209 | } | |
210 | ||
7015248a | 211 | /** |
212 | * Do any relevant smart group updates. | |
213 | * | |
214 | * @param CRM_Queue_TaskContext $ctx | |
ac241c34 | 215 | * @param array $actions |
7015248a | 216 | * |
217 | * @return bool | |
218 | */ | |
ac241c34 CW |
219 | public function updateSmartGroups($ctx, $actions) { |
220 | $groupUpdateObject = new CRM_Upgrade_Incremental_SmartGroups(); | |
221 | $groupUpdateObject->updateGroups($actions); | |
7015248a | 222 | return TRUE; |
223 | } | |
224 | ||
21ca2cb6 | 225 | /** |
226 | * Drop a column from a table if it exist. | |
227 | * | |
228 | * @param CRM_Queue_TaskContext $ctx | |
229 | * @param string $table | |
230 | * @param string $column | |
231 | * @return bool | |
232 | */ | |
233 | public static function dropColumn($ctx, $table, $column) { | |
234 | if (CRM_Core_BAO_SchemaHandler::checkIfFieldExists($table, $column)) { | |
235 | CRM_Core_DAO::executeQuery("ALTER TABLE `$table` DROP COLUMN `$column`", | |
be2fb01f | 236 | [], TRUE, NULL, FALSE, FALSE); |
21ca2cb6 | 237 | } |
238 | return TRUE; | |
239 | } | |
240 | ||
19415e64 | 241 | /** |
242 | * Add a index to a table column. | |
243 | * | |
244 | * @param CRM_Queue_TaskContext $ctx | |
245 | * @param string $table | |
b48a3167 | 246 | * @param string|array $column |
19415e64 | 247 | * @return bool |
248 | */ | |
249 | public static function addIndex($ctx, $table, $column) { | |
be2fb01f | 250 | $tables = [$table => (array) $column]; |
19415e64 | 251 | CRM_Core_BAO_SchemaHandler::createIndexes($tables); |
252 | ||
253 | return TRUE; | |
254 | } | |
255 | ||
256 | /** | |
257 | * Drop a index from a table if it exist. | |
258 | * | |
259 | * @param CRM_Queue_TaskContext $ctx | |
260 | * @param string $table | |
261 | * @param string $indexName | |
262 | * @return bool | |
263 | */ | |
264 | public static function dropIndex($ctx, $table, $indexName) { | |
265 | CRM_Core_BAO_SchemaHandler::dropIndexIfExists($table, $indexName); | |
266 | ||
267 | return TRUE; | |
268 | } | |
269 | ||
e3ed5029 TO |
270 | /** |
271 | * Drop a table... but only if it's empty. | |
272 | * | |
273 | * @param CRM_Queue_TaskContext $ctx | |
274 | * @param string $table | |
275 | * @return bool | |
276 | */ | |
277 | public static function dropTableIfEmpty($ctx, $table) { | |
278 | if (CRM_Core_DAO::checkTableExists($table)) { | |
279 | if (!CRM_Core_DAO::checkTableHasData($table)) { | |
280 | CRM_Core_BAO_SchemaHandler::dropTable($table); | |
281 | } | |
282 | else { | |
283 | $ctx->log->warning("dropTableIfEmpty($table): Found data. Preserved table."); | |
284 | } | |
285 | } | |
286 | ||
287 | return TRUE; | |
288 | } | |
289 | ||
81e30d0a SL |
290 | /** |
291 | * Rebuild Multilingual Schema. | |
292 | * @param CRM_Queue_TaskContext $ctx | |
9f266042 | 293 | * @param string|null $version CiviCRM version to use if rebuilding multilingual schema |
294 | * | |
81e30d0a SL |
295 | * @return bool |
296 | */ | |
76ee148d | 297 | public static function rebuildMultilingalSchema($ctx, $version = NULL) { |
394d18d3 CW |
298 | $locales = CRM_Core_I18n::getMultilingual(); |
299 | if ($locales) { | |
76ee148d | 300 | CRM_Core_I18n_Schema::rebuildMultilingualSchema($locales, $version); |
81e30d0a SL |
301 | } |
302 | return TRUE; | |
303 | } | |
304 | ||
bf6a5362 | 305 | } |