Merge pull request #19358 from colemanw/dashboardStatus
[civicrm-core.git] / ext / afform / gui / CRM / AfformGui / Upgrader / Base.php
1 <?php
2
3 // AUTO-GENERATED FILE -- Civix may overwrite any changes made to this file
4 use CRM_AfformGui_ExtensionUtil as E;
5
6 /**
7 * Base class which provides helpers to execute upgrade logic
8 */
9 class CRM_AfformGui_Upgrader_Base {
10
11 /**
12 * @var CRM_AfformGui_Upgrader_Base
13 */
14 public static $instance;
15
16 /**
17 * @var CRM_Queue_TaskContext
18 */
19 protected $ctx;
20
21 /**
22 * @var string
23 * eg 'com.example.myextension'
24 */
25 protected $extensionName;
26
27 /**
28 * @var string
29 * full path to the extension's source tree
30 */
31 protected $extensionDir;
32
33 /**
34 * @var revisionNumber[]
35 * sorted numerically
36 */
37 private $revisions;
38
39 /**
40 * @var bool
41 * Flag to clean up extension revision data in civicrm_setting
42 */
43 private $revisionStorageIsDeprecated = FALSE;
44
45 /**
46 * Obtain a reference to the active upgrade handler.
47 */
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__ . '/../../../')
54 );
55 }
56 return self::$instance;
57 }
58
59 /**
60 * Adapter that lets you add normal (non-static) member functions to the queue.
61 *
62 * Note: Each upgrader instance should only be associated with one
63 * task-context; otherwise, this will be non-reentrant.
64 *
65 * ```
66 * CRM_AfformGui_Upgrader_Base::_queueAdapter($ctx, 'methodName', 'arg1', 'arg2');
67 * ```
68 */
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);
76 }
77
78 /**
79 * CRM_AfformGui_Upgrader_Base constructor.
80 *
81 * @param $extensionName
82 * @param $extensionDir
83 */
84 public function __construct($extensionName, $extensionDir) {
85 $this->extensionName = $extensionName;
86 $this->extensionDir = $extensionDir;
87 }
88
89 // ******** Task helpers ********
90
91 /**
92 * Run a CustomData file.
93 *
94 * @param string $relativePath
95 * the CustomData XML file path (relative to this extension's dir)
96 * @return bool
97 */
98 public function executeCustomDataFile($relativePath) {
99 $xml_file = $this->extensionDir . '/' . $relativePath;
100 return $this->executeCustomDataFileByAbsPath($xml_file);
101 }
102
103 /**
104 * Run a CustomData file
105 *
106 * @param string $xml_file
107 * the CustomData XML file path (absolute path)
108 *
109 * @return bool
110 */
111 protected function executeCustomDataFileByAbsPath($xml_file) {
112 $import = new CRM_Utils_Migrate_Import();
113 $import->run($xml_file);
114 return TRUE;
115 }
116
117 /**
118 * Run a SQL file.
119 *
120 * @param string $relativePath
121 * the SQL file path (relative to this extension's dir)
122 *
123 * @return bool
124 */
125 public function executeSqlFile($relativePath) {
126 CRM_Utils_File::sourceSQLFile(
127 CIVICRM_DSN,
128 $this->extensionDir . DIRECTORY_SEPARATOR . $relativePath
129 );
130 return TRUE;
131 }
132
133 /**
134 * Run the sql commands in the specified file.
135 *
136 * @param string $tplFile
137 * The SQL file path (relative to this extension's dir).
138 * Ex: "sql/mydata.mysql.tpl".
139 *
140 * @return bool
141 * @throws \CRM_Core_Exception
142 */
143 public function executeSqlTemplate($tplFile) {
144 // Assign multilingual variable to Smarty.
145 $upgrade = new CRM_Upgrade_Form();
146
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
152 );
153 return TRUE;
154 }
155
156 /**
157 * Run one SQL query.
158 *
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
162 *
163 * @return bool
164 */
165 public function executeSql($query, $params = []) {
166 // FIXME verify that we raise an exception on error
167 CRM_Core_DAO::executeQuery($query, $params);
168 return TRUE;
169 }
170
171 /**
172 * Syntactic sugar for enqueuing a task which calls a function in this class.
173 *
174 * The task is weighted so that it is processed
175 * as part of the currently-pending revision.
176 *
177 * After passing the $funcName, you can also pass parameters that will go to
178 * the function. Note that all params must be serializable.
179 */
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'],
185 $args,
186 $title
187 );
188 return $this->queue->createItem($task, ['weight' => -1]);
189 }
190
191 // ******** Revision-tracking helpers ********
192
193 /**
194 * Determine if there are any pending revisions.
195 *
196 * @return bool
197 */
198 public function hasPendingRevisions() {
199 $revisions = $this->getRevisions();
200 $currentRevision = $this->getCurrentRevision();
201
202 if (empty($revisions)) {
203 return FALSE;
204 }
205 if (empty($currentRevision)) {
206 return TRUE;
207 }
208
209 return ($currentRevision < max($revisions));
210 }
211
212 /**
213 * Add any pending revisions to the queue.
214 *
215 * @param CRM_Queue_Queue $queue
216 */
217 public function enqueuePendingRevisions(CRM_Queue_Queue $queue) {
218 $this->queue = $queue;
219
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,
225 2 => $revision,
226 ]);
227
228 // note: don't use addTask() because it sets weight=-1
229
230 $task = new CRM_Queue_Task(
231 [get_class($this), '_queueAdapter'],
232 ['upgrade_' . $revision],
233 $title
234 );
235 $this->queue->createItem($task);
236
237 $task = new CRM_Queue_Task(
238 [get_class($this), '_queueAdapter'],
239 ['setCurrentRevision', $revision],
240 $title
241 );
242 $this->queue->createItem($task);
243 }
244 }
245 }
246
247 /**
248 * Get a list of revisions.
249 *
250 * @return array
251 * revisionNumbers sorted numerically
252 */
253 public function getRevisions() {
254 if (!is_array($this->revisions)) {
255 $this->revisions = [];
256
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];
262 }
263 }
264 sort($this->revisions, SORT_NUMERIC);
265 }
266
267 return $this->revisions;
268 }
269
270 public function getCurrentRevision() {
271 $revision = CRM_Core_BAO_Extension::getSchemaVersion($this->extensionName);
272 if (!$revision) {
273 $revision = $this->getCurrentRevisionDeprecated();
274 }
275 return $revision;
276 }
277
278 private function getCurrentRevisionDeprecated() {
279 $key = $this->extensionName . ':version';
280 if ($revision = \Civi::settings()->get($key)) {
281 $this->revisionStorageIsDeprecated = TRUE;
282 }
283 return $revision;
284 }
285
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();
290 return TRUE;
291 }
292
293 private function deleteDeprecatedRevision() {
294 if ($this->revisionStorageIsDeprecated) {
295 $setting = new CRM_Core_BAO_Setting();
296 $setting->name = $this->extensionName . ':version';
297 $setting->delete();
298 CRM_Core_Error::debug_log_message("Migrated extension schema revision ID for {$this->extensionName} from civicrm_setting (deprecated) to civicrm_extension.\n");
299 }
300 }
301
302 // ******** Hook delegates ********
303
304 /**
305 * @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_install
306 */
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);
312 }
313 }
314 $files = glob($this->extensionDir . '/sql/*_install.mysql.tpl');
315 if (is_array($files)) {
316 foreach ($files as $file) {
317 $this->executeSqlTemplate($file);
318 }
319 }
320 $files = glob($this->extensionDir . '/xml/*_install.xml');
321 if (is_array($files)) {
322 foreach ($files as $file) {
323 $this->executeCustomDataFileByAbsPath($file);
324 }
325 }
326 if (is_callable([$this, 'install'])) {
327 $this->install();
328 }
329 }
330
331 /**
332 * @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_postInstall
333 */
334 public function onPostInstall() {
335 $revisions = $this->getRevisions();
336 if (!empty($revisions)) {
337 $this->setCurrentRevision(max($revisions));
338 }
339 if (is_callable([$this, 'postInstall'])) {
340 $this->postInstall();
341 }
342 }
343
344 /**
345 * @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_uninstall
346 */
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);
352 }
353 }
354 if (is_callable([$this, 'uninstall'])) {
355 $this->uninstall();
356 }
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);
361 }
362 }
363 }
364
365 /**
366 * @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_enable
367 */
368 public function onEnable() {
369 // stub for possible future use
370 if (is_callable([$this, 'enable'])) {
371 $this->enable();
372 }
373 }
374
375 /**
376 * @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_disable
377 */
378 public function onDisable() {
379 // stub for possible future use
380 if (is_callable([$this, 'disable'])) {
381 $this->disable();
382 }
383 }
384
385 public function onUpgrade($op, CRM_Queue_Queue $queue = NULL) {
386 switch ($op) {
387 case 'check':
388 return [$this->hasPendingRevisions()];
389
390 case 'enqueue':
391 return $this->enqueuePendingRevisions($queue);
392
393 default:
394 }
395 }
396
397 }