Merge pull request #13582 from colemanw/kam
[civicrm-core.git] / CRM / Upgrade / Incremental / Base.php
CommitLineData
bf6a5362
CW
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 .alpha1 |
bf6a5362 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
bf6a5362
CW
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28/**
29 * Base class for incremental upgrades
30 */
31class CRM_Upgrade_Incremental_Base {
32 const BATCH_SIZE = 5000;
33
34 /**
35 * Verify DB state.
36 *
37 * @param $errors
38 *
39 * @return bool
40 */
41 public function verifyPreDBstate(&$errors) {
42 return TRUE;
43 }
44
45 /**
46 * Compute any messages which should be displayed before upgrade.
47 *
48 * Note: This function is called iteratively for each upcoming
49 * revision to the database.
50 *
51 * @param $preUpgradeMessage
52 * @param string $rev
53 * a version number, e.g. '4.8.alpha1', '4.8.beta3', '4.8.0'.
54 * @param null $currentVer
55 */
56 public function setPreUpgradeMessage(&$preUpgradeMessage, $rev, $currentVer = NULL) {
57 }
58
59 /**
60 * Compute any messages which should be displayed after upgrade.
61 *
62 * @param string $postUpgradeMessage
63 * alterable.
64 * @param string $rev
65 * an intermediate version; note that setPostUpgradeMessage is called repeatedly with different $revs.
bf6a5362
CW
66 */
67 public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
68 }
69
bf6a5362
CW
70 /**
71 * (Queue Task Callback)
54957108 72 *
73 * @param \CRM_Queue_TaskContext $ctx
74 * @param string $rev
75 *
76 * @return bool
bf6a5362
CW
77 */
78 public static function runSql(CRM_Queue_TaskContext $ctx, $rev) {
79 $upgrade = new CRM_Upgrade_Form();
80 $upgrade->processSQL($rev);
81
82 return TRUE;
83 }
84
85 /**
54957108 86 * Syntactic sugar for adding a task.
87 *
88 * Task is (a) in this class and (b) has a high priority.
bf6a5362
CW
89 *
90 * After passing the $funcName, you can also pass parameters that will go to
91 * the function. Note that all params must be serializable.
54957108 92 *
93 * @param string $title
94 * @param string $funcName
bf6a5362
CW
95 */
96 protected function addTask($title, $funcName) {
97 $queue = CRM_Queue_Service::singleton()->load(array(
98 'type' => 'Sql',
99 'name' => CRM_Upgrade_Form::QUEUE_NAME,
100 ));
101
102 $args = func_get_args();
103 $title = array_shift($args);
104 $funcName = array_shift($args);
105 $task = new CRM_Queue_Task(
106 array(get_class($this), $funcName),
107 $args,
108 $title
109 );
110 $queue->createItem($task, array('weight' => -1));
111 }
112
058b8a5e
CW
113 /**
114 * Remove a payment processor if not in use
115 *
87a33a95
CW
116 * @param CRM_Queue_TaskContext $ctx
117 * @param string $name
118 * @return bool
058b8a5e
CW
119 * @throws \CiviCRM_API3_Exception
120 */
87a33a95 121 public static function removePaymentProcessorType(CRM_Queue_TaskContext $ctx, $name) {
058b8a5e
CW
122 $processors = civicrm_api3('PaymentProcessor', 'getcount', array('payment_processor_type_id' => $name));
123 if (empty($processors['result'])) {
124 $result = civicrm_api3('PaymentProcessorType', 'get', array(
125 'name' => $name,
126 'return' => 'id',
127 ));
128 if (!empty($result['id'])) {
129 civicrm_api3('PaymentProcessorType', 'delete', array('id' => $result['id']));
130 }
131 }
87a33a95 132 return TRUE;
058b8a5e
CW
133 }
134
27e82c24
SL
135 /**
136 * @param string $table_name
137 * @param string $constraint_name
138 * @return bool
139 */
140 public static function checkFKExists($table_name, $constraint_name) {
141 return CRM_Core_BAO_SchemaHandler::checkFKExists($table_name, $constraint_name);
142 }
143
b412f764
CW
144 /**
145 * Add a column to a table if it doesn't already exist
146 *
147 * @param CRM_Queue_TaskContext $ctx
148 * @param string $table
149 * @param string $column
150 * @param string $properties
61612722 151 * @param bool $localizable is this a field that should be localized
b412f764
CW
152 * @return bool
153 */
61612722 154 public static function addColumn($ctx, $table, $column, $properties, $localizable = FALSE) {
150676ad
SL
155 $domain = new CRM_Core_DAO_Domain();
156 $domain->find(TRUE);
61612722
SL
157 $queries = array();
158 if (!CRM_Core_BAO_SchemaHandler::checkIfFieldExists($table, $column)) {
159 if ($domain->locales) {
160 if ($localizable) {
161 $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales);
162 foreach ($locales as $locale) {
41ace555
SL
163 if (!CRM_Core_BAO_SchemaHandler::checkIfFieldExists($table, "{$column}_{$locale}")) {
164 $queries[] = "ALTER TABLE `$table` ADD COLUMN `{$column}_{$locale}` $properties";
165 }
61612722
SL
166 }
167 }
168 else {
169 $queries[] = "ALTER TABLE `$table` ADD COLUMN `$column` $properties";
170 }
171 }
172 else {
173 $queries[] = "ALTER TABLE `$table` ADD COLUMN `$column` $properties";
174 }
175 foreach ($queries as $query) {
176 CRM_Core_DAO::executeQuery($query, array(), TRUE, NULL, FALSE, FALSE);
177 }
178 }
150676ad
SL
179 if ($domain->locales) {
180 $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales);
41ace555 181 CRM_Core_I18n_Schema::rebuildMultilingualSchema($locales, NULL, TRUE);
150676ad 182 }
b412f764
CW
183 return TRUE;
184 }
185
fe83c251 186 /**
187 * Do any relevant message template updates.
188 *
189 * @param CRM_Queue_TaskContext $ctx
190 * @param string $version
191 */
192 public static function updateMessageTemplates($ctx, $version) {
193 $messageTemplateObject = new CRM_Upgrade_Incremental_MessageTemplates($version);
194 $messageTemplateObject->updateTemplates();
195
196 }
197
7015248a 198 /**
199 * Do any relevant smart group updates.
200 *
201 * @param CRM_Queue_TaskContext $ctx
202 * @param string $version
203 *
204 * @return bool
205 */
206 public function updateSmartGroups($ctx, $version) {
207 $groupUpdateObject = new CRM_Upgrade_Incremental_SmartGroups($version);
208 $groupUpdateObject->updateGroups();
209 return TRUE;
210 }
211
21ca2cb6 212 /**
213 * Drop a column from a table if it exist.
214 *
215 * @param CRM_Queue_TaskContext $ctx
216 * @param string $table
217 * @param string $column
218 * @return bool
219 */
220 public static function dropColumn($ctx, $table, $column) {
221 if (CRM_Core_BAO_SchemaHandler::checkIfFieldExists($table, $column)) {
222 CRM_Core_DAO::executeQuery("ALTER TABLE `$table` DROP COLUMN `$column`",
223 array(), TRUE, NULL, FALSE, FALSE);
224 }
225 return TRUE;
226 }
227
19415e64 228 /**
229 * Add a index to a table column.
230 *
231 * @param CRM_Queue_TaskContext $ctx
232 * @param string $table
b48a3167 233 * @param string|array $column
19415e64 234 * @return bool
235 */
236 public static function addIndex($ctx, $table, $column) {
b48a3167 237 $tables = array($table => (array) $column);
19415e64 238 CRM_Core_BAO_SchemaHandler::createIndexes($tables);
239
240 return TRUE;
241 }
242
243 /**
244 * Drop a index from a table if it exist.
245 *
246 * @param CRM_Queue_TaskContext $ctx
247 * @param string $table
248 * @param string $indexName
249 * @return bool
250 */
251 public static function dropIndex($ctx, $table, $indexName) {
252 CRM_Core_BAO_SchemaHandler::dropIndexIfExists($table, $indexName);
253
254 return TRUE;
255 }
256
81e30d0a
SL
257 /**
258 * Rebuild Multilingual Schema.
259 * @param CRM_Queue_TaskContext $ctx
260 * @return bool
261 */
262 public static function rebuildMultilingalSchema($ctx) {
263 $domain = new CRM_Core_DAO_Domain();
264 $domain->find(TRUE);
265 if ($domain->locales) {
266 $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales);
267 CRM_Core_I18n_Schema::rebuildMultilingualSchema($locales);
268 }
269 return TRUE;
270 }
271
bf6a5362 272}