Merge pull request #21729 from MegaphoneJon/financial-181-rc
[civicrm-core.git] / CRM / Upgrade / Page / Upgrade.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17class CRM_Upgrade_Page_Upgrade extends CRM_Core_Page {
e8e8f3ad 18
19 /**
20 * Pre-process.
21 */
00be9182 22 public function preProcess() {
6a488035
TO
23 parent::preProcess();
24 }
25
70599df6 26 /**
27 * Run upgrade.
28 *
29 * @throws \Exception
30 */
00be9182 31 public function run() {
4d03ddb9 32 set_time_limit(0);
6a488035 33
4ef8abc2
CW
34 Civi::resources()->addStyleFile('civicrm', 'css/admin.css');
35
6a488035
TO
36 $upgrade = new CRM_Upgrade_Form();
37 list($currentVer, $latestVer) = $upgrade->getUpgradeVersions();
38
39 CRM_Utils_System::setTitle(ts('Upgrade CiviCRM to Version %1',
be2fb01f 40 [1 => $latestVer]
6a488035
TO
41 ));
42
43 $template = CRM_Core_Smarty::singleton();
44 $template->assign('pageTitle', ts('Upgrade CiviCRM to Version %1',
be2fb01f 45 [1 => $latestVer]
6a488035 46 ));
6a488035
TO
47 $template->assign('cancelURL',
48 CRM_Utils_System::url('civicrm/dashboard', 'reset=1')
49 );
50
51 $action = CRM_Utils_Array::value('action', $_REQUEST, 'intro');
52 switch ($action) {
53 case 'intro':
54 $this->runIntro();
55 break;
56
57 case 'begin':
58 $this->runBegin();
59 break;
60
61 case 'finish':
62 $this->runFinish();
63 break;
64
65 default:
800dad43 66 throw new CRM_Core_Exception(ts('Unrecognized upgrade action'));
6a488035
TO
67 }
68 }
69
70 /**
e8e8f3ad 71 * Display an introductory screen with any pre-upgrade messages.
6a488035 72 */
00be9182 73 public function runIntro() {
6a488035
TO
74 $upgrade = new CRM_Upgrade_Form();
75 $template = CRM_Core_Smarty::singleton();
76 list($currentVer, $latestVer) = $upgrade->getUpgradeVersions();
6fe73a4d 77 CRM_Core_Smarty::singleton()->assign('sid', CRM_Utils_System::getSiteID());
4ee04d40
CW
78 // Show success msg if db already upgraded
79 if (version_compare($currentVer, $latestVer) == 0) {
80 $template->assign('upgraded', TRUE);
81 $template->assign('newVersion', $latestVer);
82 CRM_Utils_System::setTitle(ts('Your database has already been upgraded to CiviCRM %1',
83 [1 => $latestVer]
84 ));
85 $template->assign('pageTitle', ts('Your database has already been upgraded to CiviCRM %1',
86 [1 => $latestVer]
87 ));
88 }
89
90 // Throw error if db in unexpected condition
91 elseif ($error = $upgrade->checkUpgradeableVersion($currentVer, $latestVer)) {
800dad43 92 throw new CRM_Core_Exception($error);
6a488035
TO
93 }
94
4ee04d40
CW
95 else {
96 $config = CRM_Core_Config::singleton();
cb0a8786 97
4ee04d40
CW
98 // All cached content needs to be cleared because the civi codebase was just replaced
99 CRM_Core_Resources::singleton()->flushStrings()->resetCacheCode();
6633dcbf 100
4ee04d40
CW
101 // cleanup only the templates_c directory
102 $config->cleanup(1, FALSE);
6a488035 103
4ee04d40
CW
104 $preUpgradeMessage = NULL;
105 $upgrade->setPreUpgradeMessage($preUpgradeMessage, $currentVer, $latestVer);
6a488035 106
4ee04d40
CW
107 $template->assign('preUpgradeMessage', $preUpgradeMessage);
108 $template->assign('currentVersion', $currentVer);
109 $template->assign('newVersion', $latestVer);
110 $template->assign('upgradeTitle', ts('Upgrade CiviCRM from v %1 To v %2',
111 [1 => $currentVer, 2 => $latestVer]
112 ));
113 $template->assign('upgraded', FALSE);
114 }
6a488035
TO
115
116 // Render page header
9dc21423 117 if (!defined('CIVICRM_UF_HEAD') && $region = CRM_Core_Region::instance('html-header', FALSE)) {
6a488035
TO
118 CRM_Utils_System::addHTMLHead($region->render(''));
119 }
120
6a488035
TO
121 $content = $template->fetch('CRM/common/success.tpl');
122 echo CRM_Utils_System::theme($content, $this->_print, TRUE);
123 }
124
125 /**
126 * Begin the upgrade by building a queue of tasks and redirecting to the queue-runner
127 */
00be9182 128 public function runBegin() {
6a488035
TO
129 $upgrade = new CRM_Upgrade_Form();
130 list($currentVer, $latestVer) = $upgrade->getUpgradeVersions();
131
132 if ($error = $upgrade->checkUpgradeableVersion($currentVer, $latestVer)) {
800dad43 133 throw new CRM_Core_Exception($error);
6a488035
TO
134 }
135
136 $config = CRM_Core_Config::singleton();
6a488035 137
3a111e86 138 $postUpgradeMessage = '<span class="bold">' . ts('Congratulations! Your upgrade was successful!') . '</span>';
6a488035
TO
139
140 // lets drop all the triggers here
141 CRM_Core_DAO::dropTriggers();
142
c76e6d43
TO
143 $this->set('isUpgradePending', TRUE);
144
6a488035
TO
145 // Persistent message storage across upgrade steps. TODO: Use structured message store
146 // Note: In clustered deployments, this file must be accessible by all web-workers.
147 $this->set('postUpgradeMessageFile', CRM_Utils_File::tempnam('civicrm-post-upgrade'));
148 file_put_contents($this->get('postUpgradeMessageFile'), $postUpgradeMessage);
149
be2fb01f 150 $queueRunner = new CRM_Queue_Runner([
6a488035
TO
151 'title' => ts('CiviCRM Upgrade Tasks'),
152 'queue' => CRM_Upgrade_Form::buildQueue($currentVer, $latestVer, $this->get('postUpgradeMessageFile')),
153 'isMinimal' => TRUE,
154 'pathPrefix' => 'civicrm/upgrade/queue',
481a74f4 155 'onEndUrl' => CRM_Utils_System::url('civicrm/upgrade', 'action=finish', FALSE, NULL, FALSE),
be2fb01f
CW
156 'buttons' => ['retry' => $config->debug, 'skip' => $config->debug],
157 ]);
6a488035 158 $queueRunner->runAllViaWeb();
800dad43 159 throw new CRM_Core_Exception(ts('Upgrade failed to redirect'));
6a488035
TO
160 }
161
162 /**
163 * Display any final messages, clear caches, etc
164 */
00be9182 165 public function runFinish() {
6a488035
TO
166 $upgrade = new CRM_Upgrade_Form();
167 $template = CRM_Core_Smarty::singleton();
168
c76e6d43
TO
169 // If we're redirected from queue-runner, then isUpgradePending=true.
170 // If user then reloads the finish page, the isUpgradePending will be unset. (Because the session has been cleared.)
171 if ($this->get('isUpgradePending')) {
43992531
TO
172 // TODO: Use structured message store
173 $postUpgradeMessage = file_get_contents($this->get('postUpgradeMessageFile'));
6a488035 174
43992531
TO
175 // This destroys $session, so do it after get('postUpgradeMessageFile')
176 CRM_Upgrade_Form::doFinish();
0db6c3e1
TO
177 }
178 else {
3655bea4
SL
179 // Session was destroyed! Can't recover messages.
180 $postUpgradeMessage = '';
43992531 181 }
6a488035
TO
182
183 // do a version check - after doFinish() sets the final version
184 list($currentVer, $latestVer) = $upgrade->getUpgradeVersions();
185 if ($error = $upgrade->checkCurrentVersion($currentVer, $latestVer)) {
800dad43 186 throw new CRM_Core_Exception($error);
6a488035
TO
187 }
188
189 $template->assign('message', $postUpgradeMessage);
190 $template->assign('upgraded', TRUE);
a343bebf 191 $template->assign('newVersion', $latestVer);
6a488035
TO
192
193 // Render page header
9dc21423 194 if (!defined('CIVICRM_UF_HEAD') && $region = CRM_Core_Region::instance('html-header', FALSE)) {
6a488035
TO
195 CRM_Utils_System::addHTMLHead($region->render(''));
196 }
197
198 $content = $template->fetch('CRM/common/success.tpl');
199 echo CRM_Utils_System::theme($content, $this->_print, TRUE);
200 }
96025800 201
6a488035 202}