[BUGFIX] Move forgotten getEnvironmentMode()
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / StorageRepository.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 * Repository for accessing the file mounts
31 *
32 * @author Andreas Wolf <andreas.wolf@ikt-werk.de>
33 * @author Ingmar Schlecht <ingmar@typo3.org>
34 */
35 class StorageRepository extends AbstractRepository {
36
37 /**
38 * @var string
39 */
40 protected $objectType = 'TYPO3\\CMS\\Core\\Resource\\ResourceStorage';
41
42 /**
43 * @var string
44 */
45 protected $table = 'sys_file_storage';
46
47 /**
48 * @var string
49 */
50 protected $typeField = 'driver';
51
52 /**
53 * @var string
54 */
55 protected $driverField = 'driver';
56
57 /**
58 * @var \TYPO3\CMS\Core\Log\Logger
59 */
60 protected $logger;
61
62 public function __construct() {
63 parent::__construct();
64
65 /** @var $logManager \TYPO3\CMS\Core\Log\LogManager */
66 $logManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Core\Log\LogManager');
67 $this->logger = $logManager->getLogger(__CLASS__);
68 }
69
70 /**
71 * Finds storages by type, i.e. the driver used
72 *
73 * @param string $storageType
74 * @return ResourceStorage[]
75 */
76 public function findByStorageType($storageType) {
77 /** @var $driverRegistry Driver\DriverRegistry */
78 $driverRegistry = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Core\Resource\Driver\DriverRegistry');
79 $storageObjects = array();
80 $whereClause = $this->typeField . ' = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($storageType, $this->table);
81 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
82 '*',
83 $this->table,
84 $whereClause . $this->getWhereClauseForEnabledFields()
85 );
86 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
87 if ($driverRegistry->driverExists($row['driver'])) {
88 $storageObjects[] = $this->createDomainObject($row);
89 } else {
90 $this->logger->warning(
91 sprintf('Could not instantiate storage "%s" because of missing driver.', array($row['name'])),
92 $row
93 );
94 }
95 }
96 $GLOBALS['TYPO3_DB']->sql_free_result($res);
97 return $storageObjects;
98 }
99
100 /**
101 * Returns a list of mountpoints that are available in the VFS.
102 * In case no storage exists this automatically created a storage for fileadmin/
103 *
104 * @return ResourceStorage[]
105 */
106 public function findAll() {
107 // check if we have never created a storage before (no records, regardless of the enableFields),
108 // only fetch one record for that (is enough). If no record is found, create the fileadmin/ storage
109 $storageObjectsCount = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('uid', $this->table, '1=1');
110 if ($storageObjectsCount === 0) {
111 $this->createLocalStorage(
112 'fileadmin/ (auto-created)',
113 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir'],
114 'relative',
115 'This is the local fileadmin/ directory. This storage mount has been created automatically by TYPO3.'
116 );
117 }
118
119 $storageObjects = array();
120 $whereClause = NULL;
121 if ($this->type != '') {
122 $whereClause = $this->typeField . ' = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($this->type, $this->table);
123 }
124 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
125 '*',
126 $this->table,
127 ($whereClause ? $whereClause : '1=1') . $this->getWhereClauseForEnabledFields()
128 );
129
130 /** @var $driverRegistry Driver\DriverRegistry */
131 $driverRegistry = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Core\Resource\Driver\DriverRegistry');
132
133 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
134 if ($driverRegistry->driverExists($row['driver'])) {
135 $storageObjects[] = $this->createDomainObject($row);
136 } else {
137 $this->logger->warning(
138 sprintf('Could not instantiate storage "%s" because of missing driver.', array($row['name'])),
139 $row
140 );
141 }
142 }
143 $GLOBALS['TYPO3_DB']->sql_free_result($res);
144 return $storageObjects;
145 }
146
147 /**
148 * Create the initial local storage base e.g. for the fileadmin/ directory.
149 *
150 * @param string $name
151 * @param string $basePath
152 * @param string $pathType
153 * @param string $description
154 * @return integer uid of the inserted record
155 */
156 public function createLocalStorage($name, $basePath, $pathType, $description = '') {
157
158 // create the FlexForm for the driver configuration
159 $flexFormData = array(
160 'data' => array(
161 'sDEF' => array(
162 'lDEF' => array(
163 'basePath' => array('vDEF' => rtrim($basePath, '/') . '/'),
164 'pathType' => array('vDEF' => $pathType)
165 )
166 )
167 )
168 );
169
170 /** @var $flexObj \TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools */
171 $flexObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Configuration\\FlexForm\\FlexFormTools');
172 $flexFormXml = $flexObj->flexArray2Xml($flexFormData, TRUE);
173
174 // create the record
175 $field_values = array(
176 'pid' => 0,
177 'tstamp' => $GLOBALS['EXEC_TIME'],
178 'crdate' => $GLOBALS['EXEC_TIME'],
179 'name' => $name,
180 'description' => $description,
181 'driver' => 'Local',
182 'configuration' => $flexFormXml,
183 'is_online' => 1,
184 'is_browsable' => 1,
185 'is_public' => 1,
186 'is_writable' => 1
187 );
188 $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_file_storage', $field_values);
189 return (int) $GLOBALS['TYPO3_DB']->sql_insert_id();
190 }
191
192 /**
193 * Creates an object managed by this repository.
194 *
195 * @param array $databaseRow
196 * @return ResourceStorage
197 */
198 protected function createDomainObject(array $databaseRow) {
199 return $this->factory->getStorageObject($databaseRow['uid'], $databaseRow);
200 }
201
202 }
203
204
205 ?>