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