CombinedExtensionRepository.php 4.54 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
declare(strict_types = 1);

namespace T3o\TerFe2\Domain\Repository;

/*
 * This file is part of TYPO3 CMS-extension "ter_fe2", created by Benni Mack.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 */

use T3o\TerFe2\Service\DocumentationService;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
 * This class combines all data of an extension and all versions from various database tables
 *
 * tx_ter_extensions -> contains all versions of all extensions
23
 * tx_terfe2_domain_model_extension -> contains all registered extension keys and meta data (even if there was no version uploaded)
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
 *
 * Currently this class is used as a wrapper, but should vanish once we migrated the database structures properly
 */
class CombinedExtensionRepository
{
    /**
     * @var DocumentationService
     */
    protected $documentationService;

    public function __construct(DocumentationService $documentationService = null)
    {
        $this->documentationService = $documentationService ?? GeneralUtility::makeInstance(DocumentationService::class);
    }

    /**
     * Fetches all information about a given list of extension keys that is not version-specific.
     *
     * @param array $extensionKeys
     * @return array
     */
    protected function getBasicExtensionInformation(array $extensionKeys): array
    {
        $basicExtensionInformation = [];
        foreach ($extensionKeys as $extensionKey) {
49
            $keysQueryBuilder = $this->getQueryBuilder('tx_terfe2_domain_model_extension');
50
            $extensionKeyRow = $keysQueryBuilder
51
                ->select('uid', 'frontend_user', 'downloads', 'external_manual')
52
53
                ->from('tx_terfe2_domain_model_extension')
                ->where(
54
                    $keysQueryBuilder->expr()->eq('ext_key', $keysQueryBuilder->createNamedParameter($extensionKey))
55
56
57
                )
                ->execute()
                ->fetch();
58

59
            $basicExtensionInformation[$extensionKey] = [
60
61
62
                'ownerusername' => $extensionKeyRow['frontend_user'],
                'downloads' => $extensionKeyRow['downloads'],
                'external_manual' => $extensionKey['external_manual'] ?: ''
63
64
65
66
67
68
69
70
71
72
73
            ];
        }
        return $basicExtensionInformation;
    }

    public function getExtensionDetailsWithVersionsAndDownloadNumbers(): array
    {
        $groupedVersionsByExtension = [];

        $queryBuilder = $this->getQueryBuilder('tx_ter_extensions');
        $versions = $queryBuilder
74
            ->select('uid', 'tstamp', 'extensionkey', 'version', 'title', 'description', 'state', 'reviewstate', 'category', 'downloadcounter', 't3xfilemd5', 'lastuploaddate', 'uploadcomment', 'dependencies', 'composerinfo', 'authorname', 'authoremail', 'authorcompany')
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
            ->from('tx_ter_extensions')
            ->execute()
            ->fetchAll();

        $usedExtensionKeys = array_column($versions, 'extensionkey');
        $usedExtensionKeys = array_unique($usedExtensionKeys);

        $basicExtensionInformation = $this->getBasicExtensionInformation($usedExtensionKeys);

        foreach ($versions as $row) {
            $extensionKey = $row['extensionkey'];
            $versionNumber = $row['version'];
            $genericExtensionInformation = $basicExtensionInformation[$extensionKey];
            $groupedVersionsByExtension[$extensionKey]['ownerusername'] = $genericExtensionInformation['ownerusername'];
            $groupedVersionsByExtension[$extensionKey]['downloads'] = $genericExtensionInformation['downloads'];

91
            if (!empty($genericExtensionInformation['external_manual'])) {
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
                $row['external_manual'] = $genericExtensionInformation['external_manual'];
            } else {
                try {
                    $row['external_manual'] = $this->documentationService->getDocumentationLink($extensionKey, $versionNumber, true);
                } catch (\Exception $e) {
                    $row['external_manual'] = '';
                }
            }
            $groupedVersionsByExtension[$extensionKey]['versions'][$versionNumber] = $row;
        }
        return $groupedVersionsByExtension;
    }

    protected function getQueryBuilder(string $table): QueryBuilder
    {
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
        $queryBuilder->getRestrictions()->removeAll();
        return $queryBuilder;
    }
}