‪TYPO3CMS  10.4
CoreVersionService.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
26 
31 class CoreVersionService
32 {
38  protected $apiBaseUrl = 'https://get.typo3.org/api/v1/';
39 
46  public function isInstalledVersionAReleasedVersion(): bool
47  {
48  $version = $this->getInstalledVersion();
49  return substr($version, -4) !== '-dev';
50  }
51 
57  public function getInstalledVersion(): string
58  {
59  return (string)GeneralUtility::makeInstance(Typo3Version::class);
60  }
61 
62  public function getMaintenanceWindow(): MaintenanceWindow
63  {
64  $url = 'major/' . $this->getInstalledMajorVersion();
65  $result = $this->fetchFromRemote($url);
66 
68  }
69 
73  public function getSupportedMajorReleases(): array
74  {
75  $url = 'major';
76  $result = $this->fetchFromRemote($url);
77 
78  $majorReleases = [
79  'community' => [],
80  'elts' => [],
81  ];
82  foreach ($result as $release) {
83  $majorRelease = ‪MajorRelease::fromApiResponse($release);
84  $maintenanceWindow = $majorRelease->getMaintenanceWindow();
85 
86  if ($maintenanceWindow->isSupportedByCommunity()) {
87  $group = 'community';
88  } elseif ($maintenanceWindow->isSupportedByElts()) {
89  $group = 'elts';
90  } else {
91  // Major version is unsupported
92  continue;
93  }
94 
95  $majorReleases[$group][] = $majorRelease->getLts() ?? $majorRelease->getVersion();
96  }
97 
98  return $majorReleases;
99  }
100 
101  public function isPatchReleaseSuitableForUpdate(CoreRelease $coreRelease): bool
102  {
103  return version_compare($this->getInstalledVersion(), $coreRelease->getVersion()) === -1;
104  }
105 
112  public function isUpdateSecurityRelevant(CoreRelease $releaseToCheck): bool
113  {
114  $url = 'major/' . $this->getInstalledMajorVersion() . '/release';
115  $result = $this->fetchFromRemote($url);
116 
117  $installedVersion = $this->getInstalledVersion();
118  foreach ($result as $release) {
119  $coreRelease = ‪CoreRelease::fromApiResponse($release);
120  if ($coreRelease->isSecurityUpdate()
121  && version_compare($installedVersion, $coreRelease->getVersion()) === -1 // installed version is lower than release
122  && version_compare($releaseToCheck->getVersion(), $coreRelease->getVersion()) > -1 // release to check is equal or higher than release
123  ) {
124  return true;
125  }
126  }
127 
128  return false;
129  }
130 
131  public function isCurrentInstalledVersionElts(): bool
132  {
133  $url = 'major/' . $this->getInstalledMajorVersion() . '/release';
134  $result = $this->fetchFromRemote($url);
135 
136  $installedVersion = $this->getInstalledVersion();
137  foreach ($result as $release) {
138  if (version_compare($installedVersion, $release['version']) === 0) {
139  return $release['elts'] ?? false;
140  }
141  }
142 
143  return false;
144  }
145 
152  public function getYoungestPatchRelease(): CoreRelease
153  {
154  $url = 'major/' . $this->getInstalledMajorVersion() . '/release/latest';
155  $result = $this->fetchFromRemote($url);
156  return ‪CoreRelease::fromApiResponse($result);
157  }
158 
159  public function getYoungestCommunityPatchRelease(): CoreRelease
160  {
161  $url = 'major/' . $this->getInstalledMajorVersion() . '/release';
162  $result = $this->fetchFromRemote($url);
163 
164  // Make sure all releases are sorted by their version
165  $columns = array_column($result, 'version');
166  array_multisort($columns, SORT_NATURAL, $result);
167 
168  // Remove any ELTS release
169  $releases = array_filter($result, static function (array $release) {
170  return ($release['elts'] ?? false) === false;
171  });
172 
173  $latestRelease = end($releases);
174 
175  return ‪CoreRelease::fromApiResponse($latestRelease);
176  }
177 
183  protected function fetchFromRemote(string $url): array
184  {
185  $url = $this->apiBaseUrl . $url;
186  $json = ‪GeneralUtility::getUrl($url);
187 
188  if (!$json) {
189  $this->throwFetchException($url);
190  }
191  return json_decode($json, true);
192  }
193 
199  protected function getInstalledMajorVersion(): string
200  {
201  return (string)GeneralUtility::makeInstance(Typo3Version::class)->getMajorVersion();
202  }
203 
210  protected function throwFetchException(string $url): void
211  {
212  throw new RemoteFetchException(
213  'Fetching ' .
214  $url .
215  ' failed. Maybe this instance can not connect to the remote system properly.',
216  1380897593
217  );
218  }
219 }
‪TYPO3\CMS\Core\Information\Typo3Version
Definition: Typo3Version.php:21
‪TYPO3\CMS\Core\Utility\GeneralUtility\getUrl
‪static mixed getUrl($url, $includeHeader=0, $requestHeaders=null, &$report=null)
Definition: GeneralUtility.php:1748
‪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\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\Service\Exception\RemoteFetchException
Definition: RemoteFetchException.php:24
‪TYPO3\CMS\Install\CoreVersion\MajorRelease
Definition: MajorRelease.php:21
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Install\Service
Definition: ClearCacheService.php:16