Merge remote-tracking branch 'upstream/4.3' into 4.3-master-2013-04-23-15-17-29
[civicrm-core.git] / CRM / Upgrade / Form.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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 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-2013
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 protected $_config;
46
47 // note latestVersion is legacy code, and
48 // only used for 2.0 -> 2.1 upgrade
49 public $latestVersion;
50
51 /**
52 * Upgrade for multilingual
53 *
54 * @var boolean
55 * @public
56 */
57 public $multilingual = FALSE;
58
59 /**
60 * locales available for multilingual upgrade
61 *
62 * @var array
63 * @public
64 */
65 public $locales;
66
67 /**
68 * number to string mapper
69 *
70 * @var array
71 * @public
72 */
73 static $_numberMap = array(
74 0 => 'Zero',
75 1 => 'One',
76 2 => 'Two',
77 3 => 'Three',
78 4 => 'Four',
79 5 => 'Five',
80 6 => 'Fix',
81 7 => 'Seven',
82 8 => 'Eight',
83 9 => 'Nine',
84 );
85
86 function __construct($state = NULL,
87 $action = CRM_Core_Action::NONE,
88 $method = 'post',
89 $name = NULL
90 ) {
91 $this->_config = CRM_Core_Config::singleton();
92
93 // this->latestVersion is legacy code, only used for 2.0 -> 2.1 upgrade
94 // latest ver in 2.1 series
95 $this->latestVersion = '2.1.6';
96
97 $domain = new CRM_Core_DAO_Domain();
98 $domain->find(TRUE);
99
100 $this->multilingual = (bool) $domain->locales;
101 $this->locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales);
102
103 $smarty = CRM_Core_Smarty::singleton();
104 $smarty->compile_dir = $this->_config->templateCompileDir;
105 $smarty->assign('multilingual', $this->multilingual);
106 $smarty->assign('locales', $this->locales);
107
108 // we didn't call CRM_Core_BAO_ConfigSetting::retrieve(), so we need to set $dbLocale by hand
109 if ($this->multilingual) {
110 global $dbLocale;
111 $dbLocale = "_{$this->_config->lcMessages}";
112 }
113
114 parent::__construct($state, $action, $method, $name);
115 }
116
117 static function &incrementalPhpObject($version) {
118 static $incrementalPhpObject = array();
119
120 $versionParts = explode('.', $version);
121 $versionName = self::$_numberMap[$versionParts[0]] . self::$_numberMap[$versionParts[1]];
122
123 if (!array_key_exists($versionName, $incrementalPhpObject)) {
124 eval("\$incrementalPhpObject['$versionName'] = new CRM_Upgrade_Incremental_php_{$versionName};");
125 }
126 return $incrementalPhpObject[$versionName];
127 }
128
129 function checkVersionRelease($version, $release) {
130 $versionParts = explode('.', $version);
131 if ($versionParts[2] == $release) {
132 return TRUE;
133 }
134 return FALSE;
135 }
136
137 function checkSQLConstraints(&$constraints) {
138 $pass = $fail = 0;
139 foreach ($constraints as $constraint) {
140 if ($this->checkSQLConstraint($constraint)) {
141 $pass++;
142 }
143 else {
144 $fail++;
145 }
146 return array($pass, $fail);
147 }
148 }
149
150 function checkSQLConstraint($constraint) {
151 // check constraint here
152 return TRUE;
153 }
154
155 function source($fileName, $isQueryString = FALSE) {
156
157 CRM_Utils_File::sourceSQLFile($this->_config->dsn,
158 $fileName, NULL, $isQueryString
159 );
160 }
161
162 function preProcess() {
163 CRM_Utils_System::setTitle($this->getTitle());
164 if (!$this->verifyPreDBState($errorMessage)) {
165 if (!isset($errorMessage)) {
166 $errorMessage = 'pre-condition failed for current upgrade step';
167 }
168 CRM_Core_Error::fatal($errorMessage);
169 }
170 $this->assign('recentlyViewed', FALSE);
171 }
172
173 function buildQuickForm() {
174 $this->addDefaultButtons($this->getButtonTitle(),
175 'next',
176 NULL,
177 TRUE
178 );
179 }
180
181 function getTitle() {
182 return ts('Title not Set');
183 }
184
185 function getFieldsetTitle() {
186 return ts('');
187 }
188
189 function getButtonTitle() {
190 return ts('Continue');
191 }
192
193 function getTemplateFileName() {
194 $this->assign('title',
195 $this->getFieldsetTitle()
196 );
197 $this->assign('message',
198 $this->getTemplateMessage()
199 );
200 return 'CRM/Upgrade/Base.tpl';
201 }
202
203 function postProcess() {
204 $this->upgrade();
205
206 if (!$this->verifyPostDBState($errorMessage)) {
207 if (!isset($errorMessage)) {
208 $errorMessage = 'post-condition failed for current upgrade step';
209 }
210 CRM_Core_Error::fatal($errorMessage);
211 }
212 }
213
214 function runQuery($query) {
215 return CRM_Core_DAO::executeQuery($query,
216 CRM_Core_DAO::$_nullArray
217 );
218 }
219
220 function setVersion($version) {
221 $this->logVersion($version);
222
223 $query = "
224 UPDATE civicrm_domain
225 SET version = '$version'
226 ";
227 return $this->runQuery($query);
228 }
229
230 function logVersion($newVersion) {
231 if ($newVersion) {
232 $oldVersion = CRM_Core_BAO_Domain::version();
233
234 $session = CRM_Core_Session::singleton();
235 $logParams = array(
236 'entity_table' => 'civicrm_domain',
237 'entity_id' => 1,
238 'data' => "upgrade:{$oldVersion}->{$newVersion}",
239 // lets skip 'modified_id' for now, as it causes FK issues And
240 // is not very important for now.
241 'modified_date' => date('YmdHis'),
242 );
243 CRM_Core_BAO_Log::add($logParams);
244 return TRUE;
245 }
246
247 return FALSE;
248 }
249
250 function checkVersion($version) {
251 $domainID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain',
252 $version, 'id',
253 'version'
254 );
255 return $domainID ? TRUE : FALSE;
256 }
257
258 function getRevisionSequence() {
259 $revList = array();
260 $sqlDir = implode(DIRECTORY_SEPARATOR,
261 array(dirname(__FILE__), 'Incremental', 'sql')
262 );
263 $sqlFiles = scandir($sqlDir);
264
265 $sqlFilePattern = '/^((\d{1,2}\.\d{1,2})\.(\d{1,2}\.)?(\d{1,2}|\w{4,7}))\.(my)?sql(\.tpl)?$/i';
266 foreach ($sqlFiles as $file) {
267 if (preg_match($sqlFilePattern, $file, $matches)) {
268 if ($matches[2] == '4.0') {
269 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."));
270 }
271 if (!in_array($matches[1], $revList)) {
272 $revList[] = $matches[1];
273 }
274 }
275 }
276
277 // sample test list
278 /* $revList = array(
279 '2.1.0', '2.2.beta2', '2.2.beta1', '2.2.alpha1', */
280
281 /* '2.2.alpha3', '2.2.0', '2.2.2', '2.1.alpha1', '2.1.3'); */
282
283
284 usort($revList, 'version_compare');
285 return $revList;
286 }
287
288 static function getRevisionPart($rev, $index = 1) {
289 $revPattern = '/^((\d{1,2})\.\d{1,2})\.(\d{1,2}|\w{4,7})?$/i';
290 preg_match($revPattern, $rev, $matches);
291
292 return array_key_exists($index, $matches) ? $matches[$index] : NULL;
293 }
294
295 function processLocales($tplFile, $rev) {
296 $smarty = CRM_Core_Smarty::singleton();
297 $smarty->assign('domainID', CRM_Core_Config::domainID());
298
299 $this->source($smarty->fetch($tplFile), TRUE);
300
301 if ($this->multilingual) {
302 CRM_Core_I18n_Schema::rebuildMultilingualSchema($this->locales, $rev);
303 }
304 return $this->multilingual;
305 }
306
307 function setSchemaStructureTables($rev) {
308 if ($this->multilingual) {
309 CRM_Core_I18n_Schema::schemaStructureTables($rev, TRUE);
310 }
311 }
312
313 function processSQL($rev) {
314 $sqlFile = implode(DIRECTORY_SEPARATOR,
315 array(
316 dirname(__FILE__), 'Incremental',
317 'sql', $rev . '.mysql',
318 )
319 );
320 $tplFile = "$sqlFile.tpl";
321
322 if (file_exists($tplFile)) {
323 $this->processLocales($tplFile, $rev);
324 }
325 else {
326 if (!file_exists($sqlFile)) {
327 CRM_Core_Error::fatal("sqlfile - $rev.mysql not found.");
328 }
329 $this->source($sqlFile);
330 }
331 }
332
333 /**
334 * Determine the start and end version of the upgrade process
335 *
336 * @return array(0=>$currentVer, 1=>$latestVer)
337 */
338 function getUpgradeVersions() {
339 $latestVer = CRM_Utils_System::version();
340 $currentVer = CRM_Core_BAO_Domain::version(true);
341 if (!$currentVer) {
342 CRM_Core_Error::fatal(ts('Version information missing in civicrm database.'));
343 }
344 elseif (stripos($currentVer, 'upgrade')) {
345 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.'));
346 }
347 if (!$latestVer) {
348 CRM_Core_Error::fatal(ts('Version information missing in civicrm codebase.'));
349 }
350
351 // hack to make past ver compatible /w new incremental upgrade process
352 $convertVer = array(
353 '2.1' => '2.1.0',
354 '2.2' => '2.2.alpha1',
355 '2.2.alph' => '2.2.alpha3',
356 // since 3.1.1 had domain.version set as 3.1.0
357 '3.1.0' => '3.1.1',
358 );
359 if (isset($convertVer[$currentVer])) {
360 $currentVer = $convertVer[$currentVer];
361 }
362
363 return array($currentVer, $latestVer);
364 }
365
366 /**
367 * Determine if $currentVer can be upgraded to $latestVer
368 *
369 * @return mixed, a string error message or boolean 'false' if OK
370 */
371 function checkUpgradeableVersion($currentVer, $latestVer) {
372 $error = FALSE;
373 // since version is suppose to be in valid format at this point, especially after conversion ($convertVer),
374 // lets do a pattern check -
375 if (!CRM_Utils_System::isVersionFormatValid($currentVer)) {
376 $error = ts('Database is marked with invalid version format. You may want to investigate this before you proceed further.');
377 }
378 elseif (version_compare($currentVer, $latestVer) > 0) {
379 // DB version number is higher than codebase being upgraded to. This is unexpected condition-fatal error.
380 $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.',
381 array(1 => $currentVer, 2 => $latestVer, 3 => $dbToolsLink)
382 );
383 }
384 elseif (version_compare($currentVer, $latestVer) == 0) {
385 $error = ts('Your database has already been upgraded to CiviCRM %1',
386 array(1 => $latestVer)
387 );
388 }
389
390 $phpVersion = phpversion();
391 $minPhpVersion = '5.3.3';
392 if (version_compare($phpVersion, $minPhpVersion) < 0) {
393 $error = ts('CiviCRM %3 requires PHP version %1 (or newer), but the current system uses %2 ',
394 array(
395 1 => $minPhpVersion,
396 2 => $phpVersion,
397 3 => $latestVer
398 ));
399 }
400
401 // check for mysql trigger privileges
402 if (!CRM_Core_DAO::checkTriggerViewPermission(FALSE, TRUE)) {
403 $error = ts('CiviCRM %1 requires MySQL trigger privileges.',
404 array(1 => $latestVer));
405 }
406
407 if (CRM_Core_DAO::getGlobalSetting('thread_stack', 0) < (1024*self::MINIMUM_THREAD_STACK)) {
408 $error = ts('CiviCRM %1 requires MySQL thread stack >= %2k', array(
409 1 => $latestVer,
410 2 => self::MINIMUM_THREAD_STACK
411 ));
412 }
413
414 return $error;
415 }
416
417 /**
418 * Determine if $currentver already matches $latestVer
419 *
420 * @return mixed, a string error message or boolean 'false' if OK
421 */
422 function checkCurrentVersion($currentVer, $latestVer) {
423 $error = FALSE;
424
425 // since version is suppose to be in valid format at this point, especially after conversion ($convertVer),
426 // lets do a pattern check -
427 if (!CRM_Utils_System::isVersionFormatValid($currentVer)) {
428 $error = ts('Database is marked with invalid version format. You may want to investigate this before you proceed further.');
429 }
430 elseif (version_compare($currentVer, $latestVer) != 0) {
431 $error = ts('Your database is not configured for version %1',
432 array(1 => $latestVer)
433 );
434 }
435 return $error;
436 }
437
438 /**
439 * Fill the queue with upgrade tasks
440 *
441 * @param $currentVer string, the original revision
442 * @param $latestVer string, the target (final) revision
443 * @param $postUpgradeMessageFile string, path of a modifiable file which lists the post-upgrade messages
444 *
445 * @return CRM_Queue
446 */
447 static function buildQueue($currentVer, $latestVer, $postUpgradeMessageFile) {
448 $upgrade = new CRM_Upgrade_Form();
449
450 // hack to make 4.0.x (D7,J1.6) codebase go through 3.4.x (d6, J1.5) upgrade files,
451 // since schema wise they are same
452 if (CRM_Upgrade_Form::getRevisionPart($currentVer) == '4.0') {
453 $currentVer = str_replace('4.0.', '3.4.', $currentVer);
454 }
455
456 // Ensure that queue can be created
457 if (!CRM_Queue_BAO_QueueItem::findCreateTable()) {
458 CRM_Core_Error::fatal(ts('Failed to find or create queueing table'));
459 }
460 $queue = CRM_Queue_Service::singleton()->create(array(
461 'name' => self::QUEUE_NAME,
462 'type' => 'Sql',
463 'reset' => TRUE,
464 ));
465
466 $revisions = $upgrade->getRevisionSequence();
467 foreach ($revisions as $rev) {
468 // proceed only if $currentVer < $rev
469 if (version_compare($currentVer, $rev) < 0) {
470 $beginTask = new CRM_Queue_Task(
471 // callback
472 array('CRM_Upgrade_Form', 'doIncrementalUpgradeStart'),
473 // arguments
474 array($rev),
475 "Begin Upgrade to $rev"
476 );
477 $queue->createItem($beginTask);
478
479 $task = new CRM_Queue_Task(
480 // callback
481 array('CRM_Upgrade_Form', 'doIncrementalUpgradeStep'),
482 // arguments
483 array($rev, $currentVer, $latestVer, $postUpgradeMessageFile),
484 "Upgrade DB to $rev"
485 );
486 $queue->createItem($task);
487
488 $task = new CRM_Queue_Task(
489 // callback
490 array('CRM_Upgrade_Form', 'doIncrementalUpgradeFinish'),
491 // arguments
492 array($rev),
493 "Finish Upgrade DB to $rev"
494 );
495 $queue->createItem($task);
496 }
497 }
498
499 return $queue;
500 }
501
502 /**
503 * Perform an incremental version update
504 *
505 * @param $rev string, the target (intermediate) revision e.g '3.2.alpha1'
506 * @param $currentVer string, the original revision
507 * @param $latestVer string, the target (final) revision
508 * @param $postUpgradeMessageFile string, path of a modifiable file which lists the post-upgrade messages
509 */
510 static function doIncrementalUpgradeStart(CRM_Queue_TaskContext $ctx, $rev) {
511 $upgrade = new CRM_Upgrade_Form();
512
513 // as soon as we start doing anything we append ".upgrade" to version.
514 // this also helps detect any partial upgrade issues
515 $upgrade->setVersion($rev . '.upgrade');
516
517 return TRUE;
518 }
519
520 /**
521 * Perform an incremental version update
522 *
523 * @param $rev string, the target (intermediate) revision e.g '3.2.alpha1'
524 * @param $currentVer string, the original revision
525 * @param $latestVer string, the target (final) revision
526 * @param $postUpgradeMessageFile string, path of a modifiable file which lists the post-upgrade messages
527 */
528 static function doIncrementalUpgradeStep(CRM_Queue_TaskContext$ctx, $rev, $currentVer, $latestVer, $postUpgradeMessageFile) {
529 $upgrade = new CRM_Upgrade_Form();
530
531 $phpFunctionName = 'upgrade_' . str_replace('.', '_', $rev);
532
533 // follow old upgrade process for all version
534 // below 3.2.alpha1
535 if (version_compare($rev, '3.2.alpha1') < 0) {
536 if (is_callable(array(
537 'CRM_Upgrade_Incremental_Legacy', $phpFunctionName))) {
538 call_user_func(array('CRM_Upgrade_Incremental_Legacy', $phpFunctionName), $rev);
539 }
540 else {
541 $upgrade->processSQL($rev);
542 }
543 }
544 else {
545 // new upgrade process from version
546 // 3.2.alpha1
547 $versionObject = $upgrade->incrementalPhpObject($rev);
548
549 // pre-db check for major release.
550 if ($upgrade->checkVersionRelease($rev, 'alpha1')) {
551 if (!(is_callable(array(
552 $versionObject, 'verifyPreDBstate')))) {
553 CRM_Core_Error::fatal("verifyPreDBstate method was not found for $rev");
554 }
555
556 $error = NULL;
557 if (!($versionObject->verifyPreDBstate($error))) {
558 if (!isset($error)) {
559 $error = "post-condition failed for current upgrade for $rev";
560 }
561 CRM_Core_Error::fatal($error);
562 }
563
564 }
565
566 $upgrade->setSchemaStructureTables($rev);
567
568 if (is_callable(array(
569 $versionObject, $phpFunctionName))) {
570 $versionObject->$phpFunctionName($rev);
571 }
572 else {
573 $upgrade->processSQL($rev);
574 }
575
576 // set post-upgrade-message if any
577 if (is_callable(array(
578 $versionObject, 'setPostUpgradeMessage'))) {
579 $postUpgradeMessage = file_get_contents($postUpgradeMessageFile);
580 $versionObject->setPostUpgradeMessage($postUpgradeMessage, $rev);
581 file_put_contents($postUpgradeMessageFile, $postUpgradeMessage);
582 } else {
583 $postUpgradeMessage = file_get_contents($postUpgradeMessageFile);
584 CRM_Upgrade_Incremental_Legacy::setPostUpgradeMessage($postUpgradeMessage, $rev);
585 file_put_contents($postUpgradeMessageFile, $postUpgradeMessage);
586 }
587 }
588
589 return TRUE;
590 }
591
592 /**
593 * Perform an incremental version update
594 *
595 * @param $rev string, the target (intermediate) revision e.g '3.2.alpha1'
596 * @param $currentVer string, the original revision
597 * @param $latestVer string, the target (final) revision
598 * @param $postUpgradeMessageFile string, path of a modifiable file which lists the post-upgrade messages
599 */
600 static function doIncrementalUpgradeFinish(CRM_Queue_TaskContext $ctx, $rev) {
601 $upgrade = new CRM_Upgrade_Form();
602 $upgrade->setVersion($rev);
603 CRM_Utils_System::flushCache();
604
605 $config = CRM_Core_Config::singleton();
606 $config->userSystem->flush();
607
608 if (version_compare($currentVer, '4.1.alpha1') >= 0) {
609 CRM_Core_BAO_Setting::updateSettingsFromMetaData();
610 }
611 return TRUE;
612 }
613
614 static function doFinish() {
615 $upgrade = new CRM_Upgrade_Form();
616 list($ignore, $latestVer) = $upgrade->getUpgradeVersions();
617 // Seems extraneous in context, but we'll preserve old behavior
618 $upgrade->setVersion($latestVer);
619
620 // lets rebuild the config array in case we've made a few changes in the
621 // code base
622 // this also helps us always store the latest version of civi in the DB
623 $params = array();
624 CRM_Core_BAO_ConfigSetting::add($params);
625
626 // cleanup caches CRM-8739
627 $config = CRM_Core_Config::singleton();
628 $config->cleanupCaches(1);
629
630 // Rebuild all triggers and re-enable logging if needed
631 $logging = new CRM_Logging_Schema();
632 $logging->fixSchemaDifferences();
633 }
634
635 /**
636 * Compute any messages which should be displayed before upgrade
637 * by calling the 'setPreUpgradeMessage' on each incremental upgrade
638 * object.
639 *
640 * @param $preUpgradeMessage string, alterable
641 */
642 function setPreUpgradeMessage(&$preUpgradeMessage, $currentVer, $latestVer) {
643 CRM_Upgrade_Incremental_Legacy::setPreUpgradeMessage($preUpgradeMessage, $currentVer, $latestVer);
644
645 // Scan through all php files and see if any file is interested in setting pre-upgrade-message
646 // based on $currentVer, $latestVer.
647 // Please note, at this point upgrade hasn't started executing queries.
648 $revisions = $this->getRevisionSequence();
649 foreach ($revisions as $rev) {
650 if (version_compare($currentVer, $rev) < 0 &&
651 version_compare($rev, '3.2.alpha1') > 0
652 ) {
653 $versionObject = $this->incrementalPhpObject($rev);
654 if (is_callable(array(
655 $versionObject, 'setPreUpgradeMessage'))) {
656 $versionObject->setPreUpgradeMessage($preUpgradeMessage, $rev, $currentVer);
657 }
658 }
659 }
660 }
661 }