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