CRM/Upgrade - Snapshots should have name `civicrm_snap_{OWNER}_{VERSION}_{NAME}`
[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 set_time_limit(0);
33
34 Civi::resources()->addStyleFile('civicrm', 'css/admin.css');
35
36 $upgrade = new CRM_Upgrade_Form();
37 list($currentVer, $latestVer) = $upgrade->getUpgradeVersions();
38
39 CRM_Utils_System::setTitle(ts('Upgrade CiviCRM to Version %1',
40 [1 => $latestVer]
41 ));
42
43 $template = CRM_Core_Smarty::singleton();
44 $template->assign('pageTitle', ts('Upgrade CiviCRM to Version %1',
45 [1 => $latestVer]
46 ));
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:
66 throw new CRM_Core_Exception(ts('Unrecognized upgrade action'));
67 }
68 }
69
70 /**
71 * Display an introductory screen with any pre-upgrade messages.
72 */
73 public function runIntro() {
74 $upgrade = new CRM_Upgrade_Form();
75 $template = CRM_Core_Smarty::singleton();
76 list($currentVer, $latestVer) = $upgrade->getUpgradeVersions();
77 CRM_Core_Smarty::singleton()->assign('sid', CRM_Utils_System::getSiteID());
78 // Show success msg if db already upgraded
79 if (version_compare($currentVer, $latestVer) == 0) {
80 $template->assign('message', '');
81 $template->assign('upgraded', TRUE);
82 $template->assign('newVersion', $latestVer);
83 CRM_Utils_System::setTitle(ts('Your database has already been upgraded to CiviCRM %1',
84 [1 => $latestVer]
85 ));
86 $template->assign('pageTitle', ts('Your database has already been upgraded to CiviCRM %1',
87 [1 => $latestVer]
88 ));
89 }
90
91 // Throw error if db in unexpected condition
92 elseif ($error = $upgrade->checkUpgradeableVersion($currentVer, $latestVer)) {
93 throw new CRM_Core_Exception($error);
94 }
95
96 else {
97 $config = CRM_Core_Config::singleton();
98
99 // All cached content needs to be cleared because the civi codebase was just replaced
100 CRM_Core_Resources::singleton()->flushStrings()->resetCacheCode();
101
102 // cleanup only the templates_c directory
103 $config->cleanup(1, FALSE);
104
105 $preUpgradeMessage = NULL;
106 $upgrade->setPreUpgradeMessage($preUpgradeMessage, $currentVer, $latestVer);
107
108 $template->assign('preUpgradeMessage', $preUpgradeMessage);
109 $template->assign('currentVersion', $currentVer);
110 $template->assign('newVersion', $latestVer);
111 $template->assign('upgradeTitle', ts('Upgrade CiviCRM from v %1 To v %2',
112 [1 => $currentVer, 2 => $latestVer]
113 ));
114 $template->assign('upgraded', FALSE);
115 }
116
117 // Render page header
118 if (!defined('CIVICRM_UF_HEAD') && $region = CRM_Core_Region::instance('html-header', FALSE)) {
119 CRM_Utils_System::addHTMLHead($region->render(''));
120 }
121
122 $content = $template->fetch('CRM/common/success.tpl');
123 echo CRM_Utils_System::theme($content, $this->_print, TRUE);
124 }
125
126 /**
127 * Begin the upgrade by building a queue of tasks and redirecting to the queue-runner
128 */
129 public function runBegin() {
130 $upgrade = new CRM_Upgrade_Form();
131 list($currentVer, $latestVer) = $upgrade->getUpgradeVersions();
132
133 if ($error = $upgrade->checkUpgradeableVersion($currentVer, $latestVer)) {
134 throw new CRM_Core_Exception($error);
135 }
136
137 $config = CRM_Core_Config::singleton();
138
139 $postUpgradeMessage = '<span class="bold">' . ts('Congratulations! Your upgrade was successful!') . '</span>';
140
141 // lets drop all the triggers here
142 CRM_Core_DAO::dropTriggers();
143
144 $this->set('isUpgradePending', TRUE);
145
146 // Persistent message storage across upgrade steps. TODO: Use structured message store
147 // Note: In clustered deployments, this file must be accessible by all web-workers.
148 $this->set('postUpgradeMessageFile', CRM_Utils_File::tempnam('civicrm-post-upgrade'));
149 file_put_contents($this->get('postUpgradeMessageFile'), $postUpgradeMessage);
150
151 $queueRunner = new CRM_Queue_Runner([
152 'title' => ts('CiviCRM Upgrade Tasks'),
153 'queue' => CRM_Upgrade_Form::buildQueue($currentVer, $latestVer, $this->get('postUpgradeMessageFile')),
154 'isMinimal' => TRUE,
155 'pathPrefix' => 'civicrm/upgrade/queue',
156 'onEndUrl' => CRM_Utils_System::url('civicrm/upgrade', 'action=finish', FALSE, NULL, FALSE),
157 'buttons' => ['retry' => $config->debug, 'skip' => $config->debug],
158 ]);
159 $queueRunner->runAllViaWeb();
160 throw new CRM_Core_Exception(ts('Upgrade failed to redirect'));
161 }
162
163 /**
164 * Display any final messages, clear caches, etc
165 */
166 public function runFinish() {
167 $upgrade = new CRM_Upgrade_Form();
168 $template = CRM_Core_Smarty::singleton();
169
170 // If we're redirected from queue-runner, then isUpgradePending=true.
171 // If user then reloads the finish page, the isUpgradePending will be unset. (Because the session has been cleared.)
172 if ($this->get('isUpgradePending')) {
173 // TODO: Use structured message store
174 $postUpgradeMessage = file_get_contents($this->get('postUpgradeMessageFile'));
175
176 // This destroys $session, so do it after get('postUpgradeMessageFile')
177 CRM_Upgrade_Form::doFinish();
178 }
179 else {
180 // Session was destroyed! Can't recover messages.
181 $postUpgradeMessage = '';
182 }
183
184 // do a version check - after doFinish() sets the final version
185 list($currentVer, $latestVer) = $upgrade->getUpgradeVersions();
186 if ($error = $upgrade->checkCurrentVersion($currentVer, $latestVer)) {
187 throw new CRM_Core_Exception($error);
188 }
189
190 $template->assign('message', $postUpgradeMessage);
191 $template->assign('upgraded', TRUE);
192 $template->assign('newVersion', $latestVer);
193 $template->assign('sid', CRM_Utils_System::getSiteID());
194
195 // Render page header
196 if (!defined('CIVICRM_UF_HEAD') && $region = CRM_Core_Region::instance('html-header', FALSE)) {
197 CRM_Utils_System::addHTMLHead($region->render(''));
198 }
199
200 $content = $template->fetch('CRM/common/success.tpl');
201 echo CRM_Utils_System::theme($content, $this->_print, TRUE);
202 }
203
204 }