3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Util\Writer;
24 use Friendica\Core\Renderer;
25 use Friendica\Database\Definition\DbaDefinition;
26 use Friendica\Network\HTTPException\ServiceUnavailableException;
29 * Utility class to write content into the '/doc' directory
34 * Creates all database definitions as Markdown fields and create the mkdoc config file.
36 * @param DbaDefinition $definition The Database definition class
37 * @param string $basePath The basepath of Friendica
40 * @throws ServiceUnavailableException in really unexpected cases!
42 public static function writeDbDefinition(DbaDefinition $definition, string $basePath)
45 foreach ($definition->getAll() as $name => $definition) {
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));
65 array_walk_recursive($indexes, function (&$value, $key) use ($lengths) {
66 $value = str_pad($value, $lengths[$key], $value === '-' ? '-' : ' ');
73 'comment' => 'Description',
77 'default' => 'Default',
99 foreach ($definition['fields'] as $key => $value) {
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'] ?? '';
109 foreach ($field as $fieldName => $fieldvalue) {
110 $lengths[$fieldName] = max($lengths[$fieldName] ?? 0, strlen($fieldvalue));
114 if (!empty($value['foreign'])) {
117 'targettable' => array_keys($value['foreign'])[0],
118 'targetfield' => array_values($value['foreign'])[0]
123 array_walk_recursive($fields, function (&$value, $key) use ($lengths) {
124 $value = str_pad($value, $lengths[$key], $value === '-' ? '-' : ' ');
127 $tables[] = ['name' => $name, 'comment' => $definition['comment']];
128 $content = Renderer::replaceMacros(Renderer::getMarkupTemplate('structure.tpl'), [
130 '$comment' => $definition['comment'],
131 '$fields' => $fields,
132 '$indexes' => $indexes,
133 '$foreign' => $foreign,
135 $filename = $basePath . '/doc/database/db_' . $name . '.md';
136 file_put_contents($filename, $content);
139 $content = Renderer::replaceMacros(Renderer::getMarkupTemplate('tables.tpl'), [
140 '$tables' => $tables,
142 $filename = $basePath . '/doc/database.md';
143 file_put_contents($filename, $content);