]> git.mxchange.org Git - friendica.git/blobdiff - src/Database/DBStructure.php
Use an exception
[friendica.git] / src / Database / DBStructure.php
index bda6f81f76464abca6c0b2950d83369eeaed0de4..9b69f7b1114143107a50fa15991666f7d58342ea 100644 (file)
@@ -64,6 +64,46 @@ class DBStructure
                echo DI::l10n()->t('The database version had been set to %s.', $version);
        }
 
+       /**
+        * Drop unused tables
+        *
+        * @param boolean $execute
+        * @return void
+        */
+       public static function dropTables(bool $execute)
+       {
+               $old_tables = ['fserver', 'gcign', 'gcontact', 'gcontact-relation', 'gfollower' ,'glink', 'item-delivery-data',
+               'item_id', 'poll', 'poll_result', 'queue', 'retriever_rule', 'sign', 'spam', 'term'];
+
+               $tables = DBA::selectToArray(['INFORMATION_SCHEMA' => 'TABLES'], ['TABLE_NAME'],
+                       ['TABLE_SCHEMA' => DBA::databaseName(), 'TABLE_TYPE' => 'BASE TABLE']);
+
+               if (empty($tables)) {
+                       echo DI::l10n()->t('No unused tables found.');
+                       return;
+               }
+
+               if (!$execute) {
+                       echo DI::l10n()->t('These tables are not used for friendica and will be deleted when you execute "dbstructure drop -e":') . "\n\n";
+               }
+
+               foreach ($tables as $table) {
+                       if (in_array($table['TABLE_NAME'], $old_tables)) {
+                               if ($execute) {
+                                       $sql = 'DROP TABLE ' . DBA::quoteIdentifier($table['TABLE_NAME']) . ';';
+                                       echo $sql . "\n";
+
+                                       $result = DBA::e($sql);
+                                       if (!DBA::isResult($result)) {
+                                               self::printUpdateError($sql);
+                                       }
+                               } else {
+                                       echo $table['TABLE_NAME'] . "\n";
+                               }
+                       }
+               }
+       }
+
        /**
         * Converts all tables from MyISAM/InnoDB Antelope to InnoDB Barracuda
         */
@@ -1090,9 +1130,9 @@ class DBStructure
                                        echo "Zero user added\n";
                                }
                        }
-               } elseif (self::existsTable('user')) {
+               } elseif (self::existsTable('user') && $verbose) {
                        echo "Zero user already added\n";
-               } else {
+               } elseif ($verbose) {
                        echo "user: Table not found\n";
                }
 
@@ -1105,12 +1145,27 @@ class DBStructure
                                        echo "Zero contact added\n";
                                }
                        }               
-               } elseif (self::existsTable('contact')) {
+               } elseif (self::existsTable('contact') && $verbose) {
                        echo "Zero contact already added\n";
-               } else {
+               } 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' => '']);       
@@ -1118,7 +1173,7 @@ class DBStructure
                                if ($lastid != 0) {
                                        DBA::update('permissionset', ['id' => 0], ['id' => $lastid]);
                                        if ($verbose) {
-                                               echo "Zero tag added\n";
+                                               echo "Zero permissionset added\n";
                                        }
                                }
                        } elseif ($verbose) {
@@ -1149,21 +1204,6 @@ class DBStructure
                        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 ($verbose) {
-                                       echo "Zero tag added\n";
-                               }
-                       }
-               } elseif (self::existsTable('tag')) {
-                       echo "Zero tag already added\n";
-               } else {
-                       echo "tag: 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`