]> git.mxchange.org Git - friendica.git/blobdiff - src/Database/View.php
Detection of local requests
[friendica.git] / src / Database / View.php
index e1335d9df65f049307877085451e832b23cab5d7..e0d30481524d3bb0c6fe07b2806dfaa202b5c1cc 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2021, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -77,6 +77,20 @@ class View
 
        public static function create(bool $verbose, bool $action)
        {
+               // Delete previously used views that aren't used anymore
+               foreach(['post-view', 'post-thread-view'] as $view) {
+                       if (self::isView($view)) {
+                               $sql = sprintf("DROP VIEW IF EXISTS `%s`", DBA::escape($view));
+                               if (!empty($sql) && $verbose) {
+                                       echo $sql . ";\n";
+                               }
+               
+                               if (!empty($sql) && $action) {
+                                       DBA::e($sql);
+                               }
+                       }
+               }
+
                $definition = self::definition();
 
                foreach ($definition as $name => $structure) {
@@ -111,13 +125,17 @@ class View
                        }
                }
 
-               $sql = sprintf("DROP VIEW IF EXISTS `%s`", DBA::escape($name));
+               if (self::isView($name)) {
+                       $sql = sprintf("DROP VIEW IF EXISTS `%s`", DBA::escape($name));
+               } elseif (self::isTable($name)) {
+                       $sql = sprintf("DROP TABLE IF EXISTS `%s`", DBA::escape($name));
+               }
 
-               if ($verbose) {
+               if (!empty($sql) && $verbose) {
                        echo $sql . ";\n";
                }
 
-               if ($action) {
+               if (!empty($sql) && $action) {
                        DBA::e($sql);
                }
 
@@ -134,4 +152,40 @@ class View
 
                return $r;
        }
+
+       /**
+        * Check if the given table/view is a view
+        *
+        * @param string $view
+        * @return boolean "true" if it's a view
+        */
+       private static function isView(string $view)
+       {
+               $status = DBA::selectFirst(['INFORMATION_SCHEMA' => 'TABLES'], ['TABLE_TYPE'],
+                       ['TABLE_SCHEMA' => DBA::databaseName(), 'TABLE_NAME' => $view]);
+
+               if (empty($status['TABLE_TYPE'])) {
+                       return false;
+               }
+
+               return $status['TABLE_TYPE'] == 'VIEW';
+       }
+
+       /**
+        * Check if the given table/view is a table
+        *
+        * @param string $table
+        * @return boolean "true" if it's a table
+        */
+       private static function isTable(string $table)
+       {
+               $status = DBA::selectFirst(['INFORMATION_SCHEMA' => 'TABLES'], ['TABLE_TYPE'],
+                       ['TABLE_SCHEMA' => DBA::databaseName(), 'TABLE_NAME' => $table]);
+
+               if (empty($status['TABLE_TYPE'])) {
+                       return false;
+               }
+
+               return $status['TABLE_TYPE'] == 'BASE TABLE';
+       }
 }