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