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