3 // AUTO-GENERATED FILE -- Civix may overwrite any changes made to this file
4 use CRM_AfformGui_ExtensionUtil
as E
;
7 * Base class which provides helpers to execute upgrade logic
9 class CRM_AfformGui_Upgrader_Base
{
12 * @var CRM_AfformGui_Upgrader_Base
14 public static $instance;
17 * @var CRM_Queue_TaskContext
23 * eg 'com.example.myextension'
25 protected $extensionName;
29 * full path to the extension's source tree
31 protected $extensionDir;
34 * @var revisionNumber[]
41 * Flag to clean up extension revision data in civicrm_setting
43 private $revisionStorageIsDeprecated = FALSE;
46 * Obtain a reference to the active upgrade handler.
48 public static function instance() {
49 if (!self
::$instance) {
50 // FIXME auto-generate
51 self
::$instance = new CRM_AfformGui_Upgrader(
52 'org.civicrm.afform-gui',
53 realpath(__DIR__
. '/../../../')
56 return self
::$instance;
60 * Adapter that lets you add normal (non-static) member functions to the queue.
62 * Note: Each upgrader instance should only be associated with one
63 * task-context; otherwise, this will be non-reentrant.
66 * CRM_AfformGui_Upgrader_Base::_queueAdapter($ctx, 'methodName', 'arg1', 'arg2');
69 public static function _queueAdapter() {
70 $instance = self
::instance();
71 $args = func_get_args();
72 $instance->ctx
= array_shift($args);
73 $instance->queue
= $instance->ctx
->queue
;
74 $method = array_shift($args);
75 return call_user_func_array([$instance, $method], $args);
79 * CRM_AfformGui_Upgrader_Base constructor.
81 * @param $extensionName
82 * @param $extensionDir
84 public function __construct($extensionName, $extensionDir) {
85 $this->extensionName
= $extensionName;
86 $this->extensionDir
= $extensionDir;
89 // ******** Task helpers ********
92 * Run a CustomData file.
94 * @param string $relativePath
95 * the CustomData XML file path (relative to this extension's dir)
98 public function executeCustomDataFile($relativePath) {
99 $xml_file = $this->extensionDir
. '/' . $relativePath;
100 return $this->executeCustomDataFileByAbsPath($xml_file);
104 * Run a CustomData file
106 * @param string $xml_file
107 * the CustomData XML file path (absolute path)
111 protected function executeCustomDataFileByAbsPath($xml_file) {
112 $import = new CRM_Utils_Migrate_Import();
113 $import->run($xml_file);
120 * @param string $relativePath
121 * the SQL file path (relative to this extension's dir)
125 public function executeSqlFile($relativePath) {
126 CRM_Utils_File
::sourceSQLFile(
128 $this->extensionDir
. DIRECTORY_SEPARATOR
. $relativePath
134 * Run the sql commands in the specified file.
136 * @param string $tplFile
137 * The SQL file path (relative to this extension's dir).
138 * Ex: "sql/mydata.mysql.tpl".
141 * @throws \CRM_Core_Exception
143 public function executeSqlTemplate($tplFile) {
144 // Assign multilingual variable to Smarty.
145 $upgrade = new CRM_Upgrade_Form();
147 $tplFile = CRM_Utils_File
::isAbsolute($tplFile) ?
$tplFile : $this->extensionDir
. DIRECTORY_SEPARATOR
. $tplFile;
148 $smarty = CRM_Core_Smarty
::singleton();
149 $smarty->assign('domainID', CRM_Core_Config
::domainID());
150 CRM_Utils_File
::sourceSQLFile(
151 CIVICRM_DSN
, $smarty->fetch($tplFile), NULL, TRUE
159 * This is just a wrapper for CRM_Core_DAO::executeSql, but it
160 * provides syntactic sugar for queueing several tasks that
161 * run different queries
165 public function executeSql($query, $params = []) {
166 // FIXME verify that we raise an exception on error
167 CRM_Core_DAO
::executeQuery($query, $params);
172 * Syntactic sugar for enqueuing a task which calls a function in this class.
174 * The task is weighted so that it is processed
175 * as part of the currently-pending revision.
177 * After passing the $funcName, you can also pass parameters that will go to
178 * the function. Note that all params must be serializable.
180 public function addTask($title) {
181 $args = func_get_args();
182 $title = array_shift($args);
183 $task = new CRM_Queue_Task(
184 [get_class($this), '_queueAdapter'],
188 return $this->queue
->createItem($task, ['weight' => -1]);
191 // ******** Revision-tracking helpers ********
194 * Determine if there are any pending revisions.
198 public function hasPendingRevisions() {
199 $revisions = $this->getRevisions();
200 $currentRevision = $this->getCurrentRevision();
202 if (empty($revisions)) {
205 if (empty($currentRevision)) {
209 return ($currentRevision < max($revisions));
213 * Add any pending revisions to the queue.
215 * @param CRM_Queue_Queue $queue
217 public function enqueuePendingRevisions(CRM_Queue_Queue
$queue) {
218 $this->queue
= $queue;
220 $currentRevision = $this->getCurrentRevision();
221 foreach ($this->getRevisions() as $revision) {
222 if ($revision > $currentRevision) {
223 $title = E
::ts('Upgrade %1 to revision %2', [
224 1 => $this->extensionName
,
228 // note: don't use addTask() because it sets weight=-1
230 $task = new CRM_Queue_Task(
231 [get_class($this), '_queueAdapter'],
232 ['upgrade_' . $revision],
235 $this->queue
->createItem($task);
237 $task = new CRM_Queue_Task(
238 [get_class($this), '_queueAdapter'],
239 ['setCurrentRevision', $revision],
242 $this->queue
->createItem($task);
248 * Get a list of revisions.
251 * revisionNumbers sorted numerically
253 public function getRevisions() {
254 if (!is_array($this->revisions
)) {
255 $this->revisions
= [];
257 $clazz = new ReflectionClass(get_class($this));
258 $methods = $clazz->getMethods();
259 foreach ($methods as $method) {
260 if (preg_match('/^upgrade_(.*)/', $method->name
, $matches)) {
261 $this->revisions
[] = $matches[1];
264 sort($this->revisions
, SORT_NUMERIC
);
267 return $this->revisions
;
270 public function getCurrentRevision() {
271 $revision = CRM_Core_BAO_Extension
::getSchemaVersion($this->extensionName
);
273 $revision = $this->getCurrentRevisionDeprecated();
278 private function getCurrentRevisionDeprecated() {
279 $key = $this->extensionName
. ':version';
280 if ($revision = \Civi
::settings()->get($key)) {
281 $this->revisionStorageIsDeprecated
= TRUE;
286 public function setCurrentRevision($revision) {
287 CRM_Core_BAO_Extension
::setSchemaVersion($this->extensionName
, $revision);
288 // clean up legacy schema version store (CRM-19252)
289 $this->deleteDeprecatedRevision();
293 private function deleteDeprecatedRevision() {
294 if ($this->revisionStorageIsDeprecated
) {
295 $setting = new CRM_Core_BAO_Setting();
296 $setting->name
= $this->extensionName
. ':version';
298 CRM_Core_Error
::debug_log_message("Migrated extension schema revision ID for {$this->extensionName} from civicrm_setting (deprecated) to civicrm_extension.\n");
302 // ******** Hook delegates ********
305 * @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_install
307 public function onInstall() {
308 $files = glob($this->extensionDir
. '/sql/*_install.sql');
309 if (is_array($files)) {
310 foreach ($files as $file) {
311 CRM_Utils_File
::sourceSQLFile(CIVICRM_DSN
, $file);
314 $files = glob($this->extensionDir
. '/sql/*_install.mysql.tpl');
315 if (is_array($files)) {
316 foreach ($files as $file) {
317 $this->executeSqlTemplate($file);
320 $files = glob($this->extensionDir
. '/xml/*_install.xml');
321 if (is_array($files)) {
322 foreach ($files as $file) {
323 $this->executeCustomDataFileByAbsPath($file);
326 if (is_callable([$this, 'install'])) {
332 * @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_postInstall
334 public function onPostInstall() {
335 $revisions = $this->getRevisions();
336 if (!empty($revisions)) {
337 $this->setCurrentRevision(max($revisions));
339 if (is_callable([$this, 'postInstall'])) {
340 $this->postInstall();
345 * @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_uninstall
347 public function onUninstall() {
348 $files = glob($this->extensionDir
. '/sql/*_uninstall.mysql.tpl');
349 if (is_array($files)) {
350 foreach ($files as $file) {
351 $this->executeSqlTemplate($file);
354 if (is_callable([$this, 'uninstall'])) {
357 $files = glob($this->extensionDir
. '/sql/*_uninstall.sql');
358 if (is_array($files)) {
359 foreach ($files as $file) {
360 CRM_Utils_File
::sourceSQLFile(CIVICRM_DSN
, $file);
366 * @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_enable
368 public function onEnable() {
369 // stub for possible future use
370 if (is_callable([$this, 'enable'])) {
376 * @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_disable
378 public function onDisable() {
379 // stub for possible future use
380 if (is_callable([$this, 'disable'])) {
385 public function onUpgrade($op, CRM_Queue_Queue
$queue = NULL) {
388 return [$this->hasPendingRevisions()];
391 return $this->enqueuePendingRevisions($queue);