Merge pull request #21620 from colemanw/purifier2
[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('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)) {
92 throw new CRM_Core_Exception($error);
93 }
94
95 else {
96 $config = CRM_Core_Config::singleton();
97
98 // All cached content needs to be cleared because the civi codebase was just replaced
99 CRM_Core_Resources::singleton()->flushStrings()->resetCacheCode();
100
101 // cleanup only the templates_c directory
102 $config->cleanup(1, FALSE);
103
104 $preUpgradeMessage = NULL;
105 $upgrade->setPreUpgradeMessage($preUpgradeMessage, $currentVer, $latestVer);
106
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 }
115
116 // Render page header
117 if (!defined('CIVICRM_UF_HEAD') && $region = CRM_Core_Region::instance('html-header', FALSE)) {
118 CRM_Utils_System::addHTMLHead($region->render(''));
119 }
120
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 */
128 public function runBegin() {
129 $upgrade = new CRM_Upgrade_Form();
130 list($currentVer, $latestVer) = $upgrade->getUpgradeVersions();
131
132 if ($error = $upgrade->checkUpgradeableVersion($currentVer, $latestVer)) {
133 throw new CRM_Core_Exception($error);
134 }
135
136 $config = CRM_Core_Config::singleton();
137
138 $postUpgradeMessage = '<span class="bold">' . ts('Congratulations! Your upgrade was successful!') . '</span>';
139
140 // lets drop all the triggers here
141 CRM_Core_DAO::dropTriggers();
142
143 $this->set('isUpgradePending', TRUE);
144
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
150 $queueRunner = new CRM_Queue_Runner([
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',
155 'onEndUrl' => CRM_Utils_System::url('civicrm/upgrade', 'action=finish', FALSE, NULL, FALSE),
156 'buttons' => ['retry' => $config->debug, 'skip' => $config->debug],
157 ]);
158 $queueRunner->runAllViaWeb();
159 throw new CRM_Core_Exception(ts('Upgrade failed to redirect'));
160 }
161
162 /**
163 * Display any final messages, clear caches, etc
164 */
165 public function runFinish() {
166 $upgrade = new CRM_Upgrade_Form();
167 $template = CRM_Core_Smarty::singleton();
168
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')) {
172 // TODO: Use structured message store
173 $postUpgradeMessage = file_get_contents($this->get('postUpgradeMessageFile'));
174
175 // This destroys $session, so do it after get('postUpgradeMessageFile')
176 CRM_Upgrade_Form::doFinish();
177 }
178 else {
179 // Session was destroyed! Can't recover messages.
180 $postUpgradeMessage = '';
181 }
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)) {
186 throw new CRM_Core_Exception($error);
187 }
188
189 $template->assign('message', $postUpgradeMessage);
190 $template->assign('upgraded', TRUE);
191 $template->assign('newVersion', $latestVer);
192
193 // Render page header
194 if (!defined('CIVICRM_UF_HEAD') && $region = CRM_Core_Region::instance('html-header', FALSE)) {
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 }
201
202 }