38894e4cc38769db653dc43f64e9ea8bb363e241
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / FileReference.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011 Ingmar Schlecht <ingmar@typo3.org>
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 * Representation of a specific usage of a file with possibilities to override certain
31 * properties of the original file just for this usage of the file.
32 *
33 * It acts as a decorator over the original file in the way that most method calls are
34 * directly passed along to the original file object.
35 *
36 * All file related methods are directly passed along; only meta data functionality is adopted
37 * in this decorator class to priorities possible overrides for the metadata for this specific usage
38 * of the file.
39 *
40 * @author Ingmar Schlecht <ingmar@typo3.org>
41 */
42 class FileReference implements \TYPO3\CMS\Core\Resource\FileInterface {
43
44 /**
45 * Various properties of the FileReference. Note that these information can be different
46 * to the ones found in the originalFile.
47 *
48 * @var array
49 */
50 protected $propertiesOfFileReference;
51
52 /**
53 * The identifier of this file to identify it on the storage.
54 * On some drivers, this is the path to the file, but drivers could also just
55 * provide any other unique identifier for this file on the specific storage.
56 *
57 * @var string
58 */
59 protected $uidOfFileReference;
60
61 /**
62 * The file name of this file. It's either the fileName of the original underlying file,
63 * or the overlay file name supplied by the user for this particular usage (FileReference) of the file.
64 *
65 * @var string
66 */
67 protected $name;
68
69 /**
70 * The FileRepository object. Is needed e.g. for the delete() method to delete the usage record
71 * (sys_file_reference record) of this file usage.
72 *
73 * @var \TYPO3\CMS\Core\Resource\FileRepository
74 */
75 protected $fileRepository;
76
77 /**
78 * Reference to the original File object underlying this FileReference.
79 *
80 * @var \TYPO3\CMS\Core\Resource\File
81 */
82 protected $originalFile;
83
84 /**
85 * Constructor for a file in use object. Should normally not be used
86 * directly, use the corresponding factory methods instead.
87 *
88 * @param array $fileReferenceData
89 * @param \TYPO3\CMS\Core\Resource\ResourceFactory $factory
90 */
91 public function __construct(array $fileReferenceData, $factory = NULL) {
92 $this->propertiesOfFileReference = $fileReferenceData;
93 if (!$fileReferenceData['uid_local']) {
94 throw new \InvalidArgumentException('Incorrect reference to original file given for FileReference.', 1300098528);
95 }
96 if (!$factory) {
97 /** @var $factory \TYPO3\CMS\Core\Resource\ResourceFactory */
98 $factory = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\ResourceFactory');
99 }
100 $this->originalFile = $factory->getFileObject($fileReferenceData['uid_local']);
101 $this->fileRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\FileRepository');
102 if (!is_object($this->originalFile)) {
103 throw new \RuntimeException('Original File not found for FileReference.', 1300098529);
104 }
105 $this->name = $fileReferenceData['name'] !== '' ? $fileReferenceData['name'] : $this->originalFile->getName();
106 }
107
108 /*******************************
109 * VARIOUS FILE PROPERTY GETTERS
110 *******************************/
111 /**
112 * Returns true if the given key exists for this file.
113 *
114 * @param string $key The property to be looked up
115 * @return boolean
116 */
117 public function hasProperty($key) {
118 return array_key_exists($key, $this->propertiesOfFileReference);
119 }
120
121 /**
122 * Gets a property.
123 *
124 * @param string $key The property to be looked up
125 * @return mixed
126 * @throws \InvalidArgumentException
127 */
128 public function getProperty($key) {
129 if (!$this->hasProperty($key)) {
130 throw new \InvalidArgumentException('Property "' . $key . '" was not found.', 1314226805);
131 }
132 return $this->propertiesOfFileReference[$key];
133 }
134
135 /**
136 * Gets all properties.
137 *
138 * @return array
139 */
140 public function getProperties() {
141 return \TYPO3\CMS\Core\Utility\GeneralUtility::array_merge_recursive_overrule($this->originalFile->getProperties(), $this->propertiesOfFileReference);
142 }
143
144 /**
145 * Returns the name of this file
146 *
147 * @return string
148 */
149 public function getName() {
150 return $this->originalFile->getName();
151 }
152
153 /**
154 * Returns the title text to this image
155 *
156 * TODO: Possibly move this to the image domain object instead
157 *
158 * @return string
159 */
160 public function getTitle() {
161 return $this->propertiesOfFileReference['title'] ? $this->propertiesOfFileReference['title'] : $this->originalFile->getName();
162 }
163
164 /**
165 * Returns the alternative text to this image
166 *
167 * TODO: Possibly move this to the image domain object instead
168 *
169 * @return string
170 */
171 public function getAlternative() {
172 return $this->propertiesOfFileReference['alternative'] ? $this->propertiesOfFileReference['alternative'] : $this->originalFile->getName();
173 }
174
175 /**
176 * Returns the description text to this file
177 *
178 * TODO: Possibly move this to the image domain object instead
179 *
180 * @return string
181 */
182 public function getDescription() {
183 return $this->propertiesOfFileReference['description'];
184 }
185
186 /**
187 * Returns the link that should be active when clicking on this image
188 *
189 * TODO: Move this to the image domain object instead
190 *
191 * @return string
192 */
193 public function getLink() {
194 return $this->propertiesOfFileReference['link'];
195 }
196
197 /**
198 * Returns the uid of this File In Use
199 *
200 * @return integer
201 */
202 public function getUid() {
203 return (int) $this->propertiesOfFileReference['uid'];
204 }
205
206 /**
207 * Returns the size of this file
208 *
209 * @return integer
210 */
211 public function getSize() {
212 return (int) $this->originalFile->getSize();
213 }
214
215 /**
216 * Returns the Sha1 of this file
217 *
218 * @return string
219 */
220 public function getSha1() {
221 return $this->originalFile->getSha1();
222 }
223
224 /**
225 * Get the file extension of this file
226 *
227 * @return string The file extension
228 */
229 public function getExtension() {
230 return $this->originalFile->getExtension();
231 }
232
233 /**
234 * Get the MIME type of this file
235 *
236 * @return array file information
237 */
238 public function getMimeType() {
239 return $this->originalFile->getMimeType();
240 }
241
242 /**
243 * Returns the modification time of the file as Unix timestamp
244 *
245 * @return integer
246 */
247 public function getModificationTime() {
248 return (int) $this->originalFile->getModificationTime();
249 }
250
251 /**
252 * Returns the creation time of the file as Unix timestamp
253 *
254 * @return integer
255 */
256 public function getCreationTime() {
257 return (int) $this->originalFile->getCreationTime();
258 }
259
260 /**
261 * Returns the fileType of this file
262 *
263 * @return integer $fileType
264 */
265 public function getType() {
266 return (int) $this->originalFile->getType();
267 }
268
269 /******************
270 * CONTENTS RELATED
271 ******************/
272 /**
273 * Get the contents of this file
274 *
275 * @return string File contents
276 */
277 public function getContents() {
278 return $this->originalFile->getContents();
279 }
280
281 /**
282 * Replace the current file contents with the given string
283 *
284 * @param string $contents The contents to write to the file.
285 * @return \TYPO3\CMS\Core\Resource\File The file object (allows chaining).
286 */
287 public function setContents($contents) {
288 return $this->originalFile->setContents($contents);
289 }
290
291 /****************************************
292 * STORAGE AND MANAGEMENT RELATED METHDOS
293 ****************************************/
294 /**
295 * Get the storage the original file is located in
296 *
297 * @return \TYPO3\CMS\Core\Resource\ResourceStorage
298 */
299 public function getStorage() {
300 return $this->originalFile->getStorage();
301 }
302
303 /**
304 * Returns the identifier of the underlying original file
305 *
306 * @return string
307 */
308 public function getIdentifier() {
309 return $this->originalFile->getIdentifier();
310 }
311
312 /**
313 * Returns a combined identifier of the underlying original file
314 *
315 * @return string Combined storage and file identifier, e.g. StorageUID:path/and/fileName.png
316 */
317 public function getCombinedIdentifier() {
318 return $this->originalFile->getCombinedIdentifier();
319 }
320
321 /**
322 * Deletes only this particular FileReference from the persistence layer
323 * (database table sys_file_reference) but leaves the original file untouched.
324 *
325 * @return boolean TRUE if deletion succeeded
326 */
327 public function delete() {
328 // TODO: Implement this function. This should only delete the
329 // FileReference (sys_file_reference) record, not the file itself.
330 throw new \BadMethodCallException('Function not implemented FileReference::delete().', 1333754461);
331 return $this->fileRepository->removeUsageRecord($this);
332 }
333
334 /**
335 * Renames the fileName in this particular usage.
336 *
337 * @param string $newName The new name
338 * @return \TYPO3\CMS\Core\Resource\FileReference
339 */
340 public function rename($newName) {
341 // TODO: Implement this function. This should only rename the
342 // FileReference (sys_file_reference) record, not the file itself.
343 throw new \BadMethodCallException('Function not implemented FileReference::rename().', 1333754473);
344 return $this->fileRepository->renameUsageRecord($this, $newName);
345 }
346
347 /*****************
348 * SPECIAL METHODS
349 *****************/
350 /**
351 * Returns a publicly accessible URL for this file
352 *
353 * WARNING: Access to the file may be restricted by further means, e.g.
354 * some web-based authentication. You have to take care of this yourself.
355 *
356 * @param bool $relativeToCurrentScript Determines whether the URL returned should be relative to the current script, in case it is relative at all (only for the LocalDriver)
357 * @return string
358 */
359 public function getPublicUrl($relativeToCurrentScript = FALSE) {
360 return $this->originalFile->getPublicUrl($relativeToCurrentScript);
361 }
362
363 /**
364 * Returns TRUE if this file is indexed.
365 * This is always true for FileReference objects, as they rely on a
366 * sys_file_reference record to be present, which in turn can only exist if
367 * the original file is indexed.
368 *
369 * @return boolean
370 */
371 public function isIndexed() {
372 return TRUE;
373 }
374
375 /**
376 * Returns a path to a local version of this file to process it locally (e.g. with some system tool).
377 * If the file is normally located on a remote storages, this creates a local copy.
378 * If the file is already on the local system, this only makes a new copy if $writable is set to TRUE.
379 *
380 * @param boolean $writable Set this to FALSE if you only want to do read operations on the file.
381 * @return string
382 */
383 public function getForLocalProcessing($writable = TRUE) {
384 return $this->originalFile->getForLocalProcessing($writable);
385 }
386
387 /**
388 * Returns an array representation of the file.
389 * (This is used by the generic listing module vidi when displaying file records.)
390 *
391 * @return array Array of main data of the file. Don't rely on all data to be present here, it's just a selection of the most relevant information.
392 */
393 public function toArray() {
394 $array = array_merge($this->originalFile->toArray(), $this->propertiesOfFileReference);
395 return $array;
396 }
397
398 /**
399 * Gets the original file being referenced.
400 *
401 * @return \TYPO3\CMS\Core\Resource\File
402 */
403 public function getOriginalFile() {
404 return $this->originalFile;
405 }
406
407 }
408
409
410 ?>