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