[BUGFIX] Ignore dependencies of inactive packages
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / File.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011-2013 Ingo Renner <ingo@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 use TYPO3\CMS\Core\Utility\GeneralUtility;
31
32 /**
33 * File representation in the file abstraction layer.
34 *
35 * @author Andreas Wolf <andreas.wolf@ikt-werk.de>
36 */
37 class File extends AbstractFile {
38
39 /**
40 * File indexing status. True, if the file is indexed in the database;
41 * NULL is the default value, this means that the index status is unknown
42 *
43 * @var boolean|NULL
44 */
45 protected $indexed = NULL;
46
47 /**
48 * Tells whether to index a file or not.
49 * If yes, the file will be persisted into sys_file.
50 *
51 * @var boolean
52 */
53 protected $indexable = TRUE;
54
55 /**
56 * @var array
57 */
58 protected $metaDataProperties = array();
59
60 /**
61 * Set to TRUE while this file is being indexed - used to prevent some endless loops
62 *
63 * @var boolean
64 */
65 protected $indexingInProgress = FALSE;
66
67 /**
68 * Contains the names of all properties that have been update since the
69 * instantiation of this object
70 *
71 * @var array
72 */
73 protected $updatedProperties = array();
74
75 /**
76 * @var \TYPO3\CMS\Core\Resource\Service\IndexerService
77 */
78 protected $indexerService = NULL;
79
80 /**
81 * Constructor for a file object. Should normally not be used directly, use
82 * the corresponding factory methods instead.
83 *
84 * @param array $fileData
85 * @param ResourceStorage $storage
86 */
87 public function __construct(array $fileData, ResourceStorage $storage) {
88 $this->identifier = $fileData['identifier'];
89 $this->name = $fileData['name'];
90 $this->properties = $fileData;
91 $this->storage = $storage;
92
93 if (isset($fileData['uid']) && intval($fileData['uid']) > 0) {
94 $this->indexed = TRUE;
95 $this->loadMetaData();
96 }
97 }
98
99 /*******************************
100 * VARIOUS FILE PROPERTY GETTERS
101 *******************************/
102 /**
103 * Returns a property value
104 *
105 * @param string $key
106 * @return mixed Property value
107 */
108 public function getProperty($key) {
109 if ($this->indexed === NULL) {
110 $this->loadIndexRecord();
111 }
112 if (parent::hasProperty($key)) {
113 return parent::getProperty($key);
114 } else {
115 return array_key_exists($key, $this->metaDataProperties) ? $this->metaDataProperties[$key] : NULL;
116 }
117 }
118
119 /**
120 * Returns the properties of this object.
121 *
122 * @return array
123 */
124 public function getProperties() {
125 if ($this->indexed === NULL) {
126 $this->loadIndexRecord();
127 }
128 return array_merge(parent::getProperties(), array_diff_key((array)$this->metaDataProperties, parent::getProperties()));
129 }
130
131 /**
132 * Returns the MetaData
133 *
134 * @return array|null
135 * @internal
136 */
137 public function _getMetaData() {
138 return $this->metaDataProperties;
139 }
140
141 /******************
142 * CONTENTS RELATED
143 ******************/
144 /**
145 * Get the contents of this file
146 *
147 * @return string File contents
148 */
149 public function getContents() {
150 return $this->getStorage()->getFileContents($this);
151 }
152
153 /**
154 * Replace the current file contents with the given string
155 *
156 * @param string $contents The contents to write to the file.
157 * @return File The file object (allows chaining).
158 */
159 public function setContents($contents) {
160 $this->getStorage()->setFileContents($this, $contents);
161 return $this;
162 }
163
164 /***********************
165 * INDEX RELATED METHODS
166 ***********************/
167 /**
168 * Returns TRUE if this file is indexed
169 *
170 * @return boolean|NULL
171 */
172 public function isIndexed() {
173 if ($this->indexed === NULL && !$this->indexingInProgress) {
174 $this->loadIndexRecord();
175 }
176 return $this->indexed;
177 }
178
179 /**
180 * @param bool $indexIfNotIndexed
181 *
182 * @throws \RuntimeException
183 * @return void
184 */
185 protected function loadIndexRecord($indexIfNotIndexed = TRUE) {
186 if ($this->indexed !== NULL || !$this->indexable || $this->indexingInProgress) {
187 return;
188 }
189 $this->indexingInProgress = TRUE;
190
191 $indexRecord = $this->getFileIndexRepository()->findOneByCombinedIdentifier($this->getCombinedIdentifier());
192 if ($indexRecord === FALSE && $indexIfNotIndexed) {
193 $this->getIndexerService()->updateIndexEntry($this);
194 $this->updatedProperties = array();
195 } elseif ($indexRecord !== FALSE) {
196 $this->mergeIndexRecord($indexRecord);
197 $this->indexed = TRUE;
198 $this->loadMetaData();
199 } else {
200 throw new \RuntimeException('Could not load index record for "' . $this->getIdentifier() . '"', 1321288316);
201 }
202 $this->indexingInProgress = FALSE;
203 }
204
205 /**
206 * Loads MetaData from Repository
207 */
208 protected function loadMetaData() {
209 $this->metaDataProperties = $this->getMetaDataRepository()->findByFile($this);
210 }
211
212 /**
213 * Merges the contents of this file's index record into the file properties.
214 *
215 * @param array $recordData The index record as fetched from the database
216 *
217 * @throws \InvalidArgumentException
218 * @return void
219 */
220 protected function mergeIndexRecord(array $recordData) {
221 if ($this->properties['uid'] != 0) {
222 throw new \InvalidArgumentException('uid property is already set. Cannot merge index record.', 1321023156);
223 }
224 $this->properties = array_merge($recordData, $this->properties);
225 }
226
227 /**
228 * Updates the properties of this file, e.g. after re-indexing or moving it.
229 * By default, only properties that exist as a key in the $properties array
230 * are overwritten. If you want to explicitly unset a property, set the
231 * corresponding key to NULL in the array.
232 *
233 * NOTE: This method should not be called from outside the File Abstraction Layer (FAL)!
234 *
235 * @param array $properties
236 * @return void
237 * @internal
238 */
239 public function updateProperties(array $properties) {
240 // Setting identifier and name to update values; we have to do this
241 // here because we might need a new identifier when loading
242 // (and thus possibly indexing) a file.
243 if (isset($properties['identifier'])) {
244 $this->identifier = $properties['identifier'];
245 }
246 if (isset($properties['name'])) {
247 $this->name = $properties['name'];
248 }
249 if ($this->indexed === NULL && !isset($properties['uid'])) {
250 $this->loadIndexRecord();
251 }
252 if ($this->properties['uid'] != 0 && isset($properties['uid'])) {
253 unset($properties['uid']);
254 }
255 foreach ($properties as $key => $value) {
256 if ($this->properties[$key] !== $value) {
257 if (!in_array($key, $this->updatedProperties)) {
258 $this->updatedProperties[] = $key;
259 }
260 // TODO check if we should completely remove properties that
261 // are set to NULL
262 $this->properties[$key] = $value;
263 }
264 }
265 // Updating indexing status
266 if (isset($properties['uid']) && intval($properties['uid']) > 0) {
267 $this->indexed = TRUE;
268 $this->loadMetaData();
269 }
270 if (array_key_exists('storage', $properties) && in_array('storage', $this->updatedProperties)) {
271 $this->storage = ResourceFactory::getInstance()->getStorageObject($properties['storage']);
272 }
273 }
274
275 /**
276 * Returns the names of all properties that have been updated in this record
277 *
278 * @return array
279 */
280 public function getUpdatedProperties() {
281 return $this->updatedProperties;
282 }
283
284 /****************************************
285 * STORAGE AND MANAGEMENT RELATED METHODS
286 ****************************************/
287 /**
288 * Check if a file operation (= action) is allowed for this file
289 *
290 * @param string $action, can be read, write, delete
291 * @return boolean
292 */
293 public function checkActionPermission($action) {
294 return $this->getStorage()->checkFileActionPermission($action, $this);
295 }
296
297 /*****************
298 * SPECIAL METHODS
299 *****************/
300 /**
301 * Creates a MD5 hash checksum based on the combined identifier of the file,
302 * the files' mimetype and the systems' encryption key.
303 * used to generate a thumbnail, and this hash is checked if valid
304 *
305 * @todo maybe \TYPO3\CMS\Core\Utility\GeneralUtility::hmac() could be used?
306 * @return string the MD5 hash
307 */
308 public function calculateChecksum() {
309 return md5($this->getCombinedIdentifier() . '|' . $this->getMimeType() . '|' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey']);
310 }
311
312 /**
313 * Returns a modified version of the file.
314 *
315 * @param string $taskType The task type of this processing
316 * @param array $configuration the processing configuration, see manual for that
317 * @return ProcessedFile The processed file
318 */
319 public function process($taskType, array $configuration) {
320 return $this->getStorage()->processFile($this, $taskType, $configuration);
321 }
322
323 /**
324 * Returns an array representation of the file.
325 * (This is used by the generic listing module vidi when displaying file records.)
326 *
327 * @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.
328 */
329 public function toArray() {
330 $array = array(
331 'id' => $this->getCombinedIdentifier(),
332 'name' => $this->getName(),
333 'extension' => $this->getExtension(),
334 'type' => $this->getType(),
335 'mimetype' => $this->getMimeType(),
336 'size' => $this->getSize(),
337 'url' => $this->getPublicUrl(),
338 'indexed' => $this->indexed,
339 'uid' => $this->getUid(),
340 'permissions' => array(
341 'read' => $this->checkActionPermission('read'),
342 'write' => $this->checkActionPermission('write'),
343 'delete' => $this->checkActionPermission('delete')
344 ),
345 'checksum' => $this->calculateChecksum()
346 );
347 foreach ($this->properties as $key => $value) {
348 $array[$key] = $value;
349 }
350 $stat = $this->getStorage()->getFileInfo($this);
351 foreach ($stat as $key => $value) {
352 $array[$key] = $value;
353 }
354 return $array;
355 }
356
357 /**
358 * @return boolean
359 */
360 public function isIndexable() {
361 return $this->indexable;
362 }
363
364 /**
365 * @param boolean $indexable
366 */
367 public function setIndexable($indexable) {
368 $this->indexable = $indexable;
369 }
370
371 /**
372 * @return boolean
373 */
374 public function isMissing() {
375 return (bool) $this->getProperty('missing');
376 }
377
378 /**
379 * @param boolean $missing
380 */
381 public function setMissing($missing) {
382 $this->updateProperties(array('missing' => $missing ? 1 : 0));
383 }
384
385 /**
386 * Returns a publicly accessible URL for this file
387 * When file is marked as missing or deleted no url is returned
388 *
389 * WARNING: Access to the file may be restricted by further means, e.g. some
390 * web-based authentication. You have to take care of this yourself.
391 *
392 * @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)
393 *
394 * @return string
395 */
396 public function getPublicUrl($relativeToCurrentScript = FALSE) {
397 if ($this->isMissing() || $this->deleted) {
398 return FALSE;
399 } else {
400 return $this->getStorage()->getPublicUrl($this, $relativeToCurrentScript);
401 }
402 }
403
404 /**
405 * @return \TYPO3\CMS\Core\Resource\Index\MetaDataRepository
406 */
407 protected function getMetaDataRepository() {
408 return GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\Index\\MetaDataRepository');
409 }
410
411 /**
412 * @return \TYPO3\CMS\Core\Resource\Index\FileIndexRepository
413 */
414 protected function getFileIndexRepository() {
415 return GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\Index\\FileIndexRepository');
416 }
417
418 /**
419 * Internal function to retrieve the indexer service,
420 * if it does not exist, an instance will be created
421 *
422 * @return Index\Indexer
423 */
424 protected function getIndexerService() {
425 if ($this->indexerService === NULL) {
426 $this->indexerService = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\Index\\Indexer', $this->storage);
427 }
428 return $this->indexerService;
429 }
430
431 /**
432 * @param boolean $indexingState
433 * @internal Only for usage in Indexer
434 */
435 public function setIndexingInProgess($indexingState) {
436 $this->indexingInProgress = (boolean)$indexingState;
437 }
438
439 /**
440 * @param $key
441 * @internal Only for use in Repositories and indexer
442 * @return mixed
443 */
444 public function _getPropertyRaw($key) {
445 return parent::getProperty($key);
446 }
447 }