Closed (outdated)
Project:
Drupal core
Version:
7.x-dev
Component:
file system
Priority:
Normal
Category:
Bug report
Assigned:
Unassigned
Reporter:
Created:
7 Oct 2012 at 12:03 UTC
Updated:
18 Apr 2024 at 20:36 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #1
chx commentedComment #2
andypost...references a {file_managed}
Needs description, probably a field_instance
blank line needed before
need more inline docs
Comment #3
chx commentedAdded a lot of comments.
Comment #5
chx commentedSee, we do have test coverage for this. And, I went overboard with caching. Also, I clarified another weird edge case where both field and field_type is specified but the field is not the correct type. Added more and more comments.
Comment #6
chx commentedI would like to point out that foreign keys is implemented in entityreference_field_schema so the only contrib module I can think of already is reliable. And, it makes an excellent argument of why you can't just presume fid.
Comment #7
quicksketch@chx has been asking me to review this for ages. Upon initial inspection it all seems to make sense to me. It's a more logical way of collecting file usages than what we're doing currently, which is basically legacy code from before we had the file_usage table to reference.
Comment #8
chx commentedInstead of this monstrosity of code I put together can we remove this and use the file_usage table in file_file_download?
Comment #9
quicksketchActually upon investigation (and trying to compare to the previous code), it looks like this entire patch was actually committed in #1801726: EntityFieldQuery v2. Doh.
Well the new approach may be more efficient but it certainly is really ugly. Any new cleanup to simplify the code would be great, but seems like it should be a different issue.
Comment #10
quicksketchComment #10.0
quicksketchAdded Entityreference to the summary.
Comment #11
devin carlson commentedAs mentioned in #9 this was already committed to Drupal 8.
I've attached a backport of the current code for Drupal 7 assuming that's the remaining task here with cleanup done in another issue. Otherwise, please feel free to update the issue title/status.
Comment #12
magtak commentedSo, I was debugging a site using Commons with a private filesystem. The root cause of the issue is quite irrelevant at this point, but it led me down the rabbit hole to realise that there is an issue with the latest patch. Let me elaborate.
file_usage_list, will return an array mapping (in the deepest level) IDs to counts https://api.drupal.org/api/drupal/includes%21file.inc/function/file_usag...
thus resulting in an array eg:
Later on in the patch, there is this foreach:
In which, entity_ids is an array eg:
Notice how the entity ID is actually the key to the array and not the value?
Further down, this array is "fed" into entity_load:
which does not comply with the array structure entity_load expects, loading node/1 in every case (for the site I was troubleshooting at least - other numbers may come up), which in turn propagates to the root issue I started investigating 3 hours ago :)
I'm attaching a patch that adds an
That should ensure that the correct entities are loaded in every case.
Comment #14
magtak commented12: 1805690_12.patch queued for re-testing.
Comment #16
magtak commentedAttempting to recreate the patch.
Comment #17
chx commentedComment #19
chx commented16: 1805690.patch queued for re-testing.
Comment #20
magtak commented@chx, sorry if the patch is failing, it's my first attempt at committing an actual patch and not proposing the the chance in code blocks :/
Comment #21
chx commentedNo, no, it's just the bot. Your code is great! Thanks much. I will follow up with a proper review. Do you think you could add tests?
Comment #22
magtak commentedNot at this point I'm afraid. Time limitations. Sorry.
Comment #23
BarisW commentedIt seems that this is already committed to core?
Comment #24
chx commentedNot in D7, I believe.
Comment #25
Paul B commentedRe-roll for Drupal 7.43
Comment #26
fietserwinA few nitpicks and it is OK.
Let's add the type: array or even array[].
In fact, let's specify the type of the params as well. it is not a must for D7, but it clarifies a lot (while looking at this code, all the time I was thinking that $field was a string...).
space at end of line after ;
add 1 space of indentation. to the code lines.
Other remarks:
1) This patch solves bugs that are not described in the IS:
- If you call this function multiple times with different fids, you always get the same results.
- If you call this function multiple times with different values for $age, you always get the same results.
- You can't leave $field AND $field_type empty (hmmm, it is described but differently).
I updated the IS for this.
2) I closed #993082: file_get_file_references() @return doc totally wrong as a duplicate.
Comment #27
Paul B commentedFietserwin, I think you reviewed the Drupal 8 patch (which has already been committed), not the Drupal 7 patch.
Comment #28
fietserwinI reviewed the patch in #25 ?!
Comment #29
Paul B commentedThat is the right patch, I need glasses.
Meanwhile I noticed a more serious problem with the patch:
If a file is referenced by something other than the file module, the references are not found.
In fact I have a custom module that references files. It's based on the file module.
I had been wondering why I'm spammed with errors 'Did not delete temporary file "foo" during garbage collection, because it is in use by the following modules: bar'
I'm not sure if it's a defect in the patch, or if I'm doing something weird?
I can' t test it directly without the patch because without it the function doesn't work with mongo_entities
because of the EntityFieldQuery that's missing an 'entity_type' entityCondition.
Comment #30
fietserwinYes: you can refer to images/files on the public/private file system directly or via a managed file. If you use the insert module to get inline images you even have both.
But I think that the function file_get_file_references() and thus this patch is about references to a managed file (a record in the table file_managed), not to the underlying file/image. So, for me, this patch is not incorrect because of this.
I am at this moment writing a module to find and remove duplicate images(/files) and there, besides searching in the table file_managed, yes, I also have to search through all long text fields and such.
Comment #31
Paul B commentedBut my custom module does write records to file_managed and file_usage. Other modules than 'file' can do that,
otherwise why would file_usage_list() be keyed by module?
Looking at the image module, it seems to do the same thing. In image_field_update_field() it calls
file_save() writes to file_managed and file_usage_add() writes to file_usage.
What is weird is that if I insert an image and then look at the file_usage table, the "module" and "type" are somehow overwritten: module becomes "file" and type becomes "node". In my custom module, no such overwriting takes place.
Anyway, file_get_file_references() for the image also returns an empty array, with or without the patch. Is it supposed to do that?
Comment #32
Paul B commentedOK my tests with images were a bit confused - image_field_update_field() is only for default images, also to get the image with file_get_file_references() you have to pass the fourth argument. file_file_download() does that when called from image_file_download().
Comment #33
fietserwin[EDIT: updated usages of file_usage_add() by contrib]
@Paul B: you are completely right with your remark in #29: only usages via the file module will be considered.
But what does this mean? I dove into how file_usage_add() is used:
What does this mean:
Comment #34
Paul B commentedI think it may be more of a private function, so it should be renamed to _file_get_file_references. Anyway, any bugfixes and improvements have to be added to Drupal 8 first, so I think they're out of scope for this issue. The fix added in #1805690-12: file_get_file_references() is rather bogus has already been added to Drupal 8, see #1997716: file_get_file_references() doesn't load entities correctly.
That would leave only the trailing whitespace issue I guess, however
if I understand #1452100: Private file download returns access denied, when file attached to revision other than current correctly, the new code doesn't work for revisions, and can't work in general, see #1452100-16: Private file download returns access denied, when file attached to revision other than current.
It shouldn't matter for file_file_download() since that function calls file_get_file_references() with FIELD_LOAD_CURRENT (but also see
#992674-22: Private file download returns access denied, but if the old code worked correctly that would still be a regression.
Comment #35
fietserwinYeah, let's do the remarks of #26 and update the function documentation that it is a file field module function and thus only returns file field references (and image field as that "extends" file field) and nothing else. Also renaming is out of scope for D7, so we also don't do that.
Comment #36
ey commentedI've done the remarks of #26 and re-rolled the patch. Please review.
Comment #38
ey commentedComment #39
Paul B commentedRe-rolled it again for 7.56
Comment #40
Paul B commentedprevious patch had an extraneous path component
Comment #42
Paul B commentedReroll. I'm not sure if SA-CORE-2017-003 affects this, but the tests should determine that.
Comment #43
idebr commentedReroll against current 7.x
Comment #44
klausiThe entity field query in file_get_file_references() is unlimited and will bomb your site if you use a reference many times. Opened #3207669: file_get_file_references() can cause PHP out of memory errors for that small aspect.
Comment #45
avpaderno