]> git.mxchange.org Git - friendica.git/blobdiff - src/Database/DBStructure.php
Added sort order
[friendica.git] / src / Database / DBStructure.php
index 7fc1b3feee7e5ab9a507d3f1c4c92c8a988c323a..d1975f504235148479b018cd6c53ed87b7f683a8 100644 (file)
@@ -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;
@@ -80,7 +81,7 @@ class DBStructure
 
                $old_tables = ['fserver', 'gcign', 'gcontact', 'gcontact-relation', 'gfollower' ,'glink', 'item-delivery-data',
                        'item-activity', 'item-content', 'item_id', 'participation', 'poll', 'poll_result', 'queue', 'retriever_rule',
-                       'sign', 'spam', 'term', 'user-item', 'thread', 'item'];
+                       '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']);
@@ -159,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);
@@ -192,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';
 
@@ -238,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];
                        }
                }