]> git.mxchange.org Git - friendica.git/blob - src/Util/Writer/DocWriter.php
spelling: one
[friendica.git] / src / Util / Writer / DocWriter.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
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\Util\Writer;
23
24 use Friendica\Core\Renderer;
25 use Friendica\Database\Definition\DbaDefinition;
26 use Friendica\Network\HTTPException\ServiceUnavailableException;
27
28 /**
29  * Utility class to write content into the '/doc' directory
30  */
31 class DocWriter
32 {
33         /**
34          * Creates all database definitions as Markdown fields and create the mkdoc config file.
35          *
36          * @param DbaDefinition $definition The Database definition class
37          * @param string        $basePath   The basepath of Friendica
38          *
39          * @return void
40          * @throws ServiceUnavailableException in really unexpected cases!
41          */
42         public static function writeDbDefinition(DbaDefinition $definition, string $basePath)
43         {
44                 $tables = [];
45                 foreach ($definition->getAll() as $name => $definition) {
46                         $indexes = [
47                                 [
48                                         'name'   => 'Name',
49                                         'fields' => 'Fields',
50                                 ],
51                                 [
52                                         'name'   => '-',
53                                         'fields' => '-',
54                                 ]
55                         ];
56
57                         $lengths = ['name' => 4, 'fields' => 6];
58                         foreach ($definition['indexes'] as $key => $value) {
59                                 $fieldlist         = implode(', ', $value);
60                                 $indexes[]         = ['name' => $key, 'fields' => $fieldlist];
61                                 $lengths['name']   = max($lengths['name'], strlen($key));
62                                 $lengths['fields'] = max($lengths['fields'], strlen($fieldlist));
63                         }
64
65                         array_walk_recursive($indexes, function (&$value, $key) use ($lengths) {
66                                 $value = str_pad($value, $lengths[$key], $value === '-' ? '-' : ' ');
67                         });
68
69                         $foreign = [];
70                         $fields  = [
71                                 [
72                                         'name'    => 'Field',
73                                         'comment' => 'Description',
74                                         'type'    => 'Type',
75                                         'null'    => 'Null',
76                                         'primary' => 'Key',
77                                         'default' => 'Default',
78                                         'extra'   => 'Extra',
79                                 ],
80                                 [
81                                         'name'    => '-',
82                                         'comment' => '-',
83                                         'type'    => '-',
84                                         'null'    => '-',
85                                         'primary' => '-',
86                                         'default' => '-',
87                                         'extra'   => '-',
88                                 ]
89                         ];
90                         $lengths = [
91                                 'name'    => 5,
92                                 'comment' => 11,
93                                 'type'    => 4,
94                                 'null'    => 4,
95                                 'primary' => 3,
96                                 'default' => 7,
97                                 'extra'   => 5,
98                         ];
99                         foreach ($definition['fields'] as $key => $value) {
100                                 $field            = [];
101                                 $field['name']    = $key;
102                                 $field['comment'] = $value['comment'] ?? '';
103                                 $field['type']    = $value['type'];
104                                 $field['null']    = ($value['not null'] ?? false) ? 'NO' : 'YES';
105                                 $field['primary'] = ($value['primary'] ?? false) ? 'PRI' : '';
106                                 $field['default'] = $value['default'] ?? 'NULL';
107                                 $field['extra']   = $value['extra']   ?? '';
108
109                                 foreach ($field as $fieldName => $fieldvalue) {
110                                         $lengths[$fieldName] = max($lengths[$fieldName] ?? 0, strlen($fieldvalue));
111                                 }
112                                 $fields[] = $field;
113
114                                 if (!empty($value['foreign'])) {
115                                         $foreign[] = [
116                                                 'field'       => $key,
117                                                 'targettable' => array_keys($value['foreign'])[0],
118                                                 'targetfield' => array_values($value['foreign'])[0]
119                                         ];
120                                 }
121                         }
122
123                         array_walk_recursive($fields, function (&$value, $key) use ($lengths) {
124                                 $value = str_pad($value, $lengths[$key], $value === '-' ? '-' : ' ');
125                         });
126
127                         $tables[] = ['name' => $name, 'comment' => $definition['comment']];
128                         $content  = Renderer::replaceMacros(Renderer::getMarkupTemplate('structure.tpl'), [
129                                 '$name'    => $name,
130                                 '$comment' => $definition['comment'],
131                                 '$fields'  => $fields,
132                                 '$indexes' => $indexes,
133                                 '$foreign' => $foreign,
134                         ]);
135                         $filename = $basePath . '/doc/database/db_' . $name . '.md';
136                         file_put_contents($filename, $content);
137                 }
138                 asort($tables);
139                 $content = Renderer::replaceMacros(Renderer::getMarkupTemplate('tables.tpl'), [
140                         '$tables' => $tables,
141                 ]);
142                 $filename = $basePath . '/doc/database.md';
143                 file_put_contents($filename, $content);
144         }
145 }