Elgg /
Elgg
implicit conversion of array to boolean.
| 1 | <?php |
||
| 2 | |||
| 3 | use Phinx\Db\Adapter\MysqlAdapter; |
||
| 4 | use Phinx\Migration\AbstractMigration; |
||
| 5 | |||
| 6 | /** |
||
| 7 | * Removes multisite support from 2.x schema |
||
| 8 | */ |
||
| 9 | class RemoveSiteGuid extends AbstractMigration { |
||
| 10 | |||
| 11 | /** |
||
| 12 | * Ensure that legacy schema only has 1 site entity |
||
| 13 | * Refuse to upgrade if it doesn't |
||
| 14 | * |
||
| 15 | * @throws InstallationException |
||
| 16 | */ |
||
| 17 | public function validate() { |
||
| 18 | |||
| 19 | // validate if multiple sites are in the database |
||
| 20 | $tables = [ |
||
| 21 | 'access_collections', |
||
| 22 | 'api_users', |
||
| 23 | 'config', |
||
| 24 | 'entities', |
||
| 25 | 'users_apisessions' |
||
| 26 | ]; |
||
| 27 | |||
| 28 | foreach ($tables as $table) { |
||
| 29 | if (!$this->hasTable($table)) { |
||
| 30 | continue; |
||
| 31 | } |
||
| 32 | |||
| 33 | $prefix = $this->getAdapter()->getOption('table_prefix'); |
||
| 34 | $row = $this->fetchRow(" |
||
| 35 | SELECT count(DISTINCT site_guid) as count |
||
| 36 | FROM {$prefix}{$table} |
||
| 37 | "); |
||
| 38 | |||
| 39 | if ($row && $row['count'] > 1) { |
||
|
1 ignored issue
–
show
|
|||
| 40 | throw new InstallationException("Multiple sites detected in table: '{$prefix}{$table}'. Can't upgrade the database."); |
||
| 41 | } |
||
| 42 | } |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Removes site guid from legacy 2.x tables |
||
| 47 | */ |
||
| 48 | public function up() { |
||
| 49 | |||
| 50 | $this->validate(); |
||
| 51 | |||
| 52 | if ($this->hasTable('access_collections')) { |
||
| 53 | $table = $this->table('access_collections'); |
||
| 54 | |||
| 55 | if ($table->hasIndex('site_guid')) { |
||
| 56 | $table->removeIndexByName('site_guid'); |
||
| 57 | } |
||
| 58 | |||
| 59 | if ($table->hasColumn('site_guid')) { |
||
| 60 | $table->removeColumn('site_guid'); |
||
| 61 | } |
||
| 62 | |||
| 63 | $table->save(); |
||
| 64 | } |
||
| 65 | |||
| 66 | if ($this->hasTable('api_users')) { |
||
| 67 | $table = $this->table('api_users'); |
||
| 68 | |||
| 69 | if ($table->hasColumn('site_guid')) { |
||
| 70 | $table->removeColumn('site_guid'); |
||
| 71 | } |
||
| 72 | |||
| 73 | $table->save(); |
||
| 74 | } |
||
| 75 | |||
| 76 | if ($this->hasTable('config')) { |
||
| 77 | $table = $this->table('config', [ |
||
| 78 | 'primary_key' => ["name"], |
||
| 79 | ]); |
||
| 80 | |||
| 81 | if ($table->hasIndex('site_guid')) { |
||
| 82 | $table->removeIndexByName('site_guid'); |
||
| 83 | } |
||
| 84 | |||
| 85 | if ($table->hasColumn('site_guid')) { |
||
| 86 | $table->removeColumn('site_guid'); |
||
| 87 | } |
||
| 88 | |||
| 89 | $table->save(); |
||
| 90 | } |
||
| 91 | |||
| 92 | if ($this->hasTable('entities')) { |
||
| 93 | $table = $this->table('entities'); |
||
| 94 | |||
| 95 | if ($table->hasIndex('site_guid')) { |
||
| 96 | $table->removeIndexByName('site_guid'); |
||
| 97 | } |
||
| 98 | |||
| 99 | if ($table->hasColumn('site_guid')) { |
||
| 100 | $table->removeColumn('site_guid'); |
||
| 101 | } |
||
| 102 | |||
| 103 | $table->save(); |
||
| 104 | } |
||
| 105 | |||
| 106 | if ($this->hasTable('users_apisessions')) { |
||
| 107 | $table = $this->table('users_apisessions'); |
||
| 108 | |||
| 109 | if ($table->hasIndex('site_guid')) { |
||
| 110 | $table->removeIndexByName('site_guid'); |
||
| 111 | } |
||
| 112 | |||
| 113 | if ($table->hasColumn('site_guid')) { |
||
| 114 | $table->removeColumn('site_guid'); |
||
| 115 | } |
||
| 116 | |||
| 117 | if ($table->hasIndex('user_guid')) { |
||
| 118 | $table->removeIndex('user_guid'); |
||
| 119 | } |
||
| 120 | |||
| 121 | $table->addIndex(['user_guid'], [ |
||
| 122 | 'name' => "user_guid", |
||
| 123 | 'unique' => false, |
||
| 124 | ]); |
||
| 125 | |||
| 126 | $table->save(); |
||
| 127 | } |
||
| 128 | |||
| 129 | if ($this->hasTable('entity_relationships')) { |
||
| 130 | // Remove member_of_site relaitonship following site_guid removal |
||
| 131 | $prefix = $this->getAdapter()->getOption('table_prefix'); |
||
| 132 | $this->query(" |
||
| 133 | DELETE FROM {$prefix}entity_relationships |
||
| 134 | WHERE relationship = 'member_of_site' |
||
| 135 | "); |
||
| 136 | } |
||
| 137 | |||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Add site_guid column and index |
||
| 142 | */ |
||
| 143 | public function down() { |
||
| 144 | |||
| 145 | if ($this->hasTable('access_collections')) { |
||
| 146 | $table = $this->table('access_collections'); |
||
| 147 | |||
| 148 | if (!$table->hasColumn('site_guid')) { |
||
| 149 | $table->addColumn('site_guid', 'integer', [ |
||
| 150 | 'null' => false, |
||
| 151 | 'limit' => MysqlAdapter::INT_BIG, |
||
| 152 | 'precision' => 20, |
||
| 153 | 'signed' => false, |
||
| 154 | ]); |
||
| 155 | } |
||
| 156 | |||
| 157 | if (!$table->hasIndex('site_guid')) { |
||
| 158 | $table->addIndex(['site_guid'], [ |
||
| 159 | 'name' => 'site_guid', |
||
| 160 | 'unique' => false, |
||
| 161 | ]); |
||
| 162 | } |
||
| 163 | |||
| 164 | $table->save(); |
||
| 165 | |||
| 166 | $prefix = $this->getAdapter()->getOption('table_prefix'); |
||
| 167 | $this->query(" |
||
| 168 | UPDATE {$prefix}access_collections |
||
| 169 | SET site_guid = 1 |
||
| 170 | WHERE site_guid != 1 |
||
| 171 | "); |
||
| 172 | } |
||
| 173 | |||
| 174 | if ($this->hasTable('api_users')) { |
||
| 175 | $table = $this->table('api_users'); |
||
| 176 | |||
| 177 | if (!$table->hasColumn('site_guid')) { |
||
| 178 | $table->addColumn('site_guid', 'integer', [ |
||
| 179 | 'null' => false, |
||
| 180 | 'limit' => MysqlAdapter::INT_BIG, |
||
| 181 | 'precision' => 20, |
||
| 182 | 'signed' => false, |
||
| 183 | ]); |
||
| 184 | } |
||
| 185 | |||
| 186 | $table->save(); |
||
| 187 | |||
| 188 | $prefix = $this->getAdapter()->getOption('table_prefix'); |
||
| 189 | $this->query(" |
||
| 190 | UPDATE {$prefix}api_users |
||
| 191 | SET site_guid = 1 |
||
| 192 | WHERE site_guid != 1 |
||
| 193 | "); |
||
| 194 | } |
||
| 195 | |||
| 196 | if ($this->hasTable('config')) { |
||
| 197 | $table = $this->table('config', [ |
||
| 198 | 'primary_key' => [ |
||
| 199 | "name", |
||
| 200 | "site_guid" |
||
| 201 | ], |
||
| 202 | ]); |
||
| 203 | |||
| 204 | if (!$table->hasColumn('site_guid')) { |
||
| 205 | $table->addColumn('site_guid', 'integer', [ |
||
| 206 | 'null' => false, |
||
| 207 | 'limit' => MysqlAdapter::INT_BIG, |
||
| 208 | 'precision' => 20, |
||
| 209 | 'signed' => false, |
||
| 210 | ]); |
||
| 211 | } |
||
| 212 | |||
| 213 | if (!$table->hasIndex('site_guid')) { |
||
| 214 | $table->addIndex(['site_guid'], [ |
||
| 215 | 'name' => 'site_guid', |
||
| 216 | 'unique' => false, |
||
| 217 | ]); |
||
| 218 | } |
||
| 219 | |||
| 220 | $table->save(); |
||
| 221 | |||
| 222 | $prefix = $this->getAdapter()->getOption('table_prefix'); |
||
| 223 | $this->query(" |
||
| 224 | UPDATE {$prefix}config |
||
| 225 | SET site_guid = 1 |
||
| 226 | WHERE site_guid != 1 |
||
| 227 | "); |
||
| 228 | } |
||
| 229 | |||
| 230 | if ($this->hasTable('entities')) { |
||
| 231 | // remove site guid from entities |
||
| 232 | $table = $this->table('entities'); |
||
| 233 | |||
| 234 | if (!$table->hasColumn('site_guid')) { |
||
| 235 | $table->addColumn('site_guid', 'integer', [ |
||
| 236 | 'null' => false, |
||
| 237 | 'limit' => MysqlAdapter::INT_BIG, |
||
| 238 | 'precision' => 20, |
||
| 239 | 'signed' => false, |
||
| 240 | ]); |
||
| 241 | } |
||
| 242 | |||
| 243 | if (!$table->hasIndex('site_guid')) { |
||
| 244 | $table->addIndex(['site_guid'], [ |
||
| 245 | 'name' => 'site_guid', |
||
| 246 | 'unique' => false, |
||
| 247 | ]); |
||
| 248 | } |
||
| 249 | |||
| 250 | $table->save(); |
||
| 251 | |||
| 252 | $prefix = $this->getAdapter()->getOption('table_prefix'); |
||
| 253 | $this->query(" |
||
| 254 | UPDATE {$prefix}entities |
||
| 255 | SET site_guid = 1 |
||
| 256 | WHERE site_guid != 1 |
||
| 257 | "); |
||
| 258 | |||
| 259 | if ($this->hasTable('entity_relationships')) { |
||
| 260 | $rows = $this->fetchAll(" |
||
| 261 | SELECT guid FROM {$prefix}entities |
||
| 262 | WHERE type = 'user' |
||
| 263 | "); |
||
| 264 | |||
| 265 | foreach ($rows as $row) { |
||
| 266 | $this->insert('entity_relationships', [ |
||
| 267 | 'guid_one' => $row['guid'], |
||
| 268 | 'relationship' => 'member_of_site', |
||
| 269 | 'guid_two' => 1, |
||
| 270 | 'time_created' => time(), |
||
| 271 | ]); |
||
| 272 | } |
||
| 273 | } |
||
| 274 | } |
||
| 275 | |||
| 276 | if ($this->hasTable('users_apisessions')) { |
||
| 277 | // remove site guid from users_apisessions |
||
| 278 | $table = $this->table('users_apisessions'); |
||
| 279 | |||
| 280 | if ($table->hasIndex('site_guid')) { |
||
| 281 | $table->removeIndexByName('site_guid'); |
||
| 282 | } |
||
| 283 | |||
| 284 | if (!$table->hasColumn('site_guid')) { |
||
| 285 | $table->addColumn('site_guid', 'integer', [ |
||
| 286 | 'null' => false, |
||
| 287 | 'limit' => MysqlAdapter::INT_BIG, |
||
| 288 | 'precision' => 20, |
||
| 289 | 'signed' => false, |
||
| 290 | ]); |
||
| 291 | } |
||
| 292 | |||
| 293 | if ($table->hasIndex('user_guid')) { |
||
| 294 | $table->removeIndexByName('user_guid'); |
||
| 295 | } |
||
| 296 | |||
| 297 | $table->addIndex([ |
||
| 298 | 'user_guid', |
||
| 299 | 'site_guid' |
||
| 300 | ], [ |
||
| 301 | 'name' => "user_guid", |
||
| 302 | 'unique' => true, |
||
| 303 | ]); |
||
| 304 | |||
| 305 | $table->save(); |
||
| 306 | |||
| 307 | $prefix = $this->getAdapter()->getOption('table_prefix'); |
||
| 308 | $this->query(" |
||
| 309 | UPDATE {$prefix}users_apisessions |
||
| 310 | SET site_guid = 1 |
||
| 311 | WHERE site_guid != 1 |
||
| 312 | "); |
||
| 313 | } |
||
| 314 | |||
| 315 | } |
||
| 316 | } |
||
| 317 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.