[BUGFIX] Catch freshly introduced ResourceDoesNotExistException
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / FileRepository.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource;
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\Resource\Exception\ResourceDoesNotExistException;
18 use TYPO3\CMS\Core\Resource\Index\FileIndexRepository;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * Repository for accessing files
23 * it also serves as the public API for the indexing part of files in general
24 *
25 * @author Andreas Wolf <andreas.wolf@ikt-werk.de>
26 * @author Ingmar Schlecht <ingmar@typo3.org>
27 */
28 class FileRepository extends AbstractRepository {
29
30 /**
31 * The main object type of this class. In some cases (fileReference) this
32 * repository can also return FileReference objects, implementing the
33 * common FileInterface.
34 *
35 * @var string
36 */
37 protected $objectType = 'TYPO3\\CMS\\Core\\Resource\\File';
38
39 /**
40 * Main File object storage table. Note that this repository also works on
41 * the sys_file_reference table when returning FileReference objects.
42 *
43 * @var string
44 */
45 protected $table = 'sys_file';
46
47 /**
48 * @var Service\IndexerService
49 */
50 protected $indexerService = NULL;
51
52 /**
53 * @param int $uid
54 * @return File
55 * @deprecated since TYPO3 6.2 CMS, will be removed 2 versions later
56 */
57 public function findByUid($uid) {
58 GeneralUtility::logDeprecatedFunction();
59 return ResourceFactory::getInstance()->getFileObject($uid);
60 }
61
62
63 /**
64 * Internal function to retrieve the indexer service,
65 * if it does not exist, an instance will be created
66 *
67 * @return Service\IndexerService
68 */
69 protected function getIndexerService() {
70 if ($this->indexerService === NULL) {
71 $this->indexerService = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\Service\\IndexerService');
72 }
73 return $this->indexerService;
74 }
75
76 /**
77 * Creates an object managed by this repository.
78 *
79 * @param array $databaseRow
80 * @return File
81 */
82 protected function createDomainObject(array $databaseRow) {
83 return $this->factory->getFileObject($databaseRow['uid'], $databaseRow);
84 }
85
86 /**
87 * Index a file object given as parameter
88 *
89 * @param File $fileObject
90 * @return array The indexed file data
91 * @deprecated since TYPO3 6.2, will be removed two versions later - indexing should be handled transparently, not only upon request
92 */
93 public function addToIndex(File $fileObject) {
94 GeneralUtility::logDeprecatedFunction();
95 return $this->getIndexerService()->indexFile($fileObject, FALSE);
96 }
97
98 /**
99 * Checks the index status of a file and returns FALSE if the file is not
100 * indexed, the uid otherwise.
101 *
102 * @param File $fileObject
103 * @return boolean|integer
104 * @deprecated since TYPO3 6.2, will be removed two versions later - use FileIndexRepository::isIndexed
105 */
106 public function getFileIndexStatus(File $fileObject) {
107 GeneralUtility::logDeprecatedFunction();
108 $storageUid = $fileObject->getStorage()->getUid();
109 $identifier = $fileObject->getIdentifier();
110 $row = $this->getFileIndexRepository()->findOneByStorageUidAndIdentifier($storageUid, $identifier);
111 return is_array($row) ? $row['uid'] : FALSE;
112 }
113
114 /**
115 * Returns an index record of a file, or FALSE if the file is not indexed.
116 *
117 * @param File $fileObject
118 * @return bool|array
119 * @deprecated since TYPO3 6.2, will be removed two versions later - use FileIndexRepository instead
120 */
121 public function getFileIndexRecord(File $fileObject) {
122 GeneralUtility::logDeprecatedFunction();
123 return $this->getFileIndexRepository()->findOneByFileObject($fileObject);
124 }
125
126 /**
127 * Returns the index-data of all files within that folder
128 *
129 * @param Folder $folder
130 * @return array
131 * @deprecated since 6.2 - will be removed 2 versions later
132 */
133 public function getFileIndexRecordsForFolder(Folder $folder) {
134 GeneralUtility::logDeprecatedFunction();
135 return $this->getFileIndexRepository()->findByFolder($folder);
136 }
137
138 /**
139 * Returns all files with the corresponding SHA-1 hash. This is queried
140 * against the database, so only indexed files will be found
141 *
142 * @param string $hash A SHA1 hash of a file
143 * @return array
144 * @deprecated since TYPO3 6.2, will be removed two versions later - use FileIndexRepository::findByContentHash
145 */
146 public function findBySha1Hash($hash) {
147 GeneralUtility::logDeprecatedFunction();
148 $resultRows = $this->getFileIndexRepository()->findByContentHash($hash);
149
150 $objects = array();
151 foreach ($resultRows as $row) {
152 $objects[] = $this->createDomainObject($row);
153 }
154 return $objects;
155 }
156
157 /**
158 * Find FileReference objects by relation to other records
159 *
160 * @param integer $tableName Table name of the related record
161 * @param integer $fieldName Field name of the related record
162 * @param integer $uid The UID of the related record (needs to be the localized uid, as translated IRRE elements relate to them)
163 * @return array An array of objects, empty if no objects found
164 * @throws \InvalidArgumentException
165 * @api
166 */
167 public function findByRelation($tableName, $fieldName, $uid) {
168 $itemList = array();
169 if (!\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($uid)) {
170 throw new \InvalidArgumentException('Uid of related record has to be an integer.', 1316789798);
171 }
172 $referenceUids = NULL;
173 if ($this->getEnvironmentMode() === 'FE' && !empty($GLOBALS['TSFE']->sys_page)) {
174 /** @var $frontendController \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController */
175 $frontendController = $GLOBALS['TSFE'];
176 $references = $this->getDatabaseConnection()->exec_SELECTgetRows(
177 'uid',
178 'sys_file_reference',
179 'tablenames=' . $this->getDatabaseConnection()->fullQuoteStr($tableName, 'sys_file_reference') .
180 ' AND uid_foreign=' . (int)$uid .
181 ' AND fieldname=' . $this->getDatabaseConnection()->fullQuoteStr($fieldName, 'sys_file_reference')
182 . $frontendController->sys_page->enableFields('sys_file_reference', $frontendController->showHiddenRecords),
183 '',
184 'sorting_foreign',
185 '',
186 'uid'
187 );
188 if (!empty($references)) {
189 $referenceUids = array_keys($references);
190 }
191 } else {
192 /** @var $relationHandler \TYPO3\CMS\Core\Database\RelationHandler */
193 $relationHandler = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Database\\RelationHandler');
194 $relationHandler->start(
195 '', 'sys_file_reference', '', $uid, $tableName,
196 \TYPO3\CMS\Backend\Utility\BackendUtility::getTcaFieldConfiguration($tableName, $fieldName)
197 );
198 if (!empty($relationHandler->tableArray['sys_file_reference'])) {
199 $referenceUids = $relationHandler->tableArray['sys_file_reference'];
200 }
201 }
202 if (!empty($referenceUids)) {
203 foreach ($referenceUids as $referenceUid) {
204 try {
205 // Just passing the reference uid, the factory is doing workspace
206 // overlays automatically depending on the current environment
207 $itemList[] = $this->factory->getFileReferenceObject($referenceUid);
208 } catch (ResourceDoesNotExistException $exception) {
209 // No handling, just omit the invalid reference uid
210 }
211 }
212 }
213 return $itemList;
214 }
215
216 /**
217 * Find FileReference objects by uid
218 *
219 * @param integer $uid The UID of the sys_file_reference record
220 * @return FileReference|boolean
221 * @throws \InvalidArgumentException
222 * @api
223 */
224 public function findFileReferenceByUid($uid) {
225 if (!\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($uid)) {
226 throw new \InvalidArgumentException('uid of record has to be an integer.', 1316889798);
227 }
228 try {
229 $fileReferenceObject = $this->factory->getFileReferenceObject($uid);
230 } catch (\InvalidArgumentException $exception) {
231 $fileReferenceObject = FALSE;
232 }
233 return $fileReferenceObject;
234 }
235
236 /**
237 * Updates an existing file object in the database
238 *
239 * @param AbstractFile $modifiedObject
240 * @return void
241 * @deprecated since TYPO3 6.2 LTS, will be removed two versions later - use FileIndexRepository::update
242 */
243 public function update($modifiedObject) {
244 GeneralUtility::logDeprecatedFunction();
245 if ($modifiedObject instanceof File) {
246 $this->getFileIndexRepository()->update($modifiedObject);
247 }
248 }
249
250 /**
251 * Creates a FileReference object
252 *
253 * @param array $databaseRow
254 * @return FileReference
255 * @deprecated Use $this->factory->getFileReferenceObject() directly
256 */
257 protected function createFileReferenceObject(array $databaseRow) {
258 return $this->factory->getFileReferenceObject($databaseRow['uid'], $databaseRow);
259 }
260
261 /**
262 * Return a file index repository
263 *
264 * @return FileIndexRepository
265 */
266 protected function getFileIndexRepository() {
267 return FileIndexRepository::getInstance();
268 }
269
270 }