Skip to content

Commit 16796f5

Browse files
[BUGFIX] Generate database documentation via typo3 command (kitodo#1486)
Co-authored-by: Sebastian Meyer <sebastian.meyer@opencultureconsulting.com>
1 parent da8c199 commit 16796f5

File tree

12 files changed

+255
-150
lines changed

12 files changed

+255
-150
lines changed

Build/Documentation/dbdocs/generate.php

Lines changed: 0 additions & 35 deletions
This file was deleted.

Build/Documentation/dbdocs/public/typo3

Lines changed: 0 additions & 1 deletion
This file was deleted.

Build/Documentation/dbdocs/public/typo3conf/LocalConfiguration.php

Lines changed: 0 additions & 15 deletions
This file was deleted.

Build/Documentation/dbdocs/public/typo3conf/PackageStates.php

Lines changed: 0 additions & 31 deletions
This file was deleted.

Build/Documentation/dbdocs/public/typo3conf/ext/dlf

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,21 @@
1010
* LICENSE.txt file that was distributed with this source code.
1111
*/
1212

13-
namespace Kitodo\DbDocs;
13+
namespace Kitodo\Dlf\Command\DbDocs;
1414

1515
use Doctrine\DBAL\Schema\Table;
1616
use Kitodo\Dlf\Common\Helper;
1717
use ReflectionClass;
18+
use ReflectionProperty;
1819
use TYPO3\CMS\Core\Database\Schema\Parser\Parser;
1920
use TYPO3\CMS\Core\Database\Schema\SqlReader;
2021
use TYPO3\CMS\Core\Localization\LanguageService;
2122
use TYPO3\CMS\Core\Utility\GeneralUtility;
2223
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
2324
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
25+
use TYPO3\CMS\Extbase\Persistence\ClassesConfiguration;
26+
use TYPO3\CMS\Extbase\Persistence\ClassesConfigurationFactory;
27+
use TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory;
2428
use TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper;
2529

2630
/**
@@ -53,14 +57,6 @@ class Generator
5357
*/
5458
protected $configurationManager;
5559

56-
/**
57-
* @param LanguageService $languageService
58-
*/
59-
public function injectLanguageService(LanguageService $languageService)
60-
{
61-
$this->languageService = $languageService;
62-
}
63-
6460
/**
6561
* @param DataMapper $dataMapper
6662
*/
@@ -72,7 +68,7 @@ public function injectDataMapper(DataMapper $dataMapper)
7268
/**
7369
* @param SqlReader $sqlReader
7470
*/
75-
public function injectSqlReader(DataMapper $sqlReader)
71+
public function injectSqlReader(SqlReader $sqlReader)
7672
{
7773
$this->sqlReader = $sqlReader;
7874
}
@@ -87,7 +83,7 @@ public function injectConfigurationManager(ConfigurationManager $configurationMa
8783

8884
public function __construct()
8985
{
90-
86+
$this->languageService = GeneralUtility::makeInstance(LanguageService::class);
9187
}
9288

9389
/**
@@ -125,12 +121,19 @@ public function collectTables(): array
125121
*/
126122
public function getTableClassMap(): array
127123
{
128-
$frameworkConfiguration = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
124+
$dataMapFactory = GeneralUtility::makeInstance(DataMapFactory::class);
129125

130-
$result = [];
126+
// access classes configuration through reflection, which is otherwise not available?
127+
$reflectionProperty = new ReflectionProperty(DataMapFactory::class, 'classesConfiguration');
128+
$reflectionProperty->setAccessible(true);
129+
$classesConfiguration = $reflectionProperty->getValue($dataMapFactory);
130+
$reflectionProperty = new ReflectionProperty(ClassesConfiguration::class, 'configuration');
131+
$reflectionProperty->setAccessible(true);
132+
$configuration = $reflectionProperty->getValue($classesConfiguration);
131133

132-
foreach ($frameworkConfiguration['persistence']['classes'] as $className => $tableConf) {
133-
$tableName = $tableConf['mapping']['tableName'];
134+
$result = [];
135+
foreach ($configuration as $className => $tableConf) {
136+
$tableName = $tableConf['tableName'];
134137
$result[$tableName] = $className;
135138
}
136139

@@ -193,7 +196,7 @@ protected function getTableInfo(Table $table, ?string $className): object
193196
: $column->getColumnName();
194197

195198
if (isset($result->columns[$columnName])) {
196-
$result->columns[$columnName]->fieldComment = $this->parseDocComment($property->getDocComment());
199+
$result->columns[$columnName]->fieldComment = $this->parsePropertyDocComment($property->getDocComment());
197200
}
198201
}
199202

@@ -204,6 +207,20 @@ protected function getTableInfo(Table $table, ?string $className): object
204207
return $result;
205208
}
206209

210+
protected function parsePropertyDocComment($docComment)
211+
{
212+
$lines = explode("\n", $docComment);
213+
foreach ($lines as $line) {
214+
// extract text from @var line
215+
if ($line !== '' && strpos($line, '@var') !== false) {
216+
$text = preg_replace('#\\s*/?[*/]*\\s?(.*)$#', '$1', $line) . "\n";
217+
$text = preg_replace('/@var [^ ]+ ?/', '', $text);
218+
return trim($text);
219+
}
220+
}
221+
return '';
222+
}
223+
207224
protected function parseDocComment($docComment)
208225
{
209226
// TODO: Consider using phpDocumentor (though that splits the docblock into summary and description)
@@ -237,7 +254,7 @@ public function generatePage(array $tables)
237254
$page->addText(<<<RST
238255
This is a reference of all database tables defined by Kitodo.Presentation.
239256
240-
.. tip:: This page is auto-generated. If you would like to edit it, please use doc-comments in the model class, COMMENT fields in ``ext_tables.sql`` if the table does not have one, or TCA labels. Then, you may re-generate the page by running ``composer docs:db`` inside the Kitodo.Presentation base folder.
257+
.. tip:: This page is auto-generated. If you would like to edit it, please use doc-comments in the model class, COMMENT fields in ``ext_tables.sql`` if the table does not have one, or TCA labels. Then, you may re-generate the page by running ``typo3 kitodo:dbdocs`` inside the Kitodo.Presentation base folder.
241258
RST);
242259

243260
// Sort tables alphabetically
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* LICENSE.txt file that was distributed with this source code.
1111
*/
1212

13-
namespace Kitodo\DbDocs;
13+
namespace Kitodo\Dlf\Command\DbDocs;
1414

1515
/**
1616
* Simple utility to write .rst (reStructuredText).
@@ -55,7 +55,7 @@ public static function paragraphs(array $paragraphs)
5555

5656
public function subsection()
5757
{
58-
$section = new static();
58+
$section = new self();
5959
$this->subsections[] = $section;
6060
return $section;
6161
}

Classes/Command/DbDocsCommand.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
/**
4+
* (c) Kitodo. Key to digital objects e.V. <contact@kitodo.org>
5+
*
6+
* This file is part of the Kitodo and TYPO3 projects.
7+
*
8+
* @license GNU General Public License version 3 or later.
9+
* For the full copyright and license information, please read the
10+
* LICENSE.txt file that was distributed with this source code.
11+
*/
12+
13+
namespace Kitodo\Dlf\Command;
14+
15+
use Kitodo\Dlf\Common\AbstractDocument;
16+
use Kitodo\Dlf\Common\Indexer;
17+
use Kitodo\Dlf\Command\DbDocs\Generator;
18+
use Kitodo\Dlf\Domain\Model\Document;
19+
use Symfony\Component\Console\Command\Command;
20+
use Symfony\Component\Console\Input\InputInterface;
21+
use Symfony\Component\Console\Input\InputOption;
22+
use Symfony\Component\Console\Input\InputArgument;
23+
use Symfony\Component\Console\Output\OutputInterface;
24+
use Symfony\Component\Console\Style\SymfonyStyle;
25+
use TYPO3\CMS\Core\Utility\GeneralUtility;
26+
use TYPO3\CMS\Core\Utility\MathUtility;
27+
28+
/**
29+
* CLI Command for generating the reStructuredText file containing documentation
30+
* about the database schema.
31+
*
32+
* @package TYPO3
33+
* @subpackage dlf
34+
*
35+
* @access public
36+
*/
37+
class DbDocsCommand extends Command
38+
{
39+
40+
protected Generator $generator;
41+
42+
public function __construct(Generator $generator)
43+
{
44+
parent::__construct();
45+
46+
$this->generator = $generator;
47+
}
48+
49+
/**
50+
* Configure the command by defining the name, options and arguments
51+
*
52+
* @access public
53+
*
54+
* @return void
55+
*/
56+
public function configure(): void
57+
{
58+
$this
59+
->setDescription('Generate database rst file.')
60+
->setHelp('')
61+
->addArgument('outputPath', InputArgument::OPTIONAL, 'the path to the output rst file');
62+
}
63+
64+
/**
65+
* Executes the command to generate the documentation.
66+
*
67+
* @access protected
68+
*
69+
* @param InputInterface $input The input parameters
70+
* @param OutputInterface $output The Symfony interface for outputs on console
71+
*
72+
* @return int
73+
*/
74+
protected function execute(InputInterface $input, OutputInterface $output): int
75+
{
76+
$io = new SymfonyStyle($input, $output);
77+
$io->title($this->getDescription());
78+
79+
$outputPath = "kitodo-presentation/Documentation/Developers/Database.rst";
80+
if ($input->getArgument('outputPath')) {
81+
$outputPath = $input->getArgument('outputPath');
82+
}
83+
84+
$tables = $this->generator->collectTables();
85+
$page = $this->generator->generatePage($tables);
86+
87+
GeneralUtility::writeFile($outputPath, $page->render());
88+
89+
$io->write("Database documentation written to output file:\n" . $outputPath . "\n");
90+
return Command::SUCCESS;
91+
}
92+
}

Configuration/Services.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ services:
77
Kitodo\Dlf\:
88
resource: '../Classes/*'
99

10+
Kitodo\Dlf\Command\DbDocsCommand:
11+
tags:
12+
- name: console.command
13+
command: 'kitodo:dbdocs'
14+
1015
Kitodo\Dlf\Command\DeleteCommand:
1116
tags:
1217
- name: console.command

DEVELOPMENT.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ Generate the database reference table:
119119

120120
```bash
121121
composer install
122-
composer docs:db
122+
typo3 kitodo:dbdocs
123123
```
124124

125125
### Troubleshooting

0 commit comments

Comments
 (0)