]> git.mxchange.org Git - friendica.git/blobdiff - src/Database/DBStructure.php
Added sort order
[friendica.git] / src / Database / DBStructure.php
index 8a3d6cc80ac09ff80fb7c9c694eb1ebdd887f113..d1975f504235148479b018cd6c53ed87b7f683a8 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2021, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -24,6 +24,7 @@ namespace Friendica\Database;
 use Exception;
 use Friendica\Core\Hook;
 use Friendica\Core\Logger;
+use Friendica\Core\Renderer;
 use Friendica\DI;
 use Friendica\Model\Item;
 use Friendica\Model\User;
@@ -79,7 +80,8 @@ class DBStructure
                }
 
                $old_tables = ['fserver', 'gcign', 'gcontact', 'gcontact-relation', 'gfollower' ,'glink', 'item-delivery-data',
-                       'item-activity', 'item-content', 'item_id', 'poll', 'poll_result', 'queue', 'retriever_rule', 'sign', 'spam', 'term'];
+                       'item-activity', 'item-content', 'item_id', 'participation', 'poll', 'poll_result', 'queue', 'retriever_rule',
+                       'deliverq', 'dsprphotoq', 'ffinder', 'sign', 'spam', 'term', 'user-item', 'thread', 'item'];
 
                $tables = DBA::selectToArray(['INFORMATION_SCHEMA' => 'TABLES'], ['TABLE_NAME'],
                        ['TABLE_SCHEMA' => DBA::databaseName(), 'TABLE_TYPE' => 'BASE TABLE']);
@@ -158,6 +160,42 @@ class DBStructure
                return DI::l10n()->t('Errors encountered performing database changes: ') . $message . EOL;
        }
 
+       public static function writeStructure()
+       {
+               Renderer::registerTemplateEngine('Friendica\Render\FriendicaSmartyEngine');
+
+               $tables = [];
+               foreach (self::definition(null) as $name => $definition) {
+                       $fields = [];
+                       foreach ($definition['fields'] as $key => $value) {
+                               $field = [];
+                               $field['name']    = $key;
+                               $field['comment'] = $value['comment'] ?? '';
+                               $field['type']    = $value['type'];
+                               $field['notnull'] = ($value['not null'] ?? false) ? 'YES' : 'NO';
+                               $field['primary'] = ($value['primary'] ?? false) ? 'PRI' : '';
+                               $field['default'] = $value['default'] ?? '';
+                               $field['extra']   = $value['extra'] ?? '';
+               
+                               $fields[] = $field;
+                       }
+                       $tables[] = ['name' => $name, 'comment' => $definition['comment']];
+                       $content = Renderer::replaceMacros(Renderer::getMarkupTemplate('structure.tpl'), [
+                               '$name'    => $name,
+                               '$comment' => $definition['comment'],
+                               '$fields'  => $fields,
+                       ]);
+                       $filename = DI::basePath() . '/doc/database/db_' . $name . '.md';
+                       file_put_contents($filename, $content);
+               }
+               asort($tables);
+               $content = Renderer::replaceMacros(Renderer::getMarkupTemplate('tables.tpl'), [
+                       '$tables'  => $tables,  
+               ]);
+               $filename = DI::basePath() . '/doc/database.md';
+               file_put_contents($filename, $content);         
+       }
+
        public static function printStructure($basePath)
        {
                $database = self::definition($basePath, false);
@@ -191,6 +229,9 @@ class DBStructure
        public static function definition($basePath, $with_addons_structure = true)
        {
                if (!self::$definition) {
+                       if (empty($basePath)) {
+                               $basePath = DI::app()->getBasePath();
+                       }
 
                        $filename = $basePath . '/static/dbstructure.config.php';
 
@@ -237,6 +278,12 @@ class DBStructure
                // Assign all field that are present in the table
                foreach ($fieldnames as $field) {
                        if (isset($data[$field])) {
+                               // Limit the length of varchar, varbinary, char and binrary fields
+                               if (is_string($data[$field]) && preg_match("/char\((\d*)\)/", $definition[$table]['fields'][$field]['type'], $result)) {
+                                       $data[$field] = mb_substr($data[$field], 0, $result[1]);
+                               } elseif (is_string($data[$field]) && preg_match("/binary\((\d*)\)/", $definition[$table]['fields'][$field]['type'], $result)) {
+                                       $data[$field] = substr($data[$field], 0, $result[1]);
+                               }
                                $fields[$field] = $data[$field];
                        }
                }
@@ -1231,7 +1278,7 @@ class DBStructure
                        } elseif ($verbose) {
                                echo "Zero permissionset already added\n";
                        }
-                       if (!self::existsForeignKeyForField('item', 'psid')) {
+                       if (self::existsTable('item') && !self::existsForeignKeyForField('item', 'psid')) {
                                $sets = DBA::p("SELECT `psid`, `item`.`uid`, `item`.`private` FROM `item`
                                        LEFT JOIN `permissionset` ON `permissionset`.`id` = `item`.`psid`
                                        WHERE `permissionset`.`id` IS NULL AND NOT `psid` IS NULL");