08d16de2f9b7b55519c2baa081a0154d302d124e
[civicrm-core.git] / CRM / Upgrade / Incremental / Legacy.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This class is a container for legacy upgrade logic which predates
38 * the current 'CRM/Incremental/php/*' structure.
39 */
40 class CRM_Upgrade_Incremental_Legacy {
41
42 /**
43 * Compute any messages which should be displayed before upgrade
44 *
45 * @param $preUpgradeMessage
46 * String, alterable.
47 * @param $currentVer
48 * @param $latestVer
49 */
50 public static function setPreUpgradeMessage(&$preUpgradeMessage, $currentVer, $latestVer) {
51 $upgrade = new CRM_Upgrade_Form();
52 $template = CRM_Core_Smarty::singleton();
53
54 if ((version_compare($currentVer, '3.3.alpha1') < 0 &&
55 version_compare($latestVer, '3.3.alpha1') >= 0
56 ) ||
57 (version_compare($currentVer, '3.4.alpha1') < 0 &&
58 version_compare($latestVer, '3.4.alpha1') >= 0
59 )
60 ) {
61 $query = "
62 SELECT id
63 FROM civicrm_mailing_job
64 WHERE status NOT IN ( 'Complete', 'Canceled' ) AND is_test = 0 LIMIT 1";
65 $mjId = CRM_Core_DAO::singleValueQuery($query);
66 if ($mjId) {
67 $preUpgradeMessage = ts("There are one or more Scheduled or In Progress mailings in your install. Scheduled mailings will not be sent and In Progress mailings will not finish if you continue with the upgrade. We strongly recommend that all Scheduled and In Progress mailings be completed or cancelled and then upgrade your CiviCRM install.");
68 }
69 }
70
71 //turning some tables to monolingual during 3.4.beta3, CRM-7869
72 $upgradeTo = str_replace('4.0.', '3.4.', $latestVer);
73 $upgradeFrom = str_replace('4.0.', '3.4.', $currentVer);
74
75 // check for changed message templates
76 self::checkMessageTemplate($template, $preUpgradeMessage, $upgradeTo, $upgradeFrom);
77
78 $upgrade = new CRM_Upgrade_Form();
79 if ($upgrade->multilingual &&
80 version_compare($upgradeFrom, '3.4.beta3') == -1 &&
81 version_compare($upgradeTo, '3.4.beta3') >= 0
82 ) {
83 $config = CRM_Core_Config::singleton();
84 $preUpgradeMessage .= '<br />' . ts("As per <a href='%1'>the related blog post</a>, we are making contact names, addresses and mailings monolingual; the values entered for the default locale (%2) will be preserved and values for other locales removed.", array(1 => 'http://civicrm.org/blogs/shot/multilingual-civicrm-3440-making-some-fields-monolingual', 2 => $config->lcMessages));
85 }
86
87 if (version_compare($currentVer, '3.4.6') == -1 &&
88 version_compare($latestVer, '3.4.6') >= 0
89 ) {
90 $googleProcessorExists = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_payment_processor WHERE payment_processor_type = 'Google_Checkout' AND is_active = 1 LIMIT 1;");
91
92 if ($googleProcessorExists) {
93 $preUpgradeMessage .= '<br />' . ts('To continue using Google Checkout Payment Processor with latest version of CiviCRM, requires updating merchant account settings. Please refer "Set API callback URL and other settings" section of <a href="%1" target="_blank"><strong>Google Checkout Configuration</strong></a> doc.', array(1 => 'http://wiki.civicrm.org/confluence/x/zAJTAg'));
94 }
95 }
96
97 // http://issues.civicrm.org/jira/browse/CRM-13572
98 // Depending on how the code was upgraded, some sites may still have copies of old
99 // source files left behind. This is often a forgivable offense, but it's quite
100 // dangerous for CIVI-SA-2013-001.
101 global $civicrm_root;
102 $ofcFile = "$civicrm_root/packages/OpenFlashChart/php-ofc-library/ofc_upload_image.php";
103 if (file_exists($ofcFile)) {
104 if (@unlink($ofcFile)) {
105 $preUpgradeMessage .= '<br />' . ts('This system included an outdated, insecure script (%1). The file was automatically deleted.', array(
106 1 => $ofcFile
107 ));
108 } else {
109 $preUpgradeMessage .= '<br />' . ts('This system includes an outdated, insecure script (%1). Please delete it.', array(
110 1 => $ofcFile
111 ));
112 }
113 }
114
115 if (CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SEARCH_PREFERENCES_NAME, 'enable_innodb_fts', NULL, FALSE)) {
116 // The FTS indexing feature dynamically manipulates the schema which could
117 // cause conflicts with other layers that manipulate the schema. The
118 // simplest thing is to turn it off and back on.
119
120 // It may not always be necessary to do this -- but I doubt we're going to test
121 // systematically in future releases. When it is necessary, one could probably
122 // ignore the matter and simply run CRM_Core_InnoDBIndexer::fixSchemaDifferences
123 // after the upgrade. But that's speculative. For now, we'll leave this
124 // advanced feature in the hands of the sysadmin.
125 $preUpgradeMessage .= '<br />' . ts('This database uses InnoDB Full Text Search for optimized searching. The upgrade procedure has not been tested with this feature. You should disable (and later re-enable) the feature by navigating to "Administer => System Settings => Miscellaneous".');
126 }
127 }
128
129 /**
130 * @param $template
131 * @param $message
132 * @param $latestVer
133 * @param $currentVer
134 */
135 public static function checkMessageTemplate(&$template, &$message, $latestVer, $currentVer) {
136 if (version_compare($currentVer, '3.1.alpha1') < 0) {
137 return;
138 }
139
140 $sql = "SELECT orig.workflow_id as workflow_id,
141 orig.msg_title as title
142 FROM civicrm_msg_template diverted JOIN civicrm_msg_template orig ON (
143 diverted.workflow_id = orig.workflow_id AND
144 orig.is_reserved = 1 AND (
145 diverted.msg_subject != orig.msg_subject OR
146 diverted.msg_text != orig.msg_text OR
147 diverted.msg_html != orig.msg_html
148 )
149 )";
150
151 $dao = &CRM_Core_DAO::executeQuery($sql);
152 while ($dao->fetch()) {
153 $workflows[$dao->workflow_id] = $dao->title;
154 }
155
156 if (empty($workflows)) {
157 return;
158 }
159
160 $html = NULL;
161 $pathName = dirname(dirname(__FILE__));
162 $flag = FALSE;
163 foreach ($workflows as $workflow => $title) {
164 $name = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue',
165 $workflow,
166 'name',
167 'id'
168 );
169
170 // check if file exists locally
171 $textFileName = implode(DIRECTORY_SEPARATOR,
172 array(
173 $pathName,
174 "{$latestVer}.msg_template",
175 'message_templates',
176 "{$name}_text.tpl",
177 )
178 );
179
180 $htmlFileName = implode(DIRECTORY_SEPARATOR,
181 array(
182 $pathName,
183 "{$latestVer}.msg_template",
184 'message_templates',
185 "{$name}_html.tpl",
186 )
187 );
188
189 if (file_exists($textFileName) ||
190 file_exists($htmlFileName)
191 ) {
192 $flag = TRUE;
193 $html .= "<li>{$title}</li>";
194 }
195 }
196
197 if ($flag == TRUE) {
198 $html = "<ul>" . $html . "<ul>";
199
200 $message .= '<br />' . ts("The default copies of the message templates listed below will be updated to handle new features or correct a problem. Your installation has customized versions of these message templates, and you will need to apply the updates manually after running this upgrade. <a href='%1' style='color:white; text-decoration:underline; font-weight:bold;' target='_blank'>Click here</a> for detailed instructions. %2", array(1 => 'http://wiki.civicrm.org/confluence/display/CRMDOC/Message+Templates#MessageTemplates-UpgradesandCustomizedSystemWorkflowTemplates', 2 => $html));
201 }
202 }
203
204 /**
205 * Compute any messages which should be displayed after upgrade
206 *
207 * @param $postUpgradeMessage
208 * String, alterable.
209 * @param $rev
210 * String, an intermediate version; note that setPostUpgradeMessage is called repeatedly with different $revs.
211 * @return void
212 */
213 public static function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
214 if ($rev == '3.2.alpha1') {
215 $postUpgradeMessage .= '<br />' . ts("We have reset the COUNTED flag to false for the event participant status 'Pending from incomplete transaction'. This change ensures that people who have a problem during registration can try again.");
216 }
217 if ($rev == '3.2.beta3') {
218 $subTypes = CRM_Contact_BAO_ContactType::subTypes();
219
220 if (is_array($subTypes) && !empty($subTypes)) {
221 $config = CRM_Core_Config::singleton();
222 $subTypeTemplates = array();
223
224 if (isset($config->customTemplateDir)) {
225 foreach ($subTypes as $key => $subTypeName) {
226 $customContactSubTypeEdit = $config->customTemplateDir . "CRM/Contact/Form/Edit/" . $subTypeName . ".tpl";
227 $customContactSubTypeView = $config->customTemplateDir . "CRM/Contact/Page/View/" . $subTypeName . ".tpl";
228 if (file_exists($customContactSubTypeEdit) || file_exists($customContactSubTypeView)) {
229 $subTypeTemplates[$subTypeName] = $subTypeName;
230 }
231 }
232 }
233
234 foreach ($subTypes as $key => $subTypeName) {
235 $customContactSubTypeEdit = $config->templateDir . "CRM/Contact/Form/Edit/" . $subTypeName . ".tpl";
236 $customContactSubTypeView = $config->templateDir . "CRM/Contact/Page/View/" . $subTypeName . ".tpl";
237 if (file_exists($customContactSubTypeEdit) || file_exists($customContactSubTypeView)) {
238 $subTypeTemplates[$subTypeName] = $subTypeName;
239 }
240 }
241
242 if (!empty($subTypeTemplates)) {
243 $subTypeTemplates = implode(',', $subTypeTemplates);
244 $postUpgradeMessage .= '<br />' . ts('You are using custom template for contact subtypes: %1.', array(1 => $subTypeTemplates)) . '<br />' . ts('You need to move these subtype templates to the SubType directory in %1 and %2 respectively.', array(1 => 'CRM/Contact/Form/Edit', 2 => 'CRM/Contact/Page/View'));
245 }
246 }
247 }
248 if ($rev == '3.2.beta4') {
249 $statuses = array('New', 'Current', 'Grace', 'Expired', 'Pending', 'Cancelled', 'Deceased');
250 $sql = "
251 SELECT count( id ) as statusCount
252 FROM civicrm_membership_status
253 WHERE name IN ( '" . implode("' , '", $statuses) . "' ) ";
254 $count = CRM_Core_DAO::singleValueQuery($sql);
255 if ($count < count($statuses)) {
256 $postUpgradeMessage .= '<br />' . ts("One or more Membership Status Rules was disabled during the upgrade because it did not match a recognized status name. if custom membership status rules were added to this site - review the disabled statuses and re-enable any that are still needed (Administer > CiviMember > Membership Status Rules).");
257 }
258 }
259 if ($rev == '3.4.alpha1') {
260 $renamedBinScripts = array(
261 'ParticipantProcessor.php',
262 'RespondentProcessor.php',
263 'UpdateGreeting.php',
264 'UpdateMembershipRecord.php',
265 'UpdatePledgeRecord.php ',
266 );
267 $postUpgradeMessage .= '<br />' . ts('The following files have been renamed to have a ".php" extension instead of a ".php.txt" extension') . ': ' . implode(', ', $renamedBinScripts);
268 }
269 }
270
271 /**
272 * Perform an incremental upgrade
273 *
274 * @param $rev
275 * String, the revision to which we are upgrading (Note: When processing a series of upgrades, this is the immediate upgrade - not the final).
276 */
277 public static function upgrade_2_2_alpha1($rev) {
278 for ($stepID = 1; $stepID <= 4; $stepID++) {
279 $formName = "CRM_Upgrade_TwoTwo_Form_Step{$stepID}";
280 $form = new $formName();
281
282 $error = NULL;
283 if (!$form->verifyPreDBState($error)) {
284 if (!isset($error)) {
285 $error = "pre-condition failed for current upgrade step $stepID, rev $rev";
286 }
287 CRM_Core_Error::fatal($error);
288 }
289
290 if ($stepID == 4) {
291 return;
292 }
293
294 $template = CRM_Core_Smarty::singleton();
295
296 $eventFees = array();
297 $query = "SELECT og.id ogid FROM civicrm_option_group og WHERE og.name LIKE %1";
298 $params = array(1 => array('civicrm_event_page.amount%', 'String'));
299 $dao = CRM_Core_DAO::executeQuery($query, $params);
300 while ($dao->fetch()) {
301 $eventFees[$dao->ogid] = $dao->ogid;
302 }
303 $template->assign('eventFees', $eventFees);
304
305 $form->upgrade();
306
307 if (!$form->verifyPostDBState($error)) {
308 if (!isset($error)) {
309 $error = "post-condition failed for current upgrade step $stepID, rev $rev";
310 }
311 CRM_Core_Error::fatal($error);
312 }
313 }
314 }
315
316 /**
317 * Perform an incremental upgrade
318 *
319 * @param $rev
320 * String, the revision to which we are upgrading (Note: When processing a series of upgrades, this is the immediate upgrade - not the final).
321 */
322 public static function upgrade_2_1_2($rev) {
323 $formName = "CRM_Upgrade_TwoOne_Form_TwoOneTwo";
324 $form = new $formName($rev);
325
326 $error = NULL;
327 if (!$form->verifyPreDBState($error)) {
328 if (!isset($error)) {
329 $error = "pre-condition failed for current upgrade for $rev";
330 }
331 CRM_Core_Error::fatal($error);
332 }
333
334 $form->upgrade();
335
336 if (!$form->verifyPostDBState($error)) {
337 if (!isset($error)) {
338 $error = "post-condition failed for current upgrade for $rev";
339 }
340 CRM_Core_Error::fatal($error);
341 }
342 }
343
344 /**
345 * This function should check if if need to skip current sql file
346 * Name of this function will change according to the latest release
347 *
348 */
349 public static function upgrade_2_2_alpha3($rev) {
350 // skip processing sql file, if fresh install -
351 if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'mail_protocol', 'id', 'name')) {
352 $upgrade = new CRM_Upgrade_Form();
353 $upgrade->processSQL($rev);
354 }
355 return TRUE;
356 }
357
358 /**
359 * Perform an incremental upgrade
360 *
361 * @param $rev
362 * String, the revision to which we are upgrading (Note: When processing a series of upgrades, this is the immediate upgrade - not the final).
363 */
364 public static function upgrade_2_2_beta1($rev) {
365 if (!CRM_Core_DAO::checkFieldExists('civicrm_pcp_block', 'notify_email')) {
366 $template = CRM_Core_Smarty::singleton();
367 $template->assign('notifyAbsent', TRUE);
368 }
369 $upgrade = new CRM_Upgrade_Form();
370 $upgrade->processSQL($rev);
371 }
372
373 /**
374 * Perform an incremental upgrade
375 *
376 * @param $rev
377 * String, the revision to which we are upgrading (Note: When processing a series of upgrades, this is the immediate upgrade - not the final).
378 */
379 public static function upgrade_2_2_beta2($rev) {
380 $template = CRM_Core_Smarty::singleton();
381 if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue',
382 'CRM_Contact_Form_Search_Custom_ZipCodeRange', 'id', 'name'
383 )) {
384 $template->assign('customSearchAbsentAll', TRUE);
385 }
386 elseif (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue',
387 'CRM_Contact_Form_Search_Custom_MultipleValues', 'id', 'name'
388 )) {
389 $template->assign('customSearchAbsent', TRUE);
390 }
391 $upgrade = new CRM_Upgrade_Form();
392 $upgrade->processSQL($rev);
393 }
394
395 /**
396 * Perform an incremental upgrade
397 *
398 * @param $rev
399 * String, the revision to which we are upgrading (Note: When processing a series of upgrades, this is the immediate upgrade - not the final).
400 */
401 public static function upgrade_2_2_beta3($rev) {
402 $template = CRM_Core_Smarty::singleton();
403 if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'custom_data_type', 'id', 'name')) {
404 $template->assign('customDataType', TRUE);
405 }
406
407 $upgrade = new CRM_Upgrade_Form();
408 $upgrade->processSQL($rev);
409 }
410
411 /**
412 * Perform an incremental upgrade
413 *
414 * @param $rev
415 * String, the revision to which we are upgrading (Note: When processing a series of upgrades, this is the immediate upgrade - not the final).
416 */
417 public static function upgrade_3_0_alpha1($rev) {
418
419 $threeZero = new CRM_Upgrade_ThreeZero_ThreeZero();
420
421 $error = NULL;
422 if (!$threeZero->verifyPreDBState($error)) {
423 if (!isset($error)) {
424 $error = 'pre-condition failed for current upgrade for 3.0.alpha2';
425 }
426 CRM_Core_Error::fatal($error);
427 }
428
429 $threeZero->upgrade($rev);
430 }
431
432 /**
433 * Perform an incremental upgrade
434 *
435 * @param $rev
436 * String, the revision to which we are upgrading (Note: When processing a series of upgrades, this is the immediate upgrade - not the final).
437 */
438 public static function upgrade_3_1_alpha1($rev) {
439
440 $threeOne = new CRM_Upgrade_ThreeOne_ThreeOne();
441
442 $error = NULL;
443 if (!$threeOne->verifyPreDBState($error)) {
444 if (!isset($error)) {
445 $error = 'pre-condition failed for current upgrade for 3.0.alpha2';
446 }
447 CRM_Core_Error::fatal($error);
448 }
449
450 $threeOne->upgrade($rev);
451 }
452
453 /**
454 * Perform an incremental upgrade
455 *
456 * @param $rev
457 * String, the revision to which we are upgrading (Note: When processing a series of upgrades, this is the immediate upgrade - not the final).
458 */
459 public static function upgrade_2_2_7($rev) {
460 $upgrade = new CRM_Upgrade_Form();
461 $upgrade->processSQL($rev);
462 $sql = "UPDATE civicrm_report_instance
463 SET form_values = REPLACE(form_values,'#',';') ";
464 CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray);
465
466 // make report component enabled by default
467 $domain = new CRM_Core_DAO_Domain();
468 $domain->selectAdd();
469 $domain->selectAdd('config_backend');
470 $domain->find(TRUE);
471 if ($domain->config_backend) {
472 $defaults = unserialize($domain->config_backend);
473
474 if (is_array($defaults['enableComponents'])) {
475 $compId = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_component WHERE name = 'CiviReport'");
476 if ($compId) {
477 $defaults['enableComponents'][] = 'CiviReport';
478 $defaults['enableComponentIDs'][] = $compId;
479
480 CRM_Core_BAO_ConfigSetting::add($defaults);
481 }
482 }
483 }
484 }
485
486 /**
487 * Perform an incremental upgrade
488 *
489 * @param $rev
490 * String, the revision to which we are upgrading (Note: When processing a series of upgrades, this is the immediate upgrade - not the final).
491 */
492 public static function upgrade_3_0_2($rev) {
493
494 $template = CRM_Core_Smarty::singleton();
495 //check whether upgraded from 2.1.x or 2.2.x
496 $inboundEmailID = CRM_Core_OptionGroup::getValue('activity_type', 'Inbound Email', 'name');
497
498 if (!empty($inboundEmailID)) {
499 $template->assign('addInboundEmail', FALSE);
500 }
501 else {
502 $template->assign('addInboundEmail', TRUE);
503 }
504
505 $upgrade = new CRM_Upgrade_Form();
506 $upgrade->processSQL($rev);
507 }
508
509 /**
510 * Perform an incremental upgrade
511 *
512 * @param $rev
513 * String, the revision to which we are upgrading (Note: When processing a series of upgrades, this is the immediate upgrade - not the final).
514 */
515 public static function upgrade_3_0_4($rev) {
516 //make sure 'Deceased' membership status present in db,CRM-5636
517 $template = CRM_Core_Smarty::singleton();
518
519 $addDeceasedStatus = FALSE;
520 $sql = "SELECT max(id) FROM civicrm_membership_status where name = 'Deceased'";
521 if (!CRM_Core_DAO::singleValueQuery($sql)) {
522 $addDeceasedStatus = TRUE;
523 }
524 $template->assign('addDeceasedStatus', $addDeceasedStatus);
525
526 $upgrade = new CRM_Upgrade_Form();
527 $upgrade->processSQL($rev);
528 }
529
530 /**
531 * Perform an incremental upgrade
532 *
533 * @param $rev
534 * String, the revision to which we are upgrading (Note: When processing a series of upgrades, this is the immediate upgrade - not the final).
535 */
536 public static function upgrade_3_1_0($rev) {
537 // upgrade all roles who have 'access CiviEvent' permission, to also have
538 // newly added permission 'edit_all_events', CRM-5472
539 $config = CRM_Core_Config::singleton();
540 if (is_callable(array(
541 $config->userSystem, 'replacePermission'))) {
542 $config->userSystem->replacePermission('access CiviEvent', array('access CiviEvent', 'edit all events'));
543 }
544
545 //make sure 'Deceased' membership status present in db,CRM-5636
546 $template = CRM_Core_Smarty::singleton();
547
548 $addDeceasedStatus = FALSE;
549 $sql = "SELECT max(id) FROM civicrm_membership_status where name = 'Deceased'";
550 if (!CRM_Core_DAO::singleValueQuery($sql)) {
551 $addDeceasedStatus = TRUE;
552 }
553 $template->assign('addDeceasedStatus', $addDeceasedStatus);
554
555 $upgrade = new CRM_Upgrade_Form();
556 $upgrade->processSQL($rev);
557 }
558
559 /**
560 * Perform an incremental upgrade
561 *
562 * @param $rev
563 * String, the revision to which we are upgrading (Note: When processing a series of upgrades, this is the immediate upgrade - not the final).
564 */
565 public static function upgrade_3_1_3($rev) {
566 $threeOne = new CRM_Upgrade_ThreeOne_ThreeOne();
567 $threeOne->upgrade_3_1_3();
568
569 $upgrade = new CRM_Upgrade_Form();
570 $upgrade->processSQL($rev);
571 }
572
573 /**
574 * Perform an incremental upgrade
575 *
576 * @param $rev
577 * String, the revision to which we are upgrading (Note: When processing a series of upgrades, this is the immediate upgrade - not the final).
578 */
579 public static function upgrade_3_1_4($rev) {
580 $threeOne = new CRM_Upgrade_ThreeOne_ThreeOne();
581 $threeOne->upgrade_3_1_4();
582
583 $upgrade = new CRM_Upgrade_Form();
584 $upgrade->processSQL($rev);
585 }
586 }