2 namespace TYPO3\CMS\Core\
Resource;
4 /***************************************************************
7 * (c) 2011 Andreas Wolf <andreas.wolf@ikt-werk.de>
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.
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.
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.
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
30 * Repository for accessing the file mounts
32 * @author Andreas Wolf <andreas.wolf@ikt-werk.de>
33 * @author Ingmar Schlecht <ingmar@typo3.org>
37 class StorageRepository
extends \TYPO3\CMS\Core\
Resource\AbstractRepository
{
42 protected $objectType = 'TYPO3\\CMS\\Core\\Resource\\ResourceStorage';
47 protected $table = 'sys_file_storage';
52 protected $typeField = 'type';
55 * @var \TYPO3\CMS\Core\Log\Logger
59 public function __construct() {
60 parent
::__construct();
62 /** @var $logManager \TYPO3\CMS\Core\Log\LogManager */
63 $logManager = \TYPO3\CMS\Core\Utility\GeneralUtility
::makeInstance('TYPO3\CMS\Core\Log\LogManager');
64 $this->logger
= $logManager->getLogger(__CLASS__
);
68 * Finds storages by type.
70 * @param string $storageType
71 * @return \TYPO3\CMS\Core\Resource\ResourceStorage[]
73 public function findByStorageType($storageType) {
74 /** @var $driverRegistry \TYPO3\CMS\Core\Resource\Driver\DriverRegistry */
75 $driverRegistry = \TYPO3\CMS\Core\Utility\GeneralUtility
::makeInstance('TYPO3\CMS\Core\Resource\Driver\DriverRegistry');
76 $storageObjects = array();
77 $whereClause = $this->typeField
. ' = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($storageType, $this->table
);
78 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
81 $whereClause . $this->getWhereClauseForEnabledFields()
83 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
84 if ($driverRegistry->driverExists($row['driver'])) {
85 $storageObjects[] = $this->createDomainObject($row);
87 $this->logger
->warning(
88 sprintf('Could not instantiate storage "%s" because of missing driver.', array($row['name'])),
93 $GLOBALS['TYPO3_DB']->sql_free_result($res);
94 return $storageObjects;
98 * Returns a list of mountpoints that are available in the VFS.
99 * In case no storage exists this automatically created a storage for fileadmin/
101 * @return \TYPO3\CMS\Core\Resource\ResourceStorage[]
103 public function findAll() {
104 // check if we have never created a storage before (no records, regardless of the enableFields),
105 // only fetch one record for that (is enough). If no record is found, create the fileadmin/ storage
106 $storageObjectsCount = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('uid', $this->table
, '1=1');
107 if ($storageObjectsCount === 0) {
108 $this->createLocalStorage(
109 'fileadmin/ (auto-created)',
110 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir'],
112 'This is the local fileadmin/ directory. This storage mount has been created automatically by TYPO3.'
116 $storageObjects = array();
118 if ($this->type
!= '') {
119 $whereClause = $this->typeField
. ' = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($this->type
, $this->table
);
121 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
124 ($whereClause ?
$whereClause : '1=1') . $this->getWhereClauseForEnabledFields()
127 /** @var $driverRegistry \TYPO3\CMS\Core\Resource\Driver\DriverRegistry */
128 $driverRegistry = \TYPO3\CMS\Core\Utility\GeneralUtility
::makeInstance('TYPO3\CMS\Core\Resource\Driver\DriverRegistry');
130 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
131 if ($driverRegistry->driverExists($row['driver'])) {
132 $storageObjects[] = $this->createDomainObject($row);
134 $this->logger
->warning(
135 sprintf('Could not instantiate storage "%s" because of missing driver.', array($row['name'])),
140 $GLOBALS['TYPO3_DB']->sql_free_result($res);
141 return $storageObjects;
145 * Create the initial local storage base e.g. for the fileadmin/ directory.
147 * @param string $name
148 * @param string $basePath
149 * @param string $pathType
150 * @param string $description
151 * @return integer uid of the inserted record
153 public function createLocalStorage($name, $basePath, $pathType, $description = '') {
154 $field_values = array(
156 'tstamp' => $GLOBALS['EXEC_TIME'],
157 'crdate' => $GLOBALS['EXEC_TIME'],
159 'description' => $description,
161 'configuration' => '<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
165 <language index="lDEF">
166 <field index="basePath">
167 <value index="vDEF">' . rtrim($basePath, '/') . '/</value>
169 <field index="pathType">
170 <value index="vDEF">' . $pathType . '</value>
181 $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_file_storage', $field_values);
182 return (int) $GLOBALS['TYPO3_DB']->sql_insert_id();
186 * Creates an object managed by this repository.
188 * @param array $databaseRow
189 * @return \TYPO3\CMS\Core\Resource\ResourceStorage
191 protected function createDomainObject(array $databaseRow) {
192 return $this->factory
->getStorageObject($databaseRow['uid'], $databaseRow);
196 * get the WHERE clause for the enabled fields of this TCA table
197 * depending on the context
199 * @return string the additional where clause, something like " AND deleted=0 AND hidden=0"
201 protected function getWhereClauseForEnabledFields() {
202 if (is_object($GLOBALS['TSFE'])) {
204 $whereClause = $GLOBALS['TSFE']->sys_page
->enableFields($this->table
);
207 $whereClause = \TYPO3\CMS\Backend\Utility\BackendUtility
::BEenableFields($this->table
);