3 * @copyright Copyright (C) 2010-2021, 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 boolean $with_addons_structure Whether to tack on addons additional tables
43 * @param string $basePath The base path of this application
47 public static function definition($basePath = '', $with_addons_structure = true)
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);
78 public static function create(bool $verbose, bool $action)
80 // Delete previously used views that aren't used anymore
81 foreach(['post-view', 'post-thread-view'] as $view) {
82 if (self::isView($view)) {
83 $sql = sprintf("DROP VIEW IF EXISTS `%s`", DBA::escape($view));
84 if (!empty($sql) && $verbose) {
88 if (!empty($sql) && $action) {
94 $definition = self::definition();
96 foreach ($definition as $name => $structure) {
97 self::createview($name, $structure, $verbose, $action);
101 public static function printStructure($basePath)
103 $database = self::definition($basePath, false);
105 foreach ($database as $name => $structure) {
107 echo "-- VIEW $name\n";
109 self::createView($name, $structure, true, false);
115 private static function createview($name, $structure, $verbose, $action)
120 foreach ($structure["fields"] as $fieldname => $origin) {
121 if (is_string($origin)) {
122 $sql_rows[] = $origin . " AS `" . DBA::escape($fieldname) . "`";
123 } elseif (is_array($origin) && (sizeof($origin) == 2)) {
124 $sql_rows[] = "`" . DBA::escape($origin[0]) . "`.`" . DBA::escape($origin[1]) . "` AS `" . DBA::escape($fieldname) . "`";
128 if (self::isView($name)) {
129 $sql = sprintf("DROP VIEW IF EXISTS `%s`", DBA::escape($name));
130 } elseif (self::isTable($name)) {
131 $sql = sprintf("DROP TABLE IF EXISTS `%s`", DBA::escape($name));
134 if (!empty($sql) && $verbose) {
138 if (!empty($sql) && $action) {
142 $sql = sprintf("CREATE VIEW `%s` AS SELECT \n\t", DBA::escape($name)) .
143 implode(",\n\t", $sql_rows) . "\n\t" . $structure['query'];
157 * Check if the given table/view is a view
159 * @param string $view
160 * @return boolean "true" if it's a view
162 private static function isView(string $view)
164 $status = DBA::selectFirst(['INFORMATION_SCHEMA' => 'TABLES'], ['TABLE_TYPE'],
165 ['TABLE_SCHEMA' => DBA::databaseName(), 'TABLE_NAME' => $view]);
167 if (empty($status['TABLE_TYPE'])) {
171 return $status['TABLE_TYPE'] == 'VIEW';
175 * Check if the given table/view is a table
177 * @param string $table
178 * @return boolean "true" if it's a table
180 private static function isTable(string $table)
182 $status = DBA::selectFirst(['INFORMATION_SCHEMA' => 'TABLES'], ['TABLE_TYPE'],
183 ['TABLE_SCHEMA' => DBA::databaseName(), 'TABLE_NAME' => $table]);
185 if (empty($status['TABLE_TYPE'])) {
189 return $status['TABLE_TYPE'] == 'BASE TABLE';