+
+ /**
+ * Check if initial database values do exist - or create them
+ */
+ public static function checkInitialValues(bool $verbose = false)
+ {
+ if (self::existsTable('verb')) {
+ if (!DBA::exists('verb', ['id' => 1])) {
+ foreach (Item::ACTIVITIES as $index => $activity) {
+ DBA::insert('verb', ['id' => $index + 1, 'name' => $activity], Database::INSERT_IGNORE);
+ }
+ if ($verbose) {
+ echo "verb: activities added\n";
+ }
+ } elseif ($verbose) {
+ echo "verb: activities already added\n";
+ }
+
+ if (!DBA::exists('verb', ['id' => 0])) {
+ DBA::insert('verb', ['name' => '']);
+ $lastid = DBA::lastInsertId();
+ if ($lastid != 0) {
+ DBA::update('verb', ['id' => 0], ['id' => $lastid]);
+ if ($verbose) {
+ echo "Zero verb added\n";
+ }
+ }
+ } elseif ($verbose) {
+ echo "Zero verb already added\n";
+ }
+ } elseif ($verbose) {
+ echo "verb: Table not found\n";
+ }
+
+ if (self::existsTable('user') && !DBA::exists('user', ['uid' => 0])) {
+ $user = [
+ "verified" => true,
+ "page-flags" => User::PAGE_FLAGS_SOAPBOX,
+ "account-type" => User::ACCOUNT_TYPE_RELAY,
+ ];
+ DBA::insert('user', $user);
+ $lastid = DBA::lastInsertId();
+ if ($lastid != 0) {
+ DBA::update('user', ['uid' => 0], ['uid' => $lastid]);
+ if ($verbose) {
+ echo "Zero user added\n";
+ }
+ }
+ } elseif (self::existsTable('user') && $verbose) {
+ echo "Zero user already added\n";
+ } elseif ($verbose) {
+ echo "user: Table not found\n";
+ }
+
+ if (self::existsTable('contact') && !DBA::exists('contact', ['id' => 0])) {
+ DBA::insert('contact', ['nurl' => '']);
+ $lastid = DBA::lastInsertId();
+ if ($lastid != 0) {
+ DBA::update('contact', ['id' => 0], ['id' => $lastid]);
+ if ($verbose) {
+ echo "Zero contact added\n";
+ }
+ }
+ } elseif (self::existsTable('contact') && $verbose) {
+ echo "Zero contact already added\n";
+ } elseif ($verbose) {
+ echo "contact: Table not found\n";
+ }
+
+ if (self::existsTable('tag') && !DBA::exists('tag', ['id' => 0])) {
+ DBA::insert('tag', ['name' => '']);
+ $lastid = DBA::lastInsertId();
+ if ($lastid != 0) {
+ DBA::update('tag', ['id' => 0], ['id' => $lastid]);
+ if ($verbose) {
+ echo "Zero tag added\n";
+ }
+ }
+ } 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;
+ }