3 * @copyright Copyright (C) 2020, Friendica
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;
28 require_once __DIR__ . '/../../include/dba.php';
33 * view definition loaded from config/dbview.config.php
37 private static $definition = [];
40 * Loads the database structure definition from the config/dbview.config.php file.
41 * On first pass, defines DB_UPDATE_VERSION constant.
43 * @see static/dbview.config.php
44 * @param boolean $with_addons_structure Whether to tack on addons additional tables
45 * @param string $basePath The base path of this application
49 public static function definition($basePath = '', $with_addons_structure = true)
51 if (!self::$definition) {
52 if (empty($basePath)) {
53 $basePath = DI::app()->getBasePath();
56 $filename = $basePath . '/static/dbview.config.php';
58 if (!is_readable($filename)) {
59 throw new Exception('Missing database view config file static/dbview.config.php');
62 $definition = require $filename;
65 throw new Exception('Corrupted database view config file static/dbview.config.php');
68 self::$definition = $definition;
70 $definition = self::$definition;
73 if ($with_addons_structure) {
74 Hook::callAll('dbview_definition', $definition);
80 public static function create(bool $verbose, bool $action)
82 $definition = self::definition();
84 foreach ($definition as $name => $structure) {
85 self::createview($name, $structure, $verbose, $action);
89 public static function printStructure($basePath)
91 $database = self::definition($basePath, false);
93 foreach ($database AS $name => $structure) {
95 echo "-- VIEW $name\n";
97 self::createView($name, $structure, true, false);
103 private static function createview($name, $structure, $verbose, $action)
108 foreach ($structure["fields"] AS $fieldname => $origin) {
109 if (is_string($origin)) {
110 $sql_rows[] = $origin . " AS `" . DBA::escape($fieldname) . "`";
111 } elseif (is_array($origin) && (sizeof($origin) == 2)) {
112 $sql_rows[] = "`" . DBA::escape($origin[0]) . "`.`" . DBA::escape($origin[1]) . "` AS `" . DBA::escape($fieldname) . "`";
116 $sql = sprintf("DROP VIEW IF EXISTS `%s`", DBA::escape($name));
126 $sql = sprintf("CREATE VIEW `%s` AS SELECT \n\t", DBA::escape($name)) .
127 implode(",\n\t", $sql_rows) . "\n\t" . $structure['query'];