TYPO3 11.5.40:生成fileadmin文件列表及关联页面ID
处理TYPO3 11.5.40中fileadmin文件引用与页面关联的方法
以下是三种实用方案,帮你生成fileadmin文件列表并输出被引用文件的关联页面ID:
方法1:直接数据库查询(快速高效)
TYPO3的文件引用存储在sys_file_reference表,可通过SQL关联查询直接获取结果:
-- 生成所有fileadmin文件列表,含引用次数与关联页面ID SELECT f.identifier AS 文件路径, COUNT(sfr.uid) AS 引用次数, GROUP_CONCAT(DISTINCT p.uid SEPARATOR ', ') AS 关联页面ID FROM sys_file f LEFT JOIN sys_file_reference sfr ON f.uid = sfr.uid_local LEFT JOIN tt_content c ON sfr.uid_foreign = c.uid AND sfr.tablenames = 'tt_content' LEFT JOIN pages p ON c.pid = p.uid OR sfr.uid_foreign = p.uid AND sfr.tablenames = 'pages' WHERE f.identifier LIKE 'fileadmin/%' GROUP BY f.uid, f.identifier ORDER BY 引用次数 ASC;
说明:
- 未被引用的文件
引用次数为0,关联页面ID为空 - 如需覆盖更多引用场景(比如分类、用户表),可自行添加对应表的关联逻辑
方法2:自定义CLI命令(适合无直接数据库权限场景)
编写一个简单的TYPO3 CLI命令,通过代码遍历文件并统计引用:
- 在自定义扩展中创建命令类
typo3conf/ext/your_ext/Classes/Command/FileReferenceCommand.php:
<?php namespace YourVendor\YourExt\Command; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use TYPO3\CMS\Core\Resource\FileRepository; use TYPO3\CMS\Core\Resource\ResourceFactory; use TYPO3\CMS\Core\Database\ConnectionPool; class FileReferenceCommand extends Command { protected function configure() { $this->setName('file:list-references') ->setDescription('列出所有fileadmin文件及其关联页面ID'); } protected function execute(InputInterface $input, OutputInterface $output) { $fileRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(FileRepository::class); $connectionPool = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(ConnectionPool::class); // 查询所有fileadmin文件 $fileQuery = $connectionPool->getConnectionForTable('sys_file') ->select(['uid', 'identifier'], 'sys_file') ->where($connectionPool->getQueryBuilder()->expr()->like('identifier', $connectionPool->getQueryBuilder()->createNamedParameter('fileadmin/%'))); $output->writeln("文件路径 | 引用次数 | 关联页面ID"); $output->writeln("----------|----------|------------"); foreach ($fileQuery->fetchAllAssociative() as $fileRow) { $file = ResourceFactory::getInstance()->getFileObject($fileRow['uid']); $references = $fileRepository->findByFile($file); $pageIds = []; foreach ($references as $reference) { $table = $reference->getTable(); $foreignUid = $reference->getUidForeign(); if ($table === 'tt_content') { $contentRow = $connectionPool->getConnectionForTable('tt_content') ->select(['pid'], 'tt_content') ->where('uid = ' . (int)$foreignUid) ->fetchAssociative(); if ($contentRow) $pageIds[] = $contentRow['pid']; } elseif ($table === 'pages') { $pageIds[] = $foreignUid; } // 可自行添加其他引用表的处理逻辑 } $uniquePageIds = array_unique($pageIds); $output->writeln( $fileRow['identifier'] . " | " . count($references) . " | " . implode(', ', $uniquePageIds) ); } return Command::SUCCESS; } }
- 在
ext_localconf.php注册命令:
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers'][] = \YourVendor\YourExt\Command\FileReferenceCommand::class;
- 执行命令:
./typo3/sysext/core/bin/typo3 file:list-references
方法3:第三方扩展(无需编码)
如果不想自行开发,可使用兼容TYPO3 11.5的wacon/fileusage扩展,安装后可在后台直接查看文件的引用情况及关联页面。
内容的提问来源于stack exchange,提问作者Leo
相关产品推荐
相关产品推荐

