TYPO3 CMS  TYPO3_7-6
DocumentRepository.php
Go to the documentation of this file.
1 <?php
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 
18 
23 {
27  protected $objectManager;
28 
32  public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
33  {
34  $this->objectManager = $objectManager;
35  }
36 
42  public function findAll()
43  {
44  $documents = $this->findSphinxDocuments();
45  $openOfficeDocuments = $this->findOpenOfficeDocuments();
46 
47  // Add OpenOffice documents if there is not already an existing, non OpenOffice version
48  foreach ($openOfficeDocuments as $documentKey => $document) {
49  if (!isset($documents[$documentKey])) {
50  $documents[$documentKey] = $document;
51  }
52  }
53 
54  return $documents;
55  }
56 
63  public function findByLanguage($language)
64  {
65  $allDocuments = $this->findAll();
66 
67  // Initialize the dependency of languages
68  $languageDependencies = [];
70  $locales = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Localization\Locales::class);
71  // Language is found. Configure it:
72  $shortLanguage = $language;
73  if (!in_array($shortLanguage, $locales->getLocales()) && strpos($shortLanguage, '_') !== false) {
74  list($shortLanguage, $_) = explode('_', $shortLanguage);
75  }
76  if (in_array($shortLanguage, $locales->getLocales())) {
77  $languageDependencies[] = $language;
78  if ($language !== $shortLanguage) {
79  $languageDependencies[] = $shortLanguage;
80  }
81  foreach ($locales->getLocaleDependencies($shortLanguage) as $languageDependency) {
82  $languageDependencies[] = $languageDependency;
83  }
84  }
85  if ($language !== 'default') {
86  $languageDependencies[] = 'default';
87  }
88 
89  foreach ($allDocuments as $document) {
90  // Remove every unwanted translation
91  $selectedTranslation = null;
92  $highestPriorityLanguageIndex = count($languageDependencies);
93 
94  $translations = $document->getTranslations();
95  foreach ($translations as $translation) {
96  $languageIndex = array_search($translation->getLanguage(), $languageDependencies);
97  if ($languageIndex !== false) {
98  if ($languageIndex < $highestPriorityLanguageIndex) {
99  $selectedTranslation = $translation;
100  $highestPriorityLanguageIndex = $languageIndex;
101  }
102  } else {
103  // No exact translation found, perhaps another locale would fit as well. E.g., when requesting
104  // a documentation as fr_CA but only fr_FR exists
105  if (strpos($translation->getLanguage(), '_') !== false) {
106  list($translationLanguage, $_) = explode('_', $translation->getLanguage());
107  $languageIndex = array_search($translationLanguage, $languageDependencies);
108  if ($languageIndex !== false && $languageIndex < $highestPriorityLanguageIndex) {
109  $selectedTranslation = $translation;
110  $highestPriorityLanguageIndex = $languageIndex;
111  }
112  }
113  }
114  }
115 
116  $newTranslations = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
117  $document->setTranslations($newTranslations);
118  if ($selectedTranslation !== null) {
119  $document->addTranslation($selectedTranslation);
120  }
121  }
122 
123  return $allDocuments;
124  }
125 
131  protected function findSphinxDocuments()
132  {
133  $basePath = 'typo3conf/Documentation/';
134 
135  $documents = [];
136  $documentKeys = GeneralUtility::get_dirs(PATH_site . $basePath);
137  // Early return in case no document keys were found
138  if (!is_array($documentKeys)) {
139  return $documents;
140  }
141 
142  foreach ($documentKeys as $documentKey) {
144 
146  $document = $this->objectManager->get(\TYPO3\CMS\Documentation\Domain\Model\Document::class)
147  ->setPackageKey($documentKey)
148  ->setIcon($icon);
149 
150  $languagePath = $basePath . $documentKey . '/';
151  $languages = GeneralUtility::get_dirs(PATH_site . $languagePath);
152  foreach ($languages as $language) {
153  $metadata = $this->getMetadata($documentKey, $language);
154  if (!empty($metadata['extensionKey'])) {
155  $document->setExtensionKey($metadata['extensionKey']);
156  }
157 
159  $documentTranslation = $this->objectManager->get(\TYPO3\CMS\Documentation\Domain\Model\DocumentTranslation::class)
160  ->setLanguage($language)
161  ->setTitle($metadata['title'])
162  ->setDescription($metadata['description']);
163 
164  $formatPath = $languagePath . $language . '/';
165  $formats = GeneralUtility::get_dirs(PATH_site . $formatPath);
166  foreach ($formats as $format) {
167  $documentFile = '';
168  switch ($format) {
169  case 'html':
170  // Try to find a valid index file
171  $indexFiles = ['Index.html', 'index.html', 'index.htm'];
172  foreach ($indexFiles as $indexFile) {
173  if (file_exists(PATH_site . $formatPath . $format . '/' . $indexFile)) {
174  $documentFile = $indexFile;
175  break;
176  }
177  }
178  break;
179  case 'pdf':
180  // Retrieve first PDF
181  $files = GeneralUtility::getFilesInDir(PATH_site . $formatPath . $format, 'pdf');
182  if (is_array($files) && !empty($files)) {
183  $documentFile = current($files);
184  }
185  break;
186  }
187  if (!empty($documentFile)) {
189  $documentFormat = $this->objectManager->get(\TYPO3\CMS\Documentation\Domain\Model\DocumentFormat::class)
190  ->setFormat($format)
191  ->setPath($formatPath . $format . '/' . $documentFile);
192 
193  $documentTranslation->addFormat($documentFormat);
194  }
195  }
196 
197  if (!empty($documentTranslation->getFormats())) {
198  $document->addTranslation($documentTranslation);
199  $documents[$documentKey] = $document;
200  }
201  }
202  }
203 
204  return $documents;
205  }
206 
212  protected function findOpenOfficeDocuments()
213  {
214  $documents = [];
215  $language = 'default';
216 
217  foreach ($GLOBALS['TYPO3_LOADED_EXT'] as $extensionKey => $extensionData) {
218  $path = $extensionData['siteRelPath'] . 'doc/';
219  if (is_file(PATH_site . $path . 'manual.sxw')) {
220  $documentKey = 'typo3cms.extensions.' . $extensionKey;
222 
224  $document = $this->objectManager->get(\TYPO3\CMS\Documentation\Domain\Model\Document::class)
225  ->setPackageKey($documentKey)
226  ->setExtensionKey($extensionKey)
227  ->setIcon($icon);
228 
229  $metadata = $this->getMetadata($documentKey, $language);
231  $documentTranslation = $this->objectManager->get(\TYPO3\CMS\Documentation\Domain\Model\DocumentTranslation::class)
232  ->setLanguage($language)
233  ->setTitle($metadata['title'])
234  ->setDescription($metadata['description']);
235 
237  $documentFormat = $this->objectManager->get(\TYPO3\CMS\Documentation\Domain\Model\DocumentFormat::class)
238  ->setFormat('sxw')
239  ->setPath($path . 'manual.sxw');
240 
241  $documentTranslation->addFormat($documentFormat);
242  $document->addTranslation($documentTranslation);
243  $documents[$documentKey] = $document;
244  }
245  }
246 
247  return $documents;
248  }
249 
257  protected function getMetadata($documentKey, $language)
258  {
259  $documentPath = PATH_site . 'typo3conf/Documentation/' . $documentKey . '/' . $language . '/';
260  $metadata = [
261  'title' => $documentKey,
262  'description' => '',
263  ];
264  if (GeneralUtility::isFirstPartOfStr($documentKey, 'typo3cms.extensions.')) {
265  $extensionKey = substr($documentKey, 20);
266  if (\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded($extensionKey)) {
268  }
269  } elseif (is_file($documentPath . 'composer.json')) {
270  $info = json_decode(file_get_contents($documentPath . 'composer.json'), true);
271  if (is_array($info)) {
272  $metadata['title'] = $info['name'];
273  $metadata['description'] = $info['description'];
274  }
275  }
276  return $metadata;
277  }
278 }
static getFilesInDir($path, $extensionList='', $prependPath=false, $order='', $excludePattern='')
static isFirstPartOfStr($str, $partStr)
injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
$locales
Definition: be_users.php:6