Merge pull request #23743 from totten/master-queue-runas
[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) {
e20f6493 80 $template->assign('message', '');
4ee04d40
CW
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)) {
800dad43 93 throw new CRM_Core_Exception($error);
6a488035
TO
94 }
95
4ee04d40
CW
96 else {
97 $config = CRM_Core_Config::singleton();
cb0a8786 98
4ee04d40
CW
99 // All cached content needs to be cleared because the civi codebase was just replaced
100 CRM_Core_Resources::singleton()->flushStrings()->resetCacheCode();
6633dcbf 101
4ee04d40
CW
102 // cleanup only the templates_c directory
103 $config->cleanup(1, FALSE);
6a488035 104
4ee04d40
CW
105 $preUpgradeMessage = NULL;
106 $upgrade->setPreUpgradeMessage($preUpgradeMessage, $currentVer, $latestVer);
6a488035 107
4ee04d40
CW
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 }
6a488035
TO
116
117 // Render page header
9dc21423 118 if (!defined('CIVICRM_UF_HEAD') && $region = CRM_Core_Region::instance('html-header', FALSE)) {
6a488035
TO
119 CRM_Utils_System::addHTMLHead($region->render(''));
120 }
121
6a488035
TO
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 */
00be9182 129 public function runBegin() {
6a488035
TO
130 $upgrade = new CRM_Upgrade_Form();
131 list($currentVer, $latestVer) = $upgrade->getUpgradeVersions();
132
133 if ($error = $upgrade->checkUpgradeableVersion($currentVer, $latestVer)) {
800dad43 134 throw new CRM_Core_Exception($error);
6a488035
TO
135 }
136
137 $config = CRM_Core_Config::singleton();
6a488035 138
3a111e86 139 $postUpgradeMessage = '<span class="bold">' . ts('Congratulations! Your upgrade was successful!') . '</span>';
6a488035
TO
140
141 // lets drop all the triggers here
142 CRM_Core_DAO::dropTriggers();
143
c76e6d43
TO
144 $this->set('isUpgradePending', TRUE);
145
6a488035
TO
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
be2fb01f 151 $queueRunner = new CRM_Queue_Runner([
6a488035
TO
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',
481a74f4 156 'onEndUrl' => CRM_Utils_System::url('civicrm/upgrade', 'action=finish', FALSE, NULL, FALSE),
be2fb01f
CW
157 'buttons' => ['retry' => $config->debug, 'skip' => $config->debug],
158 ]);
6a488035 159 $queueRunner->runAllViaWeb();
800dad43 160 throw new CRM_Core_Exception(ts('Upgrade failed to redirect'));
6a488035
TO
161 }
162
163 /**
164 * Display any final messages, clear caches, etc
165 */
00be9182 166 public function runFinish() {
6a488035
TO
167 $upgrade = new CRM_Upgrade_Form();
168 $template = CRM_Core_Smarty::singleton();
169
c76e6d43
TO
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')) {
43992531
TO
173 // TODO: Use structured message store
174 $postUpgradeMessage = file_get_contents($this->get('postUpgradeMessageFile'));
6a488035 175
43992531
TO
176 // This destroys $session, so do it after get('postUpgradeMessageFile')
177 CRM_Upgrade_Form::doFinish();
0db6c3e1
TO
178 }
179 else {
3655bea4
SL
180 // Session was destroyed! Can't recover messages.
181 $postUpgradeMessage = '';
43992531 182 }
6a488035
TO
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)) {
800dad43 187 throw new CRM_Core_Exception($error);
6a488035
TO
188 }
189
190 $template->assign('message', $postUpgradeMessage);
191 $template->assign('upgraded', TRUE);
a343bebf 192 $template->assign('newVersion', $latestVer);
e20f6493 193 $template->assign('sid', CRM_Utils_System::getSiteID());
6a488035
TO
194
195 // Render page header
9dc21423 196 if (!defined('CIVICRM_UF_HEAD') && $region = CRM_Core_Region::instance('html-header', FALSE)) {
6a488035
TO
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 }
96025800 203
6a488035 204}