]> git.mxchange.org Git - friendica.git/blobdiff - src/Database/DBStructure.php
Issue 9657: Check the age of an item
[friendica.git] / src / Database / DBStructure.php
index e75e2f58ccb286f6df23b56a1858affc89222c37..aa6a0cf29b66741682882cd9fdfc351667a67d5e 100644 (file)
@@ -48,6 +48,22 @@ class DBStructure
         */
        private static $definition = [];
 
+       /**
+        * Set a database version to trigger update functions
+        *
+        * @param string $version
+        * @return void
+        */
+       public static function setDatabaseVersion(string $version)
+       {
+               if (!is_numeric($version)) {
+                       throw new \Asika\SimpleConsole\CommandArgsException('The version number must be numeric');
+               }
+
+               DI::config()->set('system', 'build', $version);
+               echo DI::l10n()->t('The database version had been set to %s.', $version);
+       }
+
        /**
         * Converts all tables from MyISAM/InnoDB Antelope to InnoDB Barracuda
         */
@@ -154,6 +170,34 @@ class DBStructure
                return $definition;
        }
 
+       /**
+        * Get field data for the given table
+        *
+        * @param string $table
+        * @param array $data data fields
+        * @return array fields for the given
+        */
+       public static function getFieldsForTable(string $table, array $data = [])
+       {
+               $definition = DBStructure::definition('', false);
+               if (empty($definition[$table])) {
+                       return [];
+               }
+
+               $fieldnames = array_keys($definition[$table]['fields']);
+
+               $fields = [];
+
+               // Assign all field that are present in the table
+               foreach ($fieldnames as $field) {
+                       if (isset($data[$field])) {
+                               $fields[$field] = $data[$field];
+                       }
+               }
+
+               return $fields;
+       }
+
        private static function createTable($name, $structure, $verbose, $action)
        {
                $r = true;
@@ -306,7 +350,7 @@ class DBStructure
 
                $errors = '';
 
-               Logger::log('updating structure', Logger::DEBUG);
+               Logger::info('updating structure');
 
                // Get the current structure
                $database = [];
@@ -319,7 +363,7 @@ class DBStructure
                        foreach ($tables AS $table) {
                                $table = current($table);
 
-                               Logger::log(sprintf('updating structure for table %s ...', $table), Logger::DEBUG);
+                               Logger::info('updating structure', ['table' => $table]);
                                $database[$table] = self::tableStructure($table);
                        }
                }
@@ -483,8 +527,8 @@ class DBStructure
                                        }
                                }
 
-                               foreach ($existing_foreign_keys as $constraint => $param) {
-                                       $sql2 = self::dropForeignKey($constraint);
+                               foreach ($existing_foreign_keys as $param) {
+                                       $sql2 = self::dropForeignKey($param['CONSTRAINT_NAME']);
 
                                        if ($sql3 == "") {
                                                $sql3 = "ALTER" . $ignore . " TABLE `" . $temp_name . "` " . $sql2;
@@ -693,8 +737,8 @@ class DBStructure
 
                if (DBA::isResult($foreign_keys)) {
                        foreach ($foreign_keys as $foreign_key) {
-                               $constraint = $foreign_key['CONSTRAINT_NAME'];
-                               unset($foreign_key['CONSTRAINT_NAME']); 
+                               $parameters = ['foreign' => [$foreign_key['REFERENCED_TABLE_NAME'] => $foreign_key['REFERENCED_COLUMN_NAME']]];
+                               $constraint = self::getConstraintName($table, $foreign_key['COLUMN_NAME'], $parameters);
                                $foreigndata[$constraint] = $foreign_key;
                        }
                }
@@ -783,10 +827,7 @@ class DBStructure
                $foreign_table = array_keys($parameters['foreign'])[0];
                $foreign_field = array_values($parameters['foreign'])[0];
 
-               $constraint = self::getConstraintName($tablename, $fieldname, $parameters);
-
-               $sql = "CONSTRAINT `" . $constraint . "` FOREIGN KEY (`" . $fieldname . "`)" .
-                       " REFERENCES `" . $foreign_table . "` (`" . $foreign_field . "`)";
+               $sql = "FOREIGN KEY (`" . $fieldname . "`) REFERENCES `" . $foreign_table . "` (`" . $foreign_field . "`)";
 
                if (!empty($parameters['foreign']['on update'])) {
                        $sql .= " ON UPDATE " . strtoupper($parameters['foreign']['on update']);
@@ -1005,14 +1046,84 @@ class DBStructure
        /**
         * Check if initial database values do exist - or create them
         */
-       public static function checkInitialValues()
+       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')) {
@@ -1021,7 +1132,12 @@ class DBStructure
                                $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::existsForeignKeyForField('item', 'psid')) {
                                $sets = DBA::p("SELECT `psid`, `item`.`uid`, `item`.`private` FROM `item`
@@ -1044,16 +1160,10 @@ class DBStructure
                                }
                                DBA::close($sets);
                        }
+               } elseif ($verbose) {
+                       echo "permissionset: 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 (!self::existsForeignKeyForField('tokens', 'client_id')) {
                        $tokens = DBA::p("SELECT `tokens`.`id` FROM `tokens`
                                LEFT JOIN `clients` ON `clients`.`client_id` = `tokens`.`client_id`