]> git.mxchange.org Git - friendica.git/blobdiff - src/Database/DBStructure.php
Fixed table name
[friendica.git] / src / Database / DBStructure.php
index 8f599a67d72a3e15b7886531181ec285987e8e76..0ff97d0c6c3685aa162b733b9b2b766902e332f7 100644 (file)
@@ -25,6 +25,8 @@ use Exception;
 use Friendica\Core\Hook;
 use Friendica\Core\Logger;
 use Friendica\DI;
+use Friendica\Model\Item;
+use Friendica\Model\User;
 use Friendica\Util\DateTimeFormat;
 
 /**
@@ -294,6 +296,10 @@ class DBStructure
                        DI::config()->set('system', 'maintenance_reason', DI::l10n()->t('%s: Database update', DateTimeFormat::utcNow() . ' ' . date('e')));
                }
 
+               // ensure that all initial values exist. This test has to be done prior and after the structure check.
+               // Prior is needed if the specific tables already exists - after is needed when they had been created.
+               self::checkInitialValues();
+
                $errors = '';
 
                Logger::log('updating structure', Logger::DEBUG);
@@ -640,6 +646,8 @@ class DBStructure
 
                View::create(false, $action);
 
+               self::checkInitialValues();
+
                if ($action && !$install) {
                        DI::config()->set('system', 'maintenance', 0);
                        DI::config()->set('system', 'maintenance_reason', '');
@@ -657,7 +665,7 @@ class DBStructure
        private static function tableStructure($table)
        {
                // This query doesn't seem to be executable as a prepared statement
-               $indexes = DBA::toArray(DBA::p(sprintf("SHOW INDEX FROM `%s`", $table)));
+               $indexes = DBA::toArray(DBA::p("SHOW INDEX FROM " . DBA::quoteIdentifier($table)));
 
                $fields = DBA::selectToArray(['INFORMATION_SCHEMA' => 'COLUMNS'],
                        ['COLUMN_NAME', 'COLUMN_TYPE', 'IS_NULLABLE', 'COLUMN_DEFAULT', 'EXTRA',
@@ -717,7 +725,7 @@ class DBStructure
                                $fielddata[$field['COLUMN_NAME']]['type'] = $field['COLUMN_TYPE'];
 
                                if ($field['IS_NULLABLE'] == 'NO') {
-                                       $fielddata[$field['COLUMN_NAME']]['not null'] = 1;
+                                       $fielddata[$field['COLUMN_NAME']]['not null'] = true;
                                }
 
                                if (isset($field['COLUMN_DEFAULT'])) {
@@ -729,7 +737,7 @@ class DBStructure
                                }
 
                                if ($field['COLUMN_KEY'] == 'PRI') {
-                                       $fielddata[$field['COLUMN_NAME']]['primary'] = 1;
+                                       $fielddata[$field['COLUMN_NAME']]['primary'] = true;
                                }
 
                                $fielddata[$field['COLUMN_NAME']]['Collation'] = $field['COLLATION_NAME'];
@@ -938,6 +946,19 @@ class DBStructure
                return true;
        }
 
+       /**
+        * Check if a foreign key exists for the given table field
+        *
+        * @param string $table
+        * @param string $field
+        * @return boolean
+        */
+       public static function existsForeignKeyForField(string $table, string $field)
+       {
+               return DBA::exists(['INFORMATION_SCHEMA' => 'KEY_COLUMN_USAGE'],
+                       ["`TABLE_SCHEMA` = ? AND `TABLE_NAME` = ? AND `COLUMN_NAME` = ? AND `REFERENCED_TABLE_SCHEMA` IS NOT NULL",
+                       DBA::databaseName(), $table, $field]);
+       }
        /**
         *    Check if a table exists
         *
@@ -976,4 +997,67 @@ class DBStructure
                $stmtColumns = DBA::p("SHOW COLUMNS FROM `" . $table . "`");
                return DBA::toArray($stmtColumns);
        }
+
+       /**
+        * Check if initial database values do exist - or create them
+        */
+       public static function checkInitialValues()
+       {
+               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 (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 (!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);
+                       }
+               }
+       
+               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 (!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);
+               }
+       }
 }