31453 31498 code cleanup
[com.zyxware.civiwci.git] / CRM / Wci / Upgrader / Base.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM Widget Creation Interface (WCI) Version 1.0 |
5 +--------------------------------------------------------------------+
6 | Copyright Zyxware Technologies (c) 2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM WCI. |
9 | |
10 | CiviCRM WCI is free software; you can copy, modify, and distribute |
11 | it under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007. |
13 | |
14 | CiviCRM WCI is distributed in the hope that it will be useful, |
15 | but 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 along with this program; if not, contact Zyxware |
21 | Technologies at info[AT]zyxware[DOT]com. |
22 +--------------------------------------------------------------------+
23 */
24
25 // AUTO-GENERATED FILE -- Civix may overwrite any changes made to this file
26
27 /**
28 * Base class which provides helpers to execute upgrade logic
29 */
30 class CRM_Wci_Upgrader_Base {
31
32 /**
33 * @var varies, subclass of htis
34 */
35 static $instance;
36
37 /**
38 * @var CRM_Queue_TaskContext
39 */
40 protected $ctx;
41
42 /**
43 * @var string, eg 'com.example.myextension'
44 */
45 protected $extensionName;
46
47 /**
48 * @var string, full path to the extension's source tree
49 */
50 protected $extensionDir;
51
52 /**
53 * @var array(revisionNumber) sorted numerically
54 */
55 private $revisions;
56
57 /**
58 * Obtain a refernece to the active upgrade handler
59 */
60 static public function instance() {
61 if (! self::$instance) {
62 // FIXME auto-generate
63 self::$instance = new CRM_Wci_Upgrader(
64 'org.civicrm.wci',
65 realpath(__DIR__ .'/../../../')
66 );
67 }
68 return self::$instance;
69 }
70
71 /**
72 * Adapter that lets you add normal (non-static) member functions to the queue.
73 *
74 * Note: Each upgrader instance should only be associated with one
75 * task-context; otherwise, this will be non-reentrant.
76 *
77 * @code
78 * CRM_Wci_Upgrader_Base::_queueAdapter($ctx, 'methodName', 'arg1', 'arg2');
79 * @endcode
80 */
81 static public function _queueAdapter() {
82 $instance = self::instance();
83 $args = func_get_args();
84 $instance->ctx = array_shift($args);
85 $instance->queue = $instance->ctx->queue;
86 $method = array_shift($args);
87 return call_user_func_array(array($instance, $method), $args);
88 }
89
90 public function __construct($extensionName, $extensionDir) {
91 $this->extensionName = $extensionName;
92 $this->extensionDir = $extensionDir;
93 }
94
95 // ******** Task helpers ********
96
97 /**
98 * Run a CustomData file
99 *
100 * @param string $relativePath the CustomData XML file path (relative to this extension's dir)
101 * @return bool
102 */
103 public function executeCustomDataFile($relativePath) {
104 $xml_file = $this->extensionDir . '/' . $relativePath;
105 return $this->executeCustomDataFileByAbsPath($xml_file);
106 }
107
108 /**
109 * Run a CustomData file
110 *
111 * @param string $xml_file the CustomData XML file path (absolute path)
112 * @return bool
113 */
114 protected static function executeCustomDataFileByAbsPath($xml_file) {
115 require_once 'CRM/Utils/Migrate/Import.php';
116 $import = new CRM_Utils_Migrate_Import();
117 $import->run($xml_file);
118 return TRUE;
119 }
120
121 /**
122 * Run a SQL file
123 *
124 * @param string $relativePath the SQL file path (relative to this extension's dir)
125 * @return bool
126 */
127 public function executeSqlFile($relativePath) {
128 CRM_Utils_File::sourceSQLFile(
129 CIVICRM_DSN,
130 $this->extensionDir . '/' . $relativePath
131 );
132 return TRUE;
133 }
134
135 /**
136 * Run one SQL query
137 *
138 * This is just a wrapper for CRM_Core_DAO::executeSql, but it
139 * provides syntatic sugar for queueing several tasks that
140 * run different queries
141 */
142 public function executeSql($query, $params = array()) {
143 // FIXME verify that we raise an exception on error
144 CRM_Core_DAO::executeSql($query, $params);
145 return TRUE;
146 }
147
148 /**
149 * Syntatic sugar for enqueuing a task which calls a function
150 * in this class. The task is weighted so that it is processed
151 * as part of the currently-pending revision.
152 *
153 * After passing the $funcName, you can also pass parameters that will go to
154 * the function. Note that all params must be serializable.
155 */
156 public function addTask($title) {
157 $args = func_get_args();
158 $title = array_shift($args);
159 $task = new CRM_Queue_Task(
160 array(get_class($this), '_queueAdapter'),
161 $args,
162 $title
163 );
164 return $this->queue->createItem($task, array('weight' => -1));
165 }
166
167 // ******** Revision-tracking helpers ********
168
169 /**
170 * Determine if there are any pending revisions
171 *
172 * @return bool
173 */
174 public function hasPendingRevisions() {
175 $revisions = $this->getRevisions();
176 $currentRevision = $this->getCurrentRevision();
177
178 if (empty($revisions)) {
179 return FALSE;
180 }
181 if (empty($currentRevision)) {
182 return TRUE;
183 }
184
185 return ($currentRevision < max($revisions));
186 }
187
188 /**
189 * Add any pending revisions to the queue
190 */
191 public function enqueuePendingRevisions(CRM_Queue_Queue $queue) {
192 $this->queue = $queue;
193
194 $currentRevision = $this->getCurrentRevision();
195 foreach ($this->getRevisions() as $revision) {
196 if ($revision > $currentRevision) {
197 $title = ts('Upgrade %1 to revision %2', array(
198 1 => $this->extensionName,
199 2 => $revision,
200 ));
201
202 // note: don't use addTask() because it sets weight=-1
203
204 $task = new CRM_Queue_Task(
205 array(get_class($this), '_queueAdapter'),
206 array('upgrade_' . $revision),
207 $title
208 );
209 $this->queue->createItem($task);
210
211 $task = new CRM_Queue_Task(
212 array(get_class($this), '_queueAdapter'),
213 array('setCurrentRevision', $revision),
214 $title
215 );
216 $this->queue->createItem($task);
217 }
218 }
219 }
220
221 /**
222 * Get a list of revisions
223 *
224 * @return array(revisionNumbers) sorted numerically
225 */
226 public function getRevisions() {
227 if (! is_array($this->revisions)) {
228 $this->revisions = array();
229
230 $clazz = new ReflectionClass(get_class($this));
231 $methods = $clazz->getMethods();
232 foreach ($methods as $method) {
233 if (preg_match('/^upgrade_(.*)/', $method->name, $matches)) {
234 $this->revisions[] = $matches[1];
235 }
236 }
237 sort($this->revisions, SORT_NUMERIC);
238 }
239
240 return $this->revisions;
241 }
242
243 public function getCurrentRevision() {
244 // return CRM_Core_BAO_Extension::getSchemaVersion($this->extensionName);
245 $key = $this->extensionName . ':version';
246 return CRM_Core_BAO_Setting::getItem('Extension', $key);
247 }
248
249 public function setCurrentRevision($revision) {
250 // We call this during hook_civicrm_install, but the underlying SQL
251 // UPDATE fails because the extension record hasn't been INSERTed yet.
252 // Instead, track revisions in our own namespace.
253 // CRM_Core_BAO_Extension::setSchemaVersion($this->extensionName, $revision);
254
255 $key = $this->extensionName . ':version';
256 CRM_Core_BAO_Setting::setItem($revision, 'Extension', $key);
257 return TRUE;
258 }
259
260 // ******** Hook delegates ********
261
262 public function onInstall() {
263 $files = glob($this->extensionDir . '/sql/*_install.sql');
264 if (is_array($files)) {
265 foreach ($files as $file) {
266 CRM_Utils_File::sourceSQLFile(CIVICRM_DSN, $file);
267 }
268 }
269 $files = glob($this->extensionDir . '/xml/*_install.xml');
270 if (is_array($files)) {
271 foreach ($files as $file) {
272 $this->executeCustomDataFileByAbsPath($file);
273 }
274 }
275 if (is_callable(array($this, 'install'))) {
276 $this->install();
277 }
278 $revisions = $this->getRevisions();
279 if (!empty($revisions)) {
280 $this->setCurrentRevision(max($revisions));
281 }
282 }
283
284 public function onUninstall() {
285 if (is_callable(array($this, 'uninstall'))) {
286 $this->uninstall();
287 }
288 $files = glob($this->extensionDir . '/sql/*_uninstall.sql');
289 if (is_array($files)) {
290 foreach ($files as $file) {
291 CRM_Utils_File::sourceSQLFile(CIVICRM_DSN, $file);
292 }
293 }
294 $this->setCurrentRevision(NULL);
295 }
296
297 public function onEnable() {
298 // stub for possible future use
299 if (is_callable(array($this, 'enable'))) {
300 $this->enable();
301 }
302 }
303
304 public function onDisable() {
305 // stub for possible future use
306 if (is_callable(array($this, 'disable'))) {
307 $this->disable();
308 }
309 }
310
311 public function onUpgrade($op, CRM_Queue_Queue $queue = NULL) {
312 switch($op) {
313 case 'check':
314 return array($this->hasPendingRevisions());
315 case 'enqueue':
316 return $this->enqueuePendingRevisions($queue);
317 default:
318 }
319 }
320 }