]> git.mxchange.org Git - friendica.git/blob - src/Database/DBStructure.php
Merge remote-tracking branch 'upstream/develop' into item-view
[friendica.git] / src / Database / DBStructure.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\Core\Logger;
27 use Friendica\DI;
28 use Friendica\Model\Item;
29 use Friendica\Model\User;
30 use Friendica\Util\DateTimeFormat;
31
32 /**
33  * This class contains functions that doesn't need to know if pdo, mysqli or whatever is used.
34  */
35 class DBStructure
36 {
37         const UPDATE_NOT_CHECKED = 0; // Database check wasn't executed before
38         const UPDATE_SUCCESSFUL  = 1; // Database check was successful
39         const UPDATE_FAILED      = 2; // Database check failed
40
41         const RENAME_COLUMN      = 0;
42         const RENAME_PRIMARY_KEY = 1;
43
44         /**
45          * Database structure definition loaded from config/dbstructure.config.php
46          *
47          * @var array
48          */
49         private static $definition = [];
50
51         /**
52          * Set a database version to trigger update functions
53          *
54          * @param string $version
55          * @return void
56          */
57         public static function setDatabaseVersion(string $version)
58         {
59                 if (!is_numeric($version)) {
60                         throw new \Asika\SimpleConsole\CommandArgsException('The version number must be numeric');
61                 }
62
63                 DI::config()->set('system', 'build', $version);
64                 echo DI::l10n()->t('The database version had been set to %s.', $version);
65         }
66
67         /**
68          * Drop unused tables
69          *
70          * @param boolean $execute
71          * @return void
72          */
73         public static function dropTables(bool $execute)
74         {
75                 $old_tables = ['fserver', 'gcign', 'gcontact', 'gcontact-relation', 'gfollower' ,'glink', 'item-delivery-data',
76                 'item_id', 'poll', 'poll_result', 'queue', 'retriever_rule', 'sign', 'spam', 'term'];
77
78                 $tables = DBA::selectToArray(['INFORMATION_SCHEMA' => 'TABLES'], ['TABLE_NAME'],
79                         ['TABLE_SCHEMA' => DBA::databaseName(), 'TABLE_TYPE' => 'BASE TABLE']);
80
81                 if (empty($tables)) {
82                         echo DI::l10n()->t('No unused tables found.');
83                         return;
84                 }
85
86                 if (!$execute) {
87                         echo DI::l10n()->t('These tables are not used for friendica and will be deleted when you execute "dbstructure drop -e":') . "\n\n";
88                 }
89
90                 foreach ($tables as $table) {
91                         if (in_array($table['TABLE_NAME'], $old_tables)) {
92                                 if ($execute) {
93                                         $sql = 'DROP TABLE ' . DBA::quoteIdentifier($table['TABLE_NAME']) . ';';
94                                         echo $sql . "\n";
95
96                                         $result = DBA::e($sql);
97                                         if (!DBA::isResult($result)) {
98                                                 self::printUpdateError($sql);
99                                         }
100                                 } else {
101                                         echo $table['TABLE_NAME'] . "\n";
102                                 }
103                         }
104                 }
105         }
106
107         /**
108          * Converts all tables from MyISAM/InnoDB Antelope to InnoDB Barracuda
109          */
110         public static function convertToInnoDB()
111         {
112                 $tables = DBA::selectToArray(
113                         ['information_schema' => 'tables'],
114                         ['table_name'],
115                         ['engine' => 'MyISAM', 'table_schema' => DBA::databaseName()]
116                 );
117
118                 $tables = array_merge($tables, DBA::selectToArray(
119                         ['information_schema' => 'tables'],
120                         ['table_name'],
121                         ['engine' => 'InnoDB', 'ROW_FORMAT' => ['COMPACT', 'REDUNDANT'], 'table_schema' => DBA::databaseName()]
122                 ));
123
124                 if (!DBA::isResult($tables)) {
125                         echo DI::l10n()->t('There are no tables on MyISAM or InnoDB with the Antelope file format.') . "\n";
126                         return;
127                 }
128
129                 foreach ($tables AS $table) {
130                         $sql = "ALTER TABLE " . DBA::quoteIdentifier($table['table_name']) . " ENGINE=InnoDB ROW_FORMAT=DYNAMIC;";
131                         echo $sql . "\n";
132
133                         $result = DBA::e($sql);
134                         if (!DBA::isResult($result)) {
135                                 self::printUpdateError($sql);
136                         }
137                 }
138         }
139
140         /**
141          * Print out database error messages
142          *
143          * @param string $message Message to be added to the error message
144          *
145          * @return string Error message
146          */
147         private static function printUpdateError($message)
148         {
149                 echo DI::l10n()->t("\nError %d occurred during database update:\n%s\n",
150                         DBA::errorNo(), DBA::errorMessage());
151
152                 return DI::l10n()->t('Errors encountered performing database changes: ') . $message . EOL;
153         }
154
155         public static function printStructure($basePath)
156         {
157                 $database = self::definition($basePath, false);
158
159                 echo "-- ------------------------------------------\n";
160                 echo "-- " . FRIENDICA_PLATFORM . " " . FRIENDICA_VERSION . " (" . FRIENDICA_CODENAME, ")\n";
161                 echo "-- DB_UPDATE_VERSION " . DB_UPDATE_VERSION . "\n";
162                 echo "-- ------------------------------------------\n\n\n";
163                 foreach ($database AS $name => $structure) {
164                         echo "--\n";
165                         echo "-- TABLE $name\n";
166                         echo "--\n";
167                         self::createTable($name, $structure, true, false);
168
169                         echo "\n";
170                 }
171
172                 View::printStructure($basePath);
173         }
174
175         /**
176          * Loads the database structure definition from the static/dbstructure.config.php file.
177          * On first pass, defines DB_UPDATE_VERSION constant.
178          *
179          * @see static/dbstructure.config.php
180          * @param boolean $with_addons_structure Whether to tack on addons additional tables
181          * @param string  $basePath              The base path of this application
182          * @return array
183          * @throws Exception
184          */
185         public static function definition($basePath, $with_addons_structure = true)
186         {
187                 if (!self::$definition) {
188
189                         $filename = $basePath . '/static/dbstructure.config.php';
190
191                         if (!is_readable($filename)) {
192                                 throw new Exception('Missing database structure config file static/dbstructure.config.php');
193                         }
194
195                         $definition = require $filename;
196
197                         if (!$definition) {
198                                 throw new Exception('Corrupted database structure config file static/dbstructure.config.php');
199                         }
200
201                         self::$definition = $definition;
202                 } else {
203                         $definition = self::$definition;
204                 }
205
206                 if ($with_addons_structure) {
207                         Hook::callAll('dbstructure_definition', $definition);
208                 }
209
210                 return $definition;
211         }
212
213         /**
214          * Get field data for the given table
215          *
216          * @param string $table
217          * @param array $data data fields
218          * @return array fields for the given
219          */
220         public static function getFieldsForTable(string $table, array $data = [])
221         {
222                 $definition = DBStructure::definition('', false);
223                 if (empty($definition[$table])) {
224                         return [];
225                 }
226
227                 $fieldnames = array_keys($definition[$table]['fields']);
228
229                 $fields = [];
230
231                 // Assign all field that are present in the table
232                 foreach ($fieldnames as $field) {
233                         if (isset($data[$field])) {
234                                 $fields[$field] = $data[$field];
235                         }
236                 }
237
238                 return $fields;
239         }
240
241         private static function createTable($name, $structure, $verbose, $action)
242         {
243                 $r = true;
244
245                 $engine = "";
246                 $comment = "";
247                 $sql_rows = [];
248                 $primary_keys = [];
249                 $foreign_keys = [];
250
251                 foreach ($structure["fields"] AS $fieldname => $field) {
252                         $sql_rows[] = "`" . DBA::escape($fieldname) . "` " . self::FieldCommand($field);
253                         if (!empty($field['primary'])) {
254                                 $primary_keys[] = $fieldname;
255                         }
256                         if (!empty($field['foreign'])) {
257                                 $foreign_keys[$fieldname] = $field;
258                         }
259                 }
260
261                 if (!empty($structure["indexes"])) {
262                         foreach ($structure["indexes"] AS $indexname => $fieldnames) {
263                                 $sql_index = self::createIndex($indexname, $fieldnames, "");
264                                 if (!is_null($sql_index)) {
265                                         $sql_rows[] = $sql_index;
266                                 }
267                         }
268                 }
269
270                 foreach ($foreign_keys AS $fieldname => $parameters) {
271                         $sql_rows[] = self::foreignCommand($name, $fieldname, $parameters);
272                 }
273
274                 if (isset($structure["engine"])) {
275                         $engine = " ENGINE=" . $structure["engine"];
276                 }
277
278                 if (isset($structure["comment"])) {
279                         $comment = " COMMENT='" . DBA::escape($structure["comment"]) . "'";
280                 }
281
282                 $sql = implode(",\n\t", $sql_rows);
283
284                 $sql = sprintf("CREATE TABLE IF NOT EXISTS `%s` (\n\t", DBA::escape($name)) . $sql .
285                         "\n)" . $engine . " DEFAULT COLLATE utf8mb4_general_ci" . $comment;
286                 if ($verbose) {
287                         echo $sql . ";\n";
288                 }
289
290                 if ($action) {
291                         $r = DBA::e($sql);
292                 }
293
294                 return $r;
295         }
296
297         private static function FieldCommand($parameters, $create = true)
298         {
299                 $fieldstruct = $parameters["type"];
300
301                 if (isset($parameters["Collation"])) {
302                         $fieldstruct .= " COLLATE " . $parameters["Collation"];
303                 }
304
305                 if (isset($parameters["not null"])) {
306                         $fieldstruct .= " NOT NULL";
307                 }
308
309                 if (isset($parameters["default"])) {
310                         if (strpos(strtolower($parameters["type"]), "int") !== false) {
311                                 $fieldstruct .= " DEFAULT " . $parameters["default"];
312                         } else {
313                                 $fieldstruct .= " DEFAULT '" . $parameters["default"] . "'";
314                         }
315                 }
316                 if (isset($parameters["extra"])) {
317                         $fieldstruct .= " " . $parameters["extra"];
318                 }
319
320                 if (isset($parameters["comment"])) {
321                         $fieldstruct .= " COMMENT '" . DBA::escape($parameters["comment"]) . "'";
322                 }
323
324                 /*if (($parameters["primary"] != "") && $create)
325                         $fieldstruct .= " PRIMARY KEY";*/
326
327                 return ($fieldstruct);
328         }
329
330         private static function createIndex($indexname, $fieldnames, $method = "ADD")
331         {
332                 $method = strtoupper(trim($method));
333                 if ($method != "" && $method != "ADD") {
334                         throw new Exception("Invalid parameter 'method' in self::createIndex(): '$method'");
335                 }
336
337                 if (in_array($fieldnames[0], ["UNIQUE", "FULLTEXT"])) {
338                         $index_type = array_shift($fieldnames);
339                         $method .= " " . $index_type;
340                 }
341
342                 $names = "";
343                 foreach ($fieldnames AS $fieldname) {
344                         if ($names != "") {
345                                 $names .= ",";
346                         }
347
348                         if (preg_match('|(.+)\((\d+)\)|', $fieldname, $matches)) {
349                                 $names .= "`" . DBA::escape($matches[1]) . "`(" . intval($matches[2]) . ")";
350                         } else {
351                                 $names .= "`" . DBA::escape($fieldname) . "`";
352                         }
353                 }
354
355                 if ($indexname == "PRIMARY") {
356                         return sprintf("%s PRIMARY KEY(%s)", $method, $names);
357                 }
358
359
360                 $sql = sprintf("%s INDEX `%s` (%s)", $method, DBA::escape($indexname), $names);
361                 return ($sql);
362         }
363
364         /**
365          * Updates DB structure and returns eventual errors messages
366          *
367          * @param string $basePath   The base path of this application
368          * @param bool   $verbose
369          * @param bool   $action     Whether to actually apply the update
370          * @param bool   $install    Is this the initial update during the installation?
371          * @param array  $tables     An array of the database tables
372          * @param array  $definition An array of the definition tables
373          * @return string Empty string if the update is successful, error messages otherwise
374          * @throws Exception
375          */
376         public static function update($basePath, $verbose, $action, $install = false, array $tables = null, array $definition = null)
377         {
378                 if ($action && !$install) {
379                         if (self::isUpdating()) {
380                                 return DI::l10n()->t('Another database update is currently running.');
381                         }
382
383                         DI::config()->set('system', 'maintenance', 1);
384                         DI::config()->set('system', 'maintenance_reason', DI::l10n()->t('%s: Database update', DateTimeFormat::utcNow() . ' ' . date('e')));
385                 }
386
387                 // ensure that all initial values exist. This test has to be done prior and after the structure check.
388                 // Prior is needed if the specific tables already exists - after is needed when they had been created.
389                 self::checkInitialValues();
390
391                 $errors = '';
392
393                 Logger::info('updating structure');
394
395                 // Get the current structure
396                 $database = [];
397
398                 if (is_null($tables)) {
399                         $tables = DBA::toArray(DBA::p("SHOW TABLES"));
400                 }
401
402                 if (DBA::isResult($tables)) {
403                         foreach ($tables AS $table) {
404                                 $table = current($table);
405
406                                 Logger::info('updating structure', ['table' => $table]);
407                                 $database[$table] = self::tableStructure($table);
408                         }
409                 }
410
411                 // Get the definition
412                 if (is_null($definition)) {
413                         $definition = self::definition($basePath);
414                 }
415
416                 // MySQL >= 5.7.4 doesn't support the IGNORE keyword in ALTER TABLE statements
417                 if ((version_compare(DBA::serverInfo(), '5.7.4') >= 0) &&
418                         !(strpos(DBA::serverInfo(), 'MariaDB') !== false)) {
419                         $ignore = '';
420                 } else {
421                         $ignore = ' IGNORE';
422                 }
423
424                 // Compare it
425                 foreach ($definition AS $name => $structure) {
426                         $is_new_table = false;
427                         $group_by = "";
428                         $sql3 = "";
429                         $is_unique = false;
430                         $temp_name = $name;
431                         if (!isset($database[$name])) {
432                                 $r = self::createTable($name, $structure, $verbose, $action);
433                                 if (!DBA::isResult($r)) {
434                                         $errors .= self::printUpdateError($name);
435                                 }
436                                 $is_new_table = true;
437                         } else {
438                                 foreach ($structure["indexes"] AS $indexname => $fieldnames) {
439                                         if (isset($database[$name]["indexes"][$indexname])) {
440                                                 $current_index_definition = implode(",", $database[$name]["indexes"][$indexname]);
441                                         } else {
442                                                 $current_index_definition = "__NOT_SET__";
443                                         }
444                                         $new_index_definition = implode(",", $fieldnames);
445                                         if ($current_index_definition != $new_index_definition) {
446                                                 if ($fieldnames[0] == "UNIQUE") {
447                                                         $is_unique = true;
448                                                         if ($ignore == "") {
449                                                                 $temp_name = "temp-" . $name;
450                                                         }
451                                                 }
452                                         }
453                                 }
454
455                                 /*
456                                  * Drop the index if it isn't present in the definition
457                                  * or the definition differ from current status
458                                  * and index name doesn't start with "local_"
459                                  */
460                                 foreach ($database[$name]["indexes"] as $indexname => $fieldnames) {
461                                         $current_index_definition = implode(",", $fieldnames);
462                                         if (isset($structure["indexes"][$indexname])) {
463                                                 $new_index_definition = implode(",", $structure["indexes"][$indexname]);
464                                         } else {
465                                                 $new_index_definition = "__NOT_SET__";
466                                         }
467                                         if ($current_index_definition != $new_index_definition && substr($indexname, 0, 6) != 'local_') {
468                                                 $sql2 = self::dropIndex($indexname);
469                                                 if ($sql3 == "") {
470                                                         $sql3 = "ALTER" . $ignore . " TABLE `" . $temp_name . "` " . $sql2;
471                                                 } else {
472                                                         $sql3 .= ", " . $sql2;
473                                                 }
474                                         }
475                                 }
476                                 // Compare the field structure field by field
477                                 foreach ($structure["fields"] AS $fieldname => $parameters) {
478                                         if (!isset($database[$name]["fields"][$fieldname])) {
479                                                 $sql2 = self::addTableField($fieldname, $parameters);
480                                                 if ($sql3 == "") {
481                                                         $sql3 = "ALTER" . $ignore . " TABLE `" . $temp_name . "` " . $sql2;
482                                                 } else {
483                                                         $sql3 .= ", " . $sql2;
484                                                 }
485                                         } else {
486                                                 // Compare the field definition
487                                                 $field_definition = $database[$name]["fields"][$fieldname];
488
489                                                 // Remove the relation data that is used for the referential integrity
490                                                 unset($parameters['relation']);
491                                                 unset($parameters['foreign']);
492
493                                                 // We change the collation after the indexes had been changed.
494                                                 // This is done to avoid index length problems.
495                                                 // So here we always ensure that there is no need to change it.
496                                                 unset($parameters['Collation']);
497                                                 unset($field_definition['Collation']);
498
499                                                 // Only update the comment when it is defined
500                                                 if (!isset($parameters['comment'])) {
501                                                         $parameters['comment'] = "";
502                                                 }
503
504                                                 $current_field_definition = DBA::cleanQuery(implode(",", $field_definition));
505                                                 $new_field_definition = DBA::cleanQuery(implode(",", $parameters));
506                                                 if ($current_field_definition != $new_field_definition) {
507                                                         $sql2 = self::modifyTableField($fieldname, $parameters);
508                                                         if ($sql3 == "") {
509                                                                 $sql3 = "ALTER" . $ignore . " TABLE `" . $temp_name . "` " . $sql2;
510                                                         } else {
511                                                                 $sql3 .= ", " . $sql2;
512                                                         }
513                                                 }
514                                         }
515                                 }
516                         }
517
518                         /*
519                          * Create the index if the index don't exists in database
520                          * or the definition differ from the current status.
521                          * Don't create keys if table is new
522                          */
523                         if (!$is_new_table) {
524                                 foreach ($structure["indexes"] AS $indexname => $fieldnames) {
525                                         if (isset($database[$name]["indexes"][$indexname])) {
526                                                 $current_index_definition = implode(",", $database[$name]["indexes"][$indexname]);
527                                         } else {
528                                                 $current_index_definition = "__NOT_SET__";
529                                         }
530                                         $new_index_definition = implode(",", $fieldnames);
531                                         if ($current_index_definition != $new_index_definition) {
532                                                 $sql2 = self::createIndex($indexname, $fieldnames);
533
534                                                 // Fetch the "group by" fields for unique indexes
535                                                 $group_by = self::groupBy($fieldnames);
536                                                 if ($sql2 != "") {
537                                                         if ($sql3 == "") {
538                                                                 $sql3 = "ALTER" . $ignore . " TABLE `" . $temp_name . "` " . $sql2;
539                                                         } else {
540                                                                 $sql3 .= ", " . $sql2;
541                                                         }
542                                                 }
543                                         }
544                                 }
545
546                                 $existing_foreign_keys = $database[$name]['foreign_keys'];
547
548                                 // Foreign keys
549                                 // Compare the field structure field by field
550                                 foreach ($structure["fields"] AS $fieldname => $parameters) {
551                                         if (empty($parameters['foreign'])) {
552                                                 continue;
553                                         }
554
555                                         $constraint = self::getConstraintName($name, $fieldname, $parameters);
556
557                                         unset($existing_foreign_keys[$constraint]);
558
559                                         if (empty($database[$name]['foreign_keys'][$constraint])) {
560                                                 $sql2 = self::addForeignKey($name, $fieldname, $parameters);
561
562                                                 if ($sql3 == "") {
563                                                         $sql3 = "ALTER" . $ignore . " TABLE `" . $temp_name . "` " . $sql2;
564                                                 } else {
565                                                         $sql3 .= ", " . $sql2;
566                                                 }
567                                         }
568                                 }
569
570                                 foreach ($existing_foreign_keys as $param) {
571                                         $sql2 = self::dropForeignKey($param['CONSTRAINT_NAME']);
572
573                                         if ($sql3 == "") {
574                                                 $sql3 = "ALTER" . $ignore . " TABLE `" . $temp_name . "` " . $sql2;
575                                         } else {
576                                                 $sql3 .= ", " . $sql2;
577                                         }
578                                 }
579
580                                 if (isset($database[$name]["table_status"]["TABLE_COMMENT"])) {
581                                         $structurecomment = $structure["comment"] ?? '';
582                                         if ($database[$name]["table_status"]["TABLE_COMMENT"] != $structurecomment) {
583                                                 $sql2 = "COMMENT = '" . DBA::escape($structurecomment) . "'";
584
585                                                 if ($sql3 == "") {
586                                                         $sql3 = "ALTER" . $ignore . " TABLE `" . $temp_name . "` " . $sql2;
587                                                 } else {
588                                                         $sql3 .= ", " . $sql2;
589                                                 }
590                                         }
591                                 }
592
593                                 if (isset($database[$name]["table_status"]["ENGINE"]) && isset($structure['engine'])) {
594                                         if ($database[$name]["table_status"]["ENGINE"] != $structure['engine']) {
595                                                 $sql2 = "ENGINE = '" . DBA::escape($structure['engine']) . "'";
596
597                                                 if ($sql3 == "") {
598                                                         $sql3 = "ALTER" . $ignore . " TABLE `" . $temp_name . "` " . $sql2;
599                                                 } else {
600                                                         $sql3 .= ", " . $sql2;
601                                                 }
602                                         }
603                                 }
604
605                                 if (isset($database[$name]["table_status"]["TABLE_COLLATION"])) {
606                                         if ($database[$name]["table_status"]["TABLE_COLLATION"] != 'utf8mb4_general_ci') {
607                                                 $sql2 = "DEFAULT COLLATE utf8mb4_general_ci";
608
609                                                 if ($sql3 == "") {
610                                                         $sql3 = "ALTER" . $ignore . " TABLE `" . $temp_name . "` " . $sql2;
611                                                 } else {
612                                                         $sql3 .= ", " . $sql2;
613                                                 }
614                                         }
615                                 }
616
617                                 if ($sql3 != "") {
618                                         $sql3 .= "; ";
619                                 }
620
621                                 // Now have a look at the field collations
622                                 // Compare the field structure field by field
623                                 foreach ($structure["fields"] AS $fieldname => $parameters) {
624                                         // Compare the field definition
625                                         $field_definition = ($database[$name]["fields"][$fieldname] ?? '') ?: ['Collation' => ''];
626
627                                         // Define the default collation if not given
628                                         if (!isset($parameters['Collation']) && !empty($field_definition['Collation'])) {
629                                                 $parameters['Collation'] = 'utf8mb4_general_ci';
630                                         } else {
631                                                 $parameters['Collation'] = null;
632                                         }
633
634                                         if ($field_definition['Collation'] != $parameters['Collation']) {
635                                                 $sql2 = self::modifyTableField($fieldname, $parameters);
636                                                 if (($sql3 == "") || (substr($sql3, -2, 2) == "; ")) {
637                                                         $sql3 .= "ALTER" . $ignore . " TABLE `" . $temp_name . "` " . $sql2;
638                                                 } else {
639                                                         $sql3 .= ", " . $sql2;
640                                                 }
641                                         }
642                                 }
643                         }
644
645                         if ($sql3 != "") {
646                                 if (substr($sql3, -2, 2) != "; ") {
647                                         $sql3 .= ";";
648                                 }
649
650                                 $field_list = '';
651                                 if ($is_unique && $ignore == '') {
652                                         foreach ($database[$name]["fields"] AS $fieldname => $parameters) {
653                                                 $field_list .= 'ANY_VALUE(`' . $fieldname . '`),';
654                                         }
655                                         $field_list = rtrim($field_list, ',');
656                                 }
657
658                                 if ($verbose) {
659                                         // Ensure index conversion to unique removes duplicates
660                                         if ($is_unique && ($temp_name != $name)) {
661                                                 if ($ignore != "") {
662                                                         echo "SET session old_alter_table=1;\n";
663                                                 } else {
664                                                         echo "DROP TABLE IF EXISTS `" . $temp_name . "`;\n";
665                                                         echo "CREATE TABLE `" . $temp_name . "` LIKE `" . $name . "`;\n";
666                                                 }
667                                         }
668
669                                         echo $sql3 . "\n";
670
671                                         if ($is_unique && ($temp_name != $name)) {
672                                                 if ($ignore != "") {
673                                                         echo "SET session old_alter_table=0;\n";
674                                                 } else {
675                                                         echo "INSERT INTO `" . $temp_name . "` SELECT " . DBA::anyValueFallback($field_list) . " FROM `" . $name . "`" . $group_by . ";\n";
676                                                         echo "DROP TABLE `" . $name . "`;\n";
677                                                         echo "RENAME TABLE `" . $temp_name . "` TO `" . $name . "`;\n";
678                                                 }
679                                         }
680                                 }
681
682                                 if ($action) {
683                                         if (!$install) {
684                                                 DI::config()->set('system', 'maintenance_reason', DI::l10n()->t('%s: updating %s table.', DateTimeFormat::utcNow() . ' ' . date('e'), $name));
685                                         }
686
687                                         // Ensure index conversion to unique removes duplicates
688                                         if ($is_unique && ($temp_name != $name)) {
689                                                 if ($ignore != "") {
690                                                         DBA::e("SET session old_alter_table=1;");
691                                                 } else {
692                                                         $r = DBA::e("DROP TABLE IF EXISTS `" . $temp_name . "`;");
693                                                         if (!DBA::isResult($r)) {
694                                                                 $errors .= self::printUpdateError($sql3);
695                                                                 return $errors;
696                                                         }
697
698                                                         $r = DBA::e("CREATE TABLE `" . $temp_name . "` LIKE `" . $name . "`;");
699                                                         if (!DBA::isResult($r)) {
700                                                                 $errors .= self::printUpdateError($sql3);
701                                                                 return $errors;
702                                                         }
703                                                 }
704                                         }
705
706                                         $r = DBA::e($sql3);
707                                         if (!DBA::isResult($r)) {
708                                                 $errors .= self::printUpdateError($sql3);
709                                         }
710                                         if ($is_unique && ($temp_name != $name)) {
711                                                 if ($ignore != "") {
712                                                         DBA::e("SET session old_alter_table=0;");
713                                                 } else {
714                                                         $r = DBA::e("INSERT INTO `" . $temp_name . "` SELECT " . $field_list . " FROM `" . $name . "`" . $group_by . ";");
715                                                         if (!DBA::isResult($r)) {
716                                                                 $errors .= self::printUpdateError($sql3);
717                                                                 return $errors;
718                                                         }
719                                                         $r = DBA::e("DROP TABLE `" . $name . "`;");
720                                                         if (!DBA::isResult($r)) {
721                                                                 $errors .= self::printUpdateError($sql3);
722                                                                 return $errors;
723                                                         }
724                                                         $r = DBA::e("RENAME TABLE `" . $temp_name . "` TO `" . $name . "`;");
725                                                         if (!DBA::isResult($r)) {
726                                                                 $errors .= self::printUpdateError($sql3);
727                                                                 return $errors;
728                                                         }
729                                                 }
730                                         }
731                                 }
732                         }
733                 }
734
735                 View::create(false, $action);
736
737                 self::checkInitialValues();
738
739                 if ($action && !$install) {
740                         DI::config()->set('system', 'maintenance', 0);
741                         DI::config()->set('system', 'maintenance_reason', '');
742
743                         if ($errors) {
744                                 DI::config()->set('system', 'dbupdate', self::UPDATE_FAILED);
745                         } else {
746                                 DI::config()->set('system', 'dbupdate', self::UPDATE_SUCCESSFUL);
747                         }
748                 }
749
750                 return $errors;
751         }
752
753         private static function tableStructure($table)
754         {
755                 // This query doesn't seem to be executable as a prepared statement
756                 $indexes = DBA::toArray(DBA::p("SHOW INDEX FROM " . DBA::quoteIdentifier($table)));
757
758                 $fields = DBA::selectToArray(['INFORMATION_SCHEMA' => 'COLUMNS'],
759                         ['COLUMN_NAME', 'COLUMN_TYPE', 'IS_NULLABLE', 'COLUMN_DEFAULT', 'EXTRA',
760                         'COLUMN_KEY', 'COLLATION_NAME', 'COLUMN_COMMENT'],
761                         ["`TABLE_SCHEMA` = ? AND `TABLE_NAME` = ?",
762                         DBA::databaseName(), $table]);
763
764                 $foreign_keys = DBA::selectToArray(['INFORMATION_SCHEMA' => 'KEY_COLUMN_USAGE'],
765                         ['COLUMN_NAME', 'CONSTRAINT_NAME', 'REFERENCED_TABLE_NAME', 'REFERENCED_COLUMN_NAME'],
766                         ["`TABLE_SCHEMA` = ? AND `TABLE_NAME` = ? AND `REFERENCED_TABLE_SCHEMA` IS NOT NULL",
767                         DBA::databaseName(), $table]);
768
769                 $table_status = DBA::selectFirst(['INFORMATION_SCHEMA' => 'TABLES'],
770                         ['ENGINE', 'TABLE_COLLATION', 'TABLE_COMMENT'],
771                         ["`TABLE_SCHEMA` = ? AND `TABLE_NAME` = ?",
772                         DBA::databaseName(), $table]);
773
774                 $fielddata = [];
775                 $indexdata = [];
776                 $foreigndata = [];
777
778                 if (DBA::isResult($foreign_keys)) {
779                         foreach ($foreign_keys as $foreign_key) {
780                                 $parameters = ['foreign' => [$foreign_key['REFERENCED_TABLE_NAME'] => $foreign_key['REFERENCED_COLUMN_NAME']]];
781                                 $constraint = self::getConstraintName($table, $foreign_key['COLUMN_NAME'], $parameters);
782                                 $foreigndata[$constraint] = $foreign_key;
783                         }
784                 }
785
786                 if (DBA::isResult($indexes)) {
787                         foreach ($indexes AS $index) {
788                                 if ($index["Key_name"] != "PRIMARY" && $index["Non_unique"] == "0" && !isset($indexdata[$index["Key_name"]])) {
789                                         $indexdata[$index["Key_name"]] = ["UNIQUE"];
790                                 }
791
792                                 if ($index["Index_type"] == "FULLTEXT" && !isset($indexdata[$index["Key_name"]])) {
793                                         $indexdata[$index["Key_name"]] = ["FULLTEXT"];
794                                 }
795
796                                 $column = $index["Column_name"];
797
798                                 if ($index["Sub_part"] != "") {
799                                         $column .= "(" . $index["Sub_part"] . ")";
800                                 }
801
802                                 $indexdata[$index["Key_name"]][] = $column;
803                         }
804                 }
805
806                 $fielddata = [];
807                 if (DBA::isResult($fields)) {
808                         foreach ($fields AS $field) {
809                                 $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)'];
810                                 $replace = ['boolean', 'tinyint unsigned', 'tinyint', 'smallint unsigned', 'smallint', 'mediumint unsigned', 'mediumint', 'bigint', 'int unsigned', 'int'];
811                                 $field['COLUMN_TYPE'] = str_replace($search, $replace, $field['COLUMN_TYPE']);
812
813                                 $fielddata[$field['COLUMN_NAME']]['type'] = $field['COLUMN_TYPE'];
814
815                                 if ($field['IS_NULLABLE'] == 'NO') {
816                                         $fielddata[$field['COLUMN_NAME']]['not null'] = true;
817                                 }
818
819                                 if (isset($field['COLUMN_DEFAULT']) && ($field['COLUMN_DEFAULT'] != 'NULL')) {
820                                         $fielddata[$field['COLUMN_NAME']]['default'] = trim($field['COLUMN_DEFAULT'], "'");
821                                 }
822
823                                 if (!empty($field['EXTRA'])) {
824                                         $fielddata[$field['COLUMN_NAME']]['extra'] = $field['EXTRA'];
825                                 }
826
827                                 if ($field['COLUMN_KEY'] == 'PRI') {
828                                         $fielddata[$field['COLUMN_NAME']]['primary'] = true;
829                                 }
830
831                                 $fielddata[$field['COLUMN_NAME']]['Collation'] = $field['COLLATION_NAME'];
832                                 $fielddata[$field['COLUMN_NAME']]['comment'] = $field['COLUMN_COMMENT'];
833                         }
834                 }
835
836                 return ["fields" => $fielddata, "indexes" => $indexdata,
837                         "foreign_keys" => $foreigndata, "table_status" => $table_status];
838         }
839
840         private static function dropIndex($indexname)
841         {
842                 $sql = sprintf("DROP INDEX `%s`", DBA::escape($indexname));
843                 return ($sql);
844         }
845
846         private static function addTableField($fieldname, $parameters)
847         {
848                 $sql = sprintf("ADD `%s` %s", DBA::escape($fieldname), self::FieldCommand($parameters));
849                 return ($sql);
850         }
851
852         private static function modifyTableField($fieldname, $parameters)
853         {
854                 $sql = sprintf("MODIFY `%s` %s", DBA::escape($fieldname), self::FieldCommand($parameters, false));
855                 return ($sql);
856         }
857
858         private static function getConstraintName(string $tablename, string $fieldname, array $parameters)
859         {
860                 $foreign_table = array_keys($parameters['foreign'])[0];
861                 $foreign_field = array_values($parameters['foreign'])[0];
862
863                 return $tablename . "-" . $fieldname. "-" . $foreign_table. "-" . $foreign_field;
864         }
865
866         private static function foreignCommand(string $tablename, string $fieldname, array $parameters) {
867                 $foreign_table = array_keys($parameters['foreign'])[0];
868                 $foreign_field = array_values($parameters['foreign'])[0];
869
870                 $sql = "FOREIGN KEY (`" . $fieldname . "`) REFERENCES `" . $foreign_table . "` (`" . $foreign_field . "`)";
871
872                 if (!empty($parameters['foreign']['on update'])) {
873                         $sql .= " ON UPDATE " . strtoupper($parameters['foreign']['on update']);
874                 } else {
875                         $sql .= " ON UPDATE RESTRICT";
876                 }
877
878                 if (!empty($parameters['foreign']['on delete'])) {
879                         $sql .= " ON DELETE " . strtoupper($parameters['foreign']['on delete']);
880                 } else {
881                         $sql .= " ON DELETE CASCADE";
882                 }
883
884                 return $sql;
885         }
886
887         private static function addForeignKey(string $tablename, string $fieldname, array $parameters)
888         {
889                 return sprintf("ADD %s", self::foreignCommand($tablename, $fieldname, $parameters));
890         }
891
892         private static function dropForeignKey(string $constraint)
893         {
894                 return sprintf("DROP FOREIGN KEY `%s`", $constraint);
895         }
896
897         /**
898          * Constructs a GROUP BY clause from a UNIQUE index definition.
899          *
900          * @param array $fieldnames
901          * @return string
902          */
903         private static function groupBy(array $fieldnames)
904         {
905                 if ($fieldnames[0] != "UNIQUE") {
906                         return "";
907                 }
908
909                 array_shift($fieldnames);
910
911                 $names = "";
912                 foreach ($fieldnames AS $fieldname) {
913                         if ($names != "") {
914                                 $names .= ",";
915                         }
916
917                         if (preg_match('|(.+)\((\d+)\)|', $fieldname, $matches)) {
918                                 $names .= "`" . DBA::escape($matches[1]) . "`";
919                         } else {
920                                 $names .= "`" . DBA::escape($fieldname) . "`";
921                         }
922                 }
923
924                 $sql = sprintf(" GROUP BY %s", $names);
925                 return $sql;
926         }
927
928         /**
929          * Renames columns or the primary key of a table
930          *
931          * @todo You cannot rename a primary key if "auto increment" is set
932          *
933          * @param string $table            Table name
934          * @param array  $columns          Columns Syntax for Rename: [ $old1 => [ $new1, $type1 ], $old2 => [ $new2, $type2 ], ... ]
935          *                                 Syntax for Primary Key: [ $col1, $col2, ...]
936          * @param int    $type             The type of renaming (Default is Column)
937          *
938          * @return boolean Was the renaming successful?
939          * @throws Exception
940          */
941         public static function rename($table, $columns, $type = self::RENAME_COLUMN)
942         {
943                 if (empty($table) || empty($columns)) {
944                         return false;
945                 }
946
947                 if (!is_array($columns)) {
948                         return false;
949                 }
950
951                 $table = DBA::escape($table);
952
953                 $sql = "ALTER TABLE `" . $table . "`";
954                 switch ($type) {
955                         case self::RENAME_COLUMN:
956                                 if (!self::existsColumn($table, array_keys($columns))) {
957                                         return false;
958                                 }
959                                 $sql .= implode(',', array_map(
960                                         function ($to, $from) {
961                                                 return " CHANGE `" . $from . "` `" . $to[0] . "` " . $to[1];
962                                         },
963                                         $columns,
964                                         array_keys($columns)
965                                 ));
966                                 break;
967                         case self::RENAME_PRIMARY_KEY:
968                                 if (!self::existsColumn($table, $columns)) {
969                                         return false;
970                                 }
971                                 $sql .= " DROP PRIMARY KEY, ADD PRIMARY KEY(`" . implode('`, `', $columns) . "`)";
972                                 break;
973                         default:
974                                 return false;
975                 }
976
977                 $sql .= ";";
978
979                 $stmt = DBA::p($sql);
980
981                 if (is_bool($stmt)) {
982                         $retval = $stmt;
983                 } else {
984                         $retval = true;
985                 }
986
987                 DBA::close($stmt);
988
989                 return $retval;
990         }
991
992         /**
993          *    Check if the columns of the table exists
994          *
995          * @param string $table   Table name
996          * @param array  $columns Columns to check ( Syntax: [ $col1, $col2, .. ] )
997          *
998          * @return boolean Does the table exist?
999          * @throws Exception
1000          */
1001         public static function existsColumn($table, $columns = [])
1002         {
1003                 if (empty($table)) {
1004                         return false;
1005                 }
1006
1007                 if (is_null($columns) || empty($columns)) {
1008                         return self::existsTable($table);
1009                 }
1010
1011                 $table = DBA::escape($table);
1012
1013                 foreach ($columns AS $column) {
1014                         $sql = "SHOW COLUMNS FROM `" . $table . "` LIKE '" . $column . "';";
1015
1016                         $stmt = DBA::p($sql);
1017
1018                         if (is_bool($stmt)) {
1019                                 $retval = $stmt;
1020                         } else {
1021                                 $retval = (DBA::numRows($stmt) > 0);
1022                         }
1023
1024                         DBA::close($stmt);
1025
1026                         if (!$retval) {
1027                                 return false;
1028                         }
1029                 }
1030
1031                 return true;
1032         }
1033
1034         /**
1035          * Check if a foreign key exists for the given table field
1036          *
1037          * @param string $table
1038          * @param string $field
1039          * @return boolean
1040          */
1041         public static function existsForeignKeyForField(string $table, string $field)
1042         {
1043                 return DBA::exists(['INFORMATION_SCHEMA' => 'KEY_COLUMN_USAGE'],
1044                         ["`TABLE_SCHEMA` = ? AND `TABLE_NAME` = ? AND `COLUMN_NAME` = ? AND `REFERENCED_TABLE_SCHEMA` IS NOT NULL",
1045                         DBA::databaseName(), $table, $field]);
1046         }
1047         /**
1048          *    Check if a table exists
1049          *
1050          * @param string|array $table Table name
1051          *
1052          * @return boolean Does the table exist?
1053          * @throws Exception
1054          */
1055         public static function existsTable($table)
1056         {
1057                 if (empty($table)) {
1058                         return false;
1059                 }
1060
1061                 if (is_array($table)) {
1062                         $condition = ['table_schema' => key($table), 'table_name' => current($table)];
1063                 } else {
1064                         $condition = ['table_schema' => DBA::databaseName(), 'table_name' => $table];
1065                 }
1066
1067                 $result = DBA::exists(['information_schema' => 'tables'], $condition);
1068
1069                 return $result;
1070         }
1071
1072         /**
1073          * Returns the columns of a table
1074          *
1075          * @param string $table Table name
1076          *
1077          * @return array An array of the table columns
1078          * @throws Exception
1079          */
1080         public static function getColumns($table)
1081         {
1082                 $stmtColumns = DBA::p("SHOW COLUMNS FROM `" . $table . "`");
1083                 return DBA::toArray($stmtColumns);
1084         }
1085
1086         /**
1087          * Check if initial database values do exist - or create them
1088          */
1089         public static function checkInitialValues(bool $verbose = false)
1090         {
1091                 if (self::existsTable('verb')) {
1092                         if (!DBA::exists('verb', ['id' => 1])) {
1093                                 foreach (Item::ACTIVITIES as $index => $activity) {
1094                                         DBA::insert('verb', ['id' => $index + 1, 'name' => $activity], Database::INSERT_IGNORE);
1095                                 }
1096                                 if ($verbose) {
1097                                         echo "verb: activities added\n";
1098                                 }
1099                         } elseif ($verbose) {
1100                                 echo "verb: activities already added\n";
1101                         }
1102
1103                         if (!DBA::exists('verb', ['id' => 0])) {
1104                                 DBA::insert('verb', ['name' => '']);
1105                                 $lastid = DBA::lastInsertId();
1106                                 if ($lastid != 0) {
1107                                         DBA::update('verb', ['id' => 0], ['id' => $lastid]);
1108                                         if ($verbose) {
1109                                                 echo "Zero verb added\n";
1110                                         }
1111                                 }
1112                         } elseif ($verbose) {
1113                                 echo "Zero verb already added\n";
1114                         }
1115                 } elseif ($verbose) {
1116                         echo "verb: Table not found\n";
1117                 }
1118
1119                 if (self::existsTable('user') && !DBA::exists('user', ['uid' => 0])) {
1120                         $user = [
1121                                 "verified" => true,
1122                                 "page-flags" => User::PAGE_FLAGS_SOAPBOX,
1123                                 "account-type" => User::ACCOUNT_TYPE_RELAY,
1124                         ];
1125                         DBA::insert('user', $user);
1126                         $lastid = DBA::lastInsertId();
1127                         if ($lastid != 0) {
1128                                 DBA::update('user', ['uid' => 0], ['uid' => $lastid]);
1129                                 if ($verbose) {
1130                                         echo "Zero user added\n";
1131                                 }
1132                         }
1133                 } elseif (self::existsTable('user') && $verbose) {
1134                         echo "Zero user already added\n";
1135                 } elseif ($verbose) {
1136                         echo "user: Table not found\n";
1137                 }
1138
1139                 if (self::existsTable('contact') && !DBA::exists('contact', ['id' => 0])) {
1140                         DBA::insert('contact', ['nurl' => '']);
1141                         $lastid = DBA::lastInsertId();
1142                         if ($lastid != 0) {
1143                                 DBA::update('contact', ['id' => 0], ['id' => $lastid]);
1144                                 if ($verbose) {
1145                                         echo "Zero contact added\n";
1146                                 }
1147                         }               
1148                 } elseif (self::existsTable('contact') && $verbose) {
1149                         echo "Zero contact already added\n";
1150                 } elseif ($verbose) {
1151                         echo "contact: Table not found\n";
1152                 }
1153
1154                 if (self::existsTable('tag') && !DBA::exists('tag', ['id' => 0])) {
1155                         DBA::insert('tag', ['name' => '']);
1156                         $lastid = DBA::lastInsertId();
1157                         if ($lastid != 0) {
1158                                 DBA::update('tag', ['id' => 0], ['id' => $lastid]);
1159                                 if ($verbose) {
1160                                         echo "Zero tag added\n";
1161                                 }
1162                         }
1163                 } elseif (self::existsTable('tag') && $verbose) {
1164                         echo "Zero tag already added\n";
1165                 } elseif ($verbose) {
1166                         echo "tag: Table not found\n";
1167                 }
1168
1169                 if (self::existsTable('permissionset')) {
1170                         if (!DBA::exists('permissionset', ['id' => 0])) {
1171                                 DBA::insert('permissionset', ['allow_cid' => '', 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '']);       
1172                                 $lastid = DBA::lastInsertId();
1173                                 if ($lastid != 0) {
1174                                         DBA::update('permissionset', ['id' => 0], ['id' => $lastid]);
1175                                         if ($verbose) {
1176                                                 echo "Zero permissionset added\n";
1177                                         }
1178                                 }
1179                         } elseif ($verbose) {
1180                                 echo "Zero permissionset already added\n";
1181                         }
1182                         if (!self::existsForeignKeyForField('item', 'psid')) {
1183                                 $sets = DBA::p("SELECT `psid`, `item`.`uid`, `item`.`private` FROM `item`
1184                                         LEFT JOIN `permissionset` ON `permissionset`.`id` = `item`.`psid`
1185                                         WHERE `permissionset`.`id` IS NULL AND NOT `psid` IS NULL");
1186                                 while ($set = DBA::fetch($sets)) {
1187                                         if (($set['private'] == Item::PRIVATE) && ($set['uid'] != 0)) {
1188                                                 $owner = User::getOwnerDataById($set['uid']);
1189                                                 if ($owner) {
1190                                                         $permission = '<' . $owner['id'] . '>';
1191                                                 } else {
1192                                                         $permission = '<>';
1193                                                 }
1194                                         } else {
1195                                                 $permission = '';
1196                                         }
1197                                         $fields = ['id' => $set['psid'], 'uid' => $set['uid'], 'allow_cid' => $permission,
1198                                                 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => ''];
1199                                         DBA::insert('permissionset', $fields);
1200                                 }
1201                                 DBA::close($sets);
1202                         }
1203                 } elseif ($verbose) {
1204                         echo "permissionset: Table not found\n";
1205                 }
1206         
1207                 if (!self::existsForeignKeyForField('tokens', 'client_id')) {
1208                         $tokens = DBA::p("SELECT `tokens`.`id` FROM `tokens`
1209                                 LEFT JOIN `clients` ON `clients`.`client_id` = `tokens`.`client_id`
1210                                 WHERE `clients`.`client_id` IS NULL");
1211                         while ($token = DBA::fetch($tokens)) {
1212                                 DBA::delete('tokens', ['id' => $token['id']]);
1213                         }
1214                         DBA::close($tokens);
1215                 }
1216         }
1217
1218         /**
1219          * Checks if a database update is currently running
1220          *
1221          * @return boolean
1222          */
1223         private static function isUpdating()
1224         {
1225                 $isUpdate = false;
1226
1227                 $processes = DBA::select(['information_schema' => 'processlist'], ['info'],
1228                         ['db' => DBA::databaseName(), 'command' => ['Query', 'Execute']]);
1229
1230                 while ($process = DBA::fetch($processes)) {
1231                         $parts = explode(' ', $process['info']);
1232                         if (in_array(strtolower(array_shift($parts)), ['alter', 'create', 'drop', 'rename'])) {
1233                                 $isUpdate = true;
1234                         }
1235                 }
1236
1237                 DBA::close($processes);
1238
1239                 return $isUpdate;
1240         }
1241 }