[BUGFIX] Fix SQL syntax
[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 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 * Repository for accessing files
31 * it also serves as the public API for the indexing part of files in general
32 *
33 * @author Andreas Wolf <andreas.wolf@ikt-werk.de>
34 * @author Ingmar Schlecht <ingmar@typo3.org>
35 */
36 class FileRepository extends AbstractRepository {
37
38 /**
39 * The main object type of this class. In some cases (fileReference) this
40 * repository can also return FileReference objects, implementing the
41 * common FileInterface.
42 *
43 * @var string
44 */
45 protected $objectType = 'TYPO3\\CMS\\Core\\Resource\\File';
46
47 /**
48 * Main File object storage table. Note that this repository also works on
49 * the sys_file_reference table when returning FileReference objects.
50 *
51 * @var string
52 */
53 protected $table = 'sys_file';
54
55 /**
56 * @var Service\IndexerService
57 */
58 protected $indexerService = NULL;
59
60 /**
61 * Internal function to retrieve the indexer service,
62 * if it does not exist, an instance will be created
63 *
64 * @return Service\IndexerService
65 */
66 protected function getIndexerService() {
67 if ($this->indexerService === NULL) {
68 $this->indexerService = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\Service\\IndexerService');
69 }
70 return $this->indexerService;
71 }
72
73 /**
74 * Creates an object managed by this repository.
75 *
76 * @param array $databaseRow
77 * @return File
78 */
79 protected function createDomainObject(array $databaseRow) {
80 return $this->factory->getFileObject($databaseRow['uid'], $databaseRow);
81 }
82
83 /**
84 * Index a file object given as parameter
85 *
86 * @TODO : Check if the indexing functions really belong into the repository and shouldn't be part of an
87 * @TODO : indexing service, right now it's fine that way as this function will serve as the public API
88 * @param File $fileObject
89 * @return array The indexed file data
90 */
91 public function addToIndex(File $fileObject) {
92 return $this->getIndexerService()->indexFile($fileObject, FALSE);
93 }
94
95 /**
96 * Checks the index status of a file and returns FALSE if the file is not
97 * indexed, the uid otherwise.
98 *
99 * @TODO : Check if the indexing functions really belong into the repository and shouldn't be part of an
100 * @TODO : indexing service, right now it's fine that way as this function will serve as the public API
101 * @TODO : throw an exception if nothing found, for consistent handling as in AbstractRepository?
102 * @param File $fileObject
103 * @return bool|int
104 */
105 public function getFileIndexStatus(File $fileObject) {
106 $storageUid = $fileObject->getStorage()->getUid();
107 $identifier = $fileObject->getIdentifier();
108 $row = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
109 'uid,storage,identifier',
110 $this->table,
111 sprintf('storage=%u AND identifier=%s', $storageUid, $GLOBALS['TYPO3_DB']->fullQuoteStr($identifier, $this->table))
112 );
113 if (!is_array($row)) {
114 return FALSE;
115 } else {
116 return $row['uid'];
117 }
118 }
119
120 /**
121 * Returns an index record of a file, or FALSE if the file is not indexed.
122 *
123 * @TODO : throw an exception if nothing found, for consistent handling as in AbstractRepository?
124 * @param File $fileObject
125 * @return bool|array
126 */
127 public function getFileIndexRecord(File $fileObject) {
128 $storageUid = $fileObject->getStorage()->getUid();
129 $identifier = $fileObject->getIdentifier();
130 $row = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
131 '*',
132 $this->table,
133 sprintf('storage=%u AND identifier=%s', $storageUid, $GLOBALS['TYPO3_DB']->fullQuoteStr($identifier, $this->table))
134 );
135 if (!is_array($row)) {
136 return FALSE;
137 } else {
138 return $row;
139 }
140 }
141
142 /**
143 * Returns the index-data of all files within that folder
144 *
145 * @param Folder $folder
146 * @return array
147 */
148 public function getFileIndexRecordsForFolder(Folder $folder) {
149 $identifier = $folder->getIdentifier();
150 $storage = $folder->getStorage()->getUid();
151 $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
152 '*',
153 $this->table,
154 sprintf('storage=%u AND identifier LIKE %s AND NOT identifier LIKE %s',
155 $storage,
156 $GLOBALS['TYPO3_DB']->fullQuoteStr($GLOBALS['TYPO3_DB']->escapeStrForLike($identifier, $this->table) . '%', $this->table),
157 $GLOBALS['TYPO3_DB']->fullQuoteStr($GLOBALS['TYPO3_DB']->escapeStrForLike($identifier, $this->table) . '%/%', $this->table)
158 ),
159 '',
160 '',
161 '',
162 'identifier'
163 );
164 return (array) $rows;
165 }
166
167 /**
168 * Returns all files with the corresponding SHA-1 hash. This is queried
169 * against the database, so only indexed files will be found
170 *
171 * @param string $hash A SHA1 hash of a file
172 * @return array
173 */
174 public function findBySha1Hash($hash) {
175 if (preg_match('/[^a-f0-9]*/i', $hash)) {
176
177 }
178 $resultRows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
179 '*',
180 $this->table,
181 'sha1=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($hash, $this->table)
182 );
183 $objects = array();
184 foreach ($resultRows as $row) {
185 $objects[] = $this->createDomainObject($row);
186 }
187 return $objects;
188 }
189
190 /**
191 * Find FileReference objects by relation to other records
192 *
193 * @param int $tableName Table name of the related record
194 * @param int $fieldName Field name of the related record
195 * @param int $uid The UID of the related record (needs to be the localized uid, as translated IRRE elements relate to them)
196 * @return array An array of objects, empty if no objects found
197 * @throws \InvalidArgumentException
198 * @api
199 */
200 public function findByRelation($tableName, $fieldName, $uid) {
201 $itemList = array();
202 if (!\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($uid)) {
203 throw new \InvalidArgumentException('Uid of related record has to be an integer.', 1316789798);
204 }
205 $references = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
206 '*',
207 'sys_file_reference',
208 'tablenames=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($tableName, 'sys_file_reference') .
209 ' AND deleted = 0' .
210 ' AND hidden = 0' .
211 ' AND uid_foreign=' . intval($uid) .
212 ' AND fieldname=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($fieldName, 'sys_file_reference'),
213 '',
214 'sorting_foreign'
215 );
216 foreach ($references as $referenceRecord) {
217 $itemList[] = $this->createFileReferenceObject($referenceRecord);
218 }
219 return $itemList;
220 }
221
222 /**
223 * Find FileReference objects by uid
224 *
225 * @param integer $uid The UID of the sys_file_reference record
226 * @return FileReference|boolean
227 * @throws \InvalidArgumentException
228 * @api
229 */
230 public function findFileReferenceByUid($uid) {
231 $fileReferenceObject = FALSE;
232 if (!\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($uid)) {
233 throw new \InvalidArgumentException('uid of record has to be an integer.', 1316889798);
234 }
235 $row = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
236 '*',
237 'sys_file_reference',
238 'uid=' . $uid .
239 ' AND deleted=0' .
240 ' AND hidden=0'
241 );
242 if (is_array($row)) {
243 $fileReferenceObject = $this->createFileReferenceObject($row);
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 */
254 public function update($modifiedObject) {
255 // TODO check if $modifiedObject is an instance of AbstractFile
256 // TODO check if $modifiedObject is indexed
257 $changedProperties = $modifiedObject->getUpdatedProperties();
258 $properties = $modifiedObject->getProperties();
259 $updateFields = array();
260 foreach ($changedProperties as $propertyName) {
261 $updateFields[$propertyName] = $properties[$propertyName];
262 }
263 $GLOBALS['TYPO3_DB']->exec_UPDATEquery('sys_file', 'uid=' . $modifiedObject->getUid(), $updateFields);
264 }
265
266 /**
267 * Creates a FileReference object
268 *
269 * @param array $databaseRow
270 * @return FileReference
271 */
272 protected function createFileReferenceObject(array $databaseRow) {
273 return $this->factory->getFileReferenceObject($databaseRow['uid'], $databaseRow);
274 }
275
276 }
277
278 ?>