+ } elseif (self::existsTable('tag') && $verbose) {
+ echo "Zero tag already added\n";
+ } elseif ($verbose) {
+ echo "tag: Table not found\n";
+ }
+
+ if (self::existsTable('permissionset')) {
+ if (!DBA::exists('permissionset', ['id' => 0])) {
+ DBA::insert('permissionset', ['allow_cid' => '', 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '']);
+ $lastid = DBA::lastInsertId();
+ if ($lastid != 0) {
+ DBA::update('permissionset', ['id' => 0], ['id' => $lastid]);
+ if ($verbose) {
+ echo "Zero permissionset added\n";
+ }
+ }
+ } elseif ($verbose) {
+ echo "Zero permissionset already added\n";
+ }
+ if (self::existsTable('item') && !self::existsForeignKeyForField('item', 'psid')) {
+ $sets = DBA::p("SELECT `psid`, `item`.`uid`, `item`.`private` FROM `item`
+ LEFT JOIN `permissionset` ON `permissionset`.`id` = `item`.`psid`
+ WHERE `permissionset`.`id` IS NULL AND NOT `psid` IS NULL");
+ while ($set = DBA::fetch($sets)) {
+ if (($set['private'] == Item::PRIVATE) && ($set['uid'] != 0)) {
+ $owner = User::getOwnerDataById($set['uid']);
+ if ($owner) {
+ $permission = '<' . $owner['id'] . '>';
+ } else {
+ $permission = '<>';
+ }
+ } else {
+ $permission = '';
+ }
+ $fields = ['id' => $set['psid'], 'uid' => $set['uid'], 'allow_cid' => $permission,
+ 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => ''];
+ DBA::insert('permissionset', $fields);
+ }
+ DBA::close($sets);
+ }
+ } elseif ($verbose) {
+ echo "permissionset: Table not found\n";
+ }
+
+ if (!self::existsForeignKeyForField('tokens', 'client_id')) {
+ $tokens = DBA::p("SELECT `tokens`.`id` FROM `tokens`
+ LEFT JOIN `clients` ON `clients`.`client_id` = `tokens`.`client_id`
+ WHERE `clients`.`client_id` IS NULL");
+ while ($token = DBA::fetch($tokens)) {
+ DBA::delete('tokens', ['id' => $token['id']]);
+ }
+ DBA::close($tokens);
+ }
+ }
+
+ /**
+ * Checks if a database update is currently running
+ *
+ * @return boolean
+ */
+ private static function isUpdating()
+ {
+ $isUpdate = false;
+
+ $processes = DBA::select(['information_schema' => 'processlist'], ['info'],
+ ['db' => DBA::databaseName(), 'command' => ['Query', 'Execute']]);
+
+ while ($process = DBA::fetch($processes)) {
+ $parts = explode(' ', $process['info']);
+ if (in_array(strtolower(array_shift($parts)), ['alter', 'create', 'drop', 'rename'])) {
+ $isUpdate = true;
+ }
+ }
+
+ DBA::close($processes);
+
+ return $isUpdate;