‪TYPO3CMS  10.4
Helper.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
31 
37 {
56 
63 
69  protected ‪$repository;
70 
74  protected ‪$extensionRepository;
75 
79  protected ‪$objectManager;
80 
81  public function ‪__construct()
82  {
83  $this->objectManager = GeneralUtility::makeInstance(ObjectManager::class);
84  $repositoryRepository = $this->objectManager->get(RepositoryRepository::class);
85  $this->extensionRepository = $this->objectManager->get(ExtensionRepository::class);
87  ‪$repository = $repositoryRepository->findOneTypo3OrgRepository();
88  if (is_object(‪$repository)) {
90  }
91  }
92 
101  {
102  $this->repository = ‪$repository;
103  }
104 
113  public function ‪fetchExtListFile()
114  {
116  }
117 
126  public function ‪fetchMirrorListFile()
127  {
129  }
130 
141  protected function ‪fetchFile($remoteResource, $localResource)
142  {
143  $isOffline = (bool)GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('extensionmanager', 'offlineMode');
144  if ($isOffline) {
145  throw new ExtensionManagerException('Extension Manager is in offline mode. No TER connection available.', 1437078780);
146  }
147  if (is_string($remoteResource) && is_string($localResource) && !empty($remoteResource) && !empty($localResource)) {
148  $fileContent = ‪GeneralUtility::getUrl($remoteResource);
149  if ($fileContent !== false) {
150  if (‪GeneralUtility::writeFileToTypo3tempDir($localResource, $fileContent) !== null) {
151  throw new ExtensionManagerException(sprintf('Could not write to file %s.', $localResource), 1342635378);
152  }
153  } else {
154  throw new ExtensionManagerException(sprintf('Could not access remote resource %s.', $remoteResource), 1342635425);
155  }
156  }
157  }
158 
165  public function ‪getLocalExtListFile()
166  {
167  return ‪Environment::getVarPath() . '/extensionmanager/' . (int)$this->repository->getUid() . '.extensions.xml.gz';
168  }
169 
176  public function ‪getRemoteExtListFile()
177  {
178  $mirror = $this->‪getMirrors(true)->‪getMirror();
179  $filePath = 'https://' . $mirror['host'] . $mirror['path'] . 'extensions.xml.gz';
180  return $filePath;
181  }
182 
189  public function ‪getRemoteExtHashFile()
190  {
191  $mirror = $this->‪getMirrors(true)->‪getMirror();
192  $filePath = 'https://' . $mirror['host'] . $mirror['path'] . 'extensions.md5';
193  return $filePath;
194  }
195 
202  public function ‪getLocalMirrorListFile()
203  {
204  return ‪Environment::getVarPath() . '/extensionmanager/' . (int)$this->repository->getUid() . '.mirrors.xml.gz';
205  }
206 
213  public function ‪getRemoteMirrorListFile()
214  {
215  $filePath = $this->repository->getMirrorListUrl();
216  return $filePath;
217  }
218 
230  public function ‪getMirrors($forcedUpdateFromRemote = true)
231  {
232  $assignedMirror = $this->repository->‪getMirrors();
233  if ($forcedUpdateFromRemote || $assignedMirror === null || !is_object($assignedMirror)) {
234  if ($forcedUpdateFromRemote || !is_file($this->‪getLocalMirrorListFile())) {
235  $this->‪fetchMirrorListFile();
236  }
237  $objMirrorListImporter = GeneralUtility::makeInstance(MirrorListUtility::class);
238  $this->repository->addMirrors($objMirrorListImporter->getMirrors($this->getLocalMirrorListFile()));
239  }
240  return $this->repository->getMirrors();
241  }
242 
251  public function ‪isExtListUpdateNecessary()
252  {
253  if ($this->repository === null) {
254  throw new ExtensionManagerException('No extension repository was found.', 1500060252);
255  }
256  $updateNecessity = 0;
257  if ($this->extensionRepository->countByRepository($this->repository->getUid()) <= 0) {
258  $updateNecessity |= ‪self::PROBLEM_NO_VERSIONS_IN_DATABASE;
259  }
260  if (!is_file($this->‪getLocalExtListFile())) {
262  } else {
263  $remotemd5 = ‪GeneralUtility::getUrl($this->‪getRemoteExtHashFile());
264  if ($remotemd5 !== false) {
265  $localmd5 = md5_file($this->‪getLocalExtListFile());
266  if ($remotemd5 !== $localmd5) {
267  $updateNecessity |= ‪self::PROBLEM_EXTENSION_HASH_CHANGED;
268  }
269  } else {
270  throw new ExtensionManagerException('Could not retrieve extension hash file from remote server.', 1342635016);
271  }
272  }
273  return $updateNecessity;
274  }
275 
284  public function ‪updateExtList()
285  {
286  $updated = false;
287  $updateNecessity = $this->‪isExtListUpdateNecessary();
288  if ($updateNecessity !== 0) {
289  // retrieval of file necessary
290  $tmpBitmask = self::PROBLEM_EXTENSION_FILE_NOT_EXISTING | ‪self::PROBLEM_EXTENSION_HASH_CHANGED;
291  if (($tmpBitmask & $updateNecessity) > 0) {
292  $this->‪fetchExtListFile();
293  $updateNecessity &= ~$tmpBitmask;
294  }
295  // database table cleanup
296  if ($updateNecessity & self::PROBLEM_NO_VERSIONS_IN_DATABASE) {
297  $updateNecessity &= ~self‪::PROBLEM_NO_VERSIONS_IN_DATABASE;
298  } else {
299  // Use straight query as extbase "remove" is too slow here
300  // This truncates the whole table. It would be more correct to remove only rows of a specific
301  // repository, but multiple repository handling is not implemented, and truncate is quicker.
302  GeneralUtility::makeInstance(ConnectionPool::class)
303  ->getConnectionForTable('tx_extensionmanager_domain_model_extension')
304  ->truncate('tx_extensionmanager_domain_model_extension');
305  }
306  // no further problems - start of import process
307  if ($updateNecessity === 0) {
308  $uid = $this->repository->getUid();
309  $objExtListImporter = $this->objectManager->get(ExtensionListUtility::class);
310  $objExtListImporter->import($this->‪getLocalExtListFile(), $uid);
311  $updated = true;
312  }
313  }
314  return $updated;
315  }
316 }
‪TYPO3\CMS\Extensionmanager\Utility\Importer\MirrorListUtility
Definition: MirrorListUtility.php:30
‪TYPO3\CMS\Extensionmanager\Utility\Repository
Definition: Helper.php:16
‪TYPO3\CMS\Extensionmanager\Utility\Repository\Helper\fetchFile
‪fetchFile($remoteResource, $localResource)
Definition: Helper.php:138
‪TYPO3\CMS\Extensionmanager\Utility\Repository\Helper\getLocalMirrorListFile
‪string getLocalMirrorListFile()
Definition: Helper.php:199
‪TYPO3\CMS\Core\Configuration\ExtensionConfiguration
Definition: ExtensionConfiguration.php:45
‪TYPO3\CMS\Extensionmanager\Utility\Repository\Helper\PROBLEM_NO_VERSIONS_IN_DATABASE
‪const PROBLEM_NO_VERSIONS_IN_DATABASE
Definition: Helper.php:62
‪TYPO3\CMS\Core\Utility\GeneralUtility\getUrl
‪static mixed getUrl($url, $includeHeader=0, $requestHeaders=null, &$report=null)
Definition: GeneralUtility.php:1748
‪TYPO3\CMS\Extensionmanager\Domain\Repository\RepositoryRepository
Definition: RepositoryRepository.php:26
‪TYPO3\CMS\Extensionmanager\Domain\Model\Mirrors\getMirrors
‪array getMirrors()
Definition: Mirrors.php:101
‪TYPO3\CMS\Extensionmanager\Utility\Repository\Helper\updateExtList
‪bool updateExtList()
Definition: Helper.php:281
‪TYPO3\CMS\Extensionmanager\Utility\Repository\Helper\fetchExtListFile
‪fetchExtListFile()
Definition: Helper.php:110
‪TYPO3\CMS\Extensionmanager\Utility\Repository\Helper\getLocalExtListFile
‪string getLocalExtListFile()
Definition: Helper.php:162
‪TYPO3\CMS\Extensionmanager\Utility\Repository\Helper\PROBLEM_EXTENSION_FILE_NOT_EXISTING
‪const PROBLEM_EXTENSION_FILE_NOT_EXISTING
Definition: Helper.php:49
‪TYPO3\CMS\Extensionmanager\Domain\Model\Mirrors
Definition: Mirrors.php:25
‪TYPO3\CMS\Extensionmanager\Utility\Repository\Helper\$extensionRepository
‪ExtensionRepository $extensionRepository
Definition: Helper.php:72
‪TYPO3\CMS\Extensionmanager\Utility\Repository\Helper\$repository
‪Repository $repository
Definition: Helper.php:68
‪TYPO3\CMS\Extensionmanager\Utility\Importer\ExtensionListUtility
Definition: ExtensionListUtility.php:35
‪TYPO3\CMS\Extensionmanager\Utility\Repository\Helper\isExtListUpdateNecessary
‪int isExtListUpdateNecessary()
Definition: Helper.php:248
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository
Definition: ExtensionRepository.php:35
‪TYPO3\CMS\Extensionmanager\Domain\Model\Repository
Definition: Repository.php:26
‪TYPO3\CMS\Core\Utility\GeneralUtility\writeFileToTypo3tempDir
‪static string writeFileToTypo3tempDir($filepath, $content)
Definition: GeneralUtility.php:1928
‪TYPO3\CMS\Extensionmanager\Utility\Repository\Helper\getRemoteExtHashFile
‪string getRemoteExtHashFile()
Definition: Helper.php:186
‪TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
Definition: ExtensionManagerException.php:24
‪TYPO3\CMS\Extensionmanager\Utility\Repository\Helper\getRemoteMirrorListFile
‪string getRemoteMirrorListFile()
Definition: Helper.php:210
‪TYPO3\CMS\Extensionmanager\Utility\Repository\Helper\getMirrors
‪Mirrors getMirrors($forcedUpdateFromRemote=true)
Definition: Helper.php:227
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:23
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:40
‪TYPO3\CMS\Extensionmanager\Utility\Repository\Helper\getRemoteExtListFile
‪string getRemoteExtListFile()
Definition: Helper.php:173
‪TYPO3\CMS\Extensionmanager\Utility\Repository\Helper
Definition: Helper.php:37
‪TYPO3\CMS\Extensionmanager\Utility\Repository\Helper\__construct
‪__construct()
Definition: Helper.php:78
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Extensionmanager\Utility\Repository\Helper\setRepository
‪setRepository(Repository $repository)
Definition: Helper.php:97
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Extensionmanager\Utility\Repository\Helper\fetchMirrorListFile
‪fetchMirrorListFile()
Definition: Helper.php:123
‪TYPO3\CMS\Extensionmanager\Domain\Model\Mirrors\getMirror
‪array getMirror()
Definition: Mirrors.php:71
‪TYPO3\CMS\Extbase\Object\ObjectManager
Definition: ObjectManager.php:28
‪TYPO3\CMS\Extensionmanager\Utility\Repository\Helper\PROBLEM_EXTENSION_HASH_CHANGED
‪const PROBLEM_EXTENSION_HASH_CHANGED
Definition: Helper.php:55
‪TYPO3\CMS\Extensionmanager\Utility\Repository\Helper\$objectManager
‪ObjectManager $objectManager
Definition: Helper.php:76
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static string getVarPath()
Definition: Environment.php:192