2 declare(strict_types = 1);
17 use Doctrine\DBAL\DBALException;
18 use Psr\Log\LoggerAwareInterface;
19 use Psr\Log\LoggerAwareTrait;
20 use Symfony\Component\Console\Output\OutputInterface;
54 protected $table =
'backend_layout';
83 return 'backendLayoutIcons';
91 return 'Migrate all file relations from backend_layout.icon to sys_file_references';
99 return 'This update wizard goes through all files that are referenced in the'
100 .
' backend_layout.icon field and adds the files to the FAL File Index.'
101 .
' It also moves the files from uploads/ to the fileadmin/_migrated/ path.';
118 DatabaseUpdatedPrerequisite::class
139 $storages = GeneralUtility::makeInstance(StorageRepository::class)->findAll();
140 $this->storage = $storages[0];
142 foreach ($records as $record) {
161 $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
162 $queryBuilder = $connectionPool->getQueryBuilderForTable($this->table);
163 $queryBuilder->getRestrictions()->removeAll();
166 ->select(
'uid',
'pid', $this->fieldToMigrate)
169 $queryBuilder->expr()->isNotNull($this->fieldToMigrate),
170 $queryBuilder->expr()->neq(
171 $this->fieldToMigrate,
172 $queryBuilder->createNamedParameter(
'', \PDO::PARAM_STR)
174 $queryBuilder->expr()->comparison(
175 'CAST(CAST(' . $queryBuilder->quoteIdentifier($this->fieldToMigrate) .
' AS DECIMAL) AS CHAR)',
177 'CAST(' . $queryBuilder->quoteIdentifier($this->fieldToMigrate) .
' AS CHAR)'
183 }
catch (DBALException $e) {
184 throw new \RuntimeException(
185 'Database query failed. Error was: ' . $e->getPrevious()->getMessage(),
199 $fieldItems = GeneralUtility::trimExplode(
',', $row[$this->fieldToMigrate],
true);
200 if (empty($fieldItems) || is_numeric($row[$this->fieldToMigrate])) {
203 $fileadminDirectory = rtrim(
$GLOBALS[
'TYPO3_CONF_VARS'][
'BE'][
'fileadminDir'],
'/') .
'/';
206 $storageUid = (int)$this->storage->getUid();
207 $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
209 foreach ($fieldItems as $item) {
217 if (!is_dir($targetDirectory)) {
218 GeneralUtility::mkdir_deep($targetDirectory);
224 $queryBuilder = $connectionPool->getQueryBuilderForTable(
'sys_file');
225 $queryBuilder->getRestrictions()->removeAll();
226 $existingFileRecord = $queryBuilder->select(
'uid')->from(
'sys_file')->where(
227 $queryBuilder->expr()->eq(
229 $queryBuilder->createNamedParameter($fileSha1, \PDO::PARAM_STR)
231 $queryBuilder->expr()->eq(
233 $queryBuilder->createNamedParameter($storageUid, \PDO::PARAM_INT)
235 )->execute()->fetch();
238 if (is_array($existingFileRecord)) {
239 $fileUid = $existingFileRecord[
'uid'];
246 if ($fileUid ===
null) {
252 $file = $this->storage->getFile($this->targetPath . $item);
253 $fileUid = $file->getUid();
254 }
catch (\InvalidArgumentException $e) {
256 $this->logger->notice(
257 'File ' . $this->sourcePath . $item .
' does not exist. Reference was not migrated.',
259 'table' => $this->table,
261 'field' => $this->fieldToMigrate,
264 $format =
'File \'%s\' does not exist. Referencing field: %s.%d.%s. The reference was not migrated.';
265 $this->output->writeln(sprintf(
267 $this->sourcePath . $item,
270 $this->fieldToMigrate
279 'table_local' =>
'sys_file',
280 'pid' => $this->table ===
'pages' ? $row[
'uid'] : $row[
'pid'],
281 'uid_foreign' => $row[
'uid'],
282 'uid_local' => $fileUid,
286 'sorting' => $i + 256,
287 'sorting_foreign' => $i,
290 $queryBuilder = $connectionPool->getQueryBuilderForTable(
'sys_file_reference');
291 $queryBuilder->insert(
'sys_file_reference')->values(
$fields)->execute();
298 if ($i === count($fieldItems)) {
299 $queryBuilder = $connectionPool->getQueryBuilderForTable($this->table);
300 $queryBuilder->update($this->table)->where(
301 $queryBuilder->expr()->eq(
303 $queryBuilder->createNamedParameter($row[
'uid'], \PDO::PARAM_INT)
305 )->set($this->fieldToMigrate, $i)->execute();