[BUGFIX] Fix the unit tests to work with PHPUnit 3.6
[Packages/TYPO3.CMS.git] / t3lib / file / FileReference.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2011 Ingmar Schlecht <ingmar@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
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 * @package TYPO3
42 * @subpackage t3lib
43 */
44 class t3lib_file_FileReference implements t3lib_file_FileInterface {
45
46 /**
47 * Various properties of the FileReference. Note that these information can be different
48 * to the ones found in the originalFile.
49 *
50 * @var array
51 */
52 protected $propertiesOfFileReference;
53
54 /**
55 * The identifier of this file to identify it on the storage.
56 * On some drivers, this is the path to the file, but drivers could also just
57 * provide any other unique identifier for this file on the specific storage.
58 *
59 * @var string
60 */
61 protected $uidOfFileReference;
62
63 /**
64 * The file name of this file. It's either the fileName of the original underlying file,
65 * or the overlay file name supplied by the user for this particular usage (FileReference) of the file.
66 *
67 * @var string
68 */
69 protected $name;
70
71 /**
72 * The FileRepository object. Is needed e.g. for the delete() method to delete the usage record
73 * (sys_file_reference record) of this file usage.
74 *
75 * @var t3lib_file_Repository_FileRepository
76 */
77 protected $fileRepository;
78
79 /**
80 * Reference to the original File object underlying this FileReference.
81 *
82 * @var t3lib_file_File
83 */
84 protected $originalFile;
85
86 /**
87 * Constructor for a file in use object. Should normally not be used
88 * directly, use the corresponding factory methods instead.
89 *
90 * @param array $fileReferenceData
91 * @param t3lib_file_Factory $factory
92 */
93 public function __construct(array $fileReferenceData, $factory = NULL) {
94 $this->propertiesOfFileReference = $fileReferenceData;
95
96 if (!$fileReferenceData['uid_local']) {
97 throw new InvalidArgumentException('Incorrect reference to original file given for FileReference.', 1300098528);
98 }
99
100 if (!$factory) {
101 /** @var $factory t3lib_file_Factory */
102 $factory = t3lib_div::makeInstance('t3lib_file_Factory');
103 }
104
105 $this->originalFile = $factory->getFileObject($fileReferenceData['uid_local']);
106
107 $this->fileRepository = t3lib_div::makeInstance('t3lib_file_Repository_FileRepository');
108
109 if (!is_object($this->originalFile)) {
110 throw new RuntimeException('Original File not found for FileReference.', 1300098529);
111 }
112
113 $this->name = $fileReferenceData['name'] !== '' ? $fileReferenceData['name'] : $this->originalFile->getName();
114 }
115
116
117 /*******************************
118 * VARIOUS FILE PROPERTY GETTERS
119 *******************************/
120
121
122 /**
123 * Returns true if the given key exists for this file.
124 *
125 * @param string $key The property to be looked up
126 * @return boolean
127 */
128 public function hasProperty($key) {
129 return array_key_exists($key, $this->propertiesOfFileReference);
130 }
131
132 /**
133 * Gets a property.
134 *
135 * @param string $key The property to be looked up
136 * @return mixed
137 * @throws InvalidArgumentException
138 */
139 public function getProperty($key) {
140 if (!$this->hasProperty($key)) {
141 throw new InvalidArgumentException('Property "' . $key . '" was not found.', 1314226805);
142 }
143
144 return $this->propertiesOfFileReference[$key];
145 }
146
147 /**
148 * Gets all properties.
149 *
150 * @return array
151 */
152 public function getProperties() {
153 return t3lib_div::array_merge_recursive_overrule(
154 $this->originalFile->getProperties(),
155 $this->propertiesOfFileReference
156 );
157 }
158
159 /**
160 * Returns the name of this file
161 *
162 * @return string
163 */
164 public function getName() {
165 return $this->originalFile->getName();
166 }
167
168 /**
169 * Returns the title text to this image
170 *
171 * TODO: Possibly move this to the image domain object instead
172 *
173 * @return string
174 */
175 public function getTitle() {
176 return $this->propertiesOfFileReference['title'] ? $this->propertiesOfFileReference['title'] : $this->originalFile->getName();
177 }
178
179 /**
180 * Returns the alternative text to this image
181 *
182 * TODO: Possibly move this to the image domain object instead
183 *
184 * @return string
185 */
186 public function getAlternative() {
187 return $this->propertiesOfFileReference['alternative'] ? $this->propertiesOfFileReference['alternative'] : $this->originalFile->getName();
188 }
189
190 /**
191 * Returns the description text to this file
192 *
193 * TODO: Possibly move this to the image domain object instead
194 *
195 * @return string
196 */
197 public function getDescription() {
198 return $this->propertiesOfFileReference['description'];
199 }
200
201 /**
202 * Returns the link that should be active when clicking on this image
203 *
204 * TODO: Move this to the image domain object instead
205 *
206 * @return string
207 */
208 public function getLink() {
209 return $this->propertiesOfFileReference['link'];
210 }
211
212 /**
213 * Returns the uid of this File In Use
214 *
215 * @return integer
216 */
217 public function getUid() {
218 return (int)$this->propertiesOfFileReference['uid'];
219 }
220
221 /**
222 * Returns the size of this file
223 *
224 * @return integer
225 */
226 public function getSize() {
227 return (int)$this->originalFile->getSize();
228 }
229
230 /**
231 * Returns the Sha1 of this file
232 *
233 * @return string
234 */
235 public function getSha1() {
236 return $this->originalFile->getSha1();
237 }
238
239
240 /**
241 * Get the file extension of this file
242 *
243 * @return string The file extension
244 */
245 public function getExtension() {
246 return $this->originalFile->getExtension();
247 }
248
249 /**
250 * Get the MIME type of this file
251 *
252 * @return array file information
253 */
254 public function getMimeType() {
255 return $this->originalFile->getMimeType();
256 }
257
258 /**
259 * Returns the modification time of the file as Unix timestamp
260 *
261 * @return integer
262 */
263 public function getModificationTime() {
264 return (int)$this->originalFile->getModificationTime();
265 }
266
267 /**
268 * Returns the creation time of the file as Unix timestamp
269 *
270 * @return integer
271 */
272 public function getCreationTime() {
273 return (int)$this->originalFile->getCreationTime();
274 }
275
276 /**
277 * Returns the fileType of this file
278 *
279 * @return integer $fileType
280 */
281 public function getType() {
282 return (int)$this->originalFile->getType();
283 }
284
285
286 /******************
287 * CONTENTS RELATED
288 ******************/
289
290 /**
291 * Get the contents of this file
292 *
293 * @return string File contents
294 */
295 public function getContents() {
296 return $this->originalFile->getContents();
297 }
298
299 /**
300 * Replace the current file contents with the given string
301 *
302 * @param string $contents The contents to write to the file.
303 * @return t3lib_file_File The file object (allows chaining).
304 */
305 public function setContents($contents) {
306 return $this->originalFile->setContents($contents);
307 }
308
309
310 /****************************************
311 * STORAGE AND MANAGEMENT RELATED METHDOS
312 ****************************************/
313
314
315 /**
316 * Get the storage the original file is located in
317 *
318 * @return t3lib_file_Storage
319 */
320 public function getStorage() {
321 return $this->originalFile->getStorage();
322 }
323
324 /**
325 * Returns the identifier of the underlying original file
326 *
327 * @return string
328 */
329 public function getIdentifier() {
330 return $this->originalFile->getIdentifier();
331 }
332
333
334 /**
335 * Returns a combined identifier of the underlying original file
336 *
337 * @return string Combined storage and file identifier, e.g. StorageUID:path/and/fileName.png
338 */
339 public function getCombinedIdentifier() {
340 return $this->originalFile->getCombinedIdentifier();
341 }
342
343 /**
344 * Deletes only this particular FileReference from the persistence layer
345 * (database table sys_file_reference) but leaves the original file untouched.
346 *
347 * @return boolean TRUE if deletion succeeded
348 */
349 public function delete() {
350 // TODO: Implement this function. This should only delete the
351 // FileReference (sys_file_reference) record, not the file itself.
352 throw new BadMethodCallException('Function not implemented FileReference::delete().', 1333754461);
353 return $this->fileRepository->removeUsageRecord($this);
354 }
355
356 /**
357 * Renames the fileName in this particular usage.
358 *
359 * @param string $newName The new name
360 * @return t3lib_file_FileReference
361 */
362 public function rename($newName) {
363 // TODO: Implement this function. This should only rename the
364 // FileReference (sys_file_reference) record, not the file itself.
365 throw new BadMethodCallException('Function not implemented FileReference::rename().', 1333754473);
366 return $this->fileRepository->renameUsageRecord($this, $newName);
367 }
368
369
370 /*****************
371 * SPECIAL METHODS
372 *****************/
373
374 /**
375 * Returns a publicly accessible URL for this file
376 *
377 * WARNING: Access to the file may be restricted by further means, e.g.
378 * some web-based authentication. You have to take care of this yourself.
379 *
380 * @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)
381 *
382 * @return string
383 */
384 public function getPublicUrl($relativeToCurrentScript = FALSE) {
385 return $this->originalFile->getPublicUrl($relativeToCurrentScript);
386 }
387
388 /**
389 * Returns TRUE if this file is indexed.
390 * This is always true for FileReference objects, as they rely on a
391 * sys_file_reference record to be present, which in turn can only exist if
392 * the original file is indexed.
393 *
394 * @return boolean
395 */
396 public function isIndexed() {
397 return TRUE;
398 }
399
400 /**
401 * Returns a path to a local version of this file to process it locally (e.g. with some system tool).
402 * If the file is normally located on a remote storages, this creates a local copy.
403 * If the file is already on the local system, this only makes a new copy if $writable is set to TRUE.
404 *
405 * @param boolean $writable Set this to FALSE if you only want to do read operations on the file.
406 * @return string
407 */
408 public function getForLocalProcessing($writable = TRUE) {
409 return $this->originalFile->getForLocalProcessing($writable);
410 }
411
412 /**
413 * Returns an array representation of the file.
414 * (This is used by the generic listing module vidi when displaying file records.)
415 *
416 * @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.
417 */
418 public function toArray() {
419 $array = array_merge(
420 $this->originalFile->toArray(),
421 $this->propertiesOfFileReference
422 );
423 return $array;
424 }
425
426 /**
427 * Gets the original file being referenced.
428 *
429 * @return t3lib_file_File
430 */
431 public function getOriginalFile() {
432 return $this->originalFile;
433 }
434 }
435
436
437 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/file/FileReference.php'])) {
438 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/file/FileReference.php']);
439 }
440
441 ?>