]> git.mxchange.org Git - friendica.git/blob - src/Database/View.php
Removed redundant maximagesize = INF statements
[friendica.git] / src / Database / View.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Database;
23
24 use Friendica\DI;
25 use Friendica\Util\Writer\ViewDefinitionSqlWriter;
26
27 class View
28 {
29         /**
30          * Creates a view
31          *
32          * @param bool $verbose Whether to show SQL statements
33          * @param bool $action Whether to execute SQL statements
34          * @return void
35          */
36         public static function create(bool $verbose, bool $action)
37         {
38                 // Delete previously used views that aren't used anymore
39                 foreach (['post-view', 'post-thread-view'] as $view) {
40                         if (self::isView($view)) {
41                                 $sql = sprintf("DROP VIEW IF EXISTS `%s`", DBA::escape($view));
42                                 if (!empty($sql) && $verbose) {
43                                         echo $sql . ";\n";
44                                 }
45
46                                 if (!empty($sql) && $action) {
47                                         DBA::e($sql);
48                                 }
49                         }
50                 }
51
52                 $definition = DI::viewDefinition()->getAll();
53
54                 foreach ($definition as $name => $structure) {
55                         if (self::isView($name)) {
56                                 DBA::e(sprintf("DROP VIEW IF EXISTS `%s`", DBA::escape($name)));
57                         } elseif (self::isTable($name)) {
58                                 DBA::e(sprintf("DROP TABLE IF EXISTS `%s`", DBA::escape($name)));
59                         }
60                         DBA::e(ViewDefinitionSqlWriter::createView($name, $structure));
61                 }
62         }
63
64         /**
65          * Check if the given table/view is a view
66          *
67          * @param string $view
68          * @return boolean "true" if it's a view
69          */
70         private static function isView(string $view): bool
71         {
72                 $status = DBA::selectFirst('INFORMATION_SCHEMA.TABLES', ['TABLE_TYPE'],
73                         ['TABLE_SCHEMA' => DBA::databaseName(), 'TABLE_NAME' => $view]);
74
75                 if (empty($status['TABLE_TYPE'])) {
76                         return false;
77                 }
78
79                 return $status['TABLE_TYPE'] == 'VIEW';
80         }
81
82         /**
83          * Check if the given table/view is a table
84          *
85          * @param string $table
86          * @return boolean "true" if it's a table
87          */
88         private static function isTable(string $table): bool
89         {
90                 $status = DBA::selectFirst('INFORMATION_SCHEMA.TABLES', ['TABLE_TYPE'],
91                         ['TABLE_SCHEMA' => DBA::databaseName(), 'TABLE_NAME' => $table]);
92
93                 if (empty($status['TABLE_TYPE'])) {
94                         return false;
95                 }
96
97                 return $status['TABLE_TYPE'] == 'BASE TABLE';
98         }
99 }