Bug #99403
open
Reference count in the file list could be incorrect
Added by Dmitry Dulepov over 1 year ago.
Updated over 1 year ago.
Category:
File Abstraction Layer (FAL)
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();
}
- Status changed from New to Needs Feedback
Hi Dmitry,
I was about to change this in Core, however, there are also references to sys_file records in type=group for example, where a reference to a file could be made (I've seen this quite a lot with DCE for example).
Should we change this to 2 queries?
Hi Benni!
It can be more complicated than just changing this single method. I found more methods that do the same:
- BackendUtility::referenceCount()
- ExtendedFileUtility::func_delete()
- DatabaseService::getReferencesByPersistenceIdentifier()
- MissingRelationsCommand::findRelationsToNonExistingRecords()
I think the issue is larger than I originally expected and needs more investigation and thinking. But I think the idea is correct in general: sys_refindex is not the main source for these references and usually it is not updated when references are created in FE. So it can get wrong easily.
I think this whole thing needs a new API that would have methods for counting references and returning them. All places that currently query sys_refindex directly, should use a single central place for this. Then we could change that one single place to make sure that we use the actual source for references (sys_file_reference).
Regarding references from "group" field. While it is possible to use sys_file there, I believe it is not a recommended way. At least official docs suggest to do it via sys_file_reference. I am not sure how to solve it for DCE.
May be, put this ticket on hold until we have a better idea or a better solution. At least, we are aware of the issue and may find the solution in future. What do you think?
Dmitry Dulepov wrote in #note-2:
May be, put this ticket on hold until we have a better idea or a better solution. At least, we are aware of the issue and may find the solution in future. What do you think?
Yes. I agree.
- Status changed from Needs Feedback to On Hold
Also available in: Atom
PDF