|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @package hubzero-cms |
| 4 | + * @copyright Copyright (c) 2005-2026 The Regents of the University of California. |
| 5 | + * @license http://opensource.org/licenses/MIT MIT |
| 6 | + */ |
| 7 | + |
| 8 | +use Hubzero\Content\Migration\Base; |
| 9 | + |
| 10 | +// No direct access |
| 11 | +defined('_HZEXEC_') or die(); |
| 12 | + |
| 13 | +/** |
| 14 | + * Migration script for optional time-limited group membership |
| 15 | + * |
| 16 | + * Adds the term columns to the membership table and an archive of lapsed |
| 17 | + * memberships. Every existing row keeps `expires` NULL, which means |
| 18 | + * "never expires" - the behaviour groups have today. |
| 19 | + **/ |
| 20 | +class Migration20260810000000ComGroups extends Base |
| 21 | +{ |
| 22 | + /** |
| 23 | + * Is an index present on a table? |
| 24 | + * |
| 25 | + * The driver exposes tableExists()/tableHasField() but no key equivalent, |
| 26 | + * so this goes through getTableKeys(), which returns the SHOW KEYS rows |
| 27 | + * indexed by Key_name. |
| 28 | + * |
| 29 | + * @param string $table |
| 30 | + * @param string $key |
| 31 | + * @return boolean |
| 32 | + **/ |
| 33 | + protected function hasKey($table, $key) |
| 34 | + { |
| 35 | + $keys = $this->db->getTableKeys($table); |
| 36 | + |
| 37 | + return is_array($keys) && array_key_exists($key, $keys); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Up |
| 42 | + **/ |
| 43 | + public function up() |
| 44 | + { |
| 45 | + if ($this->db->tableExists('#__xgroups_members')) |
| 46 | + { |
| 47 | + if (!$this->db->tableHasField('#__xgroups_members', 'expires')) |
| 48 | + { |
| 49 | + $query = "ALTER TABLE `#__xgroups_members` |
| 50 | + ADD COLUMN `expires` datetime DEFAULT NULL AFTER `uidNumber`"; |
| 51 | + $this->db->setQuery($query); |
| 52 | + $this->db->query(); |
| 53 | + } |
| 54 | + |
| 55 | + if (!$this->db->tableHasField('#__xgroups_members', 'expires_set_by')) |
| 56 | + { |
| 57 | + $query = "ALTER TABLE `#__xgroups_members` |
| 58 | + ADD COLUMN `expires_set_by` int(11) DEFAULT NULL AFTER `expires`"; |
| 59 | + $this->db->setQuery($query); |
| 60 | + $this->db->query(); |
| 61 | + } |
| 62 | + |
| 63 | + if (!$this->db->tableHasField('#__xgroups_members', 'expires_notified')) |
| 64 | + { |
| 65 | + $query = "ALTER TABLE `#__xgroups_members` |
| 66 | + ADD COLUMN `expires_notified` datetime DEFAULT NULL AFTER `expires_set_by`"; |
| 67 | + $this->db->setQuery($query); |
| 68 | + $this->db->query(); |
| 69 | + } |
| 70 | + |
| 71 | + if (!$this->hasKey('#__xgroups_members', 'idx_expires')) |
| 72 | + { |
| 73 | + $query = "ALTER TABLE `#__xgroups_members` ADD KEY `idx_expires` (`expires`)"; |
| 74 | + $this->db->setQuery($query); |
| 75 | + $this->db->query(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // The reaper's heartbeat lives in the group log, which is queried by |
| 80 | + // action on every admin membership page view and pruned on every run. |
| 81 | + // That table carries only a primary key, so both are full scans |
| 82 | + // without this - measurably ~14ms per view on a 200k-row log. |
| 83 | + if ($this->db->tableExists('#__xgroups_log') |
| 84 | + && !$this->hasKey('#__xgroups_log', 'idx_action_timestamp')) |
| 85 | + { |
| 86 | + $query = "ALTER TABLE `#__xgroups_log` ADD KEY `idx_action_timestamp` (`action`,`timestamp`)"; |
| 87 | + $this->db->setQuery($query); |
| 88 | + $this->db->query(); |
| 89 | + } |
| 90 | + |
| 91 | + if (!$this->db->tableExists('#__xgroups_member_history')) |
| 92 | + { |
| 93 | + $query = "CREATE TABLE `#__xgroups_member_history` ( |
| 94 | + `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, |
| 95 | + `gidNumber` int(11) NOT NULL, |
| 96 | + `uidNumber` int(11) NOT NULL, |
| 97 | + `expires` datetime DEFAULT NULL, |
| 98 | + `revoked` datetime DEFAULT NULL, |
| 99 | + `reason` varchar(32) DEFAULT NULL, |
| 100 | + `was_manager` tinyint(1) DEFAULT 0, |
| 101 | + `actor` int(11) DEFAULT NULL, |
| 102 | + PRIMARY KEY (`id`), |
| 103 | + KEY `idx_gidNumber_uidNumber` (`gidNumber`,`uidNumber`), |
| 104 | + KEY `idx_revoked` (`revoked`) |
| 105 | + ) ENGINE=MyISAM DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci;"; |
| 106 | + |
| 107 | + $this->db->setQuery($query); |
| 108 | + $this->db->query(); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Down |
| 114 | + **/ |
| 115 | + public function down() |
| 116 | + { |
| 117 | + if ($this->db->tableExists('#__xgroups_members')) |
| 118 | + { |
| 119 | + if ($this->hasKey('#__xgroups_members', 'idx_expires')) |
| 120 | + { |
| 121 | + $query = "ALTER TABLE `#__xgroups_members` DROP KEY `idx_expires`"; |
| 122 | + $this->db->setQuery($query); |
| 123 | + $this->db->query(); |
| 124 | + } |
| 125 | + |
| 126 | + foreach (array('expires_notified', 'expires_set_by', 'expires') as $field) |
| 127 | + { |
| 128 | + if ($this->db->tableHasField('#__xgroups_members', $field)) |
| 129 | + { |
| 130 | + $query = "ALTER TABLE `#__xgroups_members` DROP COLUMN `$field`"; |
| 131 | + $this->db->setQuery($query); |
| 132 | + $this->db->query(); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + if ($this->db->tableExists('#__xgroups_log') |
| 138 | + && $this->hasKey('#__xgroups_log', 'idx_action_timestamp')) |
| 139 | + { |
| 140 | + $query = "ALTER TABLE `#__xgroups_log` DROP KEY `idx_action_timestamp`"; |
| 141 | + $this->db->setQuery($query); |
| 142 | + $this->db->query(); |
| 143 | + } |
| 144 | + |
| 145 | + if ($this->db->tableExists('#__xgroups_member_history')) |
| 146 | + { |
| 147 | + $query = "DROP TABLE IF EXISTS `#__xgroups_member_history`;"; |
| 148 | + $this->db->setQuery($query); |
| 149 | + $this->db->query(); |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments