]> git.mxchange.org Git - friendica.git/blob - src/Database/View.php
DBView is View
[friendica.git] / src / Database / View.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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 Exception;
25 use Friendica\Core\Hook;
26 use Friendica\DI;
27
28 require_once __DIR__ . '/../../include/dba.php';
29
30 class View
31 {
32         /**
33          * view definition loaded from config/dbview.config.php
34          *
35          * @var array
36          */
37         private static $definition = [];
38
39         /**
40          * Loads the database structure definition from the config/dbview.config.php file.
41          * On first pass, defines DB_UPDATE_VERSION constant.
42          *
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
46          * @return array
47          * @throws Exception
48          */
49         public static function definition($basePath = '', $with_addons_structure = true)
50         {
51                 if (!self::$definition) {
52                         if (empty($basePath)) {
53                                 $basePath = DI::app()->getBasePath();
54                         }
55
56                         $filename = $basePath . '/static/dbview.config.php';
57
58                         if (!is_readable($filename)) {
59                                 throw new Exception('Missing database view config file static/dbview.config.php');
60                         }
61
62                         $definition = require $filename;
63
64                         if (!$definition) {
65                                 throw new Exception('Corrupted database view config file static/dbview.config.php');
66                         }
67
68                         self::$definition = $definition;
69                 } else {
70                         $definition = self::$definition;
71                 }
72
73                 if ($with_addons_structure) {
74                         Hook::callAll('dbview_definition', $definition);
75                 }
76
77                 return $definition;
78         }
79
80         public static function create(bool $verbose, bool $action)
81         {
82                 $definition = self::definition();
83
84                 foreach ($definition as $name => $structure) {
85                         self::createview($name, $structure, $verbose, $action);
86                 }
87         }
88
89         public static function printStructure($basePath)
90         {
91                 $database = self::definition($basePath, false);
92
93                 foreach ($database AS $name => $structure) {
94                         echo "--\n";
95                         echo "-- VIEW $name\n";
96                         echo "--\n";
97                         self::createView($name, $structure, true, false);
98
99                         echo "\n";
100                 }
101         }
102
103         private static function createview($name, $structure, $verbose, $action)
104         {
105                 $r = true;
106
107                 $sql_rows = [];
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) . "`";
113                         }
114                 }
115
116                 $sql = sprintf("DROP VIEW IF EXISTS `%s`", DBA::escape($name));
117
118                 if ($verbose) {
119                         echo $sql . ";\n";
120                 }
121
122                 if ($action) {
123                         DBA::e($sql);
124                 }
125
126                 $sql = sprintf("CREATE VIEW `%s` AS SELECT \n\t", DBA::escape($name)) .
127                         implode(",\n\t", $sql_rows) . "\n\t" . $structure['query'];
128         
129                 if ($verbose) {
130                         echo $sql . ";\n";
131                 }
132
133                 if ($action) {
134                         $r = DBA::e($sql);
135                 }
136
137                 return $r;
138         }
139 }