[BUGFIX] Folder::getFiles directly calls Factory::createFileObject
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / Folder.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011-2013 Andreas Wolf <andreas.wolf@ikt-werk.de>
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\PathUtility;
31
32 /**
33 * A folder that groups files in a storage. This may be a folder on the local
34 * disk, a bucket in Amazon S3 or a user or a tag in Flickr.
35 *
36 * This object is not persisted in TYPO3 locally, but created on the fly by
37 * storage drivers for the folders they "offer".
38 *
39 * Some folders serve as a physical container for files (e.g. folders on the
40 * local disk, S3 buckets or Flickr users). Other folders just group files by a
41 * certain criterion, e.g. a tag.
42 * The way this is implemented depends on the storage driver.
43 *
44 * @author Andreas Wolf <andreas.wolf@ikt-werk.de>
45 * @author Ingmar Schlecht <ingmar@typo3.org>
46 */
47 class Folder implements FolderInterface {
48
49 /**
50 * The storage this folder belongs to.
51 *
52 * @var ResourceStorage
53 */
54 protected $storage;
55
56 /**
57 * The identifier of this folder to identify it on the storage.
58 * On some drivers, this is the path to the folder, but drivers could also just
59 * provide any other unique identifier for this folder on the specific storage.
60 *
61 * @var string
62 */
63 protected $identifier;
64
65 /**
66 * The name of this folder
67 *
68 * @var string
69 */
70 protected $name;
71
72 /**
73 * The filters this folder should use for a file list.
74 *
75 * @var callable[]
76 */
77 protected $fileAndFolderNameFilters = array();
78
79 /**
80 * Modes for filter usage in getFiles()/getFolders()
81 */
82 const FILTER_MODE_NO_FILTERS = 0;
83 // Merge local filters into storage's filters
84 const FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS = 1;
85 // Only use the filters provided by the storage
86 const FILTER_MODE_USE_STORAGE_FILTERS = 2;
87 // Only use the filters provided by the current class
88 const FILTER_MODE_USE_OWN_FILTERS = 3;
89
90 /**
91 * Initialization of the folder
92 *
93 * @param ResourceStorage $storage
94 * @param $identifier
95 * @param $name
96 */
97 public function __construct(ResourceStorage $storage, $identifier, $name) {
98 $this->storage = $storage;
99 $this->identifier = rtrim($identifier, '/') . '/';
100 $this->name = $name;
101 }
102
103 /**
104 * Returns the name of this folder.
105 *
106 * @return string
107 */
108 public function getName() {
109 return $this->name;
110 }
111
112 /**
113 * Sets a new name of the folder
114 * currently this does not trigger the "renaming process"
115 * as the name is more seen as a label
116 *
117 * @param string $name The new name
118 * @return void
119 */
120 public function setName($name) {
121 $this->name = $name;
122 }
123
124 /**
125 * Returns the storage this folder belongs to.
126 *
127 * @return ResourceStorage
128 */
129 public function getStorage() {
130 return $this->storage;
131 }
132
133 /**
134 * Returns the path of this folder inside the storage. It depends on the
135 * type of storage whether this is a real path or just some unique identifier.
136 *
137 * @return string
138 */
139 public function getIdentifier() {
140 return $this->identifier;
141 }
142
143 /**
144 * Get hashed identifier
145 *
146 * @return string
147 */
148 public function getHashedIdentifier() {
149 return $this->storage->hashFileIdentifier($this->identifier);
150 }
151
152 /**
153 * Returns a combined identifier of this folder, i.e. the storage UID and
154 * the folder identifier separated by a colon ":".
155 *
156 * @return string Combined storage and folder identifier, e.g. StorageUID:folder/path/
157 */
158 public function getCombinedIdentifier() {
159 return $this->getStorage()->getUid() . ':' . $this->getIdentifier();
160 }
161
162 /**
163 * Returns a publicly accessible URL for this folder
164 *
165 * WARNING: Access to the folder may be restricted by further means, e.g. some
166 * web-based authentication. You have to take care of this yourself.
167 *
168 * @param boolean $relativeToCurrentScript Determines whether the URL returned should be relative to the current script, in case it is relative at all (only for the LocalDriver)
169 * @return string
170 */
171 public function getPublicUrl($relativeToCurrentScript = FALSE) {
172 return $this->getStorage()->getPublicUrl($this, $relativeToCurrentScript);
173 }
174
175 /**
176 * Returns a list of files in this folder, optionally filtered. There are several filter modes available, see the
177 * FILTER_MODE_* constants for more information.
178 *
179 * For performance reasons the returned items can also be limited to a given range
180 *
181 * @param integer $start The item to start at
182 * @param integer $numberOfItems The number of items to return
183 * @param integer $filterMode The filter mode to use for the file list.
184 * @param boolean $recursive
185 * @return \TYPO3\CMS\Core\Resource\File[]
186 */
187 public function getFiles($start = 0, $numberOfItems = 0, $filterMode = self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, $recursive = FALSE) {
188 // Fallback for compatibility with the old method signature variable $useFilters that was used instead of $filterMode
189 if ($filterMode === FALSE) {
190 $useFilters = FALSE;
191 $backedUpFilters = array();
192 } else {
193 list($backedUpFilters, $useFilters) = $this->prepareFiltersInStorage($filterMode);
194 }
195
196 $fileObjects = $this->storage->getFilesInFolder($this, $start, $numberOfItems, $useFilters, $recursive);
197
198 $this->restoreBackedUpFiltersInStorage($backedUpFilters);
199
200 return $fileObjects;
201 }
202
203 /**
204 * Returns amount of all files within this folder, optionally filtered by
205 * the given pattern
206 *
207 * @param array $filterMethods
208 * @param boolean $recursive
209 *
210 * @return integer
211 */
212 public function getFileCount(array $filterMethods = array(), $recursive = FALSE) {
213 return count($this->storage->getFileIdentifiersInFolder($this->identifier, TRUE, $recursive));
214 }
215
216 /**
217 * Returns the object for a subfolder of the current folder, if it exists.
218 *
219 * @param string $name Name of the subfolder
220 *
221 * @throws \InvalidArgumentException
222 * @return Folder
223 */
224 public function getSubfolder($name) {
225 if (!$this->storage->hasFolderInFolder($name, $this)) {
226 throw new \InvalidArgumentException('Folder "' . $name . '" does not exist in "' . $this->identifier . '"', 1329836110);
227 }
228 /** @var $factory ResourceFactory */
229 $factory = ResourceFactory::getInstance();
230 $folderObject = $factory->createFolderObject($this->storage, $this->identifier . $name . '/', $name);
231 return $folderObject;
232 }
233
234 /**
235 * Returns a list of subfolders
236 *
237 * @param integer $start The item to start at
238 * @param integer $numberOfItems The number of items to return
239 * @param integer $filterMode The filter mode to use for the file list.
240 * @return Folder[]
241 */
242 public function getSubfolders($start = 0, $numberOfItems = 0, $filterMode = self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS) {
243 list($backedUpFilters, $useFilters) = $this->prepareFiltersInStorage($filterMode);
244
245 $folderObjects = array();
246 $folderArray = $this->storage->getFolderList($this->identifier, $start, $numberOfItems, $useFilters);
247 if (count($folderArray) > 0) {
248 /** @var $factory ResourceFactory */
249 $factory = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\ResourceFactory');
250 foreach ($folderArray as $folder) {
251 $folderObjects[$folder['name']] = $factory->createFolderObject($this->storage, $folder['identifier'], $folder['name']);
252 }
253 }
254
255 $this->restoreBackedUpFiltersInStorage($backedUpFilters);
256
257 return $folderObjects;
258 }
259
260 /**
261 * Adds a file from the local server disk. If the file already exists and
262 * overwriting is disabled,
263 *
264 * @param string $localFilePath
265 * @param string $fileName
266 * @param string $conflictMode possible value are 'cancel', 'replace'
267 * @return File The file object
268 */
269 public function addFile($localFilePath, $fileName = NULL, $conflictMode = 'cancel') {
270 $fileName = $fileName ? $fileName : PathUtility::basename($localFilePath);
271 return $this->storage->addFile($localFilePath, $this, $fileName, $conflictMode);
272 }
273
274 /**
275 * Adds an uploaded file into the Storage.
276 *
277 * @param array $uploadedFileData contains information about the uploaded file given by $_FILES['file1']
278 * @param string $conflictMode possible value are 'cancel', 'replace'
279 * @return File The file object
280 */
281 public function addUploadedFile(array $uploadedFileData, $conflictMode = 'cancel') {
282 return $this->storage->addUploadedFile($uploadedFileData, $this, $uploadedFileData['name'], $conflictMode);
283 }
284
285 /**
286 * Renames this folder.
287 *
288 * @param string $newName
289 * @return Folder
290 */
291 public function rename($newName) {
292 return $this->storage->renameFolder($this, $newName);
293 }
294
295 /**
296 * Deletes this folder from its storage. This also means that this object becomes useless.
297 *
298 * @param boolean $deleteRecursively
299 * @return boolean TRUE if deletion succeeded
300 */
301 public function delete($deleteRecursively = TRUE) {
302 return $this->storage->deleteFolder($this, $deleteRecursively);
303 }
304
305 /**
306 * Creates a new blank file
307 *
308 * @param string $fileName
309 * @return File The new file object
310 */
311 public function createFile($fileName) {
312 return $this->storage->createFile($fileName, $this);
313 }
314
315 /**
316 * Creates a new folder
317 *
318 * @param string $folderName
319 * @return Folder The new folder object
320 */
321 public function createFolder($folderName) {
322 return $this->storage->createFolder($folderName, $this);
323 }
324
325 /**
326 * Copies folder to a target folder
327 *
328 * @param Folder $targetFolder Target folder to copy to.
329 * @param string $targetFolderName an optional destination fileName
330 * @param string $conflictMode "overrideExistingFile", "renameNewFile" or "cancel
331 * @return Folder New (copied) folder object.
332 */
333 public function copyTo(Folder $targetFolder, $targetFolderName = NULL, $conflictMode = 'renameNewFile') {
334 return $targetFolder->getStorage()->copyFolder($this, $targetFolder, $targetFolderName, $conflictMode);
335 }
336
337 /**
338 * Moves folder to a target folder
339 *
340 * @param Folder $targetFolder Target folder to move to.
341 * @param string $targetFolderName an optional destination fileName
342 * @param string $conflictMode "overrideExistingFile", "renameNewFile" or "cancel
343 * @return Folder New (copied) folder object.
344 */
345 public function moveTo(Folder $targetFolder, $targetFolderName = NULL, $conflictMode = 'renameNewFile') {
346 return $targetFolder->getStorage()->moveFolder($this, $targetFolder, $targetFolderName, $conflictMode);
347 }
348
349 /**
350 * Checks if a file exists in this folder
351 *
352 * @param string $name
353 * @return boolean
354 */
355 public function hasFile($name) {
356 return $this->storage->hasFileInFolder($name, $this);
357 }
358
359 /**
360 * Checks if a folder exists in this folder.
361 *
362 * @param string $name
363 * @return boolean
364 */
365 public function hasFolder($name) {
366 return $this->storage->hasFolderInFolder($name, $this);
367 }
368
369 /**
370 * Check if a file operation (= action) is allowed on this folder
371 *
372 * @param string $action Action that can be read, write or delete
373 * @return boolean
374 */
375 public function checkActionPermission($action) {
376 return $this->getStorage()->checkFolderActionPermission($action, $this);
377 }
378
379 /**
380 * Updates the properties of this folder, e.g. after re-indexing or moving it.
381 *
382 * NOTE: This method should not be called from outside the File Abstraction Layer (FAL)!
383 *
384 * @param array $properties
385 * @return void
386 * @internal
387 */
388 public function updateProperties(array $properties) {
389 // Setting identifier and name to update values
390 if (isset($properties['identifier'])) {
391 $this->identifier = $properties['identifier'];
392 }
393 if (isset($properties['name'])) {
394 $this->name = $properties['name'];
395 }
396 }
397
398 /**
399 * Prepares the filters in this folder's storage according to a set filter mode.
400 *
401 * @param integer $filterMode The filter mode to use; one of the FILTER_MODE_* constants
402 * @return array The backed up filters as an array (NULL if filters were not backed up) and whether to use filters or not (boolean)
403 */
404 protected function prepareFiltersInStorage($filterMode) {
405 $backedUpFilters = NULL;
406 $useFilters = TRUE;
407
408 switch ($filterMode) {
409 case self::FILTER_MODE_USE_OWN_FILTERS:
410 $backedUpFilters = $this->storage->getFileAndFolderNameFilters();
411 $this->storage->setFileAndFolderNameFilters($this->fileAndFolderNameFilters);
412
413 break;
414
415 case self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS:
416 if (count($this->fileAndFolderNameFilters) > 0) {
417 $backedUpFilters = $this->storage->getFileAndFolderNameFilters();
418 foreach ($this->fileAndFolderNameFilters as $filter) {
419 $this->storage->addFileAndFolderNameFilter($filter);
420 }
421 }
422
423 break;
424
425 case self::FILTER_MODE_USE_STORAGE_FILTERS:
426 // nothing to do here
427
428 break;
429
430 case self::FILTER_MODE_NO_FILTERS:
431 $useFilters = FALSE;
432
433 break;
434 }
435 return array($backedUpFilters, $useFilters);
436 }
437
438 /**
439 * Restores the filters of a storage.
440 *
441 * @param array $backedUpFilters The filters to restore; might be NULL if no filters have been backed up, in
442 * which case this method does nothing.
443 * @see prepareFiltersInStorage()
444 */
445 protected function restoreBackedUpFiltersInStorage($backedUpFilters) {
446 if ($backedUpFilters !== NULL) {
447 $this->storage->setFileAndFolderNameFilters($backedUpFilters);
448 }
449 }
450
451 /**
452 * Sets the filters to use when listing files. These are only used if the filter mode is one of
453 * FILTER_MODE_USE_OWN_FILTERS and FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS
454 *
455 * @param array $filters
456 */
457 public function setFileAndFolderNameFilters(array $filters) {
458 $this->fileAndFolderNameFilters = $filters;
459 }
460
461 /**
462 * Returns the role of this folder (if any). See FolderInterface::ROLE_* constants for possible values.
463 *
464 * @return integer
465 */
466 public function getRole() {
467 return $this->storage->getRole($this);
468 }
469 }