[BUGFIX] getFileIndexRecordsForFolder only works for hierarchical path
[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 * @deprecated since 6.2 - will be removed 2 versions later
133 */
134 public function getFileIndexRecordsForFolder(Folder $folder) {
135 GeneralUtility::logDeprecatedFunction();
136 return $this->getFileIndexRepository()->findByFolder($folder);
137 }
138
139 /**
140 * Returns all files with the corresponding SHA-1 hash. This is queried
141 * against the database, so only indexed files will be found
142 *
143 * @param string $hash A SHA1 hash of a file
144 * @return array
145 * @deprecated since TYPO3 6.2, will be removed two versions later - use FileIndexRepository::findByContentHash
146 */
147 public function findBySha1Hash($hash) {
148 GeneralUtility::logDeprecatedFunction();
149 $resultRows = $this->getFileIndexRepository()->findByContentHash($hash);
150
151 $objects = array();
152 foreach ($resultRows as $row) {
153 $objects[] = $this->createDomainObject($row);
154 }
155 return $objects;
156 }
157
158 /**
159 * Find FileReference objects by relation to other records
160 *
161 * @param integer $tableName Table name of the related record
162 * @param integer $fieldName Field name of the related record
163 * @param integer $uid The UID of the related record (needs to be the localized uid, as translated IRRE elements relate to them)
164 * @return array An array of objects, empty if no objects found
165 * @throws \InvalidArgumentException
166 * @api
167 */
168 public function findByRelation($tableName, $fieldName, $uid) {
169 $itemList = array();
170 if (!\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($uid)) {
171 throw new \InvalidArgumentException('Uid of related record has to be an integer.', 1316789798);
172 }
173 $references = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
174 '*',
175 'sys_file_reference',
176 'tablenames=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($tableName, 'sys_file_reference') .
177 ' AND deleted = 0' .
178 ' AND hidden = 0' .
179 ' AND uid_foreign=' . intval($uid) .
180 ' AND fieldname=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($fieldName, 'sys_file_reference'),
181 '',
182 'sorting_foreign'
183 );
184 foreach ($references as $referenceRecord) {
185 $itemList[] = $this->createFileReferenceObject($referenceRecord);
186 }
187 return $itemList;
188 }
189
190 /**
191 * Find FileReference objects by uid
192 *
193 * @param integer $uid The UID of the sys_file_reference record
194 * @return FileReference|boolean
195 * @throws \InvalidArgumentException
196 * @api
197 */
198 public function findFileReferenceByUid($uid) {
199 $fileReferenceObject = FALSE;
200 if (!\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($uid)) {
201 throw new \InvalidArgumentException('uid of record has to be an integer.', 1316889798);
202 }
203 $row = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
204 '*',
205 'sys_file_reference',
206 'uid=' . $uid .
207 ' AND deleted=0' .
208 ' AND hidden=0'
209 );
210 if (is_array($row)) {
211 $fileReferenceObject = $this->createFileReferenceObject($row);
212 }
213 return $fileReferenceObject;
214 }
215
216 /**
217 * Updates an existing file object in the database
218 *
219 * @param AbstractFile $modifiedObject
220 * @return void
221 * @deprecated since TYPO3 6.2 LTS, will be removed two versions later - use FileIndexRepository::update
222 */
223 public function update($modifiedObject) {
224 GeneralUtility::logDeprecatedFunction();
225 if ($modifiedObject instanceof File) {
226 $this->getFileIndexRepository()->update($modifiedObject);
227 }
228 }
229
230 /**
231 * Creates a FileReference object
232 *
233 * @param array $databaseRow
234 * @return FileReference
235 */
236 protected function createFileReferenceObject(array $databaseRow) {
237 return $this->factory->getFileReferenceObject($databaseRow['uid'], $databaseRow);
238 }
239
240 /**
241 * Return a file index repository
242 *
243 * @return FileIndexRepository
244 */
245 protected function getFileIndexRepository() {
246 return FileIndexRepository::getInstance();
247 }
248
249 }