CRM-17147 - ensure is_deceased FALSE values are 0, not NULL
[civicrm-core.git] / CRM / Upgrade / Form.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
32 * $Id$
33 *
34 */
35 class CRM_Upgrade_Form extends CRM_Core_Form {
36 const QUEUE_NAME = 'CRM_Upgrade';
37
38 /**
39 * Minimum size of MySQL's thread_stack option
40 *
41 * @see install/index.php MINIMUM_THREAD_STACK
42 */
43 const MINIMUM_THREAD_STACK = 192;
44
45 /**
46 * Minimum previous CiviCRM version we can directly upgrade from
47 */
48 const MINIMUM_UPGRADABLE_VERSION = '4.0.8';
49
50 /**
51 * Minimum php version we support
52 */
53 const MINIMUM_PHP_VERSION = '5.3.4';
54
55 protected $_config;
56
57 /**
58 * Upgrade for multilingual.
59 *
60 * @var boolean
61 */
62 public $multilingual = FALSE;
63
64 /**
65 * Locales available for multilingual upgrade.
66 *
67 * @var array
68 */
69 public $locales;
70
71 /**
72 * Number to string mapper.
73 *
74 * @var array
75 */
76 static $_numberMap = array(
77 0 => 'Zero',
78 1 => 'One',
79 2 => 'Two',
80 3 => 'Three',
81 4 => 'Four',
82 5 => 'Five',
83 6 => 'Six',
84 7 => 'Seven',
85 8 => 'Eight',
86 9 => 'Nine',
87 );
88
89 /**
90 * Constructor for the basic form page.
91 *
92 * We should not use QuickForm directly. This class provides a lot
93 * of default convenient functions, rules and buttons
94 *
95 * @param object $state
96 * State associated with this form.
97 * @param const|\enum|int $action The mode the form is operating in (None/Create/View/Update/Delete)
98 * @param string $method
99 * The type of http method used (GET/POST).
100 * @param string $name
101 * The name of the form if different from class name.
102 */
103 public function __construct(
104 $state = NULL,
105 $action = CRM_Core_Action::NONE,
106 $method = 'post',
107 $name = NULL
108 ) {
109 $this->_config = CRM_Core_Config::singleton();
110
111 $domain = new CRM_Core_DAO_Domain();
112 $domain->find(TRUE);
113
114 $this->multilingual = (bool) $domain->locales;
115 $this->locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales);
116
117 $smarty = CRM_Core_Smarty::singleton();
118 //$smarty->compile_dir = $this->_config->templateCompileDir;
119 $smarty->assign('multilingual', $this->multilingual);
120 $smarty->assign('locales', $this->locales);
121
122 // we didn't call CRM_Core_BAO_ConfigSetting::retrieve(), so we need to set $dbLocale by hand
123 if ($this->multilingual) {
124 global $dbLocale;
125 $dbLocale = "_{$this->_config->lcMessages}";
126 }
127
128 parent::__construct($state, $action, $method, $name);
129 }
130
131 /**
132 * @param $version
133 *
134 * @return mixed
135 */
136 public static function &incrementalPhpObject($version) {
137 static $incrementalPhpObject = array();
138
139 $versionParts = explode('.', $version);
140 $versionName = self::$_numberMap[$versionParts[0]] . self::$_numberMap[$versionParts[1]];
141
142 if (!array_key_exists($versionName, $incrementalPhpObject)) {
143 $className = "CRM_Upgrade_Incremental_php_{$versionName}";
144 $incrementalPhpObject[$versionName] = new $className();
145 }
146 return $incrementalPhpObject[$versionName];
147 }
148
149 /**
150 * @param $version
151 * @param $release
152 *
153 * @return bool
154 */
155 public function checkVersionRelease($version, $release) {
156 $versionParts = explode('.', $version);
157 return ($versionParts[2] == $release);
158 }
159
160 /**
161 * @param $constraints
162 *
163 * @return array
164 */
165 public function checkSQLConstraints(&$constraints) {
166 $pass = $fail = 0;
167 foreach ($constraints as $constraint) {
168 if ($this->checkSQLConstraint($constraint)) {
169 $pass++;
170 }
171 else {
172 $fail++;
173 }
174 return array($pass, $fail);
175 }
176 }
177
178 /**
179 * @param $constraint
180 *
181 * @return bool
182 */
183 public function checkSQLConstraint($constraint) {
184 // check constraint here
185 return TRUE;
186 }
187
188 /**
189 * @param string $fileName
190 * @param bool $isQueryString
191 */
192 public function source($fileName, $isQueryString = FALSE) {
193
194 CRM_Utils_File::sourceSQLFile($this->_config->dsn,
195 $fileName, NULL, $isQueryString
196 );
197 }
198
199 public function preProcess() {
200 CRM_Utils_System::setTitle($this->getTitle());
201 if (!$this->verifyPreDBState($errorMessage)) {
202 if (!isset($errorMessage)) {
203 $errorMessage = 'pre-condition failed for current upgrade step';
204 }
205 CRM_Core_Error::fatal($errorMessage);
206 }
207 $this->assign('recentlyViewed', FALSE);
208 }
209
210 public function buildQuickForm() {
211 $this->addDefaultButtons($this->getButtonTitle(),
212 'next',
213 NULL,
214 TRUE
215 );
216 }
217
218 /**
219 * Getter function for title. Should be over-ridden by derived class
220 *
221 * @return string
222 */
223 /**
224 * @return string
225 */
226 public function getTitle() {
227 return ts('Title not Set');
228 }
229
230 /**
231 * @return string
232 */
233 public function getFieldsetTitle() {
234 return '';
235 }
236
237 /**
238 * @return string
239 */
240 public function getButtonTitle() {
241 return ts('Continue');
242 }
243
244 /**
245 * Use the form name to create the tpl file name.
246 *
247 * @return string
248 */
249 /**
250 * @return string
251 */
252 public function getTemplateFileName() {
253 $this->assign('title',
254 $this->getFieldsetTitle()
255 );
256 $this->assign('message',
257 $this->getTemplateMessage()
258 );
259 return 'CRM/Upgrade/Base.tpl';
260 }
261
262 public function postProcess() {
263 $this->upgrade();
264
265 if (!$this->verifyPostDBState($errorMessage)) {
266 if (!isset($errorMessage)) {
267 $errorMessage = 'post-condition failed for current upgrade step';
268 }
269 CRM_Core_Error::fatal($errorMessage);
270 }
271 }
272
273 /**
274 * @param $query
275 *
276 * @return Object
277 */
278 public function runQuery($query) {
279 return CRM_Core_DAO::executeQuery($query,
280 CRM_Core_DAO::$_nullArray
281 );
282 }
283
284 /**
285 * @param $version
286 *
287 * @return Object
288 */
289 public function setVersion($version) {
290 $this->logVersion($version);
291
292 $query = "
293 UPDATE civicrm_domain
294 SET version = '$version'
295 ";
296 return $this->runQuery($query);
297 }
298
299 /**
300 * @param $newVersion
301 *
302 * @return bool
303 */
304 public function logVersion($newVersion) {
305 if ($newVersion) {
306 $oldVersion = CRM_Core_BAO_Domain::version();
307
308 $session = CRM_Core_Session::singleton();
309 $logParams = array(
310 'entity_table' => 'civicrm_domain',
311 'entity_id' => 1,
312 'data' => "upgrade:{$oldVersion}->{$newVersion}",
313 // lets skip 'modified_id' for now, as it causes FK issues And
314 // is not very important for now.
315 'modified_date' => date('YmdHis'),
316 );
317 CRM_Core_BAO_Log::add($logParams);
318 return TRUE;
319 }
320
321 return FALSE;
322 }
323
324 /**
325 * @param $version
326 *
327 * @return bool
328 */
329 public function checkVersion($version) {
330 $domainID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain',
331 $version, 'id',
332 'version'
333 );
334 return $domainID ? TRUE : FALSE;
335 }
336
337 /**
338 * @return array
339 * @throws Exception
340 */
341 public function getRevisionSequence() {
342 $revList = array();
343 $sqlDir = implode(DIRECTORY_SEPARATOR,
344 array(dirname(__FILE__), 'Incremental', 'sql')
345 );
346 $sqlFiles = scandir($sqlDir);
347
348 $sqlFilePattern = '/^((\d{1,2}\.\d{1,2})\.(\d{1,2}\.)?(\d{1,2}|\w{4,7}))\.(my)?sql(\.tpl)?$/i';
349 foreach ($sqlFiles as $file) {
350 if (preg_match($sqlFilePattern, $file, $matches)) {
351 if ($matches[2] == '4.0') {
352 CRM_Core_Error::fatal(ts("4.0.x upgrade files shouldn't exist. Contact Lobo to discuss this. This is related to the issue CRM-7731."));
353 }
354 if (!in_array($matches[1], $revList)) {
355 $revList[] = $matches[1];
356 }
357 }
358 }
359
360 usort($revList, 'version_compare');
361 return $revList;
362 }
363
364 /**
365 * @param $rev
366 * @param int $index
367 *
368 * @return null
369 */
370 public static function getRevisionPart($rev, $index = 1) {
371 $revPattern = '/^((\d{1,2})\.\d{1,2})\.(\d{1,2}|\w{4,7})?$/i';
372 preg_match($revPattern, $rev, $matches);
373
374 return array_key_exists($index, $matches) ? $matches[$index] : NULL;
375 }
376
377 /**
378 * @param $tplFile
379 * @param $rev
380 *
381 * @return bool
382 */
383 public function processLocales($tplFile, $rev) {
384 $smarty = CRM_Core_Smarty::singleton();
385 $smarty->assign('domainID', CRM_Core_Config::domainID());
386
387 $this->source($smarty->fetch($tplFile), TRUE);
388
389 if ($this->multilingual) {
390 CRM_Core_I18n_Schema::rebuildMultilingualSchema($this->locales, $rev);
391 }
392 return $this->multilingual;
393 }
394
395 /**
396 * @param $rev
397 */
398 public function setSchemaStructureTables($rev) {
399 if ($this->multilingual) {
400 CRM_Core_I18n_Schema::schemaStructureTables($rev, TRUE);
401 }
402 }
403
404 /**
405 * @param $rev
406 *
407 * @throws Exception
408 */
409 public function processSQL($rev) {
410 $sqlFile = implode(DIRECTORY_SEPARATOR,
411 array(
412 dirname(__FILE__),
413 'Incremental',
414 'sql',
415 $rev . '.mysql',
416 )
417 );
418 $tplFile = "$sqlFile.tpl";
419
420 if (file_exists($tplFile)) {
421 $this->processLocales($tplFile, $rev);
422 }
423 else {
424 if (!file_exists($sqlFile)) {
425 CRM_Core_Error::fatal("sqlfile - $rev.mysql not found.");
426 }
427 $this->source($sqlFile);
428 }
429 }
430
431 /**
432 * Determine the start and end version of the upgrade process.
433 *
434 * @return array(0=>$currentVer, 1=>$latestVer)
435 */
436 public function getUpgradeVersions() {
437 $latestVer = CRM_Utils_System::version();
438 $currentVer = CRM_Core_BAO_Domain::version(TRUE);
439 if (!$currentVer) {
440 CRM_Core_Error::fatal(ts('Version information missing in civicrm database.'));
441 }
442 elseif (stripos($currentVer, 'upgrade')) {
443 CRM_Core_Error::fatal(ts('Database check failed - the database looks to have been partially upgraded. You may want to reload the database with the backup and try the upgrade process again.'));
444 }
445 if (!$latestVer) {
446 CRM_Core_Error::fatal(ts('Version information missing in civicrm codebase.'));
447 }
448
449 return array($currentVer, $latestVer);
450 }
451
452 /**
453 * Determine if $currentVer can be upgraded to $latestVer
454 *
455 * @param $currentVer
456 * @param $latestVer
457 *
458 * @return mixed, a string error message or boolean 'false' if OK
459 */
460 public function checkUpgradeableVersion($currentVer, $latestVer) {
461 $error = FALSE;
462 // since version is suppose to be in valid format at this point, especially after conversion ($convertVer),
463 // lets do a pattern check -
464 if (!CRM_Utils_System::isVersionFormatValid($currentVer)) {
465 $error = ts('Database is marked with invalid version format. You may want to investigate this before you proceed further.');
466 }
467 elseif (version_compare($currentVer, $latestVer) > 0) {
468 // DB version number is higher than codebase being upgraded to. This is unexpected condition-fatal error.
469 $error = ts('Your database is marked with an unexpected version number: %1. The automated upgrade to version %2 can not be run - and the %2 codebase may not be compatible with your database state. You will need to determine the correct version corresponding to your current database state. You may want to revert to the codebase you were using prior to beginning this upgrade until you resolve this problem.',
470 array(1 => $currentVer, 2 => $latestVer)
471 );
472 }
473 elseif (version_compare($currentVer, $latestVer) == 0) {
474 $error = ts('Your database has already been upgraded to CiviCRM %1',
475 array(1 => $latestVer)
476 );
477 }
478 elseif (version_compare($currentVer, self::MINIMUM_UPGRADABLE_VERSION) < 0) {
479 $error = ts('CiviCRM versions prior to %1 cannot be upgraded directly to %2. This upgrade will need to be done in stages. First download an intermediate version (the LTS may be a good choice) and upgrade to that before proceeding to this version.',
480 array(1 => self::MINIMUM_UPGRADABLE_VERSION, 2 => $latestVer)
481 );
482 }
483
484 if (version_compare(phpversion(), self::MINIMUM_PHP_VERSION) < 0) {
485 $error = ts('CiviCRM %3 requires PHP version %1 (or newer), but the current system uses %2 ',
486 array(
487 1 => self::MINIMUM_PHP_VERSION,
488 2 => phpversion(),
489 3 => $latestVer,
490 ));
491 }
492
493 // check for mysql trigger privileges
494 if (!CRM_Core_DAO::checkTriggerViewPermission(FALSE, TRUE)) {
495 $error = ts('CiviCRM %1 requires MySQL trigger privileges.',
496 array(1 => $latestVer));
497 }
498
499 if (CRM_Core_DAO::getGlobalSetting('thread_stack', 0) < (1024 * self::MINIMUM_THREAD_STACK)) {
500 $error = ts('CiviCRM %1 requires MySQL thread stack >= %2k', array(
501 1 => $latestVer,
502 2 => self::MINIMUM_THREAD_STACK,
503 ));
504 }
505
506 return $error;
507 }
508
509 /**
510 * Determine if $currentver already matches $latestVer
511 *
512 * @param $currentVer
513 * @param $latestVer
514 *
515 * @return mixed, a string error message or boolean 'false' if OK
516 */
517 public function checkCurrentVersion($currentVer, $latestVer) {
518 $error = FALSE;
519
520 // since version is suppose to be in valid format at this point, especially after conversion ($convertVer),
521 // lets do a pattern check -
522 if (!CRM_Utils_System::isVersionFormatValid($currentVer)) {
523 $error = ts('Database is marked with invalid version format. You may want to investigate this before you proceed further.');
524 }
525 elseif (version_compare($currentVer, $latestVer) != 0) {
526 $error = ts('Your database is not configured for version %1',
527 array(1 => $latestVer)
528 );
529 }
530 return $error;
531 }
532
533 /**
534 * Fill the queue with upgrade tasks.
535 *
536 * @param string $currentVer
537 * the original revision.
538 * @param string $latestVer
539 * the target (final) revision.
540 * @param string $postUpgradeMessageFile
541 * path of a modifiable file which lists the post-upgrade messages.
542 *
543 * @return CRM_Queue_Service
544 */
545 public static function buildQueue($currentVer, $latestVer, $postUpgradeMessageFile) {
546 $upgrade = new CRM_Upgrade_Form();
547
548 // hack to make 4.0.x (D7,J1.6) codebase go through 3.4.x (d6, J1.5) upgrade files,
549 // since schema wise they are same
550 if (CRM_Upgrade_Form::getRevisionPart($currentVer) == '4.0') {
551 $currentVer = str_replace('4.0.', '3.4.', $currentVer);
552 }
553
554 // Ensure that queue can be created
555 if (!CRM_Queue_BAO_QueueItem::findCreateTable()) {
556 CRM_Core_Error::fatal(ts('Failed to find or create queueing table'));
557 }
558 $queue = CRM_Queue_Service::singleton()->create(array(
559 'name' => self::QUEUE_NAME,
560 'type' => 'Sql',
561 'reset' => TRUE,
562 ));
563
564 $revisions = $upgrade->getRevisionSequence();
565 foreach ($revisions as $rev) {
566 // proceed only if $currentVer < $rev
567 if (version_compare($currentVer, $rev) < 0) {
568 $beginTask = new CRM_Queue_Task(
569 // callback
570 array('CRM_Upgrade_Form', 'doIncrementalUpgradeStart'),
571 // arguments
572 array($rev),
573 "Begin Upgrade to $rev"
574 );
575 $queue->createItem($beginTask);
576
577 $task = new CRM_Queue_Task(
578 // callback
579 array('CRM_Upgrade_Form', 'doIncrementalUpgradeStep'),
580 // arguments
581 array($rev, $currentVer, $latestVer, $postUpgradeMessageFile),
582 "Upgrade DB to $rev"
583 );
584 $queue->createItem($task);
585
586 $task = new CRM_Queue_Task(
587 // callback
588 array('CRM_Upgrade_Form', 'doIncrementalUpgradeFinish'),
589 // arguments
590 array($rev, $currentVer, $latestVer, $postUpgradeMessageFile),
591 "Finish Upgrade DB to $rev"
592 );
593 $queue->createItem($task);
594 }
595 }
596
597 return $queue;
598 }
599
600 /**
601 * Perform an incremental version update.
602 *
603 * @param CRM_Queue_TaskContext $ctx
604 * @param string $rev
605 * the target (intermediate) revision e.g '3.2.alpha1'.
606 *
607 * @return bool
608 */
609 public static function doIncrementalUpgradeStart(CRM_Queue_TaskContext $ctx, $rev) {
610 $upgrade = new CRM_Upgrade_Form();
611
612 // as soon as we start doing anything we append ".upgrade" to version.
613 // this also helps detect any partial upgrade issues
614 $upgrade->setVersion($rev . '.upgrade');
615
616 return TRUE;
617 }
618
619 /**
620 * Perform an incremental version update.
621 *
622 * @param CRM_Queue_TaskContext $ctx
623 * @param string $rev
624 * the target (intermediate) revision e.g '3.2.alpha1'.
625 * @param string $originalVer
626 * the original revision.
627 * @param string $latestVer
628 * the target (final) revision.
629 * @param string $postUpgradeMessageFile
630 * path of a modifiable file which lists the post-upgrade messages.
631 *
632 * @return bool
633 */
634 public static function doIncrementalUpgradeStep(CRM_Queue_TaskContext $ctx, $rev, $originalVer, $latestVer, $postUpgradeMessageFile) {
635 $upgrade = new CRM_Upgrade_Form();
636
637 $phpFunctionName = 'upgrade_' . str_replace('.', '_', $rev);
638
639 $versionObject = $upgrade->incrementalPhpObject($rev);
640
641 // pre-db check for major release.
642 if ($upgrade->checkVersionRelease($rev, 'alpha1')) {
643 if (!(is_callable(array(
644 $versionObject,
645 'verifyPreDBstate',
646 )))
647 ) {
648 CRM_Core_Error::fatal("verifyPreDBstate method was not found for $rev");
649 }
650
651 $error = NULL;
652 if (!($versionObject->verifyPreDBstate($error))) {
653 if (!isset($error)) {
654 $error = "post-condition failed for current upgrade for $rev";
655 }
656 CRM_Core_Error::fatal($error);
657 }
658
659 }
660
661 $upgrade->setSchemaStructureTables($rev);
662
663 if (is_callable(array(
664 $versionObject,
665 $phpFunctionName,
666 ))) {
667 $versionObject->$phpFunctionName($rev, $originalVer, $latestVer);
668 }
669 else {
670 $upgrade->processSQL($rev);
671 }
672
673 // set post-upgrade-message if any
674 if (is_callable(array(
675 $versionObject,
676 'setPostUpgradeMessage',
677 ))) {
678 $postUpgradeMessage = file_get_contents($postUpgradeMessageFile);
679 $versionObject->setPostUpgradeMessage($postUpgradeMessage, $rev);
680 file_put_contents($postUpgradeMessageFile, $postUpgradeMessage);
681 }
682
683 return TRUE;
684 }
685
686 /**
687 * Perform an incremental version update.
688 *
689 * @param CRM_Queue_TaskContext $ctx
690 * @param string $rev
691 * the target (intermediate) revision e.g '3.2.alpha1'.
692 * @param string $currentVer
693 * the original revision.
694 * @param string $latestVer
695 * the target (final) revision.
696 * @param string $postUpgradeMessageFile
697 * path of a modifiable file which lists the post-upgrade messages.
698 *
699 * @return bool
700 */
701 public static function doIncrementalUpgradeFinish(CRM_Queue_TaskContext $ctx, $rev, $currentVer, $latestVer, $postUpgradeMessageFile) {
702 $upgrade = new CRM_Upgrade_Form();
703 $upgrade->setVersion($rev);
704 CRM_Utils_System::flushCache();
705
706 $config = CRM_Core_Config::singleton();
707 $config->userSystem->flush();
708 return TRUE;
709 }
710
711 public static function doFinish() {
712 $upgrade = new CRM_Upgrade_Form();
713 list($ignore, $latestVer) = $upgrade->getUpgradeVersions();
714 // Seems extraneous in context, but we'll preserve old behavior
715 $upgrade->setVersion($latestVer);
716
717 // Clear cached metadata.
718 Civi::service('settings_manager')->flush();
719
720 // cleanup caches CRM-8739
721 $config = CRM_Core_Config::singleton();
722 $config->cleanupCaches(1);
723
724 // Rebuild all triggers and re-enable logging if needed
725 $logging = new CRM_Logging_Schema();
726 $logging->fixSchemaDifferences();
727
728 //CRM-16257 update Config.IDS.ini might be an old copy
729 CRM_Core_IDS::createConfigFile(TRUE);
730 }
731
732 /**
733 * Compute any messages which should be displayed before upgrade
734 * by calling the 'setPreUpgradeMessage' on each incremental upgrade
735 * object.
736 *
737 * @param string $preUpgradeMessage
738 * alterable.
739 * @param $currentVer
740 * @param $latestVer
741 */
742 public function setPreUpgradeMessage(&$preUpgradeMessage, $currentVer, $latestVer) {
743 // check for changed message templates
744 CRM_Upgrade_Incremental_General::checkMessageTemplate($preUpgradeMessage, $latestVer, $currentVer);
745 // set global messages
746 CRM_Upgrade_Incremental_General::setPreUpgradeMessage($preUpgradeMessage, $currentVer, $latestVer);
747
748 // Scan through all php files and see if any file is interested in setting pre-upgrade-message
749 // based on $currentVer, $latestVer.
750 // Please note, at this point upgrade hasn't started executing queries.
751 $revisions = $this->getRevisionSequence();
752 foreach ($revisions as $rev) {
753 if (version_compare($currentVer, $rev) < 0) {
754 $versionObject = $this->incrementalPhpObject($rev);
755 if (is_callable(array($versionObject, 'setPreUpgradeMessage'))) {
756 $versionObject->setPreUpgradeMessage($preUpgradeMessage, $rev, $currentVer);
757 }
758 }
759 }
760 }
761
762 }