3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * The revisions trait automatically enqueues any functions named 'upgrade_NNNN()'
14 * (where NNNN is taken to be a revision number).
16 trait CRM_Extension_Upgrader_RevisionsTrait
{
21 abstract public function getExtensionKey();
23 abstract protected function appendTask(string $title, string $funcName, ...$options);
33 * Flag to clean up extension revision data in civicrm_setting
35 private $revisionStorageIsDeprecated = FALSE;
38 * Determine if there are any pending revisions.
42 public function hasPendingRevisions() {
43 $revisions = $this->getRevisions();
44 $currentRevision = $this->getCurrentRevision();
46 if (empty($revisions)) {
49 if (empty($currentRevision)) {
53 return ($currentRevision < max($revisions));
57 * Add any pending revisions to the queue.
59 public function enqueuePendingRevisions() {
60 $currentRevision = $this->getCurrentRevision();
61 foreach ($this->getRevisions() as $revision) {
62 if ($revision > $currentRevision) {
63 $title = ts('Upgrade %1 to revision %2', [
64 1 => $this->getExtensionKey(),
68 // note: don't use addTask() because it sets weight=-1
70 $this->appendTask($title, 'upgrade_' . $revision);
71 $this->appendTask($title, 'setCurrentRevision', $revision);
77 * Get a list of revisions.
80 * revisionNumbers sorted numerically
82 public function getRevisions() {
83 if (!is_array($this->revisions
)) {
84 $this->revisions
= [];
86 $clazz = new \
ReflectionClass(get_class($this));
87 $methods = $clazz->getMethods();
88 foreach ($methods as $method) {
89 if (preg_match('/^upgrade_(.*)/', $method->name
, $matches)) {
90 $this->revisions
[] = $matches[1];
93 sort($this->revisions
, SORT_NUMERIC
);
96 return $this->revisions
;
99 public function getCurrentRevision() {
100 $revision = CRM_Core_BAO_Extension
::getSchemaVersion($this->getExtensionKey());
102 $revision = $this->getCurrentRevisionDeprecated();
107 private function getCurrentRevisionDeprecated() {
108 $key = $this->getExtensionKey() . ':version';
109 if ($revision = \Civi
::settings()->get($key)) {
110 $this->revisionStorageIsDeprecated
= TRUE;
115 public function setCurrentRevision($revision) {
116 CRM_Core_BAO_Extension
::setSchemaVersion($this->getExtensionKey(), $revision);
117 // clean up legacy schema version store (CRM-19252)
118 $this->deleteDeprecatedRevision();
122 private function deleteDeprecatedRevision() {
123 if ($this->revisionStorageIsDeprecated
) {
124 $setting = new \
CRM_Core_BAO_Setting();
125 $setting->name
= $this->getExtensionKey() . ':version';
127 CRM_Core_Error
::debug_log_message("Migrated extension schema revision ID for {$this->getExtensionKey()} from civicrm_setting (deprecated) to civicrm_extension.\n");