TYPO3 CMS  TYPO3_6-2
DocumentRepository.php
Go to the documentation of this file.
1 <?php
3 
18 
25 
30  protected $objectManager;
31 
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 
57  public function findByLanguage($language) {
58  $allDocuments = $this->findAll();
59 
60  // Initialize the dependency of languages
61  $languageDependencies = array();
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 
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) {
137 
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 
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)) {
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 
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;
214 
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);
223  $documentTranslation = $this->objectManager->get('TYPO3\\CMS\\Documentation\\Domain\\Model\\DocumentTranslation')
224  ->setLanguage($language)
225  ->setTitle($metadata['title'])
226  ->setDescription($metadata['description']);
227 
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 
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)) {
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 }
static isFirstPartOfStr($str, $partStr)
static getFilesInDir($path, $extensionList='', $prependPath=FALSE, $order='', $excludePattern='')
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]
$locales
Definition: be_users.php:6