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 | ||
12 | /** | |
13 | * Base class for incremental upgrades | |
14 | */ | |
15 | class CRM_Upgrade_Incremental_Base { | |
16 | const BATCH_SIZE = 5000; | |
17 | ||
18 | /** | |
19 | * Verify DB state. | |
20 | * | |
21 | * @param $errors | |
22 | * | |
23 | * @return bool | |
24 | */ | |
25 | public function verifyPreDBstate(&$errors) { | |
26 | return TRUE; | |
27 | } | |
28 | ||
29 | /** | |
30 | * Compute any messages which should be displayed before upgrade. | |
31 | * | |
32 | * Note: This function is called iteratively for each upcoming | |
33 | * revision to the database. | |
34 | * | |
35 | * @param $preUpgradeMessage | |
36 | * @param string $rev | |
37 | * a version number, e.g. '4.8.alpha1', '4.8.beta3', '4.8.0'. | |
38 | * @param null $currentVer | |
39 | */ | |
40 | public function setPreUpgradeMessage(&$preUpgradeMessage, $rev, $currentVer = NULL) { | |
41 | } | |
42 | ||
43 | /** | |
44 | * Compute any messages which should be displayed after upgrade. | |
45 | * | |
46 | * @param string $postUpgradeMessage | |
47 | * alterable. | |
48 | * @param string $rev | |
49 | * an intermediate version; note that setPostUpgradeMessage is called repeatedly with different $revs. | |
bf6a5362 CW |
50 | */ |
51 | public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) { | |
52 | } | |
53 | ||
bf6a5362 CW |
54 | /** |
55 | * (Queue Task Callback) | |
54957108 | 56 | * |
57 | * @param \CRM_Queue_TaskContext $ctx | |
58 | * @param string $rev | |
59 | * | |
60 | * @return bool | |
bf6a5362 CW |
61 | */ |
62 | public static function runSql(CRM_Queue_TaskContext $ctx, $rev) { | |
63 | $upgrade = new CRM_Upgrade_Form(); | |
64 | $upgrade->processSQL($rev); | |
65 | ||
66 | return TRUE; | |
67 | } | |
68 | ||
69 | /** | |
54957108 | 70 | * Syntactic sugar for adding a task. |
71 | * | |
72 | * Task is (a) in this class and (b) has a high priority. | |
bf6a5362 CW |
73 | * |
74 | * After passing the $funcName, you can also pass parameters that will go to | |
75 | * the function. Note that all params must be serializable. | |
54957108 | 76 | * |
77 | * @param string $title | |
78 | * @param string $funcName | |
bf6a5362 CW |
79 | */ |
80 | protected function addTask($title, $funcName) { | |
be2fb01f | 81 | $queue = CRM_Queue_Service::singleton()->load([ |
bf6a5362 CW |
82 | 'type' => 'Sql', |
83 | 'name' => CRM_Upgrade_Form::QUEUE_NAME, | |
be2fb01f | 84 | ]); |
bf6a5362 CW |
85 | |
86 | $args = func_get_args(); | |
87 | $title = array_shift($args); | |
88 | $funcName = array_shift($args); | |
89 | $task = new CRM_Queue_Task( | |
be2fb01f | 90 | [get_class($this), $funcName], |
bf6a5362 CW |
91 | $args, |
92 | $title | |
93 | ); | |
be2fb01f | 94 | $queue->createItem($task, ['weight' => -1]); |
bf6a5362 CW |
95 | } |
96 | ||
058b8a5e CW |
97 | /** |
98 | * Remove a payment processor if not in use | |
99 | * | |
87a33a95 CW |
100 | * @param CRM_Queue_TaskContext $ctx |
101 | * @param string $name | |
102 | * @return bool | |
058b8a5e CW |
103 | * @throws \CiviCRM_API3_Exception |
104 | */ | |
87a33a95 | 105 | public static function removePaymentProcessorType(CRM_Queue_TaskContext $ctx, $name) { |
be2fb01f | 106 | $processors = civicrm_api3('PaymentProcessor', 'getcount', ['payment_processor_type_id' => $name]); |
058b8a5e | 107 | if (empty($processors['result'])) { |
be2fb01f | 108 | $result = civicrm_api3('PaymentProcessorType', 'get', [ |
058b8a5e CW |
109 | 'name' => $name, |
110 | 'return' => 'id', | |
be2fb01f | 111 | ]); |
058b8a5e | 112 | if (!empty($result['id'])) { |
be2fb01f | 113 | civicrm_api3('PaymentProcessorType', 'delete', ['id' => $result['id']]); |
058b8a5e CW |
114 | } |
115 | } | |
87a33a95 | 116 | return TRUE; |
058b8a5e CW |
117 | } |
118 | ||
27e82c24 SL |
119 | /** |
120 | * @param string $table_name | |
121 | * @param string $constraint_name | |
122 | * @return bool | |
123 | */ | |
124 | public static function checkFKExists($table_name, $constraint_name) { | |
125 | return CRM_Core_BAO_SchemaHandler::checkFKExists($table_name, $constraint_name); | |
126 | } | |
127 | ||
b412f764 CW |
128 | /** |
129 | * Add a column to a table if it doesn't already exist | |
130 | * | |
131 | * @param CRM_Queue_TaskContext $ctx | |
132 | * @param string $table | |
133 | * @param string $column | |
134 | * @param string $properties | |
61612722 | 135 | * @param bool $localizable is this a field that should be localized |
9f266042 | 136 | * @param string|null $version CiviCRM version to use if rebuilding multilingual schema |
137 | * | |
b412f764 CW |
138 | * @return bool |
139 | */ | |
76ee148d | 140 | public static function addColumn($ctx, $table, $column, $properties, $localizable = FALSE, $version = NULL) { |
150676ad SL |
141 | $domain = new CRM_Core_DAO_Domain(); |
142 | $domain->find(TRUE); | |
be2fb01f | 143 | $queries = []; |
ce33da5a | 144 | if (!CRM_Core_BAO_SchemaHandler::checkIfFieldExists($table, $column, FALSE)) { |
61612722 SL |
145 | if ($domain->locales) { |
146 | if ($localizable) { | |
147 | $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales); | |
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 | } | |
150676ad SL |
165 | if ($domain->locales) { |
166 | $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales); | |
76ee148d | 167 | CRM_Core_I18n_Schema::rebuildMultilingualSchema($locales, $version, TRUE); |
150676ad | 168 | } |
b412f764 CW |
169 | return TRUE; |
170 | } | |
171 | ||
fe83c251 | 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 | ||
504770b4 | 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 | $settings = Civi::settings()->get('contribution_invoice_settings'); | |
199 | $metadata = \Civi\Core\SettingsMetadata::getMetadata(); | |
200 | $conversions = array_intersect_key((array) $settings, $metadata); | |
201 | foreach ($conversions as $key => $conversion) { | |
202 | Civi::settings()->set($key, $conversion); | |
203 | } | |
204 | return TRUE; | |
205 | } | |
206 | ||
7015248a | 207 | /** |
208 | * Do any relevant smart group updates. | |
209 | * | |
210 | * @param CRM_Queue_TaskContext $ctx | |
ac241c34 | 211 | * @param array $actions |
7015248a | 212 | * |
213 | * @return bool | |
214 | */ | |
ac241c34 CW |
215 | public function updateSmartGroups($ctx, $actions) { |
216 | $groupUpdateObject = new CRM_Upgrade_Incremental_SmartGroups(); | |
217 | $groupUpdateObject->updateGroups($actions); | |
7015248a | 218 | return TRUE; |
219 | } | |
220 | ||
21ca2cb6 | 221 | /** |
222 | * Drop a column from a table if it exist. | |
223 | * | |
224 | * @param CRM_Queue_TaskContext $ctx | |
225 | * @param string $table | |
226 | * @param string $column | |
227 | * @return bool | |
228 | */ | |
229 | public static function dropColumn($ctx, $table, $column) { | |
230 | if (CRM_Core_BAO_SchemaHandler::checkIfFieldExists($table, $column)) { | |
231 | CRM_Core_DAO::executeQuery("ALTER TABLE `$table` DROP COLUMN `$column`", | |
be2fb01f | 232 | [], TRUE, NULL, FALSE, FALSE); |
21ca2cb6 | 233 | } |
234 | return TRUE; | |
235 | } | |
236 | ||
19415e64 | 237 | /** |
238 | * Add a index to a table column. | |
239 | * | |
240 | * @param CRM_Queue_TaskContext $ctx | |
241 | * @param string $table | |
b48a3167 | 242 | * @param string|array $column |
19415e64 | 243 | * @return bool |
244 | */ | |
245 | public static function addIndex($ctx, $table, $column) { | |
be2fb01f | 246 | $tables = [$table => (array) $column]; |
19415e64 | 247 | CRM_Core_BAO_SchemaHandler::createIndexes($tables); |
248 | ||
249 | return TRUE; | |
250 | } | |
251 | ||
252 | /** | |
253 | * Drop a index from a table if it exist. | |
254 | * | |
255 | * @param CRM_Queue_TaskContext $ctx | |
256 | * @param string $table | |
257 | * @param string $indexName | |
258 | * @return bool | |
259 | */ | |
260 | public static function dropIndex($ctx, $table, $indexName) { | |
261 | CRM_Core_BAO_SchemaHandler::dropIndexIfExists($table, $indexName); | |
262 | ||
263 | return TRUE; | |
264 | } | |
265 | ||
e3ed5029 TO |
266 | /** |
267 | * Drop a table... but only if it's empty. | |
268 | * | |
269 | * @param CRM_Queue_TaskContext $ctx | |
270 | * @param string $table | |
271 | * @return bool | |
272 | */ | |
273 | public static function dropTableIfEmpty($ctx, $table) { | |
274 | if (CRM_Core_DAO::checkTableExists($table)) { | |
275 | if (!CRM_Core_DAO::checkTableHasData($table)) { | |
276 | CRM_Core_BAO_SchemaHandler::dropTable($table); | |
277 | } | |
278 | else { | |
279 | $ctx->log->warning("dropTableIfEmpty($table): Found data. Preserved table."); | |
280 | } | |
281 | } | |
282 | ||
283 | return TRUE; | |
284 | } | |
285 | ||
81e30d0a SL |
286 | /** |
287 | * Rebuild Multilingual Schema. | |
288 | * @param CRM_Queue_TaskContext $ctx | |
9f266042 | 289 | * @param string|null $version CiviCRM version to use if rebuilding multilingual schema |
290 | * | |
81e30d0a SL |
291 | * @return bool |
292 | */ | |
76ee148d | 293 | public static function rebuildMultilingalSchema($ctx, $version = NULL) { |
81e30d0a SL |
294 | $domain = new CRM_Core_DAO_Domain(); |
295 | $domain->find(TRUE); | |
296 | if ($domain->locales) { | |
297 | $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales); | |
76ee148d | 298 | CRM_Core_I18n_Schema::rebuildMultilingualSchema($locales, $version); |
81e30d0a SL |
299 | } |
300 | return TRUE; | |
301 | } | |
302 | ||
bf6a5362 | 303 | } |