VersionUtility.php 8.37 KB
Newer Older
1
<?php
2
3
namespace T3o\TerFe2\Utility;

4
5
/*
 * This file is part of the TYPO3 CMS project.
6
 *
7
8
9
 * 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.
10
 *
11
12
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
15
 * The TYPO3 project - inspiring people to share!
 */
16

17
use T3o\TerFe2\Service\LTSVersionService;
18
19
20
21
use T3o\TerFe2\Domain\Model\Download;
use T3o\TerFe2\Domain\Model\Extension;
use T3o\TerFe2\Domain\Repository\DownloadRepository;
use T3o\TerFe2\Domain\Repository\ExtensionRepository;
22
use TYPO3\CMS\Core\Utility\GeneralUtility;
23
24
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;
25

26
27
28
/**
 * Utilities to manage versions
 */
29
class VersionUtility
30
{
31
32
33
34

    const DOWNLOAD_SOURCE_TER = 1;
    const DOWNLOAD_SOURCE_PACKAGIST = 2;

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
    /**
     * Returns the three part version number (string) from an integer, eg 4012003 -> '4.12.3'
     *
     * @param int $versionInteger Integer representation of version number
     * @return string Version number as format x.x.x
     * @throws \InvalidArgumentException if $versionInteger is not an integer
     */
    public static function convertIntegerToVersionNumber(int $versionInteger): string
    {
        $versionString = str_pad($versionInteger, 9, '0', STR_PAD_LEFT);
        $parts = [
            substr($versionString, 0, 3),
            substr($versionString, 3, 3),
            substr($versionString, 6, 3)
        ];

        return (int)$parts[0] . '.' . (int)$parts[1] . '.' . (int)$parts[2];
    }

Thomas Löffler's avatar
Thomas Löffler committed
54
    /**
55
     * @param \T3o\TerFe2\Domain\Model\Extension $extension
Thomas Löffler's avatar
Thomas Löffler committed
56
57
58
     * @param int $mainVersion
     * @return bool
     */
59
    public static function doesExtensionSupportTypo3Version(\T3o\TerFe2\Domain\Model\Extension $extension, int $mainVersion): bool
Thomas Löffler's avatar
Thomas Löffler committed
60
    {
61
        foreach ($extension->getVersions() as $version) {
Thomas Löffler's avatar
Thomas Löffler committed
62
            if ($version->getTypo3Dependency() === null || $version->getReviewState() === \T3o\TerFe2\Domain\Model\Version::VERSION_IS_INSECURE) {
63
64
65
                continue;
            }

66
            if ($version->doesSupportTypo3Version($mainVersion)) {
67
68
69
                return true;
            }
        }
Thomas Löffler's avatar
Thomas Löffler committed
70

71
        return false;
Thomas Löffler's avatar
Thomas Löffler committed
72
    }
73
74
75
76
77
78
79
80
81
82
83
84
85

    /**
     * Generates a list with compatible TYPO3 versions from given dependencies
     *
     * @param array $dependencies
     * @return string
     */
    public static function getCompatibleTypo3Versions(array $dependencies): string
    {
        $compatibleTypo3Versions = [];

        foreach ($dependencies as $relation) {
            if ((string)($relation['kind'] ?? '') !== 'depends'
86
                || !in_array((string)($relation['extensionKey'] ?? ''), ['typo3', 'cms'], true)
87
88
89
            ) {
                continue;
            }
90
91
92
93
94

            $versionRange = (string)($relation['versionRange'] ?? '');

            // Assume all LTS versions are supported
            if ($versionRange === '*') {
95
                $compatibleTypo3Versions = self::getAllLtsVersionsAsMajor();
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
                continue;
            }

            // Extract all supported versions form minimal and maximal
            if (strpos($versionRange, '-') !== false) {
                [$min, $max] = explode('-', $versionRange);
                if ($min === null || $min === '0' || $min === '0.0.0') {
                    // Skip if minimal version is not valid
                    continue;
                }
                $min = self::getMajorVersion($min);
                if ($min <= 0 || $max === null) {
                    // Skip if major version could not be extracted or maximal version is not valid
                    continue;
                }
                if ($max === '0' || $max === '0.0.0') {
112
                    // Assume that all LTS versions between the minimal and the latest LTS are compatible.
113
114
115
116
117
118
119
                    $compatibleTypo3Versions = self::getVersionRange($min);
                    // Also break the foreach as all information was gathered.
                    break;
                }
                $max = self::getMajorVersion($max);
                if ($max <= 0) {
                    // Only add minimal since maximal could not be extracted
Oliver Bartsch's avatar
Oliver Bartsch committed
120
121
                    $compatibleTypo3Versions[] = $min;
                    continue;
122
123
124
125
126
127
128
129
130
131
132
133
134
                }
                // Add all major versions between min and max
                $compatibleTypo3Versions = self::getVersionRange($min, $max);
                // Also break the foreach as all information was gathered.
                break;
            }

            // Assume only the minimal version is supported
            if (strpos($versionRange, '.') !== false) {
                $major = self::getMajorVersion($versionRange);
                if ($major > 0) {
                    $compatibleTypo3Versions[] = $major;
                }
135
136
137
138
139
            }
        }

        return implode(',', array_unique($compatibleTypo3Versions));
    }
140
141
142
143
144
145

    /**
     * Generates an array of LTS versions with only their major number
     *
     * @return array
     */
146
    protected static function getAllLtsVersionsAsMajor(): array
147
148
149
150
151
152
153
154
155
156
157
158
159
    {
        $ltsVersions = GeneralUtility::makeInstance(LTSVersionService::class)->getAllLTSVersions();
        asort($ltsVersions, SORT_NUMERIC);
        foreach ($ltsVersions as &$ltsVersion) {
            $ltsVersion = self::getMajorVersion(self::convertIntegerToVersionNumber($ltsVersion));
        }
        return $ltsVersions;
    }

    /**
     * Generate range for min and max by removing invalid version numbers
     *
     * @param int $min
Oliver Bartsch's avatar
Oliver Bartsch committed
160
     * @param int|null $max
161
162
163
164
     * @return array
     */
    protected static function getVersionRange(int $min, int $max = null): array
    {
165
        $allLtsVersions = self::getAllLtsVersionsAsMajor();
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
        $max = (int)($max ?? end($allLtsVersions) ?: 0);

        if ($max <= 0) {
            return [];
        }

        $range = range($min, $max);
        foreach ($range as $key => $version) {
            if (!in_array($version, $allLtsVersions, true)) {
                unset($range[$key]);
            }
        }

        return $range;
    }

    /**
     * Extract the major version from version string
     *
     * @param string $versionString
     * @return int
     */
    protected static function getMajorVersion(string $versionString): int
    {
        $parts = explode('.', $versionString) ?: [];
        return (int)array_shift($parts);
    }
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
    
    /**
     * @param String $extensionKey
     * @param int $versionId
     * @param int $source
     * @param int $counter
     *
     * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
     * @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
     * @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException
     */
    public static function updateVersionCounter(String $extensionKey, int $versionId, int $source = self::DOWNLOAD_SOURCE_TER, int $counter = 1)
    {
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
        $persistenceManager = $objectManager->get(PersistenceManager::class);
        $extensionRepository = $objectManager->get(ExtensionRepository::class);


        $month = date('Ym');
        $downloadRepository = $objectManager->get(DownloadRepository::class);

        /** @var Download $downloadInfo */
        $downloadInfo = $downloadRepository->findDownloadCounterByMonthAndExtensionKey($month,$extensionKey,$versionId, $source);

        if (null === $downloadInfo) {
            $downloadInfo = new Download();
            $downloadInfo->setExtensionKey($extensionKey);
            $downloadInfo->setSource($source);
            $downloadInfo->setMonth($month);
            $downloadInfo->setVersionId($versionId);
            $downloadInfo->setCounter($counter);
            $downloadRepository->add($downloadInfo);
        } elseif ($downloadInfo->getCounter() > 0) {
            $downloadInfo->setCounter($downloadInfo->getCounter()+1);
            $downloadRepository->update($downloadInfo);
        }

        // Sets Extension Last Download Sync to secure oldest syncs happens first.
        /** @var Extension $extension */
        $extension = $extensionRepository->findOneByExtKey($extensionKey);
        $extension->setLastDownloadSync(time());
        $extensionRepository->update($extension);

        $persistenceManager->persistAll();
    }
238
}