[BUGFIX] StorageRepository should ignore not found driver class
[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 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 * @package TYPO3
35 * @subpackage t3lib
36 */
37 class StorageRepository extends \TYPO3\CMS\Core\Resource\AbstractRepository {
38
39 /**
40 * @var string
41 */
42 protected $objectType = 'TYPO3\\CMS\\Core\\Resource\\ResourceStorage';
43
44 /**
45 * @var string
46 */
47 protected $table = 'sys_file_storage';
48
49 /**
50 * @var string
51 */
52 protected $typeField = 'type';
53
54 /**
55 * @var \TYPO3\CMS\Core\Log\Logger
56 */
57 protected $logger;
58
59 public function __construct() {
60 parent::__construct();
61
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__);
65 }
66
67 /**
68 * Finds storages by type.
69 *
70 * @param string $storageType
71 * @return \TYPO3\CMS\Core\Resource\ResourceStorage[]
72 */
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(
79 '*',
80 $this->table,
81 $whereClause . $this->getWhereClauseForEnabledFields()
82 );
83 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
84 if ($driverRegistry->driverExists($row['driver'])) {
85 $storageObjects[] = $this->createDomainObject($row);
86 } else {
87 $this->logger->warning(
88 sprintf('Could not instantiate storage "%s" because of missing driver.', array($row['name'])),
89 $row
90 );
91 }
92 }
93 $GLOBALS['TYPO3_DB']->sql_free_result($res);
94 return $storageObjects;
95 }
96
97 /**
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/
100 *
101 * @return \TYPO3\CMS\Core\Resource\ResourceStorage[]
102 */
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'],
111 'relative',
112 'This is the local fileadmin/ directory. This storage mount has been created automatically by TYPO3.'
113 );
114 }
115
116 $storageObjects = array();
117 $whereClause = NULL;
118 if ($this->type != '') {
119 $whereClause = $this->typeField . ' = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($this->type, $this->table);
120 }
121 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
122 '*',
123 $this->table,
124 ($whereClause ? $whereClause : '1=1') . $this->getWhereClauseForEnabledFields()
125 );
126
127 /** @var $driverRegistry \TYPO3\CMS\Core\Resource\Driver\DriverRegistry */
128 $driverRegistry = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Core\Resource\Driver\DriverRegistry');
129
130 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
131 if ($driverRegistry->driverExists($row['driver'])) {
132 $storageObjects[] = $this->createDomainObject($row);
133 } else {
134 $this->logger->warning(
135 sprintf('Could not instantiate storage "%s" because of missing driver.', array($row['name'])),
136 $row
137 );
138 }
139 }
140 $GLOBALS['TYPO3_DB']->sql_free_result($res);
141 return $storageObjects;
142 }
143
144 /**
145 * Create the initial local storage base e.g. for the fileadmin/ directory.
146 *
147 * @param string $name
148 * @param string $basePath
149 * @param string $pathType
150 * @param string $description
151 * @return integer uid of the inserted record
152 */
153 public function createLocalStorage($name, $basePath, $pathType, $description = '') {
154 $field_values = array(
155 'pid' => 0,
156 'tstamp' => $GLOBALS['EXEC_TIME'],
157 'crdate' => $GLOBALS['EXEC_TIME'],
158 'name' => $name,
159 'description' => $description,
160 'driver' => 'Local',
161 'configuration' => '<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
162 <T3FlexForms>
163 <data>
164 <sheet index="sDEF">
165 <language index="lDEF">
166 <field index="basePath">
167 <value index="vDEF">' . rtrim($basePath, '/') . '/</value>
168 </field>
169 <field index="pathType">
170 <value index="vDEF">' . $pathType . '</value>
171 </field>
172 </language>
173 </sheet>
174 </data>
175 </T3FlexForms>',
176 'is_online' => 1,
177 'is_browsable' => 1,
178 'is_public' => 1,
179 'is_writable' => 1
180 );
181 $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_file_storage', $field_values);
182 return (int) $GLOBALS['TYPO3_DB']->sql_insert_id();
183 }
184
185 /**
186 * Creates an object managed by this repository.
187 *
188 * @param array $databaseRow
189 * @return \TYPO3\CMS\Core\Resource\ResourceStorage
190 */
191 protected function createDomainObject(array $databaseRow) {
192 return $this->factory->getStorageObject($databaseRow['uid'], $databaseRow);
193 }
194
195 /**
196 * get the WHERE clause for the enabled fields of this TCA table
197 * depending on the context
198 *
199 * @return string the additional where clause, something like " AND deleted=0 AND hidden=0"
200 */
201 protected function getWhereClauseForEnabledFields() {
202 if (is_object($GLOBALS['TSFE'])) {
203 // frontend context
204 $whereClause = $GLOBALS['TSFE']->sys_page->enableFields($this->table);
205 } else {
206 // backend context
207 $whereClause = \TYPO3\CMS\Backend\Utility\BackendUtility::BEenableFields($this->table);
208 }
209 return $whereClause;
210 }
211 }
212
213
214 ?>