Issue Bug On Hold Orphaned

#99403Reference count in the file list could be incorrect

Dmitry Dulepov 12/21/22, 09:46 updated 04/28/23, 18:46

Description

TYPO3\CMS\Filelist::getFileReferenceCount() uses sys_ref_index to find amount of references to the file but this number is not always correct. For example, creating or deleting file references in frontend usually does not update sys_ref_index. This causes not only wrong numbers in the UI but also prohibits file deletion if the file is unused but reference index is not updated.

It would be more practical to count records in sys_file_reference rather than in sys_refindex. Therefore I propose to change the above mentioned method to only count how many entries with uid_local = $file->getUid() in sys_file_reference, not in sys_refindex.

Basically:

    /**
     * Counts how often the given file is referenced. This is done by
     * looking up the file in the "sys_refindex" table, while excluding
     * sys_file_metadata relations as these are no such references.
     */
    protected function getFileReferenceCount2(File $file): int
    {
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_refindex');
        return (int)$queryBuilder
            ->count('*')
            ->from('sys_file_reference')
            ->where(
                $queryBuilder->expr()->eq(
                    'uid_local',
                    $queryBuilder->createNamedParameter($file->getUid(), Connection::PARAM_INT)
                )
            )
            ->executeQuery()
            ->fetchOne();
    }

Reviews 0

No change resolves or names this issue in its commit message, and no comment links one.

Fix this issue

Start the commit message of your patch with this. Add what the patch does, and the versions it goes to in "Releases:". How to create a patch

Commit message
[BUGFIX] Reference count in the file list could be incorrect Resolves: #99403 Releases: main