]> git.mxchange.org Git - friendica.git/blob - src/Database/DBStructure.php
Fix not working unarchive contact batch action
[friendica.git] / src / Database / DBStructure.php
1 <?php
2 /**
3  * @file src/Database/DBStructure.php
4  */
5 namespace Friendica\Database;
6
7 use Exception;
8 use Friendica\Core\Config;
9 use Friendica\Core\Hook;
10 use Friendica\Core\L10n;
11 use Friendica\Core\Logger;
12 use Friendica\Util\DateTimeFormat;
13
14 require_once 'boot.php';
15 require_once 'include/dba.php';
16 require_once 'include/enotify.php';
17 require_once 'include/text.php';
18
19 /**
20  * @brief This class contain functions for the database management
21  *
22  * This class contains functions that doesn't need to know if pdo, mysqli or whatever is used.
23  */
24 class DBStructure
25 {
26         const UPDATE_NOT_CHECKED = 0; // Database check wasn't executed before
27         const UPDATE_SUCCESSFUL  = 1; // Database check was successful
28         const UPDATE_FAILED      = 2; // Database check failed
29
30         /**
31          * Database structure definition loaded from config/dbstructure.php
32          *
33          * @var array
34          */
35         private static $definition = [];
36
37         /*
38          * Converts all tables from MyISAM to InnoDB
39          */
40         public static function convertToInnoDB() {
41                 $r = q("SELECT `TABLE_NAME` FROM `information_schema`.`tables` WHERE `engine` = 'MyISAM' AND `table_schema` = '%s'",
42                         DBA::escape(DBA::databaseName()));
43
44                 if (!DBA::isResult($r)) {
45                         echo L10n::t('There are no tables on MyISAM.')."\n";
46                         return;
47                 }
48
49                 foreach ($r AS $table) {
50                         $sql = sprintf("ALTER TABLE `%s` engine=InnoDB;", DBA::escape($table['TABLE_NAME']));
51                         echo $sql."\n";
52
53                         $result = DBA::e($sql);
54                         if (!DBA::isResult($result)) {
55                                 self::printUpdateError($sql);
56                         }
57                 }
58         }
59
60         private static function tableStructure($table) {
61                 $structures = q("DESCRIBE `%s`", $table);
62
63                 $full_columns = q("SHOW FULL COLUMNS FROM `%s`", $table);
64
65                 $indexes = q("SHOW INDEX FROM `%s`", $table);
66
67                 $table_status = q("SHOW TABLE STATUS WHERE `name` = '%s'", $table);
68
69                 if (DBA::isResult($table_status)) {
70                         $table_status = $table_status[0];
71                 } else {
72                         $table_status = [];
73                 }
74
75                 $fielddata = [];
76                 $indexdata = [];
77
78                 if (DBA::isResult($indexes)) {
79                         foreach ($indexes AS $index) {
80                                 if ($index['Key_name'] != 'PRIMARY' && $index['Non_unique'] == '0' && !isset($indexdata[$index["Key_name"]])) {
81                                         $indexdata[$index["Key_name"]] = ['UNIQUE'];
82                                 }
83
84                                 $column = $index["Column_name"];
85
86                                 if ($index["Sub_part"] != "") {
87                                         $column .= "(".$index["Sub_part"].")";
88                                 }
89
90                                 $indexdata[$index["Key_name"]][] = $column;
91                         }
92                 }
93                 if (DBA::isResult($structures)) {
94                         foreach ($structures AS $field) {
95                                 // Replace the default size values so that we don't have to define them
96                                 $search = ['tinyint(1)', 'tinyint(3) unsigned', 'tinyint(4)', 'smallint(5) unsigned', 'smallint(6)', 'mediumint(8) unsigned', 'mediumint(9)', 'bigint(20)', 'int(10) unsigned', 'int(11)'];
97                                 $replace = ['boolean', 'tinyint unsigned', 'tinyint', 'smallint unsigned', 'smallint', 'mediumint unsigned', 'mediumint', 'bigint', 'int unsigned', 'int'];
98                                 $field["Type"] = str_replace($search, $replace, $field["Type"]);
99
100                                 $fielddata[$field["Field"]]["type"] = $field["Type"];
101                                 if ($field["Null"] == "NO") {
102                                         $fielddata[$field["Field"]]["not null"] = true;
103                                 }
104
105                                 if (isset($field["Default"])) {
106                                         $fielddata[$field["Field"]]["default"] = $field["Default"];
107                                 }
108
109                                 if ($field["Extra"] != "") {
110                                         $fielddata[$field["Field"]]["extra"] = $field["Extra"];
111                                 }
112
113                                 if ($field["Key"] == "PRI") {
114                                         $fielddata[$field["Field"]]["primary"] = true;
115                                 }
116                         }
117                 }
118                 if (DBA::isResult($full_columns)) {
119                         foreach ($full_columns AS $column) {
120                                 $fielddata[$column["Field"]]["Collation"] = $column["Collation"];
121                                 $fielddata[$column["Field"]]["comment"] = $column["Comment"];
122                         }
123                 }
124
125                 return ["fields" => $fielddata, "indexes" => $indexdata, "table_status" => $table_status];
126         }
127
128         public static function printStructure() {
129                 $database = self::definition(false);
130
131                 echo "-- ------------------------------------------\n";
132                 echo "-- ".FRIENDICA_PLATFORM." ".FRIENDICA_VERSION." (".FRIENDICA_CODENAME,")\n";
133                 echo "-- DB_UPDATE_VERSION ".DB_UPDATE_VERSION."\n";
134                 echo "-- ------------------------------------------\n\n\n";
135                 foreach ($database AS $name => $structure) {
136                         echo "--\n";
137                         echo "-- TABLE $name\n";
138                         echo "--\n";
139                         self::createTable($name, $structure, true, false);
140
141                         echo "\n";
142                 }
143         }
144
145         /**
146          * @brief Print out database error messages
147          *
148          * @param string $message Message to be added to the error message
149          *
150          * @return string Error message
151          */
152         private static function printUpdateError($message) {
153                 echo L10n::t("\nError %d occurred during database update:\n%s\n",
154                         DBA::errorNo(), DBA::errorMessage());
155
156                 return L10n::t('Errors encountered performing database changes: ').$message.EOL;
157         }
158
159         /**
160          * Updates DB structure and returns eventual errors messages
161          *
162          * @param bool  $verbose
163          * @param bool  $action     Whether to actually apply the update
164          * @param bool  $install    Is this the initial update during the installation?
165          * @param array $tables     An array of the database tables
166          * @param array $definition An array of the definition tables
167          * @return string Empty string if the update is successful, error messages otherwise
168          */
169         public static function update($verbose, $action, $install = false, array $tables = null, array $definition = null) {
170                 if ($action && !$install) {
171                         Config::set('system', 'maintenance', 1);
172                         Config::set('system', 'maintenance_reason', L10n::t('%s: Database update', DateTimeFormat::utcNow().' '.date('e')));
173                 }
174
175                 $errors = '';
176
177                 Logger::log('updating structure', Logger::DEBUG);
178
179                 // Get the current structure
180                 $database = [];
181
182                 if (is_null($tables)) {
183                         $tables = q("SHOW TABLES");
184                 }
185
186                 if (DBA::isResult($tables)) {
187                         foreach ($tables AS $table) {
188                                 $table = current($table);
189
190                                 Logger::log(sprintf('updating structure for table %s ...', $table), Logger::DEBUG);
191                                 $database[$table] = self::tableStructure($table);
192                         }
193                 }
194
195                 // Get the definition
196                 if (is_null($definition)) {
197                         $definition = self::definition();
198                 }
199
200                 // MySQL >= 5.7.4 doesn't support the IGNORE keyword in ALTER TABLE statements
201                 if ((version_compare(DBA::serverInfo(), '5.7.4') >= 0) &&
202                         !(strpos(DBA::serverInfo(), 'MariaDB') !== false)) {
203                         $ignore = '';
204                 } else {
205                         $ignore = ' IGNORE';
206                 }
207
208                 // Compare it
209                 foreach ($definition AS $name => $structure) {
210                         $is_new_table = false;
211                         $group_by = "";
212                         $sql3 = "";
213                         $is_unique = false;
214                         $temp_name = $name;
215                         if (!isset($database[$name])) {
216                                 $r = self::createTable($name, $structure, $verbose, $action);
217                                 if (!DBA::isResult($r)) {
218                                         $errors .= self::printUpdateError($name);
219                                 }
220                                 $is_new_table = true;
221                         } else {
222                                 foreach ($structure["indexes"] AS $indexname => $fieldnames) {
223                                         if (isset($database[$name]["indexes"][$indexname])) {
224                                                 $current_index_definition = implode(",",$database[$name]["indexes"][$indexname]);
225                                         } else {
226                                                 $current_index_definition = "__NOT_SET__";
227                                         }
228                                         $new_index_definition = implode(",",$fieldnames);
229                                         if ($current_index_definition != $new_index_definition) {
230                                                 if ($fieldnames[0] == "UNIQUE") {
231                                                         $is_unique = true;
232                                                         if ($ignore == "") {
233                                                                 $temp_name = "temp-".$name;
234                                                         }
235                                                 }
236                                         }
237                                 }
238
239                                 /*
240                                  * Drop the index if it isn't present in the definition
241                                  * or the definition differ from current status
242                                  * and index name doesn't start with "local_"
243                                  */
244                                 foreach ($database[$name]["indexes"] as $indexname => $fieldnames) {
245                                         $current_index_definition = implode(",",$fieldnames);
246                                         if (isset($structure["indexes"][$indexname])) {
247                                                 $new_index_definition = implode(",",$structure["indexes"][$indexname]);
248                                         } else {
249                                                 $new_index_definition = "__NOT_SET__";
250                                         }
251                                         if ($current_index_definition != $new_index_definition && substr($indexname, 0, 6) != 'local_') {
252                                                 $sql2=self::dropIndex($indexname);
253                                                 if ($sql3 == "") {
254                                                         $sql3 = "ALTER".$ignore." TABLE `".$temp_name."` ".$sql2;
255                                                 } else {
256                                                         $sql3 .= ", ".$sql2;
257                                                 }
258                                         }
259                                 }
260                                 // Compare the field structure field by field
261                                 foreach ($structure["fields"] AS $fieldname => $parameters) {
262                                         if (!isset($database[$name]["fields"][$fieldname])) {
263                                                 $sql2=self::addTableField($fieldname, $parameters);
264                                                 if ($sql3 == "") {
265                                                         $sql3 = "ALTER" . $ignore . " TABLE `".$temp_name."` ".$sql2;
266                                                 } else {
267                                                         $sql3 .= ", ".$sql2;
268                                                 }
269                                         } else {
270                                                 // Compare the field definition
271                                                 $field_definition = $database[$name]["fields"][$fieldname];
272
273                                                 // Remove the relation data that is used for the referential integrity
274                                                 unset($parameters['relation']);
275
276                                                 // We change the collation after the indexes had been changed.
277                                                 // This is done to avoid index length problems.
278                                                 // So here we always ensure that there is no need to change it.
279                                                 unset($parameters['Collation']);
280                                                 unset($field_definition['Collation']);
281
282                                                 // Only update the comment when it is defined
283                                                 if (!isset($parameters['comment'])) {
284                                                         $parameters['comment'] = "";
285                                                 }
286
287                                                 $current_field_definition = DBA::cleanQuery(implode(",", $field_definition));
288                                                 $new_field_definition = DBA::cleanQuery(implode(",", $parameters));
289                                                 if ($current_field_definition != $new_field_definition) {
290                                                         $sql2 = self::modifyTableField($fieldname, $parameters);
291                                                         if ($sql3 == "") {
292                                                                 $sql3 = "ALTER" . $ignore . " TABLE `".$temp_name."` ".$sql2;
293                                                         } else {
294                                                                 $sql3 .= ", ".$sql2;
295                                                         }
296                                                 }
297                                         }
298                                 }
299                         }
300
301                         /*
302                          * Create the index if the index don't exists in database
303                          * or the definition differ from the current status.
304                          * Don't create keys if table is new
305                          */
306                         if (!$is_new_table) {
307                                 foreach ($structure["indexes"] AS $indexname => $fieldnames) {
308                                         if (isset($database[$name]["indexes"][$indexname])) {
309                                                 $current_index_definition = implode(",",$database[$name]["indexes"][$indexname]);
310                                         } else {
311                                                 $current_index_definition = "__NOT_SET__";
312                                         }
313                                         $new_index_definition = implode(",",$fieldnames);
314                                         if ($current_index_definition != $new_index_definition) {
315                                                 $sql2 = self::createIndex($indexname, $fieldnames);
316
317                                                 // Fetch the "group by" fields for unique indexes
318                                                 if ($fieldnames[0] == "UNIQUE") {
319                                                         $group_by = self::groupBy($indexname, $fieldnames);
320                                                 }
321                                                 if ($sql2 != "") {
322                                                         if ($sql3 == "") {
323                                                                 $sql3 = "ALTER" . $ignore . " TABLE `".$temp_name."` ".$sql2;
324                                                         } else {
325                                                                 $sql3 .= ", ".$sql2;
326                                                         }
327                                                 }
328                                         }
329                                 }
330
331                                 if (isset($database[$name]["table_status"]["Comment"])) {
332                                         if ($database[$name]["table_status"]["Comment"] != $structure['comment']) {
333                                                 $sql2 = "COMMENT = '".DBA::escape($structure['comment'])."'";
334
335                                                 if ($sql3 == "") {
336                                                         $sql3 = "ALTER" . $ignore . " TABLE `".$temp_name."` ".$sql2;
337                                                 } else {
338                                                         $sql3 .= ", ".$sql2;
339                                                 }
340                                         }
341                                 }
342
343                                 if (isset($database[$name]["table_status"]["Engine"]) && isset($structure['engine'])) {
344                                         if ($database[$name]["table_status"]["Engine"] != $structure['engine']) {
345                                                 $sql2 = "ENGINE = '".DBA::escape($structure['engine'])."'";
346
347                                                 if ($sql3 == "") {
348                                                         $sql3 = "ALTER" . $ignore . " TABLE `".$temp_name."` ".$sql2;
349                                                 } else {
350                                                         $sql3 .= ", ".$sql2;
351                                                 }
352                                         }
353                                 }
354
355                                 if (isset($database[$name]["table_status"]["Collation"])) {
356                                         if ($database[$name]["table_status"]["Collation"] != 'utf8mb4_general_ci') {
357                                                 $sql2 = "DEFAULT COLLATE utf8mb4_general_ci";
358
359                                                 if ($sql3 == "") {
360                                                         $sql3 = "ALTER" . $ignore . " TABLE `".$temp_name."` ".$sql2;
361                                                 } else {
362                                                         $sql3 .= ", ".$sql2;
363                                                 }
364                                         }
365                                 }
366
367                                 if ($sql3 != "") {
368                                         $sql3 .= "; ";
369                                 }
370
371                                 // Now have a look at the field collations
372                                 // Compare the field structure field by field
373                                 foreach ($structure["fields"] AS $fieldname => $parameters) {
374                                         // Compare the field definition
375                                         $field_definition = defaults($database[$name]["fields"], $fieldname, ['Collation' => '']);
376
377                                         // Define the default collation if not given
378                                         if (!isset($parameters['Collation']) && !empty($field_definition['Collation'])) {
379                                                 $parameters['Collation'] = 'utf8mb4_general_ci';
380                                         } else {
381                                                 $parameters['Collation'] = null;
382                                         }
383
384                                         if ($field_definition['Collation'] != $parameters['Collation']) {
385                                                 $sql2 = self::modifyTableField($fieldname, $parameters);
386                                                 if (($sql3 == "") || (substr($sql3, -2, 2) == "; ")) {
387                                                         $sql3 .= "ALTER" . $ignore . " TABLE `".$temp_name."` ".$sql2;
388                                                 } else {
389                                                         $sql3 .= ", ".$sql2;
390                                                 }
391                                         }
392                                 }
393                         }
394
395                         if ($sql3 != "") {
396                                 if (substr($sql3, -2, 2) != "; ") {
397                                         $sql3 .= ";";
398                                 }
399
400                                 $field_list = '';
401                                 if ($is_unique && $ignore == '') {
402                                         foreach ($database[$name]["fields"] AS $fieldname => $parameters) {
403                                                 $field_list .= 'ANY_VALUE(`' . $fieldname . '`),';
404                                         }
405                                         $field_list = rtrim($field_list, ',');
406                                 }
407
408                                 if ($verbose) {
409                                         // Ensure index conversion to unique removes duplicates
410                                         if ($is_unique && ($temp_name != $name)) {
411                                                 if ($ignore != "") {
412                                                         echo "SET session old_alter_table=1;\n";
413                                                 } else {
414                                                         echo "DROP TABLE IF EXISTS `".$temp_name."`;\n";
415                                                         echo "CREATE TABLE `".$temp_name."` LIKE `".$name."`;\n";
416                                                 }
417                                         }
418
419                                         echo $sql3."\n";
420
421                                         if ($is_unique && ($temp_name != $name)) {
422                                                 if ($ignore != "") {
423                                                         echo "SET session old_alter_table=0;\n";
424                                                 } else {
425                                                         echo "INSERT INTO `".$temp_name."` SELECT ".DBA::anyValueFallback($field_list)." FROM `".$name."`".$group_by.";\n";
426                                                         echo "DROP TABLE `".$name."`;\n";
427                                                         echo "RENAME TABLE `".$temp_name."` TO `".$name."`;\n";
428                                                 }
429                                         }
430                                 }
431
432                                 if ($action) {
433                                         if (!$install) {
434                                                 Config::set('system', 'maintenance_reason', L10n::t('%s: updating %s table.', DateTimeFormat::utcNow().' '.date('e'), $name));
435                                         }
436
437                                         // Ensure index conversion to unique removes duplicates
438                                         if ($is_unique && ($temp_name != $name)) {
439                                                 if ($ignore != "") {
440                                                         DBA::e("SET session old_alter_table=1;");
441                                                 } else {
442                                                         $r = DBA::e("DROP TABLE IF EXISTS `".$temp_name."`;");
443                                                         if (!DBA::isResult($r)) {
444                                                                 $errors .= self::printUpdateError($sql3);
445                                                                 return $errors;
446                                                         }
447
448                                                         $r = DBA::e("CREATE TABLE `".$temp_name."` LIKE `".$name."`;");
449                                                         if (!DBA::isResult($r)) {
450                                                                 $errors .= self::printUpdateError($sql3);
451                                                                 return $errors;
452                                                         }
453                                                 }
454                                         }
455
456                                         $r = DBA::e($sql3);
457                                         if (!DBA::isResult($r)) {
458                                                 $errors .= self::printUpdateError($sql3);
459                                         }
460                                         if ($is_unique && ($temp_name != $name)) {
461                                                 if ($ignore != "") {
462                                                         DBA::e("SET session old_alter_table=0;");
463                                                 } else {
464                                                         $r = DBA::e("INSERT INTO `".$temp_name."` SELECT ".$field_list." FROM `".$name."`".$group_by.";");
465                                                         if (!DBA::isResult($r)) {
466                                                                 $errors .= self::printUpdateError($sql3);
467                                                                 return $errors;
468                                                         }
469                                                         $r = DBA::e("DROP TABLE `".$name."`;");
470                                                         if (!DBA::isResult($r)) {
471                                                                 $errors .= self::printUpdateError($sql3);
472                                                                 return $errors;
473                                                         }
474                                                         $r = DBA::e("RENAME TABLE `".$temp_name."` TO `".$name."`;");
475                                                         if (!DBA::isResult($r)) {
476                                                                 $errors .= self::printUpdateError($sql3);
477                                                                 return $errors;
478                                                         }
479                                                 }
480                                         }
481                                 }
482                         }
483                 }
484
485                 if ($action && !$install) {
486                         Config::set('system', 'maintenance', 0);
487                         Config::set('system', 'maintenance_reason', '');
488
489                         if ($errors) {
490                                 Config::set('system', 'dbupdate', self::UPDATE_FAILED);
491                         } else {
492                                 Config::set('system', 'dbupdate', self::UPDATE_SUCCESSFUL);
493                         }
494                 }
495
496                 return $errors;
497         }
498
499         private static function FieldCommand($parameters, $create = true) {
500                 $fieldstruct = $parameters["type"];
501
502                 if (isset($parameters["Collation"])) {
503                         $fieldstruct .= " COLLATE ".$parameters["Collation"];
504                 }
505
506                 if (isset($parameters["not null"])) {
507                         $fieldstruct .= " NOT NULL";
508                 }
509
510                 if (isset($parameters["default"])) {
511                         if (strpos(strtolower($parameters["type"]),"int")!==false) {
512                                 $fieldstruct .= " DEFAULT ".$parameters["default"];
513                         } else {
514                                 $fieldstruct .= " DEFAULT '".$parameters["default"]."'";
515                         }
516                 }
517                 if (isset($parameters["extra"])) {
518                         $fieldstruct .= " ".$parameters["extra"];
519                 }
520
521                 if (isset($parameters["comment"])) {
522                         $fieldstruct .= " COMMENT '".DBA::escape($parameters["comment"])."'";
523                 }
524
525                 /*if (($parameters["primary"] != "") && $create)
526                         $fieldstruct .= " PRIMARY KEY";*/
527
528                 return($fieldstruct);
529         }
530
531         private static function createTable($name, $structure, $verbose, $action) {
532                 $r = true;
533
534                 $engine = "";
535                 $comment = "";
536                 $sql_rows = [];
537                 $primary_keys = [];
538                 foreach ($structure["fields"] AS $fieldname => $field) {
539                         $sql_rows[] = "`".DBA::escape($fieldname)."` ".self::FieldCommand($field);
540                         if (x($field,'primary') && $field['primary']!='') {
541                                 $primary_keys[] = $fieldname;
542                         }
543                 }
544
545                 if (!empty($structure["indexes"])) {
546                         foreach ($structure["indexes"] AS $indexname => $fieldnames) {
547                                 $sql_index = self::createIndex($indexname, $fieldnames, "");
548                                 if (!is_null($sql_index)) {
549                                         $sql_rows[] = $sql_index;
550                                 }
551                         }
552                 }
553
554                 if (isset($structure["engine"])) {
555                         $engine = " ENGINE=" . $structure["engine"];
556                 }
557
558                 if (isset($structure["comment"])) {
559                         $comment = " COMMENT='" . DBA::escape($structure["comment"]) . "'";
560                 }
561
562                 $sql = implode(",\n\t", $sql_rows);
563
564                 $sql = sprintf("CREATE TABLE IF NOT EXISTS `%s` (\n\t", DBA::escape($name)).$sql.
565                                 "\n)" . $engine . " DEFAULT COLLATE utf8mb4_general_ci" . $comment;
566                 if ($verbose) {
567                         echo $sql.";\n";
568                 }
569
570                 if ($action) {
571                         $r = DBA::e($sql);
572                 }
573
574                 return $r;
575         }
576
577         private static function addTableField($fieldname, $parameters) {
578                 $sql = sprintf("ADD `%s` %s", DBA::escape($fieldname), self::FieldCommand($parameters));
579                 return($sql);
580         }
581
582         private static function modifyTableField($fieldname, $parameters) {
583                 $sql = sprintf("MODIFY `%s` %s", DBA::escape($fieldname), self::FieldCommand($parameters, false));
584                 return($sql);
585         }
586
587         private static function dropIndex($indexname) {
588                 $sql = sprintf("DROP INDEX `%s`", DBA::escape($indexname));
589                 return($sql);
590         }
591
592         private static function createIndex($indexname, $fieldnames, $method = "ADD") {
593                 $method = strtoupper(trim($method));
594                 if ($method!="" && $method!="ADD") {
595                         throw new Exception("Invalid parameter 'method' in self::createIndex(): '$method'");
596                 }
597
598                 if ($fieldnames[0] == "UNIQUE") {
599                         array_shift($fieldnames);
600                         $method .= ' UNIQUE';
601                 }
602
603                 $names = "";
604                 foreach ($fieldnames AS $fieldname) {
605                         if ($names != "") {
606                                 $names .= ",";
607                         }
608
609                         if (preg_match('|(.+)\((\d+)\)|', $fieldname, $matches)) {
610                                 $names .= "`".DBA::escape($matches[1])."`(".intval($matches[2]).")";
611                         } else {
612                                 $names .= "`".DBA::escape($fieldname)."`";
613                         }
614                 }
615
616                 if ($indexname == "PRIMARY") {
617                         return sprintf("%s PRIMARY KEY(%s)", $method, $names);
618                 }
619
620
621                 $sql = sprintf("%s INDEX `%s` (%s)", $method, DBA::escape($indexname), $names);
622                 return($sql);
623         }
624
625         private static function groupBy($indexname, $fieldnames) {
626                 if ($fieldnames[0] != "UNIQUE") {
627                         return "";
628                 }
629
630                 array_shift($fieldnames);
631
632                 $names = "";
633                 foreach ($fieldnames AS $fieldname) {
634                         if ($names != "") {
635                                 $names .= ",";
636                         }
637
638                         if (preg_match('|(.+)\((\d+)\)|', $fieldname, $matches)) {
639                                 $names .= "`".DBA::escape($matches[1])."`";
640                         } else {
641                                 $names .= "`".DBA::escape($fieldname)."`";
642                         }
643                 }
644
645                 $sql = sprintf(" GROUP BY %s", $names);
646                 return $sql;
647         }
648
649         /**
650          *      Check if a table exists
651          *
652          * @param string $table Table name
653          *
654          * @return boolean Does the table exist?
655          */
656         public static function existsTable($table)
657         {
658                 if (empty($table)) {
659                         return false;
660                 }
661
662                 $table = DBA::escape($table);
663
664                 $sql = "SHOW TABLES LIKE '" . $table . "';";
665
666                 $stmt = DBA::p($sql);
667
668                 if (is_bool($stmt)) {
669                         $retval = $stmt;
670                 } else {
671                         $retval = (DBA::numRows($stmt) > 0);
672                 }
673
674                 DBA::close($stmt);
675
676                 return $retval;
677         }
678
679         /**
680          *      Check if the columns of the table exists
681          *
682          * @param string $table   Table name
683          * @param array  $columns Columns to check ( Syntax: [ $col1, $col2, .. ] )
684          *
685          * @return boolean Does the table exist?
686          */
687         public static function existsColumn($table, $columns = []) {
688                 if (empty($table)) {
689                         return false;
690                 }
691
692                 if (is_null($columns) || empty($columns)) {
693                         return self::existsTable($table);
694                 }
695
696                 $table = DBA::escape($table);
697
698                 foreach ($columns AS $column) {
699                         $sql = "SHOW COLUMNS FROM `" . $table . "` LIKE '" . $column . "';";
700
701                         $stmt = DBA::p($sql);
702
703                         if (is_bool($stmt)) {
704                                 $retval = $stmt;
705                         } else {
706                                 $retval = (DBA::numRows($stmt) > 0);
707                         }
708
709                         DBA::close($stmt);
710
711                         if (!$retval) {
712                                 return false;
713                         }
714                 }
715
716                 return true;
717         }
718
719         const RENAME_COLUMN = 0;
720         const RENAME_PRIMARY_KEY = 1;
721
722         /**
723          * Renames columns or the primary key of a table
724          * @todo You cannot rename a primary key if "auto increment" is set
725          *
726          * @param string $table    Table name
727          * @param array  $columns  Columns Syntax for Rename: [ $old1 => [ $new1, $type1 ], $old2 => [ $new2, $type2 ], ... ] )
728          *                                 Syntax for Primary Key: [ $col1, $col2, ...] )
729          * @param int    $type     The type of renaming (Default is Column)
730          *
731          * @return boolean Was the renaming successful?
732          *
733          */
734         public static function rename($table, $columns, $type = self::RENAME_COLUMN) {
735                 if (empty($table) || empty($columns)) {
736                         return false;
737                 }
738
739                 if (!is_array($columns)) {
740                         return false;
741                 }
742
743                 $table = DBA::escape($table);
744
745                 $sql = "ALTER TABLE `" . $table . "`";
746                 switch ($type) {
747                         case self::RENAME_COLUMN:
748                                 if (!self::existsColumn($table, array_keys($columns))) {
749                                         return false;
750                                 }
751                                 $sql .= implode(',', array_map(
752                                         function ($to, $from) {
753                                                 return " CHANGE `" . $from . "` `" . $to[0] . "` " . $to[1];
754                                         },
755                                         $columns,
756                                         array_keys($columns)
757                                 ));
758                                 break;
759                         case self::RENAME_PRIMARY_KEY:
760                                 if (!self::existsColumn($table, $columns)) {
761                                         return false;
762                                 }
763                                 $sql .= " DROP PRIMARY KEY, ADD PRIMARY KEY(`" . implode('`, `', $columns) . "`)";
764                                 break;
765                         default:
766                                 return false;
767                 }
768
769                 $sql .= ";";
770
771                 $stmt = DBA::p($sql);
772
773                 if (is_bool($stmt)) {
774                         $retval = $stmt;
775                 } else {
776                         $retval = true;
777                 }
778
779                 DBA::close($stmt);
780
781                 return $retval;
782         }
783
784         /**
785          * Loads the database structure definition from the config/dbstructure.php file.
786          * On first pass, defines DB_UPDATE_VERSION constant.
787          *
788          * @see config/dbstructure.php
789          * @param boolean $with_addons_structure Whether to tack on addons additional tables
790          * @return array
791          * @throws Exception
792          */
793         public static function definition($with_addons_structure = true)
794         {
795                 if (!self::$definition) {
796                         $a = \Friendica\BaseObject::getApp();
797
798                         $filename = $a->getBasePath() . '/config/dbstructure.php';
799
800                         if (!is_readable($filename)) {
801                                 throw new Exception('Missing database structure config file config/dbstructure.php');
802                         }
803
804                         $definition = require $filename;
805
806                         if (!$definition) {
807                                 throw new Exception('Corrupted database structure config file config/dbstructure.php');
808                         }
809
810                         self::$definition = $definition;
811                 } else {
812                         $definition = self::$definition;
813                 }
814
815                 if ($with_addons_structure) {
816                         Hook::callAll('dbstructure_definition', $definition);
817                 }
818
819                 return $definition;
820         }
821 }