2 namespace TYPO3\CMS\Core\Resource
;
4 /***************************************************************
7 * (c) 2011 Andreas Wolf <andreas.wolf@ikt-werk.de>
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.
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.
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.
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
30 * Repository for accessing files
31 * it also serves as the public API for the indexing part of files in general
33 * @author Andreas Wolf <andreas.wolf@ikt-werk.de>
34 * @author Ingmar Schlecht <ingmar@typo3.org>
36 class FileRepository
extends AbstractRepository
{
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.
45 protected $objectType = 'TYPO3\\CMS\\Core\\Resource\\File';
48 * Main File object storage table. Note that this repository also works on
49 * the sys_file_reference table when returning FileReference objects.
53 protected $table = 'sys_file';
56 * @var Service\IndexerService
58 protected $indexerService = NULL
;
61 * Internal function to retrieve the indexer service,
62 * if it does not exist, an instance will be created
64 * @return Service\IndexerService
66 protected function getIndexerService() {
67 if ($this->indexerService
=== NULL
) {
68 $this->indexerService
= \TYPO3\CMS\Core\Utility\GeneralUtility
::makeInstance('TYPO3\\CMS\\Core\\Resource\\Service\\IndexerService');
70 return $this->indexerService
;
74 * Creates an object managed by this repository.
76 * @param array $databaseRow
79 protected function createDomainObject(array $databaseRow) {
80 return $this->factory
->getFileObject($databaseRow['uid'], $databaseRow);
84 * Index a file object given as parameter
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
91 public function addToIndex(File
$fileObject) {
92 return $this->getIndexerService()->indexFile($fileObject, FALSE
);
96 * Checks the index status of a file and returns FALSE if the file is not
97 * indexed, the uid otherwise.
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
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',
111 sprintf('storage=%u AND identifier=%s', $storageUid, $GLOBALS['TYPO3_DB']->fullQuoteStr($identifier, $this->table
))
113 if (!is_array($row)) {
121 * Returns an index record of a file, or FALSE if the file is not indexed.
123 * @TODO : throw an exception if nothing found, for consistent handling as in AbstractRepository?
124 * @param File $fileObject
127 public function getFileIndexRecord(File
$fileObject) {
128 $storageUid = $fileObject->getStorage()->getUid();
129 $identifier = $fileObject->getIdentifier();
130 $row = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
133 sprintf('storage=%u AND identifier=%s', $storageUid, $GLOBALS['TYPO3_DB']->fullQuoteStr($identifier, $this->table
))
135 if (!is_array($row)) {
143 * Returns the index-data of all files within that folder
145 * @param Folder $folder
148 public function getFileIndexRecordsForFolder(Folder
$folder) {
149 $identifier = $folder->getIdentifier();
150 $storage = $folder->getStorage()->getUid();
151 $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
154 sprintf('storage=%u AND identifier LIKE %s AND NOT identifier LIKE %s',
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
)
164 return (array) $rows;
168 * Returns all files with the corresponding SHA-1 hash. This is queried
169 * against the database, so only indexed files will be found
171 * @param string $hash A SHA1 hash of a file
174 public function findBySha1Hash($hash) {
175 if (preg_match('/[^a-f0-9]*/i', $hash)) {
178 $resultRows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
181 'sha1=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($hash, $this->table
)
184 foreach ($resultRows as $row) {
185 $objects[] = $this->createDomainObject($row);
191 * Find FileReference objects by relation to other records
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
200 public function findByRelation($tableName, $fieldName, $uid) {
202 if (!\TYPO3\CMS\Core\Utility\MathUtility
::canBeInterpretedAsInteger($uid)) {
203 throw new \
InvalidArgumentException('Uid of related record has to be an integer.', 1316789798);
205 $references = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
207 'sys_file_reference',
208 'tablenames=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($tableName, 'sys_file_reference') .
211 ' AND uid_foreign=' . intval($uid) .
212 ' AND fieldname=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($fieldName, 'sys_file_reference'),
216 foreach ($references as $referenceRecord) {
217 $itemList[] = $this->createFileReferenceObject($referenceRecord);
223 * Find FileReference objects by uid
225 * @param integer $uid The UID of the sys_file_reference record
226 * @return FileReference|boolean
227 * @throws \InvalidArgumentException
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);
235 $row = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
237 'sys_file_reference',
242 if (is_array($row)) {
243 $fileReferenceObject = $this->createFileReferenceObject($row);
245 return $fileReferenceObject;
249 * Updates an existing file object in the database
251 * @param AbstractFile $modifiedObject
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];
263 $GLOBALS['TYPO3_DB']->exec_UPDATEquery('sys_file', 'uid=' . $modifiedObject->getUid(), $updateFields);
267 * Creates a FileReference object
269 * @param array $databaseRow
270 * @return FileReference
272 protected function createFileReferenceObject(array $databaseRow) {
273 return $this->factory
->getFileReferenceObject($databaseRow['uid'], $databaseRow);