[TASK] Rename GeneralUtility in Documentation sysext
[Packages/TYPO3.CMS.git] / typo3 / sysext / documentation / Classes / Domain / Repository / DocumentRepository.php
1 <?php
2 namespace TYPO3\CMS\Documentation\Domain\Repository;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2013-2014 Xavier Perseguers <xavier@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 use TYPO3\CMS\Core\Utility\GeneralUtility;
28
29 /**
30 * An extension helper repository to be used in ext:documentation context
31 *
32 * @author Xavier Perseguers <xavier@typo3.org>
33 */
34 class DocumentRepository {
35
36 /**
37 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
38 * @inject
39 */
40 protected $objectManager;
41
42 /**
43 * Finds all documents.
44 *
45 * @return \TYPO3\CMS\Documentation\Domain\Model\Document[]
46 */
47 public function findAll() {
48 $documents = $this->findSphinxDocuments();
49 $openOfficeDocuments = $this->findOpenOfficeDocuments();
50
51 // Add OpenOffice documents if there is not already an existing, non OpenOffice version
52 foreach ($openOfficeDocuments as $documentKey => $document) {
53 if (!isset($documents[$documentKey])) {
54 $documents[$documentKey] = $document;
55 }
56 }
57
58 return $documents;
59 }
60
61 /**
62 * Finds documents by language, always falls back to 'default' (English).
63 *
64 * @param string $language
65 * @return \TYPO3\CMS\Documentation\Domain\Model\Document[]
66 */
67 public function findByLanguage($language) {
68 $allDocuments = $this->findAll();
69
70 // Initialize the dependency of languages
71 $languageDependencies = array();
72 /** @var $locales \TYPO3\CMS\Core\Localization\Locales */
73 $locales = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Localization\\Locales');
74 // Language is found. Configure it:
75 $shortLanguage = $language;
76 if (!in_array($shortLanguage, $locales->getLocales()) && strpos($shortLanguage, '_') !== FALSE) {
77 list($shortLanguage, $_) = explode('_', $shortLanguage);
78 }
79 if (in_array($shortLanguage, $locales->getLocales())) {
80 $languageDependencies[] = $language;
81 if ($language !== $shortLanguage) {
82 $languageDependencies[] = $shortLanguage;
83 }
84 foreach ($locales->getLocaleDependencies($shortLanguage) as $languageDependency) {
85 $languageDependencies[] = $languageDependency;
86 }
87 }
88 if ($language !== 'default') {
89 $languageDependencies[] = 'default';
90 }
91
92 foreach ($allDocuments as $document) {
93 // Remove every unwanted translation
94 $selectedTranslation = NULL;
95 $highestPriorityLanguageIndex = count($languageDependencies);
96
97 $translations = $document->getTranslations();
98 foreach ($translations as $translation) {
99 $languageIndex = array_search($translation->getLanguage(), $languageDependencies);
100 if ($languageIndex !== FALSE) {
101 if ($languageIndex < $highestPriorityLanguageIndex) {
102 $selectedTranslation = $translation;
103 $highestPriorityLanguageIndex = $languageIndex;
104 }
105 } else {
106 // No exact translation found, perhaps another locale would fit as well. E.g., when requesting
107 // a documentation as fr_CA but only fr_FR exists
108 if (strpos($translation->getLanguage(), '_') !== FALSE) {
109 list($translationLanguage, $_) = explode('_', $translation->getLanguage());
110 $languageIndex = array_search($translationLanguage, $languageDependencies);
111 if ($languageIndex !== FALSE && $languageIndex < $highestPriorityLanguageIndex) {
112 $selectedTranslation = $translation;
113 $highestPriorityLanguageIndex = $languageIndex;
114 }
115 }
116 }
117 }
118
119 $newTranslations = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
120 $document->setTranslations($newTranslations);
121 if ($selectedTranslation !== NULL) {
122 $document->addTranslation($selectedTranslation);
123 }
124
125 }
126
127 return $allDocuments;
128 }
129
130 /**
131 * Retrieves Sphinx documents.
132 *
133 * @return array
134 */
135 protected function findSphinxDocuments() {
136 $basePath = 'typo3conf/Documentation/';
137
138 $documents = array();
139 $documentKeys = GeneralUtility::get_dirs(PATH_site . $basePath);
140 // Early return in case no document keys were found
141 if (!is_array($documentKeys)) {
142 return $documents;
143 }
144
145 foreach ($documentKeys as $documentKey) {
146 $icon = \TYPO3\CMS\Documentation\Utility\MiscUtility::getIcon($documentKey);
147
148 /** @var \TYPO3\CMS\Documentation\Domain\Model\Document $document */
149 $document = $this->objectManager->get('TYPO3\\CMS\\Documentation\\Domain\\Model\\Document')
150 ->setPackageKey($documentKey)
151 ->setIcon($icon);
152
153 $languagePath = $basePath . $documentKey . '/';
154 $languages = GeneralUtility::get_dirs(PATH_site . $languagePath);
155 foreach ($languages as $language) {
156 $metadata = $this->getMetadata($documentKey, $language);
157 if (!empty($metadata['extensionKey'])) {
158 $document->setExtensionKey($metadata['extensionKey']);
159 }
160
161 /** @var \TYPO3\CMS\Documentation\Domain\Model\DocumentTranslation $documentTranslation */
162 $documentTranslation = $this->objectManager->get('TYPO3\\CMS\\Documentation\\Domain\\Model\\DocumentTranslation')
163 ->setLanguage($language)
164 ->setTitle($metadata['title'])
165 ->setDescription($metadata['description']);
166
167 $formatPath = $languagePath . $language . '/';
168 $formats = GeneralUtility::get_dirs(PATH_site . $formatPath);
169 foreach ($formats as $format) {
170 $documentFile = '';
171 switch ($format) {
172 case 'html':
173 // Try to find a valid index file
174 $indexFiles = array('Index.html', 'index.html', 'index.htm');
175 foreach ($indexFiles as $indexFile) {
176 if (file_exists(PATH_site . $formatPath . $format . '/' . $indexFile)) {
177 $documentFile = $indexFile;
178 break;
179 }
180 }
181 break;
182 case 'pdf':
183 // Retrieve first PDF
184 $files = GeneralUtility::getFilesInDir(PATH_site . $formatPath . $format, 'pdf');
185 if (count($files) > 0) {
186 $documentFile = current($files);
187 }
188 break;
189 }
190 if (!empty($documentFile)) {
191 /** @var \TYPO3\CMS\Documentation\Domain\Model\DocumentFormat $documentFormat */
192 $documentFormat = $this->objectManager->get('TYPO3\\CMS\\Documentation\\Domain\\Model\\DocumentFormat')
193 ->setFormat($format)
194 ->setPath($formatPath . $format . '/' . $documentFile);
195
196 $documentTranslation->addFormat($documentFormat);
197 }
198 }
199
200 if (count($documentTranslation->getFormats()) > 0) {
201 $document->addTranslation($documentTranslation);
202 $documents[$documentKey] = $document;
203 }
204 }
205 }
206
207 return $documents;
208 }
209
210 /**
211 * Retrieves OpenOffice documents (manual.sxw).
212 *
213 * @return array
214 */
215 protected function findOpenOfficeDocuments() {
216 $documents = array();
217 $language = 'default';
218
219 foreach ($GLOBALS['TYPO3_LOADED_EXT'] as $extensionKey => $extensionData) {
220 $path = $extensionData['siteRelPath'] . 'doc/';
221 if (is_file(PATH_site . $path . 'manual.sxw')) {
222 $documentKey = 'typo3cms.extensions.' . $extensionKey;
223 $icon = \TYPO3\CMS\Documentation\Utility\MiscUtility::getIcon($documentKey);
224
225 /** @var \TYPO3\CMS\Documentation\Domain\Model\Document $document */
226 $document = $this->objectManager->get('TYPO3\\CMS\\Documentation\\Domain\\Model\\Document')
227 ->setPackageKey($documentKey)
228 ->setExtensionKey($extensionKey)
229 ->setIcon($icon);
230
231 $metadata = $this->getMetadata($documentKey, $language);
232 /** @var \TYPO3\CMS\Documentation\Domain\Model\DocumentTranslation $documentTranslation */
233 $documentTranslation = $this->objectManager->get('TYPO3\\CMS\\Documentation\\Domain\\Model\\DocumentTranslation')
234 ->setLanguage($language)
235 ->setTitle($metadata['title'])
236 ->setDescription($metadata['description']);
237
238 /** @var \TYPO3\CMS\Documentation\Domain\Model\DocumentFormat $documentFormat */
239 $documentFormat = $this->objectManager->get('TYPO3\\CMS\\Documentation\\Domain\\Model\\DocumentFormat')
240 ->setFormat('sxw')
241 ->setPath($path . 'manual.sxw');
242
243 $documentTranslation->addFormat($documentFormat);
244 $document->addTranslation($documentTranslation);
245 $documents[$documentKey] = $document;
246 }
247 }
248
249 return $documents;
250 }
251
252 /**
253 * Returns metadata associated to a given document key.
254 *
255 * @param string $documentKey
256 * @param string $language
257 * @return array
258 */
259 protected function getMetadata($documentKey, $language) {
260 $documentPath = PATH_site . 'typo3conf/Documentation/' . $documentKey . '/' . $language . '/';
261 $metadata = array(
262 'title' => $documentKey,
263 'description' => '',
264 );
265 if (GeneralUtility::isFirstPartOfStr($documentKey, 'typo3cms.extensions.')) {
266 $extensionKey = substr($documentKey, 20);
267 if (\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded($extensionKey)) {
268 $metadata = \TYPO3\CMS\Documentation\Utility\MiscUtility::getExtensionMetaData($extensionKey);
269 }
270 } elseif (is_file($documentPath . 'composer.json')) {
271 $info = json_decode(file_get_contents($documentPath . 'composer.json'), TRUE);
272 if (is_array($info)) {
273 $metadata['title'] = $info['name'];
274 $metadata['description'] = $info['description'];
275 }
276 }
277 return $metadata;
278 }
279
280 }