‪TYPO3CMS  11.5
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 
74  public function getSupportedMajorReleases(): array
75  {
76  $url = 'major';
77  $result = $this->fetchFromRemote($url);
78 
79  $majorReleases = [
80  'community' => [],
81  'elts' => [],
82  ];
83  foreach ($result as $release) {
84  $majorRelease = ‪MajorRelease::fromApiResponse($release);
85  $maintenanceWindow = $majorRelease->getMaintenanceWindow();
86 
87  if ($maintenanceWindow->isSupportedByCommunity()) {
88  $group = 'community';
89  } elseif ($maintenanceWindow->isSupportedByElts()) {
90  $group = 'elts';
91  } else {
92  // Major version is unsupported
93  continue;
94  }
95 
96  $majorReleases[$group][] = $majorRelease->getLts() ?? $majorRelease->getVersion();
97  }
98 
99  return $majorReleases;
100  }
101 
102  public function isPatchReleaseSuitableForUpdate(CoreRelease $coreRelease): bool
103  {
104  return version_compare($this->getInstalledVersion(), $coreRelease->getVersion()) === -1;
105  }
106 
113  public function isUpdateSecurityRelevant(CoreRelease $releaseToCheck): bool
114  {
115  $url = 'major/' . $this->getInstalledMajorVersion() . '/release';
116  $result = $this->fetchFromRemote($url);
117 
118  $installedVersion = $this->getInstalledVersion();
119  foreach ($result as $release) {
120  $coreRelease = ‪CoreRelease::fromApiResponse($release);
121  if ($coreRelease->isSecurityUpdate()
122  && version_compare($installedVersion, $coreRelease->getVersion()) === -1 // installed version is lower than release
123  && version_compare($releaseToCheck->getVersion(), $coreRelease->getVersion()) > -1 // release to check is equal or higher than release
124  ) {
125  return true;
126  }
127  }
128 
129  return false;
130  }
131 
132  public function isCurrentInstalledVersionElts(): bool
133  {
134  $url = 'major/' . $this->getInstalledMajorVersion() . '/release';
135  $result = $this->fetchFromRemote($url);
136 
137  $installedVersion = $this->getInstalledVersion();
138  foreach ($result as $release) {
139  if (version_compare($installedVersion, $release['version']) === 0) {
140  return $release['elts'] ?? false;
141  }
142  }
143 
144  return false;
145  }
146 
153  public function getYoungestPatchRelease(): CoreRelease
154  {
155  $url = 'major/' . $this->getInstalledMajorVersion() . '/release/latest';
156  $result = $this->fetchFromRemote($url);
157  return ‪CoreRelease::fromApiResponse($result);
158  }
159 
160  public function getYoungestCommunityPatchRelease(): CoreRelease
161  {
162  $url = 'major/' . $this->getInstalledMajorVersion() . '/release';
163  $result = $this->fetchFromRemote($url);
164 
165  // Make sure all releases are sorted by their version
166  $columns = array_column($result, 'version');
167  array_multisort($columns, SORT_NATURAL, $result);
168 
169  // Remove any ELTS release
170  $releases = array_filter($result, static function (array $release) {
171  return ($release['elts'] ?? false) === false;
172  });
173 
174  $latestRelease = end($releases);
175 
176  return ‪CoreRelease::fromApiResponse($latestRelease);
177  }
178 
184  protected function fetchFromRemote(string $url): array
185  {
186  $url = $this->apiBaseUrl . $url;
187  $json = ‪GeneralUtility::getUrl($url);
188 
189  if (!$json) {
190  $this->throwFetchException($url);
191  }
192  return json_decode($json, true);
193  }
194 
200  protected function getInstalledMajorVersion(): string
201  {
202  return (string)GeneralUtility::makeInstance(Typo3Version::class)->getMajorVersion();
203  }
204 
211  protected function throwFetchException(string $url): void
212  {
213  throw new RemoteFetchException(
214  'Fetching ' .
215  $url .
216  ' failed. Maybe this instance can not connect to the remote system properly.',
217  1380897593
218  );
219  }
220 }
‪TYPO3\CMS\Core\Information\Typo3Version
Definition: Typo3Version.php:21
‪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\GeneralUtility\getUrl
‪static string false getUrl($url)
Definition: GeneralUtility.php:1697
‪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:23
‪TYPO3\CMS\Install\CoreVersion\MajorRelease
Definition: MajorRelease.php:21
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Install\Service
Definition: ClearCacheService.php:16