[BUGFIX] Clean-up responsibilities of FAL classes
[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 textfile 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 * Internal function to retrieve the indexer service,
66 * if it does not exist, an instance will be created
67 *
68 * @return Service\IndexerService
69 */
70 protected function getIndexerService() {
71 if ($this->indexerService === NULL) {
72 $this->indexerService = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\Service\\IndexerService');
73 }
74 return $this->indexerService;
75 }
76
77 /**
78 * Creates an object managed by this repository.
79 *
80 * @param array $databaseRow
81 * @return File
82 */
83 protected function createDomainObject(array $databaseRow) {
84 return $this->factory->getFileObject($databaseRow['uid'], $databaseRow);
85 }
86
87 /**
88 * Index a file object given as parameter
89 *
90 * @param File $fileObject
91 * @return array The indexed file data
92 * @deprecated since TYPO3 6.2, will be removed two versions later - indexing should be handled transparently, not only upon request
93 */
94 public function addToIndex(File $fileObject) {
95 GeneralUtility::logDeprecatedFunction();
96 return $this->getIndexerService()->indexFile($fileObject, FALSE);
97 }
98
99 /**
100 * Checks the index status of a file and returns FALSE if the file is not
101 * indexed, the uid otherwise.
102 *
103 * @param File $fileObject
104 * @return boolean|integer
105 * @deprecated since TYPO3 6.2, will be removed two versions later - use FileIndexRepository::isIndexed
106 */
107 public function getFileIndexStatus(File $fileObject) {
108 GeneralUtility::logDeprecatedFunction();
109 $storageUid = $fileObject->getStorage()->getUid();
110 $identifier = $fileObject->getIdentifier();
111 $row = $this->getFileIndexRepository()->findOneByStorageUidAndIdentifier($storageUid, $identifier);
112 return is_array($row) ? $row['uid'] : FALSE;
113 }
114
115 /**
116 * Returns an index record of a file, or FALSE if the file is not indexed.
117 *
118 * @param File $fileObject
119 * @return bool|array
120 * @deprecated since TYPO3 6.2, will be removed two versions later - use FileIndexRepository instead
121 */
122 public function getFileIndexRecord(File $fileObject) {
123 GeneralUtility::logDeprecatedFunction();
124 return $this->getFileIndexRepository()->findOneByFileObject($fileObject);
125 }
126
127 /**
128 * Returns the index-data of all files within that folder
129 *
130 * @param Folder $folder
131 * @return array
132 */
133 public function getFileIndexRecordsForFolder(Folder $folder) {
134 $identifier = $folder->getIdentifier();
135 $storage = $folder->getStorage()->getUid();
136 $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
137 '*',
138 $this->table,
139 sprintf('storage=%u AND missing = 0 AND identifier LIKE %s AND NOT identifier LIKE %s',
140 $storage,
141 $GLOBALS['TYPO3_DB']->fullQuoteStr($GLOBALS['TYPO3_DB']->escapeStrForLike($identifier, $this->table) . '%', $this->table),
142 $GLOBALS['TYPO3_DB']->fullQuoteStr($GLOBALS['TYPO3_DB']->escapeStrForLike($identifier, $this->table) . '%/%', $this->table)
143 ),
144 '',
145 '',
146 '',
147 'identifier'
148 );
149 return (array) $rows;
150 }
151
152 /**
153 * Returns all files with the corresponding SHA-1 hash. This is queried
154 * against the database, so only indexed files will be found
155 *
156 * @param string $hash A SHA1 hash of a file
157 * @return array
158 * @deprecated since TYPO3 6.2, will be removed two versions later - use FileIndexRepository::findByContentHash
159 */
160 public function findBySha1Hash($hash) {
161 GeneralUtility::logDeprecatedFunction();
162 $resultRows = $this->getFileIndexRepository()->findByContentHash($hash);
163
164 $objects = array();
165 foreach ($resultRows as $row) {
166 $objects[] = $this->createDomainObject($row);
167 }
168 return $objects;
169 }
170
171 /**
172 * Find FileReference objects by relation to other records
173 *
174 * @param integer $tableName Table name of the related record
175 * @param integer $fieldName Field name of the related record
176 * @param integer $uid The UID of the related record (needs to be the localized uid, as translated IRRE elements relate to them)
177 * @return array An array of objects, empty if no objects found
178 * @throws \InvalidArgumentException
179 * @api
180 */
181 public function findByRelation($tableName, $fieldName, $uid) {
182 $itemList = array();
183 if (!\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($uid)) {
184 throw new \InvalidArgumentException('Uid of related record has to be an integer.', 1316789798);
185 }
186 $references = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
187 '*',
188 'sys_file_reference',
189 'tablenames=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($tableName, 'sys_file_reference') .
190 ' AND deleted = 0' .
191 ' AND hidden = 0' .
192 ' AND uid_foreign=' . intval($uid) .
193 ' AND fieldname=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($fieldName, 'sys_file_reference'),
194 '',
195 'sorting_foreign'
196 );
197 foreach ($references as $referenceRecord) {
198 $itemList[] = $this->createFileReferenceObject($referenceRecord);
199 }
200 return $itemList;
201 }
202
203 /**
204 * Find FileReference objects by uid
205 *
206 * @param integer $uid The UID of the sys_file_reference record
207 * @return FileReference|boolean
208 * @throws \InvalidArgumentException
209 * @api
210 */
211 public function findFileReferenceByUid($uid) {
212 $fileReferenceObject = FALSE;
213 if (!\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($uid)) {
214 throw new \InvalidArgumentException('uid of record has to be an integer.', 1316889798);
215 }
216 $row = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
217 '*',
218 'sys_file_reference',
219 'uid=' . $uid .
220 ' AND deleted=0' .
221 ' AND hidden=0'
222 );
223 if (is_array($row)) {
224 $fileReferenceObject = $this->createFileReferenceObject($row);
225 }
226 return $fileReferenceObject;
227 }
228
229 /**
230 * Updates an existing file object in the database
231 *
232 * @param AbstractFile $modifiedObject
233 * @return void
234 * @deprecated since TYPO3 6.2 LTS, will be removed two versions later - use FileIndexRepository::update
235 */
236 public function update($modifiedObject) {
237 GeneralUtility::logDeprecatedFunction();
238 if ($modifiedObject instanceof File) {
239 $this->getFileIndexRepository()->update($modifiedObject);
240 }
241 }
242
243 /**
244 * Creates a FileReference object
245 *
246 * @param array $databaseRow
247 * @return FileReference
248 */
249 protected function createFileReferenceObject(array $databaseRow) {
250 return $this->factory->getFileReferenceObject($databaseRow['uid'], $databaseRow);
251 }
252
253 /**
254 * Return a file index repository
255 *
256 * @return FileIndexRepository
257 */
258 protected function getFileIndexRepository() {
259 return FileIndexRepository::getInstance();
260 }
261
262 }