[TASK] Remove usage of TSFE->sys_page
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / FileRepository.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Core\Database\ConnectionPool;
19 use TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer;
20 use TYPO3\CMS\Core\Database\RelationHandler;
21 use TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException;
22 use TYPO3\CMS\Core\Resource\Index\FileIndexRepository;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24 use TYPO3\CMS\Core\Utility\MathUtility;
25
26 /**
27 * Repository for accessing files
28 * it also serves as the public API for the indexing part of files in general
29 */
30 class FileRepository extends AbstractRepository
31 {
32 /**
33 * The main object type of this class. In some cases (fileReference) this
34 * repository can also return FileReference objects, implementing the
35 * common FileInterface.
36 *
37 * @var string
38 */
39 protected $objectType = File::class;
40
41 /**
42 * Main File object storage table. Note that this repository also works on
43 * the sys_file_reference table when returning FileReference objects.
44 *
45 * @var string
46 */
47 protected $table = 'sys_file';
48
49 /**
50 * Creates an object managed by this repository.
51 *
52 * @param array $databaseRow
53 * @return File
54 */
55 protected function createDomainObject(array $databaseRow)
56 {
57 return $this->factory->getFileObject($databaseRow['uid'], $databaseRow);
58 }
59
60 /**
61 * Find FileReference objects by relation to other records
62 *
63 * @param string $tableName Table name of the related record
64 * @param string $fieldName Field name of the related record
65 * @param int $uid The UID of the related record (needs to be the localized uid, as translated IRRE elements relate to them)
66 * @return array An array of objects, empty if no objects found
67 * @throws \InvalidArgumentException
68 * @api
69 */
70 public function findByRelation($tableName, $fieldName, $uid)
71 {
72 $itemList = [];
73 if (!MathUtility::canBeInterpretedAsInteger($uid)) {
74 throw new \InvalidArgumentException(
75 'UID of related record has to be an integer. UID given: "' . $uid . '"',
76 1316789798
77 );
78 }
79 $referenceUids = [];
80 if ($this->getEnvironmentMode() === 'FE') {
81 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
82 ->getQueryBuilderForTable('sys_file_reference');
83
84 $queryBuilder->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class));
85 $res = $queryBuilder
86 ->select('uid')
87 ->from('sys_file_reference')
88 ->where(
89 $queryBuilder->expr()->eq(
90 'uid_foreign',
91 $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)
92 ),
93 $queryBuilder->expr()->eq(
94 'tablenames',
95 $queryBuilder->createNamedParameter($tableName, \PDO::PARAM_STR)
96 ),
97 $queryBuilder->expr()->eq(
98 'fieldname',
99 $queryBuilder->createNamedParameter($fieldName, \PDO::PARAM_STR)
100 )
101 )
102 ->orderBy('sorting_foreign')
103 ->execute();
104
105 while ($row = $res->fetch()) {
106 $referenceUids[] = $row['uid'];
107 }
108 } else {
109 /** @var $relationHandler RelationHandler */
110 $relationHandler = GeneralUtility::makeInstance(RelationHandler::class);
111 $relationHandler->start(
112 '',
113 'sys_file_reference',
114 '',
115 $uid,
116 $tableName,
117 BackendUtility::getTcaFieldConfiguration($tableName, $fieldName)
118 );
119 if (!empty($relationHandler->tableArray['sys_file_reference'])) {
120 $referenceUids = $relationHandler->tableArray['sys_file_reference'];
121 }
122 }
123 if (!empty($referenceUids)) {
124 foreach ($referenceUids as $referenceUid) {
125 try {
126 // Just passing the reference uid, the factory is doing workspace
127 // overlays automatically depending on the current environment
128 $itemList[] = $this->factory->getFileReferenceObject($referenceUid);
129 } catch (ResourceDoesNotExistException $exception) {
130 // No handling, just omit the invalid reference uid
131 }
132 }
133 }
134
135 return $itemList;
136 }
137
138 /**
139 * Find FileReference objects by uid
140 *
141 * @param int $uid The UID of the sys_file_reference record
142 * @return FileReference|bool
143 * @throws \InvalidArgumentException
144 * @api
145 */
146 public function findFileReferenceByUid($uid)
147 {
148 if (!MathUtility::canBeInterpretedAsInteger($uid)) {
149 throw new \InvalidArgumentException('The UID of record has to be an integer. UID given: "' . $uid . '"', 1316889798);
150 }
151 try {
152 $fileReferenceObject = $this->factory->getFileReferenceObject($uid);
153 } catch (\InvalidArgumentException $exception) {
154 $fileReferenceObject = false;
155 }
156 return $fileReferenceObject;
157 }
158
159 /**
160 * Search for files by name in a given folder
161 *
162 * @param Folder $folder
163 * @param string $fileName
164 * @return File[]
165 */
166 public function searchByName(Folder $folder, $fileName)
167 {
168 /** @var ResourceFactory $fileFactory */
169 $fileFactory = GeneralUtility::makeInstance(ResourceFactory::class);
170
171 $storage = $folder->getStorage();
172 $folders = $storage->getFoldersInFolder($folder, 0, 0, true, true);
173 $folders[$folder->getIdentifier()] = $folder;
174
175 $fileRecords = $this->getFileIndexRepository()->findByFolders($folders, false, $fileName);
176 $fileRecords = array_merge($fileRecords, $this->getFileIndexRepository()->findBySearchWordInMetaData($fileName));
177
178 $files = [];
179 foreach ($fileRecords as $fileRecord) {
180 try {
181 $file = $fileFactory->getFileObject($fileRecord['uid'], $fileRecord);
182 if ($storage->checkFileAndFolderNameFilters($file)) {
183 $files[] = $file;
184 }
185 } catch (Exception\FileDoesNotExistException $ignoredException) {
186 continue;
187 }
188 }
189
190 return $files;
191 }
192
193 /**
194 * Return a file index repository
195 *
196 * @return FileIndexRepository
197 */
198 protected function getFileIndexRepository()
199 {
200 return FileIndexRepository::getInstance();
201 }
202 }