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. |
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. |
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. |
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 +--------------------------------------------------------------------+
25 // AUTO-GENERATED FILE -- Civix may overwrite any changes made to this file
28 * Base class which provides helpers to execute upgrade logic
30 class CRM_Wci_Upgrader_Base
{
33 * @var varies, subclass of htis
38 * @var CRM_Queue_TaskContext
43 * @var string, eg 'com.example.myextension'
45 protected $extensionName;
48 * @var string, full path to the extension's source tree
50 protected $extensionDir;
53 * @var array(revisionNumber) sorted numerically
58 * Obtain a refernece to the active upgrade handler
60 static public function instance() {
61 if (! self
::$instance) {
62 // FIXME auto-generate
63 self
::$instance = new CRM_Wci_Upgrader(
65 realpath(__DIR__
.'/../../../')
68 return self
::$instance;
72 * Adapter that lets you add normal (non-static) member functions to the queue.
74 * Note: Each upgrader instance should only be associated with one
75 * task-context; otherwise, this will be non-reentrant.
78 * CRM_Wci_Upgrader_Base::_queueAdapter($ctx, 'methodName', 'arg1', 'arg2');
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);
90 public function __construct($extensionName, $extensionDir) {
91 $this->extensionName
= $extensionName;
92 $this->extensionDir
= $extensionDir;
95 // ******** Task helpers ********
98 * Run a CustomData file
100 * @param string $relativePath the CustomData XML file path (relative to this extension's dir)
103 public function executeCustomDataFile($relativePath) {
104 $xml_file = $this->extensionDir
. '/' . $relativePath;
105 return $this->executeCustomDataFileByAbsPath($xml_file);
109 * Run a CustomData file
111 * @param string $xml_file the CustomData XML file path (absolute path)
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);
124 * @param string $relativePath the SQL file path (relative to this extension's dir)
127 public function executeSqlFile($relativePath) {
128 CRM_Utils_File
::sourceSQLFile(
130 $this->extensionDir
. '/' . $relativePath
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
142 public function executeSql($query, $params = array()) {
143 // FIXME verify that we raise an exception on error
144 CRM_Core_DAO
::executeSql($query, $params);
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.
153 * After passing the $funcName, you can also pass parameters that will go to
154 * the function. Note that all params must be serializable.
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'),
164 return $this->queue
->createItem($task, array('weight' => -1));
167 // ******** Revision-tracking helpers ********
170 * Determine if there are any pending revisions
174 public function hasPendingRevisions() {
175 $revisions = $this->getRevisions();
176 $currentRevision = $this->getCurrentRevision();
178 if (empty($revisions)) {
181 if (empty($currentRevision)) {
185 return ($currentRevision < max($revisions));
189 * Add any pending revisions to the queue
191 public function enqueuePendingRevisions(CRM_Queue_Queue
$queue) {
192 $this->queue
= $queue;
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
,
202 // note: don't use addTask() because it sets weight=-1
204 $task = new CRM_Queue_Task(
205 array(get_class($this), '_queueAdapter'),
206 array('upgrade_' . $revision),
209 $this->queue
->createItem($task);
211 $task = new CRM_Queue_Task(
212 array(get_class($this), '_queueAdapter'),
213 array('setCurrentRevision', $revision),
216 $this->queue
->createItem($task);
222 * Get a list of revisions
224 * @return array(revisionNumbers) sorted numerically
226 public function getRevisions() {
227 if (! is_array($this->revisions
)) {
228 $this->revisions
= array();
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];
237 sort($this->revisions
, SORT_NUMERIC
);
240 return $this->revisions
;
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);
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);
255 $key = $this->extensionName
. ':version';
256 CRM_Core_BAO_Setting
::setItem($revision, 'Extension', $key);
260 // ******** Hook delegates ********
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);
269 $files = glob($this->extensionDir
. '/xml/*_install.xml');
270 if (is_array($files)) {
271 foreach ($files as $file) {
272 $this->executeCustomDataFileByAbsPath($file);
275 if (is_callable(array($this, 'install'))) {
278 $revisions = $this->getRevisions();
279 if (!empty($revisions)) {
280 $this->setCurrentRevision(max($revisions));
284 public function onUninstall() {
285 if (is_callable(array($this, 'uninstall'))) {
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);
294 $this->setCurrentRevision(NULL);
297 public function onEnable() {
298 // stub for possible future use
299 if (is_callable(array($this, 'enable'))) {
304 public function onDisable() {
305 // stub for possible future use
306 if (is_callable(array($this, 'disable'))) {
311 public function onUpgrade($op, CRM_Queue_Queue
$queue = NULL) {
314 return array($this->hasPendingRevisions());
316 return $this->enqueuePendingRevisions($queue);