1938bc606c46a32292d343828dc9244493063235
[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 $references = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
185 '*',
186 'sys_file_reference',
187 'tablenames=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($tableName, 'sys_file_reference') .
188 ' AND deleted = 0' .
189 ' AND hidden = 0' .
190 ' AND uid_foreign=' . (int)$uid .
191 ' AND fieldname=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($fieldName, 'sys_file_reference'),
192 '',
193 'sorting_foreign'
194 );
195 foreach ($references as $referenceRecord) {
196 $itemList[] = $this->createFileReferenceObject($referenceRecord);
197 }
198 return $itemList;
199 }
200
201 /**
202 * Find FileReference objects by uid
203 *
204 * @param integer $uid The UID of the sys_file_reference record
205 * @return FileReference|boolean
206 * @throws \InvalidArgumentException
207 * @api
208 */
209 public function findFileReferenceByUid($uid) {
210 $fileReferenceObject = FALSE;
211 if (!\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($uid)) {
212 throw new \InvalidArgumentException('uid of record has to be an integer.', 1316889798);
213 }
214 $row = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
215 '*',
216 'sys_file_reference',
217 'uid=' . $uid .
218 ' AND deleted=0' .
219 ' AND hidden=0'
220 );
221 if (is_array($row)) {
222 $fileReferenceObject = $this->createFileReferenceObject($row);
223 }
224 return $fileReferenceObject;
225 }
226
227 /**
228 * Updates an existing file object in the database
229 *
230 * @param AbstractFile $modifiedObject
231 * @return void
232 * @deprecated since TYPO3 6.2 LTS, will be removed two versions later - use FileIndexRepository::update
233 */
234 public function update($modifiedObject) {
235 GeneralUtility::logDeprecatedFunction();
236 if ($modifiedObject instanceof File) {
237 $this->getFileIndexRepository()->update($modifiedObject);
238 }
239 }
240
241 /**
242 * Creates a FileReference object
243 *
244 * @param array $databaseRow
245 * @return FileReference
246 */
247 protected function createFileReferenceObject(array $databaseRow) {
248 return $this->factory->getFileReferenceObject($databaseRow['uid'], $databaseRow);
249 }
250
251 /**
252 * Return a file index repository
253 *
254 * @return FileIndexRepository
255 */
256 protected function getFileIndexRepository() {
257 return FileIndexRepository::getInstance();
258 }
259
260 }