Merge pull request #6975 from herbdool/master-redis
[civicrm-core.git] / CRM / Upgrade / Page / Upgrade.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Upgrade_Page_Upgrade extends CRM_Core_Page {
00be9182 36 public function preProcess() {
6a488035
TO
37 parent::preProcess();
38 }
39
00be9182 40 public function run() {
6a488035
TO
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
4ef8abc2
CW
46 Civi::resources()->addStyleFile('civicrm', 'css/admin.css');
47
6a488035
TO
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 ));
6a488035
TO
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 */
00be9182 85 public function runIntro() {
6a488035
TO
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
cb0a8786
CW
94 $config = CRM_Core_Config::singleton();
95
6633dcbf 96 // All cached content needs to be cleared because the civi codebase was just replaced
4cc9b813
CW
97 CRM_Core_Resources::singleton()->flushStrings()->resetCacheCode();
98 CRM_Core_Menu::store();
6633dcbf 99
221997c6
CW
100 // cleanup only the templates_c directory
101 $config->cleanup(1, FALSE);
6a488035
TO
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
9dc21423 114 if (!defined('CIVICRM_UF_HEAD') && $region = CRM_Core_Region::instance('html-header', FALSE)) {
6a488035
TO
115 CRM_Utils_System::addHTMLHead($region->render(''));
116 }
117
118 $template->assign('preUpgradeMessage', $preUpgradeMessage);
6a488035
TO
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 */
00be9182 127 public function runBegin() {
6a488035
TO
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();
6a488035 136
3a111e86 137 $postUpgradeMessage = '<span class="bold">' . ts('Congratulations! Your upgrade was successful!') . '</span>';
6a488035
TO
138
139 // lets drop all the triggers here
140 CRM_Core_DAO::dropTriggers();
141
c76e6d43
TO
142 $this->set('isUpgradePending', TRUE);
143
6a488035
TO
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',
481a74f4 154 'onEndUrl' => CRM_Utils_System::url('civicrm/upgrade', 'action=finish', FALSE, NULL, FALSE),
6a488035 155 'buttons' => array('retry' => $config->debug, 'skip' => $config->debug),
353ffa53 156 ));
6a488035
TO
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 */
00be9182 164 public function runFinish() {
6a488035
TO
165 $upgrade = new CRM_Upgrade_Form();
166 $template = CRM_Core_Smarty::singleton();
167
c76e6d43
TO
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')) {
43992531
TO
171 // TODO: Use structured message store
172 $postUpgradeMessage = file_get_contents($this->get('postUpgradeMessageFile'));
6a488035 173
43992531
TO
174 // This destroys $session, so do it after get('postUpgradeMessageFile')
175 CRM_Upgrade_Form::doFinish();
0db6c3e1
TO
176 }
177 else {
43992531
TO
178 $postUpgradeMessage = ''; // Session was destroyed! Can't recover messages.
179 }
6a488035
TO
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);
202407d7 189 $template->assign('sid', CRM_Utils_System::getSiteID());
6a488035
TO
190
191 // Render page header
9dc21423 192 if (!defined('CIVICRM_UF_HEAD') && $region = CRM_Core_Region::instance('html-header', FALSE)) {
6a488035
TO
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 }
96025800 199
6a488035 200}