e92ecef8d81dfb6e727614374e9eb562b5d4561f
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Classes / Utility / ListUtility.php
1 <?php
2 namespace TYPO3\CMS\Extensionmanager\Utility;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Package\PackageInterface;
18 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Core\Utility\VersionNumberUtility;
21 use TYPO3\CMS\Extensionmanager\Domain\Model\Extension;
22
23 /**
24 * Utility for dealing with extension list related functions
25 *
26 * @TODO: Refactor this API class:
27 * - The methods depend on each other, they take each others result, that could be done internally
28 * - There is no good wording to distinguish existing and loaded extensions
29 * - The name 'listUtility' is not good, the methods could be moved to some 'extensionInformationUtility', or a repository?
30 *
31 * @author Susanne Moog <typo3@susannemoog.de>
32 */
33 class ListUtility implements \TYPO3\CMS\Core\SingletonInterface {
34
35 /**
36 * @var \TYPO3\CMS\Extensionmanager\Utility\EmConfUtility
37 * @inject
38 */
39 protected $emConfUtility;
40
41 /**
42 * @var \TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository
43 * @inject
44 */
45 protected $extensionRepository;
46
47 /**
48 * @var \TYPO3\CMS\Extensionmanager\Utility\InstallUtility
49 * @inject
50 */
51 protected $installUtility;
52
53 /**
54 * @var \TYPO3\CMS\Core\Package\PackageManager
55 * @inject
56 */
57 protected $packageManager;
58
59 /**
60 * @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
61 * @inject
62 */
63 protected $signalSlotDispatcher;
64
65 /**
66 * Returns the list of available, but not necessarily loaded extensions
67 *
68 * @return array Array with two sub-arrays, list array (all extensions with info) and category index
69 * @see getInstExtList()
70 */
71 public function getAvailableExtensions() {
72 $this->emitPackagesMayHaveChangedSignal();
73 $extensions = array();
74 foreach ($this->packageManager->getAvailablePackages() as $package) {
75 // Only TYPO3 related packages could be handled by the extension manager
76 // Composer packages from "Packages" folder will be instantiated as \TYPO3\Flow\Package\Package
77 if (!($package instanceof \TYPO3\CMS\Core\Package\PackageInterface)) {
78 continue;
79 }
80 $installationType = $this->getInstallTypeForPackage($package);
81 $extensions[$package->getPackageKey()] = array(
82 'siteRelPath' => str_replace(PATH_site, '', $package->getPackagePath()),
83 'type' => $installationType,
84 'key' => $package->getPackageKey(),
85 'ext_icon' => ExtensionManagementUtility::getExtensionIcon($package->getPackagePath()),
86 );
87 }
88 return $extensions;
89 }
90
91 /**
92 * Emits packages may have changed signal
93 */
94 protected function emitPackagesMayHaveChangedSignal() {
95 $this->signalSlotDispatcher->dispatch('PackageManagement', 'packagesMayHaveChanged');
96 }
97
98 /**
99 * Returns "System", "Global" or "Local" based on extension position in filesystem.
100 *
101 * @param PackageInterface $package
102 * @return string
103 */
104 protected function getInstallTypeForPackage(PackageInterface $package) {
105 foreach (Extension::returnInstallPaths() as $installType => $installPath) {
106 if (GeneralUtility::isFirstPartOfStr($package->getPackagePath(), $installPath)) {
107 return $installType;
108 }
109 }
110 return '';
111 }
112
113 /**
114 * Enrich the output of getAvailableExtensions() with an array key installed = 1 if an extension is loaded.
115 *
116 * @param array $availableExtensions
117 * @return array
118 */
119 public function getAvailableAndInstalledExtensions(array $availableExtensions) {
120 foreach ($this->packageManager->getActivePackages() as $extKey => $_) {
121 if (isset($availableExtensions[$extKey])) {
122 $availableExtensions[$extKey]['installed'] = TRUE;
123 }
124 }
125 return $availableExtensions;
126 }
127
128 /**
129 * Adds the information from the emconf array to the extension information
130 *
131 * @param array $extensions
132 * @return array
133 */
134 public function enrichExtensionsWithEmConfAndTerInformation(array $extensions) {
135 foreach ($extensions as $extensionKey => $properties) {
136 $emconf = $this->emConfUtility->includeEmConf($properties);
137 if ($emconf) {
138 $extensions[$extensionKey] = array_merge($emconf, $properties);
139 $terObject = $this->getExtensionTerData($extensionKey, $extensions[$extensionKey]['version']);
140 if ($terObject !== NULL) {
141 $extensions[$extensionKey]['terObject'] = $terObject;
142 $extensions[$extensionKey]['updateAvailable'] = $this->installUtility->isUpdateAvailable($terObject);
143 $extensions[$extensionKey]['updateToVersion'] = $this->extensionRepository->findHighestAvailableVersion($extensionKey);
144 }
145 } else {
146 unset($extensions[$extensionKey]);
147 }
148 }
149 return $extensions;
150 }
151
152 /**
153 * Tries to find given extension with given version in TER data.
154 * If extension is found but not the given version, we return TER data from highest version with version data set to
155 * given one.
156 *
157 * @param string $extensionKey Key of the extension
158 * @param string $version String representation of version number
159 * @return Extension|NULL Extension TER object or NULL if nothing found
160 */
161 protected function getExtensionTerData($extensionKey, $version) {
162 $terObject = $this->extensionRepository->findOneByExtensionKeyAndVersion($extensionKey, $version);
163 if (!$terObject instanceof Extension) {
164 // Version unknown in TER data, try to find extension
165 $terObject = $this->extensionRepository->findHighestAvailableVersion($extensionKey);
166 if ($terObject instanceof Extension) {
167 // Found in TER now, set version information to the known ones, so we can look if there is a newer one
168 // Use a cloned object, otherwise wrong information is stored in persistenceManager
169 $terObject = clone $terObject;
170 $terObject->setVersion($version);
171 $terObject->setIntegerVersion(
172 VersionNumberUtility::convertVersionNumberToInteger($terObject->getVersion())
173 );
174 } else {
175 $terObject = NULL;
176 }
177 }
178
179 return $terObject;
180 }
181
182 /**
183 * Adds information about icon size to the extension information
184 *
185 * @param array $extensions
186 * @return array
187 */
188 public function enrichExtensionsWithIconInformation(array $extensions) {
189 foreach ($extensions as &$properties) {
190 $iInfo = @getimagesize(PATH_site . $properties['siteRelPath'] . $properties['ext_icon']);
191 if ($iInfo !== FALSE) {
192 $properties['ext_icon_width'] = $iInfo[0];
193 $properties['ext_icon_height'] = $iInfo[1];
194 } else {
195 $properties['ext_icon_width'] = 0;
196 $properties['ext_icon_height'] = 0;
197 }
198 }
199 unset($properties);
200 return $extensions;
201 }
202
203 /**
204 * Gets all available and installed extension with additional information
205 * from em_conf and TER (if available)
206 *
207 * @return array
208 */
209 public function getAvailableAndInstalledExtensionsWithAdditionalInformation() {
210 $availableExtensions = $this->getAvailableExtensions();
211 $availableAndInstalledExtensions = $this->getAvailableAndInstalledExtensions($availableExtensions);
212 $availableAndInstalledExtensions = $this->enrichExtensionsWithIconInformation($availableAndInstalledExtensions);
213 return $this->enrichExtensionsWithEmConfAndTerInformation($availableAndInstalledExtensions);
214 }
215
216 }