‪TYPO3CMS  9.5
CoreVersionService.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 
5 
6 /*
7  * This file is part of the TYPO3 CMS project.
8  *
9  * It is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License, either version 2
11  * of the License, or any later version.
12  *
13  * For the full copyright and license information, please read the
14  * LICENSE.txt file that was distributed with this source code.
15  *
16  * The TYPO3 project - inspiring people to share!
17  */
18 
25 
30 class CoreVersionService
31 {
35  protected $registry;
36 
42  protected $apiBaseUrl = 'https://get.typo3.org/v1/api/';
43 
49  public function __construct(Registry $registry = null)
50  {
51  if (null !== $registry) {
52  trigger_error(
53  'The constructor parameter $registry for CoreVersionService is deprecated since TYPO3 v9 and will be removed in TYPO3 v10.0.',
54  E_USER_DEPRECATED
55  );
56  $this->registry = $registry;
57  } else {
58  $this->registry = GeneralUtility::makeInstance(Registry::class);
59  }
60  }
61 
68  public function isInstalledVersionAReleasedVersion(): bool
69  {
70  $version = $this->getInstalledVersion();
71  return substr($version, -4) !== '-dev';
72  }
73 
79  public function getInstalledVersion(): string
80  {
82  }
83 
84  public function getMaintenanceWindow(): MaintenanceWindow
85  {
86  $url = 'major/' . $this->getInstalledMajorVersion();
87  $result = $this->fetchFromRemote($url);
88 
90  }
91 
95  public function getSupportedMajorReleases(): array
96  {
97  $url = 'major';
98  $result = $this->fetchFromRemote($url);
99 
100  $majorReleases = [
101  'community' => [],
102  'elts' => [],
103  ];
104  foreach ($result as $release) {
105  $majorRelease = ‪MajorRelease::fromApiResponse($release);
106  $maintenanceWindow = $majorRelease->getMaintenanceWindow();
107 
108  if ($maintenanceWindow->isSupportedByCommunity()) {
109  $group = 'community';
110  } elseif ($maintenanceWindow->isSupportedByElts()) {
111  $group = 'elts';
112  } else {
113  // Major version is unsupported
114  continue;
115  }
116 
117  $majorReleases[$group][] = $majorRelease->getLts() ?? $majorRelease->getVersion();
118  }
119 
120  return $majorReleases;
121  }
122 
123  public function isPatchReleaseSuitableForUpdate(CoreRelease $coreRelease): bool
124  {
125  return version_compare($this->getInstalledVersion(), $coreRelease->getVersion()) === -1;
126  }
127 
134  public function isYoungerPatchReleaseAvailable(): bool
135  {
136  return version_compare($this->getInstalledVersion(), $this->getYoungestPatchRelease()->getVersion()) === -1;
137  }
138 
145  public function isUpdateSecurityRelevant(CoreRelease $releaseToCheck): bool
146  {
147  $url = 'major/' . $this->getInstalledMajorVersion() . '/release';
148  $result = $this->fetchFromRemote($url);
149 
150  $installedVersion = $this->getInstalledVersion();
151  foreach ($result as $release) {
152  $coreRelease = ‪CoreRelease::fromApiResponse($release);
153  if ($coreRelease->isSecurityUpdate()
154  && version_compare($installedVersion, $coreRelease->getVersion()) === -1 // installed version is lower than release
155  && version_compare($releaseToCheck->getVersion(), $coreRelease->getVersion()) > -1 // release to check is equal or higher than release
156  ) {
157  return true;
158  }
159  }
160 
161  return false;
162  }
163 
164  public function isCurrentInstalledVersionElts(): bool
165  {
166  $url = 'major/' . $this->getInstalledMajorVersion() . '/release';
167  $result = $this->fetchFromRemote($url);
168 
169  $installedVersion = $this->getInstalledVersion();
170  foreach ($result as $release) {
171  if (version_compare($installedVersion, $release['version']) === 0) {
172  return $release['elts'] ?? false;
173  }
174  }
175 
176  return false;
177  }
178 
185  public function getYoungestPatchRelease(): CoreRelease
186  {
187  $url = 'major/' . $this->getInstalledMajorVersion() . '/release/latest';
188  $result = $this->fetchFromRemote($url);
189  return ‪CoreRelease::fromApiResponse($result);
190  }
191 
192  public function getYoungestCommunityPatchRelease(): CoreRelease
193  {
194  $url = 'major/' . $this->getInstalledMajorVersion() . '/release';
195  $result = $this->fetchFromRemote($url);
196 
197  // Make sure all releases are sorted by their version
198  $columns = array_column($result, 'version');
199  array_multisort($columns, SORT_NATURAL, $result);
200 
201  // Remove any ELTS release
202  $releases = array_filter($result, static function (array $release) {
203  return ($release['elts'] ?? false) === false;
204  });
205 
206  $latestRelease = end($releases);
207 
208  return ‪CoreRelease::fromApiResponse($latestRelease);
209  }
210 
216  protected function fetchFromRemote(string $url): array
217  {
218  $url = $this->apiBaseUrl . $url;
219  $json = GeneralUtility::getUrl($url);
220 
221  if (!$json) {
222  $this->throwFetchException($url);
223  }
224  return json_decode($json, true);
225  }
226 
232  protected function getInstalledMajorVersion(): string
233  {
234  return $this->getMajorVersion($this->getInstalledVersion());
235  }
236 
243  protected function getMajorVersion(string $version): string
244  {
245  $explodedVersion = explode('.', $version);
246  return $explodedVersion[0];
247  }
248 
255  public function updateVersionMatrix(): void
256  {
257  trigger_error('Method updateVersionMatrix will be removed in TYPO3 v10.0, use new REST API directly (see https://get.typo3.org/v1/api/doc).', E_USER_DEPRECATED);
258  $url = 'https://get.typo3.org/json';
259  $versionJson = GeneralUtility::getUrl($url);
260  if (!$versionJson) {
261  $this->throwFetchException($url);
262  }
263  $versionArray = json_decode($versionJson, true);
264  $installedMajorVersion = (int)$this->getInstalledMajorVersion();
265 
266  foreach ($versionArray as $versionNumber => $versionDetails) {
267  if (is_array($versionDetails) && (int)$this->getMajorVersion((string)$versionNumber) < $installedMajorVersion) {
268  unset($versionArray[$versionNumber]);
269  }
270  }
271 
272  $this->registry->set('TYPO3.CMS.Install', 'coreVersionMatrix', $versionArray);
273  }
274 
282  public function getYoungestPatchDevelopmentRelease(): string
283  {
284  trigger_error(
285  'Method getYoungestPatchDevelopmentRelease() is deprecated since TYPO3 v9 and will be removed in TYPO3 v10.0, use getYoungestPatchRelease() instead.',
286  E_USER_DEPRECATED
287  );
288  return $this->getYoungestPatchRelease()->getVersion();
289  }
290 
298  public function isYoungerPatchDevelopmentReleaseAvailable(): bool
299  {
300  trigger_error(
301  'Method isYoungerPatchDevelopmentReleaseAvailable() is deprecated since TYPO3 v9 and will be removed in TYPO3 v10.0, use isYoungerPatchReleaseAvailable() instead.',
302  E_USER_DEPRECATED
303  );
304  return $this->isYoungerPatchReleaseAvailable();
305  }
306 
311  public function getDownloadBaseUrl(): string
312  {
313  trigger_error(
314  'Method getDownloadBaseUrl() is deprecated since TYPO3 v9 and will be removed in TYPO3 v10.0, use https://get.typo3.org directly.',
315  E_USER_DEPRECATED
316  );
317  return $this->apiBaseUrl;
318  }
319 
326  protected function throwFetchException(string $url): void
327  {
328  throw new Exception\RemoteFetchException(
329  'Fetching ' .
330  $url .
331  ' failed. Maybe this instance can not connect to the remote system properly.',
332  1380897593
333  );
334  }
335 }
‪TYPO3\CMS\Core\Utility\VersionNumberUtility
Definition: VersionNumberUtility.php:21
‪TYPO3\CMS\Core\Registry
Definition: Registry.php:32
‪TYPO3\CMS\Install\CoreVersion\MajorRelease\fromApiResponse
‪static fromApiResponse(array $response)
Definition: MajorRelease.php:35
‪TYPO3\CMS\Install\CoreVersion\MaintenanceWindow
Definition: MaintenanceWindow.php:21
‪TYPO3\CMS\Install\CoreVersion\CoreRelease
Definition: CoreRelease.php:21
‪TYPO3\CMS\Core\Utility\VersionNumberUtility\getCurrentTypo3Version
‪static string getCurrentTypo3Version()
Definition: VersionNumberUtility.php:114
‪TYPO3\CMS\Install\CoreVersion\CoreRelease\fromApiResponse
‪static fromApiResponse(array $response)
Definition: CoreRelease.php:40
‪TYPO3\CMS\Install\CoreVersion\MaintenanceWindow\fromApiResponse
‪static fromApiResponse(array $response)
Definition: MaintenanceWindow.php:31
‪TYPO3\CMS\Install\CoreVersion\MajorRelease
Definition: MajorRelease.php:21
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Install\Service
Definition: ClearCacheService.php:2