addOption('file', null, InputOption::VALUE_REQUIRED, 'Placeholder image path', self::DEFAULT_FILE); $this->addOption('dry-run', null, InputOption::VALUE_NONE, 'Report only'); } protected function execute(InputInterface $input, OutputInterface $output): int { Bootstrap::initializeBackendAuthentication(); $dryRun = (bool)$input->getOption('dry-run'); $path = (string)$input->getOption('file'); try { $file = GeneralUtility::makeInstance(ResourceFactory::class)->retrieveFileOrFolderObject($path); } catch (\Throwable $e) { $file = null; } if (!$file instanceof File) { $output->writeln(sprintf('Placeholder not found in FAL: %s', $path)); return Command::FAILURE; } $output->writeln(sprintf('Placeholder: %s (sys_file %d)', $file->getIdentifier(), $file->getUid())); // Markets that already have an image reference. $qb = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_file_reference'); $withImage = $qb->select('uid_foreign')->from('sys_file_reference') ->where( $qb->expr()->eq('tablenames', $qb->createNamedParameter(self::TABLE, ParameterType::STRING)), $qb->expr()->eq('fieldname', $qb->createNamedParameter('image', ParameterType::STRING)), $qb->expr()->eq('deleted', 0) ) ->executeQuery()->fetchFirstColumn(); $withImage = array_flip(array_map('intval', $withImage)); $qb = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable(self::TABLE); $markets = $qb->select('uid', 'pid', 'title')->from(self::TABLE) ->where($qb->expr()->eq('deleted', 0)) ->orderBy('title', 'ASC') ->executeQuery()->fetchAllAssociative(); $assigned = 0; $skipped = 0; foreach ($markets as $market) { $uid = (int)$market['uid']; if (isset($withImage[$uid])) { $skipped++; continue; } if ($dryRun) { $output->writeln(sprintf(' ASSIGN placeholder -> %s (uid %d)', $market['title'], $uid)); $assigned++; continue; } $dataHandler = GeneralUtility::makeInstance(DataHandler::class); $dataHandler->start([ 'sys_file_reference' => [ 'NEWref' => [ 'uid_local' => $file->getUid(), 'pid' => (int)$market['pid'], ], ], self::TABLE => [ (string)$uid => ['image' => 'NEWref'], ], ], []); $dataHandler->process_datamap(); if ($dataHandler->errorLog !== []) { $output->writeln(sprintf(' %s: %s', $market['title'], implode(' | ', $dataHandler->errorLog))); continue; } $output->writeln(sprintf(' assigned -> %s (uid %d)', $market['title'], $uid)); $assigned++; } $output->writeln(''); $output->writeln(sprintf( '%s: %d assigned, %d already had an image.', $dryRun ? 'DRY-RUN - would be' : 'Done', $assigned, $skipped )); return Command::SUCCESS; } }