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