[BUGFIX] Clean-up responsibilities of FAL classes
[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-2013 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 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 FileRepository
74 */
75 protected $fileRepository;
76
77 /**
78 * Reference to the original File object underlying this FileReference.
79 *
80 * @var File
81 */
82 protected $originalFile;
83
84 /**
85 * Properties merged with the parent object (File) if
86 * the value is not defined (NULL). Thus, FileReference properties act
87 * as overlays for the defined File properties.
88 *
89 * @var array
90 */
91 protected $mergedProperties = array();
92
93 /**
94 * Constructor for a file in use object. Should normally not be used
95 * directly, use the corresponding factory methods instead.
96 *
97 * @param array $fileReferenceData
98 * @param ResourceFactory $factory
99 *
100 * @throws \RuntimeException
101 * @throws \InvalidArgumentException
102 */
103 public function __construct(array $fileReferenceData, $factory = NULL) {
104 $this->propertiesOfFileReference = $fileReferenceData;
105 if (!$fileReferenceData['uid_local']) {
106 throw new \InvalidArgumentException('Incorrect reference to original file given for FileReference.', 1300098528);
107 }
108 if (!$factory) {
109 /** @var $factory ResourceFactory */
110 $factory = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\ResourceFactory');
111 }
112 $this->originalFile = $factory->getFileObject($fileReferenceData['uid_local']);
113 $this->fileRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\FileRepository');
114 if (!is_object($this->originalFile)) {
115 throw new \RuntimeException('Original File not found for FileReference.', 1300098529);
116 }
117 $this->name = $fileReferenceData['name'] !== '' ? $fileReferenceData['name'] : $this->originalFile->getName();
118 }
119
120 /*******************************
121 * VARIOUS FILE PROPERTY GETTERS
122 *******************************/
123 /**
124 * Returns true if the given key exists for this file.
125 *
126 * @param string $key The property to be looked up
127 * @return boolean
128 */
129 public function hasProperty($key) {
130 return array_key_exists($key, $this->getProperties());
131 }
132
133 /**
134 * Gets a property, falling back to values of the parent.
135 *
136 * @param string $key The property to be looked up
137 * @return mixed
138 * @throws \InvalidArgumentException
139 */
140 public function getProperty($key) {
141 if (!$this->hasProperty($key)) {
142 throw new \InvalidArgumentException('Property "' . $key . '" was not found in file reference or original file.', 1314226805);
143 }
144 $properties = $this->getProperties();
145 return $properties[$key];
146 }
147
148 /**
149 * Gets a property of the file reference.
150 *
151 * @param string $key The property to be looked up
152 * @return mixed
153 * @throws \InvalidArgumentException
154 */
155 public function getReferenceProperty($key) {
156 if (!array_key_exists($key, $this->propertiesOfFileReference)) {
157 throw new \InvalidArgumentException('Property "' . $key . '" of file reference was not found.', 1360684914);
158 }
159 return $this->propertiesOfFileReference[$key];
160 }
161
162 /**
163 * Gets all properties, falling back to values of the parent.
164 *
165 * @return array
166 */
167 public function getProperties() {
168 if (empty($this->mergedProperties)) {
169 $this->mergedProperties = \TYPO3\CMS\Core\Utility\GeneralUtility::array_merge_recursive_overrule(
170 $this->propertiesOfFileReference,
171 $this->originalFile->getProperties(),
172 FALSE,
173 TRUE,
174 FALSE
175 );
176 array_walk($this->mergedProperties, array($this, 'restoreNonNullValuesCallback'));
177 }
178
179 return $this->mergedProperties;
180 }
181
182 /**
183 * Callback to handle the NULL value feature
184 *
185 * @param mixed $value
186 * @param mixed $key
187 */
188 protected function restoreNonNullValuesCallback(&$value, $key) {
189 if (array_key_exists($key, $this->propertiesOfFileReference) && $this->propertiesOfFileReference[$key] !== NULL) {
190 $value = $this->propertiesOfFileReference[$key];
191 }
192 }
193
194 /**
195 * Gets all properties of the file reference.
196 *
197 * @return array
198 */
199 public function getReferenceProperties() {
200 return $this->propertiesOfFileReference;
201 }
202
203 /**
204 * Returns the name of this file
205 *
206 * @return string
207 */
208 public function getName() {
209 return $this->originalFile->getName();
210 }
211
212 /**
213 * Returns the title text to this image
214 *
215 * TODO: Possibly move this to the image domain object instead
216 *
217 * @return string
218 */
219 public function getTitle() {
220 return $this->getProperty('title');
221 }
222
223 /**
224 * Returns the alternative text to this image
225 *
226 * TODO: Possibly move this to the image domain object instead
227 *
228 * @return string
229 */
230 public function getAlternative() {
231 return $this->getProperty('alternative');
232 }
233
234 /**
235 * Returns the description text to this file
236 *
237 * TODO: Possibly move this to the image domain object instead
238 *
239 * @return string
240 */
241 public function getDescription() {
242 return $this->getProperty('description');
243 }
244
245 /**
246 * Returns the link that should be active when clicking on this image
247 *
248 * TODO: Move this to the image domain object instead
249 *
250 * @return string
251 */
252 public function getLink() {
253 return $this->propertiesOfFileReference['link'];
254 }
255
256 /**
257 * Returns the uid of this File In Use
258 *
259 * @return integer
260 */
261 public function getUid() {
262 return (int) $this->propertiesOfFileReference['uid'];
263 }
264
265 /**
266 * Returns the size of this file
267 *
268 * @return integer
269 */
270 public function getSize() {
271 return (int) $this->originalFile->getSize();
272 }
273
274 /**
275 * Returns the Sha1 of this file
276 *
277 * @return string
278 */
279 public function getSha1() {
280 return $this->originalFile->getSha1();
281 }
282
283 /**
284 * Get the file extension of this file
285 *
286 * @return string The file extension
287 */
288 public function getExtension() {
289 return $this->originalFile->getExtension();
290 }
291
292 /**
293 * Returns the basename (the name without extension) of this file.
294 *
295 * @return string
296 */
297 public function getNameWithoutExtension() {
298 return $this->originalFile->getNameWithoutExtension();
299 }
300
301 /**
302 * Get the MIME type of this file
303 *
304 * @return array file information
305 */
306 public function getMimeType() {
307 return $this->originalFile->getMimeType();
308 }
309
310 /**
311 * Returns the modification time of the file as Unix timestamp
312 *
313 * @return integer
314 */
315 public function getModificationTime() {
316 return (int) $this->originalFile->getModificationTime();
317 }
318
319 /**
320 * Returns the creation time of the file as Unix timestamp
321 *
322 * @return integer
323 */
324 public function getCreationTime() {
325 return (int) $this->originalFile->getCreationTime();
326 }
327
328 /**
329 * Returns the fileType of this file
330 *
331 * @return integer $fileType
332 */
333 public function getType() {
334 return (int) $this->originalFile->getType();
335 }
336
337 /**
338 * Check if file is marked as missing by indexer
339 *
340 * @return boolean
341 */
342 public function isMissing() {
343 return (bool) $this->originalFile->getProperty('missing');
344 }
345
346 /******************
347 * CONTENTS RELATED
348 ******************/
349 /**
350 * Get the contents of this file
351 *
352 * @return string File contents
353 */
354 public function getContents() {
355 return $this->originalFile->getContents();
356 }
357
358 /**
359 * Replace the current file contents with the given string
360 *
361 * @param string $contents The contents to write to the file.
362 * @return File The file object (allows chaining).
363 */
364 public function setContents($contents) {
365 return $this->originalFile->setContents($contents);
366 }
367
368 /****************************************
369 * STORAGE AND MANAGEMENT RELATED METHDOS
370 ****************************************/
371 /**
372 * Get the storage the original file is located in
373 *
374 * @return ResourceStorage
375 */
376 public function getStorage() {
377 return $this->originalFile->getStorage();
378 }
379
380 /**
381 * Returns the identifier of the underlying original file
382 *
383 * @return string
384 */
385 public function getIdentifier() {
386 return $this->originalFile->getIdentifier();
387 }
388
389 /**
390 * Returns a combined identifier of the underlying original file
391 *
392 * @return string Combined storage and file identifier, e.g. StorageUID:path/and/fileName.png
393 */
394 public function getCombinedIdentifier() {
395 return $this->originalFile->getCombinedIdentifier();
396 }
397
398 /**
399 * Deletes only this particular FileReference from the persistence layer
400 * (database table sys_file_reference) but leaves the original file untouched.
401 *
402 * @throws \BadMethodCallException
403 * @return boolean TRUE if deletion succeeded
404 */
405 public function delete() {
406 // TODO: Implement this function. This should only delete the
407 // FileReference (sys_file_reference) record, not the file itself.
408 throw new \BadMethodCallException('Function not implemented FileReference::delete().', 1333754461);
409 return $this->fileRepository->removeUsageRecord($this);
410 }
411
412 /**
413 * Renames the fileName in this particular usage.
414 *
415 * @param string $newName The new name
416 *
417 * @throws \BadMethodCallException
418 * @return FileReference
419 */
420 public function rename($newName) {
421 // TODO: Implement this function. This should only rename the
422 // FileReference (sys_file_reference) record, not the file itself.
423 throw new \BadMethodCallException('Function not implemented FileReference::rename().', 1333754473);
424 return $this->fileRepository->renameUsageRecord($this, $newName);
425 }
426
427 /*****************
428 * SPECIAL METHODS
429 *****************/
430 /**
431 * Returns a publicly accessible URL for this file
432 *
433 * WARNING: Access to the file may be restricted by further means, e.g.
434 * some web-based authentication. You have to take care of this yourself.
435 *
436 * @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)
437 * @return string
438 */
439 public function getPublicUrl($relativeToCurrentScript = FALSE) {
440 return $this->originalFile->getPublicUrl($relativeToCurrentScript);
441 }
442
443 /**
444 * Returns TRUE if this file is indexed.
445 * This is always true for FileReference objects, as they rely on a
446 * sys_file_reference record to be present, which in turn can only exist if
447 * the original file is indexed.
448 *
449 * @return boolean
450 */
451 public function isIndexed() {
452 return TRUE;
453 }
454
455 /**
456 * Returns a path to a local version of this file to process it locally (e.g. with some system tool).
457 * If the file is normally located on a remote storages, this creates a local copy.
458 * If the file is already on the local system, this only makes a new copy if $writable is set to TRUE.
459 *
460 * @param boolean $writable Set this to FALSE if you only want to do read operations on the file.
461 * @return string
462 */
463 public function getForLocalProcessing($writable = TRUE) {
464 return $this->originalFile->getForLocalProcessing($writable);
465 }
466
467 /**
468 * Returns an array representation of the file.
469 * (This is used by the generic listing module vidi when displaying file records.)
470 *
471 * @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.
472 */
473 public function toArray() {
474 $array = array_merge($this->originalFile->toArray(), $this->propertiesOfFileReference);
475 return $array;
476 }
477
478 /**
479 * Gets the original file being referenced.
480 *
481 * @return File
482 */
483 public function getOriginalFile() {
484 return $this->originalFile;
485 }
486
487 }