3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Database;
25 use Friendica\Core\Hook;
31 * view definition loaded from static/dbview.config.php
35 private static $definition = [];
38 * Loads the database structure definition from the static/dbview.config.php file.
39 * On first pass, defines DB_UPDATE_VERSION constant.
41 * @see static/dbview.config.php
42 * @param string $basePath The base path of this application
43 * @param boolean $with_addons_structure Whether to tack on addons additional tables
47 public static function definition(string $basePath = '', bool $with_addons_structure = true): array
49 if (!self::$definition) {
50 if (empty($basePath)) {
51 $basePath = DI::app()->getBasePath();
54 $filename = $basePath . '/static/dbview.config.php';
56 if (!is_readable($filename)) {
57 throw new Exception('Missing database view config file static/dbview.config.php');
60 $definition = require $filename;
63 throw new Exception('Corrupted database view config file static/dbview.config.php');
66 self::$definition = $definition;
68 $definition = self::$definition;
71 if ($with_addons_structure) {
72 Hook::callAll('dbview_definition', $definition);
81 * @param bool $verbose Whether to show SQL statements
82 * @param bool $action Whether to execute SQL statements
85 public static function create(bool $verbose, bool $action)
87 // Delete previously used views that aren't used anymore
88 foreach(['post-view', 'post-thread-view'] as $view) {
89 if (self::isView($view)) {
90 $sql = sprintf("DROP VIEW IF EXISTS `%s`", DBA::escape($view));
91 if (!empty($sql) && $verbose) {
95 if (!empty($sql) && $action) {
101 $definition = self::definition();
103 foreach ($definition as $name => $structure) {
104 self::createView($name, $structure, $verbose, $action);
109 * Prints view structure
111 * @param string $basePath Base path
114 public static function printStructure(string $basePath)
116 $database = self::definition($basePath, false);
118 foreach ($database as $name => $structure) {
120 echo "-- VIEW $name\n";
122 self::createView($name, $structure, true, false);
131 * @param string $name Name of view
132 * @param array $structure Structure of view
133 * @param bool $verbose Whether to show SQL statements
134 * @param bool $action Whether to execute SQL statements
135 * @return bool Whether execution went fine
137 private static function createView(string $name, array $structure, bool $verbose, bool $action): bool
142 foreach ($structure['fields'] as $fieldname => $origin) {
143 if (is_string($origin)) {
144 $sql_rows[] = $origin . " AS `" . DBA::escape($fieldname) . "`";
145 } elseif (is_array($origin) && (sizeof($origin) == 2)) {
146 $sql_rows[] = "`" . DBA::escape($origin[0]) . "`.`" . DBA::escape($origin[1]) . "` AS `" . DBA::escape($fieldname) . "`";
150 if (self::isView($name)) {
151 $sql = sprintf("DROP VIEW IF EXISTS `%s`", DBA::escape($name));
152 } elseif (self::isTable($name)) {
153 $sql = sprintf("DROP TABLE IF EXISTS `%s`", DBA::escape($name));
156 if (!empty($sql) && $verbose) {
160 if (!empty($sql) && $action) {
164 $sql = sprintf("CREATE VIEW `%s` AS SELECT \n\t", DBA::escape($name)) .
165 implode(",\n\t", $sql_rows) . "\n\t" . $structure['query'];
179 * Check if the given table/view is a view
181 * @param string $view
182 * @return boolean "true" if it's a view
184 private static function isView(string $view): bool
186 $status = DBA::selectFirst('INFORMATION_SCHEMA.TABLES', ['TABLE_TYPE'],
187 ['TABLE_SCHEMA' => DBA::databaseName(), 'TABLE_NAME' => $view]);
189 if (empty($status['TABLE_TYPE'])) {
193 return $status['TABLE_TYPE'] == 'VIEW';
197 * Check if the given table/view is a table
199 * @param string $table
200 * @return boolean "true" if it's a table
202 private static function isTable(string $table): bool
204 $status = DBA::selectFirst('INFORMATION_SCHEMA.TABLES', ['TABLE_TYPE'],
205 ['TABLE_SCHEMA' => DBA::databaseName(), 'TABLE_NAME' => $table]);
207 if (empty($status['TABLE_TYPE'])) {
211 return $status['TABLE_TYPE'] == 'BASE TABLE';