Merge pull request #2288 from eileenmcnaughton/CRM-14043
[civicrm-core.git] / CRM / Upgrade / Incremental / php / FourFour.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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. |
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 along with this program; if not, contact CiviCRM LLC |
21 | at info[AT]civicrm[DOT]org. If you have questions about the |
22 | GNU Affero General Public License or the licensing of CiviCRM, |
23 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
24 +--------------------------------------------------------------------+
25 */
26
27 /**
28 *
29 * @package CRM
30 * @copyright CiviCRM LLC (c) 2004-2013
31 * $Id$
32 *
33 */
34 class CRM_Upgrade_Incremental_php_FourFour {
35 const BATCH_SIZE = 5000;
36
37 function verifyPreDBstate(&$errors) {
38 return TRUE;
39 }
40
41 /**
42 * Compute any messages which should be displayed beforeupgrade
43 *
44 * Note: This function is called iteratively for each upcoming
45 * revision to the database.
46 *
47 * @param $postUpgradeMessage string, alterable
48 * @param $rev string, a version number, e.g. '4.4.alpha1', '4.4.beta3', '4.4.0'
49 * @return void
50 */
51 function setPreUpgradeMessage(&$preUpgradeMessage, $rev, $currentVer = NULL) {
52 }
53
54 /**
55 * Compute any messages which should be displayed after upgrade
56 *
57 * @param $postUpgradeMessage string, alterable
58 * @param $rev string, an intermediate version; note that setPostUpgradeMessage is called repeatedly with different $revs
59 * @return void
60 */
61 function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
62 if ($rev == '4.4.1') {
63 $config = CRM_Core_Config::singleton();
64 if (!empty($config->useIDS)) {
65 $postUpgradeMessage .= '<br />' . ts("The setting to skip IDS check has been removed. Your site has this configured in civicrm.settings.php but it will no longer work. Instead, use the new permission 'skip IDS check' to bypass the IDS system.");
66 }
67 }
68 if ($rev == '4.4.3') {
69 $postUpgradeMessage .= '<br /><br />' . ts('Default versions of the following System Workflow Message Templates have been modified to handle new functionality: <ul><li>Events - Registration Confirmation and Receipt (on-line)</li></ul> If you have modified these templates, please review the new default versions and implement updates as needed to your copies (Administer > Communications > Message Templates > System Workflow Messages).');
70 }
71 if ($rev == '4.4.3') {
72 $query = "SELECT cft.id financial_trxn
73 FROM civicrm_financial_trxn cft
74 LEFT JOIN civicrm_entity_financial_trxn ceft ON ceft.financial_trxn_id = cft.id
75 LEFT JOIN civicrm_contribution cc ON ceft.entity_id = cc.id
76 WHERE ceft.entity_table = 'civicrm_contribution' AND cft.payment_instrument_id IS NULL;";
77 $dao = CRM_Core_DAO::executeQuery($query);
78 if ($dao->N) {
79 $postUpgradeMessage .= '<br /><br /><strong>' . ts('Your database contains %1 financial transaction records with no payment instrument (Paid By is empty). If you use the Accounting Batches feature this may result in unbalanced transactions. If you do not use this feature, you can ignore the condition (although you will be required to select a Paid By value for new transactions). <a href="%2" target="_blank">You can review steps to correct transactions with missing payment instruments on the wiki.</a>', array(1 => $dao->N, 2 => 'http://wiki.civicrm.org/confluence/display/CRMDOC/Fixing+Transactions+Missing+a+Payment+Instrument+-+4.4.3+Upgrades')) . '</strong>';
80 }
81 }
82 }
83
84 function upgrade_4_4_alpha1($rev) {
85 // task to process sql
86 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => '4.4.alpha1')), 'task_4_4_x_runSql', $rev);
87
88 // Consolidate activity contacts CRM-12274.
89 $this->addTask('Consolidate activity contacts', 'activityContacts');
90
91 return TRUE;
92 }
93
94 function upgrade_4_4_beta1($rev) {
95 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => '4.4.beta1')), 'task_4_4_x_runSql', $rev);
96
97 // add new 'data' column in civicrm_batch
98 $query = 'ALTER TABLE civicrm_batch ADD data LONGTEXT NULL COMMENT "cache entered data"';
99 CRM_Core_DAO::executeQuery($query, array(), TRUE, NULL, FALSE, FALSE);
100
101 // check if batch entry data exists in civicrm_cache table
102 $query = 'SELECT path, data FROM civicrm_cache WHERE group_name = "batch entry"';
103 $dao = CRM_Core_DAO::executeQuery($query);
104 while ($dao->fetch()) {
105 // get batch id $batchId[2]
106 $batchId = explode('-', $dao->path);
107 $data = unserialize($dao->data);
108
109 // move the data to civicrm_batch table
110 CRM_Core_DAO::setFieldValue('CRM_Batch_DAO_Batch', $batchId[2], 'data', json_encode(array('values' => $data)));
111 }
112
113 // delete entries from civicrm_cache table
114 $query = 'DELETE FROM civicrm_cache WHERE group_name = "batch entry"';
115 CRM_Core_DAO::executeQuery($query);
116
117 $this->addTask('Migrate custom word-replacements', 'wordReplacements');
118 }
119
120 function upgrade_4_4_1($rev) {
121 $config = CRM_Core_Config::singleton();
122 // CRM-13327 upgrade handling for the newly added name badges
123 $ogID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'name_badge', 'id', 'name');
124 $nameBadges = array_flip(array_values(CRM_Core_BAO_OptionValue::getOptionValuesAssocArrayFromName('name_badge')));
125 unset($nameBadges['Avery 5395']);
126 if (!empty($nameBadges)) {
127 $dimension = '{"paper-size":"a4","orientation":"portrait","font-name":"times","font-size":6,"font-style":"","NX":2,"NY":4,"metric":"mm","lMargin":6,"tMargin":19,"SpaceX":0,"SpaceY":0,"width":100,"height":65,"lPadding":0,"tPadding":0}';
128 $query = "UPDATE civicrm_option_value
129 SET value = '{$dimension}'
130 WHERE option_group_id = %1 AND name = 'Fattorini Name Badge 100x65'";
131
132 CRM_Core_DAO::executeQuery($query, array(1 => array($ogID, 'Integer')));
133 }
134 else {
135 $dimensions = array(
136 1 => '{"paper-size":"a4","orientation":"landscape","font-name":"times","font-size":6,"font-style":"","NX":2,"NY":1,"metric":"mm","lMargin":25,"tMargin":27,"SpaceX":0,"SpaceY":35,"width":106,"height":150,"lPadding":5,"tPadding":5}',
137 2 => '{"paper-size":"a4","orientation":"portrait","font-name":"times","font-size":6,"font-style":"","NX":2,"NY":4,"metric":"mm","lMargin":6,"tMargin":19,"SpaceX":0,"SpaceY":0,"width":100,"height":65,"lPadding":0,"tPadding":0}',
138 3 => '{"paper-size":"a4","orientation":"portrait","font-name":"times","font-size":6,"font-style":"","NX":2,"NY":2,"metric":"mm","lMargin":10,"tMargin":28,"SpaceX":0,"SpaceY":0,"width":96,"height":121,"lPadding":5,"tPadding":5}',
139 );
140 $insertStatements = array(
141 1 => "($ogID, %1, '{$dimensions[1]}', %1, NULL, 0, NULL, 2, NULL, 0, 0, 1, NULL, NULL)",
142 2 => "($ogID, %2, '{$dimensions[2]}', %2, NULL, 0, NULL, 3, NULL, 0, 0, 1, NULL, NULL)",
143 3 => "($ogID, %3, '{$dimensions[3]}', %3, NULL, 0, NULL, 4, NULL, 0, 0, 1, NULL, NULL)",
144 );
145
146 $queryParams = array(
147 1 => array('A6 Badge Portrait 150x106', 'String'),
148 2 => array('Fattorini Name Badge 100x65', 'String'),
149 3 => array('Hanging Badge 3-3/4" x 4-3"/4', 'String'),
150 );
151
152 foreach ($insertStatements as $values) {
153 $query = 'INSERT INTO civicrm_option_value (`option_group_id`, `label`, `value`, `name`, `grouping`, `filter`, `is_default`, `weight`, `description`, `is_optgroup`, `is_reserved`, `is_active`, `component_id`, `visibility_id`) VALUES' . $values;
154 CRM_Core_DAO::executeQuery($query, $queryParams);
155 }
156 }
157
158 // CRM-12578 - Prior to this version a CSS file under drupal would disable core css
159 if (!empty($config->customCSSURL) && strpos($config->userFramework, 'Drupal') === 0) {
160 // The new setting doesn't exist yet - need to create it first
161 CRM_Core_BAO_Setting::updateSettingsFromMetaData();
162 CRM_Core_BAO_Setting::setItem('1', CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'disable_core_css');
163 }
164
165 // CRM-13701 - Fix $config->timeInputFormat
166 $sql = "
167 SELECT time_format
168 FROM civicrm_preferences_date
169 WHERE time_format IS NOT NULL
170 AND time_format <> ''
171 LIMIT 1
172 ";
173 $timeInputFormat = CRM_Core_DAO::singleValueQuery($sql);
174 if ($timeInputFormat && $timeInputFormat != $config->timeInputFormat) {
175 $params = array('timeInputFormat' => $timeInputFormat);
176 CRM_Core_BAO_ConfigSetting::add($params);
177 }
178
179 // CRM-13698 - add 'Available' and 'No-show' activity statuses
180 $insertStatus = array();
181 $nsinc = $avinc = $inc = 0;
182 if (!CRM_Core_OptionGroup::getValue('activity_status', 'Available', 'name')) {
183 $insertStatus[] = "(%1, 'Available', %2, 'Available', NULL, 0, NULL, %3, 0, 0, 1, NULL, NULL)";
184 $avinc = $inc = 1;
185 }
186 if (!CRM_Core_OptionGroup::getValue('activity_status', 'No_show', 'name')) {
187 $insertStatus[] = "(%1, 'No-show', %4, 'No_show', NULL, 0, NULL, %5, 0, 0, 1, NULL, NULL)";
188 $nsinc = $inc + 1;
189 }
190 if (!empty($insertStatus)) {
191 $acOptionGroupID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'activity_status', 'id', 'name');
192 $maxVal = CRM_Core_DAO::singleValueQuery("SELECT MAX(ROUND(op.value)) FROM civicrm_option_value op WHERE op.option_group_id = $acOptionGroupID");
193 $maxWeight = CRM_Core_DAO::singleValueQuery("SELECT MAX(weight) FROM civicrm_option_value WHERE option_group_id = $acOptionGroupID");
194
195 $p[1] = array($acOptionGroupID, 'Integer');
196 if ($avinc) {
197 $p[2] = array($avinc+$maxVal, 'Integer');
198 $p[3] = array($avinc+$maxWeight, 'Integer');
199 }
200 if ($nsinc) {
201 $p[4] = array($nsinc+$maxVal, 'Integer');
202 $p[5] = array($nsinc+$maxWeight, 'Integer');
203 }
204 $insertStatus = implode(',', $insertStatus);
205
206 $sql = "
207 INSERT INTO
208 civicrm_option_value (`option_group_id`, label, `value`, `name`, `grouping`, `filter`, `is_default`, `weight`, `is_optgroup`, `is_reserved`, `is_active`, `component_id`, `visibility_id`)
209 VALUES {$insertStatus}";
210 CRM_Core_DAO::executeQuery($sql, $p);
211 }
212
213 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => '4.4.1')), 'task_4_4_x_runSql', $rev);
214 $this->addTask('Patch word-replacement schema', 'wordReplacements_patch', $rev);
215 }
216
217 function upgrade_4_4_4($rev) {
218 $fkConstraint = array();
219 if (!CRM_Core_DAO::checkFKConstraintInFormat('civicrm_activity_contact', 'activity_id')) {
220 $fkConstraint[] = "ADD CONSTRAINT `FK_civicrm_activity_contact_activity_id` FOREIGN KEY (`activity_id`) REFERENCES `civicrm_activity` (`id`) ON DELETE CASCADE";
221 }
222 if (!CRM_Core_DAO::checkFKConstraintInFormat('civicrm_activity_contact', 'contact_id')) {
223 $fkConstraint[] = "ADD CONSTRAINT `FK_civicrm_activity_contact_contact_id` FOREIGN KEY (`contact_id`) REFERENCES `civicrm_contact` (`id`) ON DELETE CASCADE;
224 ";
225 }
226
227 if (!empty($fkConstraint)) {
228 $fkConstraint = implode(',', $fkConstraint);
229 $sql = "ALTER TABLE `civicrm_activity_contact`
230 {$fkConstraint}
231 ";
232 // CRM-14036 : delete entries of un-mapped contacts
233 CRM_Core_DAO::executeQuery("DELETE ac FROM civicrm_activity_contact ac
234 LEFT JOIN civicrm_contact c
235 ON c.id = ac.contact_id
236 WHERE c.id IS NULL;
237 ");
238 // delete entries of un-mapped activities
239 CRM_Core_DAO::executeQuery("DELETE ac FROM civicrm_activity_contact ac
240 LEFT JOIN civicrm_activity a
241 ON a.id = ac.activity_id
242 WHERE a.id IS NULL;
243 ");
244
245 CRM_Core_DAO::executeQuery("SET FOREIGN_KEY_CHECKS=0;");
246 CRM_Core_DAO::executeQuery($sql);
247 CRM_Core_DAO::executeQuery("SET FOREIGN_KEY_CHECKS=1;");
248 }
249
250 // task to process sql
251 $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => '4.4.4')), 'task_4_4_x_runSql', $rev);
252
253 // Consolidate activity contacts CRM-12274.
254 $this->addTask('Dashboard schema', 'dashboard');
255
256 return TRUE;
257 }
258
259 static function dashboard(CRM_Queue_TaskContext $ctx) {
260 $upgrade = new CRM_Upgrade_Form();
261 $query = "
262 ALTER TABLE civicrm_dashboard
263 ADD name varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'Internal name of dashlet.' AFTER domain_id ";
264 CRM_Core_DAO::executeQuery($query);
265
266 $dashboard = new CRM_Core_DAO_Dashboard();
267 $dashboard->find();
268 while ($dashboard->fetch()) {
269 $urlElements = explode('/', $dashboard->url);
270 if ($urlElements[1] == 'dashlet') {
271 $url = explode('&', $urlElements[2]);
272 $name = $url[0];
273 }
274 elseif ($urlElements[1] == 'report') {
275 $url = explode('&', $urlElements[3]);
276 $name = 'report/' .$url[0];
277 }
278 $values .= "
279 WHEN {$dashboard->id} THEN '{$name}'
280 ";
281 }
282
283 $query = "
284 UPDATE civicrm_dashboard
285 SET name = CASE id
286 {$values}
287 END;
288 ";
289
290 CRM_Core_DAO::executeQuery($query);
291 return TRUE;
292 }
293
294 /**
295 * Update activity contacts CRM-12274
296 *
297 * @return bool TRUE for success
298 */
299 static function activityContacts(CRM_Queue_TaskContext $ctx) {
300 $upgrade = new CRM_Upgrade_Form();
301
302 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
303 $ovValue[] = $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
304 $ovValue[] = $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
305 $ovValue[] = $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
306
307 $optionGroupID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'activity_contacts', 'id', 'name');
308 if (!empty($ovValue)) {
309 $ovValues = implode(', ', $ovValue);
310 $query = "
311 UPDATE civicrm_option_value
312 SET is_reserved = 1
313 WHERE option_group_id = {$optionGroupID} AND value IN ($ovValues)";
314
315 $dao = CRM_Core_DAO::executeQuery($query);
316 }
317
318 if (!$assigneeID) {
319 $assigneeID = 1;
320 $value[] = "({$optionGroupID}, 'Activity Assignees', 1, 'Activity Assignees', 1, 1, 1)";
321 }
322 if (!$sourceID) {
323 $sourceID = 2;
324 $value[] = "({$optionGroupID}, 'Activity Source', 2, 'Activity Source', 2, 1, 1)";
325 }
326 if (!$targetID) {
327 $targetID = 3;
328 $value[] = "({$optionGroupID}, 'Activity Targets', 3, 'Activity Targets', 3, 1, 1)";
329 }
330
331 if (!$assigneeID || !$sourceID || !$targetID ) {
332 $insert = "
333 INSERT INTO civicrm_option_value
334 (option_group_id, label, value, name, weight, is_reserved, is_active)
335 VALUES
336
337 ";
338 $values = implode(', ', $value);
339 $query = $insert . $values;
340 $dao = CRM_Core_DAO::executeQuery($query);
341 }
342
343 // sometimes an user does not make a clean backup and the above table
344 // already exists, so lets delete this table - CRM-13665
345 $query = "DROP TABLE IF EXISTS civicrm_activity_contact";
346 $dao = CRM_Core_DAO::executeQuery($query);
347
348 $query = "
349 CREATE TABLE IF NOT EXISTS civicrm_activity_contact (
350 id int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Activity contact id',
351 activity_id int(10) unsigned NOT NULL COMMENT 'Foreign key to the activity for this record.',
352 contact_id int(10) unsigned NOT NULL COMMENT 'Foreign key to the contact for this record.',
353 record_type_id int(10) unsigned DEFAULT NULL COMMENT 'The record type id for this row',
354 PRIMARY KEY (id),
355 UNIQUE KEY UI_activity_contact (contact_id,activity_id,record_type_id),
356 KEY FK_civicrm_activity_contact_activity_id (activity_id)
357 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
358 ";
359
360 $dao = CRM_Core_DAO::executeQuery($query);
361
362
363 $query = "
364 INSERT INTO civicrm_activity_contact (activity_id, contact_id, record_type_id)
365 SELECT activity_id, target_contact_id, {$targetID} as record_type_id
366 FROM civicrm_activity_target";
367
368 $dao = CRM_Core_DAO::executeQuery($query);
369
370 $query = "
371 INSERT INTO civicrm_activity_contact (activity_id, contact_id, record_type_id)
372 SELECT activity_id, assignee_contact_id, {$assigneeID} as record_type_id
373 FROM civicrm_activity_assignment";
374 $dao = CRM_Core_DAO::executeQuery($query);
375
376 $query = "
377 INSERT INTO civicrm_activity_contact (activity_id, contact_id, record_type_id)
378 SELECT id, source_contact_id, {$sourceID} as record_type_id
379 FROM civicrm_activity
380 WHERE source_contact_id IS NOT NULL";
381
382 $dao = CRM_Core_DAO::executeQuery($query);
383
384 $query = "DROP TABLE civicrm_activity_target";
385 $dao = CRM_Core_DAO::executeQuery($query);
386
387 $query = "DROP TABLE civicrm_activity_assignment";
388 $dao = CRM_Core_DAO::executeQuery($query);
389
390 $query = "ALTER TABLE civicrm_activity
391 DROP FOREIGN KEY FK_civicrm_activity_source_contact_id";
392
393 $dao = CRM_Core_DAO::executeQuery($query);
394
395 $query = "ALTER TABLE civicrm_activity DROP COLUMN source_contact_id";
396 $dao = CRM_Core_DAO::executeQuery($query);
397
398 return TRUE;
399 }
400
401 /**
402 * Migrate word-replacements from $config to civicrm_word_replacement
403 *
404 * @return bool TRUE for success
405 * @see http://issues.civicrm.org/jira/browse/CRM-13187
406 */
407 static function wordReplacements(CRM_Queue_TaskContext $ctx) {
408 $query = "
409 CREATE TABLE IF NOT EXISTS `civicrm_word_replacement` (
410 `id` int unsigned NOT NULL AUTO_INCREMENT COMMENT 'Word replacement ID',
411 `find_word` varchar(255) COLLATE utf8_bin COMMENT 'Word which need to be replaced',
412 `replace_word` varchar(255) COLLATE utf8_bin COMMENT 'Word which will replace the word in find',
413 `is_active` tinyint COMMENT 'Is this entry active?',
414 `match_type` enum('wildcardMatch', 'exactMatch') DEFAULT 'wildcardMatch',
415 `domain_id` int unsigned COMMENT 'FK to Domain ID. This is for Domain specific word replacement',
416 PRIMARY KEY ( `id` ),
417 UNIQUE INDEX `UI_domain_find` (domain_id, find_word),
418 CONSTRAINT FK_civicrm_word_replacement_domain_id FOREIGN KEY (`domain_id`) REFERENCES `civicrm_domain`(`id`)
419 ) ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ;
420 ";
421 $dao = CRM_Core_DAO::executeQuery($query);
422
423 self::rebuildWordReplacementTable();
424 return TRUE;
425 }
426
427 /**
428 * Fix misconfigured constraints created in 4.4.0. To distinguish the good
429 * and bad configurations, we change the constraint name from "UI_find"
430 * (the original name in 4.4.0) to "UI_domain_find" (the new name in
431 * 4.4.1).
432 *
433 * @return bool TRUE for success
434 * @see http://issues.civicrm.org/jira/browse/CRM-13655
435 */
436 static function wordReplacements_patch(CRM_Queue_TaskContext $ctx, $rev) {
437 if (CRM_Core_DAO::checkConstraintExists('civicrm_word_replacement', 'UI_find')) {
438 CRM_Core_DAO::executeQuery("ALTER TABLE civicrm_word_replacement DROP FOREIGN KEY FK_civicrm_word_replacement_domain_id;");
439 CRM_Core_DAO::executeQuery("ALTER TABLE civicrm_word_replacement DROP KEY FK_civicrm_word_replacement_domain_id;");
440 CRM_Core_DAO::executeQuery("ALTER TABLE civicrm_word_replacement DROP KEY UI_find;");
441 CRM_Core_DAO::executeQuery("ALTER TABLE civicrm_word_replacement MODIFY COLUMN `find_word` varchar(255) COLLATE utf8_bin DEFAULT NULL COMMENT 'Word which need to be replaced';");
442 CRM_Core_DAO::executeQuery("ALTER TABLE civicrm_word_replacement MODIFY COLUMN `replace_word` varchar(255) COLLATE utf8_bin DEFAULT NULL COMMENT 'Word which will replace the word in find';");
443 CRM_Core_DAO::executeQuery("ALTER TABLE civicrm_word_replacement ADD CONSTRAINT UI_domain_find UNIQUE KEY `UI_domain_find` (`domain_id`,`find_word`);");
444 CRM_Core_DAO::executeQuery("ALTER TABLE civicrm_word_replacement ADD CONSTRAINT FK_civicrm_word_replacement_domain_id FOREIGN KEY (`domain_id`) REFERENCES `civicrm_domain` (`id`);");
445 }
446 return TRUE;
447 }
448
449 /**
450 * (Queue Task Callback)
451 */
452 static function task_4_4_x_runSql(CRM_Queue_TaskContext $ctx, $rev) {
453 $upgrade = new CRM_Upgrade_Form();
454 $upgrade->processSQL($rev);
455
456 return TRUE;
457 }
458
459 /**
460 * Syntatic sugar for adding a task which (a) is in this class and (b) has
461 * a high priority.
462 *
463 * After passing the $funcName, you can also pass parameters that will go to
464 * the function. Note that all params must be serializable.
465 */
466 protected function addTask($title, $funcName) {
467 $queue = CRM_Queue_Service::singleton()->load(array(
468 'type' => 'Sql',
469 'name' => CRM_Upgrade_Form::QUEUE_NAME,
470 ));
471
472 $args = func_get_args();
473 $title = array_shift($args);
474 $funcName = array_shift($args);
475 $task = new CRM_Queue_Task(
476 array(get_class($this), $funcName),
477 $args,
478 $title
479 );
480 $queue->createItem($task, array('weight' => -1));
481 }
482
483 /**
484 * Get all the word-replacements stored in config-arrays
485 * and convert them to params for the WordReplacement.create API.
486 *
487 * Note: This function is duplicated in CRM_Core_BAO_WordReplacement and
488 * CRM_Upgrade_Incremental_php_FourFour to ensure that the incremental upgrade
489 * step behaves consistently even as the BAO evolves in future versions.
490 * However, if there's a bug in here prior to 4.4.0, we should apply the
491 * bugfix in both places.
492 *
493 * @param bool $rebuildEach whether to perform rebuild after each individual API call
494 * @return array Each item is $params for WordReplacement.create
495 * @see CRM_Core_BAO_WordReplacement::convertConfigArraysToAPIParams
496 */
497 static function getConfigArraysAsAPIParams($rebuildEach) {
498 $wordReplacementCreateParams = array();
499 // get all domains
500 $result = civicrm_api3('domain', 'get', array(
501 'return' => array('locale_custom_strings'),
502 ));
503 if (!empty($result["values"])) {
504 foreach ($result["values"] as $value) {
505 $params = array();
506 $params["domain_id"] = $value["id"];
507 $params["options"] = array('wp-rebuild' => $rebuildEach);
508 // unserialize word match string
509 $localeCustomArray = unserialize($value["locale_custom_strings"]);
510 if (!empty($localeCustomArray)) {
511 $wordMatchArray = array();
512 // Traverse Language array
513 foreach ($localeCustomArray as $localCustomData) {
514 // Traverse status array "enabled" "disabled"
515 foreach ($localCustomData as $status => $matchTypes) {
516 $params["is_active"] = ($status == "enabled")?TRUE:FALSE;
517 // Traverse Match Type array "wildcardMatch" "exactMatch"
518 foreach ($matchTypes as $matchType => $words) {
519 $params["match_type"] = $matchType;
520 foreach ($words as $word => $replace) {
521 $params["find_word"] = $word;
522 $params["replace_word"] = $replace;
523 $wordReplacementCreateParams[] = $params;
524 }
525 }
526 }
527 }
528 }
529 }
530 }
531 return $wordReplacementCreateParams;
532 }
533
534 /**
535 * Get all the word-replacements stored in config-arrays
536 * and write them out as records in civicrm_word_replacement.
537 *
538 * Note: This function is duplicated in CRM_Core_BAO_WordReplacement and
539 * CRM_Upgrade_Incremental_php_FourFour to ensure that the incremental upgrade
540 * step behaves consistently even as the BAO evolves in future versions.
541 * However, if there's a bug in here prior to 4.4.0, we should apply the
542 * bugfix in both places.
543 */
544 public static function rebuildWordReplacementTable() {
545 civicrm_api3('word_replacement', 'replace', array(
546 'options' => array('match' => array('domain_id', 'find_word')),
547 'values' => self::getConfigArraysAsAPIParams(FALSE),
548 ));
549 CRM_Core_BAO_WordReplacement::rebuild();
550 }
551 }