7309db612c3a338d018f6bbf5b0cc03b5e305fe6
3 // AUTO-GENERATED FILE -- Civix may overwrite any changes made to this file
6 * Base class which provides helpers to execute upgrade logic
8 class CRM_Wci_Upgrader_Base
{
11 * @var varies, subclass of htis
16 * @var CRM_Queue_TaskContext
21 * @var string, eg 'com.example.myextension'
23 protected $extensionName;
26 * @var string, full path to the extension's source tree
28 protected $extensionDir;
31 * @var array(revisionNumber) sorted numerically
36 * Obtain a refernece to the active upgrade handler
38 static public function instance() {
39 if (! self
::$instance) {
40 // FIXME auto-generate
41 self
::$instance = new CRM_Wci_Upgrader(
43 realpath(__DIR__
.'/../../../')
46 return self
::$instance;
50 * Adapter that lets you add normal (non-static) member functions to the queue.
52 * Note: Each upgrader instance should only be associated with one
53 * task-context; otherwise, this will be non-reentrant.
56 * CRM_Wci_Upgrader_Base::_queueAdapter($ctx, 'methodName', 'arg1', 'arg2');
59 static public function _queueAdapter() {
60 $instance = self
::instance();
61 $args = func_get_args();
62 $instance->ctx
= array_shift($args);
63 $instance->queue
= $instance->ctx
->queue
;
64 $method = array_shift($args);
65 return call_user_func_array(array($instance, $method), $args);
68 public function __construct($extensionName, $extensionDir) {
69 $this->extensionName
= $extensionName;
70 $this->extensionDir
= $extensionDir;
73 // ******** Task helpers ********
76 * Run a CustomData file
78 * @param string $relativePath the CustomData XML file path (relative to this extension's dir)
81 public function executeCustomDataFile($relativePath) {
82 $xml_file = $this->extensionDir
. '/' . $relativePath;
83 return $this->executeCustomDataFileByAbsPath($xml_file);
87 * Run a CustomData file
89 * @param string $xml_file the CustomData XML file path (absolute path)
92 protected static function executeCustomDataFileByAbsPath($xml_file) {
93 require_once 'CRM/Utils/Migrate/Import.php';
94 $import = new CRM_Utils_Migrate_Import();
95 $import->run($xml_file);
102 * @param string $relativePath the SQL file path (relative to this extension's dir)
105 public function executeSqlFile($relativePath) {
106 CRM_Utils_File
::sourceSQLFile(
108 $this->extensionDir
. '/' . $relativePath
116 * This is just a wrapper for CRM_Core_DAO::executeSql, but it
117 * provides syntatic sugar for queueing several tasks that
118 * run different queries
120 public function executeSql($query, $params = array()) {
121 // FIXME verify that we raise an exception on error
122 CRM_Core_DAO
::executeSql($query, $params);
127 * Syntatic sugar for enqueuing a task which calls a function
128 * in this class. The task is weighted so that it is processed
129 * as part of the currently-pending revision.
131 * After passing the $funcName, you can also pass parameters that will go to
132 * the function. Note that all params must be serializable.
134 public function addTask($title) {
135 $args = func_get_args();
136 $title = array_shift($args);
137 $task = new CRM_Queue_Task(
138 array(get_class($this), '_queueAdapter'),
142 return $this->queue
->createItem($task, array('weight' => -1));
145 // ******** Revision-tracking helpers ********
148 * Determine if there are any pending revisions
152 public function hasPendingRevisions() {
153 $revisions = $this->getRevisions();
154 $currentRevision = $this->getCurrentRevision();
156 if (empty($revisions)) {
159 if (empty($currentRevision)) {
163 return ($currentRevision < max($revisions));
167 * Add any pending revisions to the queue
169 public function enqueuePendingRevisions(CRM_Queue_Queue
$queue) {
170 $this->queue
= $queue;
172 $currentRevision = $this->getCurrentRevision();
173 foreach ($this->getRevisions() as $revision) {
174 if ($revision > $currentRevision) {
175 $title = ts('Upgrade %1 to revision %2', array(
176 1 => $this->extensionName
,
180 // note: don't use addTask() because it sets weight=-1
182 $task = new CRM_Queue_Task(
183 array(get_class($this), '_queueAdapter'),
184 array('upgrade_' . $revision),
187 $this->queue
->createItem($task);
189 $task = new CRM_Queue_Task(
190 array(get_class($this), '_queueAdapter'),
191 array('setCurrentRevision', $revision),
194 $this->queue
->createItem($task);
200 * Get a list of revisions
202 * @return array(revisionNumbers) sorted numerically
204 public function getRevisions() {
205 if (! is_array($this->revisions
)) {
206 $this->revisions
= array();
208 $clazz = new ReflectionClass(get_class($this));
209 $methods = $clazz->getMethods();
210 foreach ($methods as $method) {
211 if (preg_match('/^upgrade_(.*)/', $method->name
, $matches)) {
212 $this->revisions
[] = $matches[1];
215 sort($this->revisions
, SORT_NUMERIC
);
218 return $this->revisions
;
221 public function getCurrentRevision() {
222 // return CRM_Core_BAO_Extension::getSchemaVersion($this->extensionName);
223 $key = $this->extensionName
. ':version';
224 return CRM_Core_BAO_Setting
::getItem('Extension', $key);
227 public function setCurrentRevision($revision) {
228 // We call this during hook_civicrm_install, but the underlying SQL
229 // UPDATE fails because the extension record hasn't been INSERTed yet.
230 // Instead, track revisions in our own namespace.
231 // CRM_Core_BAO_Extension::setSchemaVersion($this->extensionName, $revision);
233 $key = $this->extensionName
. ':version';
234 CRM_Core_BAO_Setting
::setItem($revision, 'Extension', $key);
238 // ******** Hook delegates ********
240 public function onInstall() {
241 $files = glob($this->extensionDir
. '/sql/*_install.sql');
242 if (is_array($files)) {
243 foreach ($files as $file) {
244 CRM_Utils_File
::sourceSQLFile(CIVICRM_DSN
, $file);
247 $files = glob($this->extensionDir
. '/xml/*_install.xml');
248 if (is_array($files)) {
249 foreach ($files as $file) {
250 $this->executeCustomDataFileByAbsPath($file);
253 if (is_callable(array($this, 'install'))) {
256 $revisions = $this->getRevisions();
257 if (!empty($revisions)) {
258 $this->setCurrentRevision(max($revisions));
262 public function onUninstall() {
263 if (is_callable(array($this, 'uninstall'))) {
266 $files = glob($this->extensionDir
. '/sql/*_uninstall.sql');
267 if (is_array($files)) {
268 foreach ($files as $file) {
269 CRM_Utils_File
::sourceSQLFile(CIVICRM_DSN
, $file);
272 $this->setCurrentRevision(NULL);
275 public function onEnable() {
276 // stub for possible future use
277 if (is_callable(array($this, 'enable'))) {
282 public function onDisable() {
283 // stub for possible future use
284 if (is_callable(array($this, 'disable'))) {
289 public function onUpgrade($op, CRM_Queue_Queue
$queue = NULL) {
292 return array($this->hasPendingRevisions());
294 return $this->enqueuePendingRevisions($queue);