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