Implementations of hook_file_references is wrong - returns undefined variable

Also docs for hook_file_references are wrongly relates to {upload} table and

Comments

rfay’s picture

+++ modules/user/user.module	19 Mar 2010 19:25:16 -0000
@@ -811,7 +811,7 @@ function user_file_references($file) {
   $file_used = (bool) db_query_range('SELECT 1 FROM {users} WHERE picture = :fid', 0, 1, array(':fid' => $file->fid))->fetchField();
   if ($file_used) {
     // Return the name of the module and how many references it has to the file.
-    return array('user' => $count);
+    return array('user' => 1);

Wouldn't a query like db_result('select count(*) from {users} where...')); be better, and then actually return the result? Isn't the hook supposed to return the actual number of references?

I know that in core, it's always going to be one, given current assumptions. But there's no reason this shouldn't be a more "sane" query.

150 critical left. Go review some!

andypost’s picture

StatusFileSize
new1.99 KB

patch to return actual count

dries’s picture

Status: Needs review » Reviewed & tested by the community

This looks correct to me. Marking RTBC so I can commit it later.

dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed to CVS HEAD.

catch’s picture

Priority: Normal » Critical
Status: Fixed » Active

This patch was an almost direct revert of #196862: COUNT(*) is an expensive query in InnoDB., please re-revert.

rfay’s picture

Sorry, @andypost :-(

@catch, shouldn't we change the signature of hook_file_references to return a bool, then? We aren't going to want to ever return a count are we? In PHP, this is really just a change to the documentation, I think, but we should do that. The hook should return TRUE if there are existing files.

eojthebrave’s picture

err. cross posted with above comment ...

What is the appropriate way to do this with respect to InnoDB? Store a variable with the count? Select all rows and count them in PHP?

hook_file_references() says that you should return a count of the total number of uses of the file.

As @rfay points out in #1, given current assumptions in core just returning 1 or 0 will work but does that potentially break things for contrib?

catch’s picture

That makes sense to me. I can't see a use-case for returning a count here.

andypost’s picture

@catch The use case is when some contrib module is sharing one image per set of users. Suppose it's possible to have one avatar per group of users.

andypost’s picture

Status: Active » Needs review
StatusFileSize
new1.96 KB

Suppose in this case 1 is enough to indicate that file still used for some picture.

EDIT: There's still TODO in http://api.drupal.org/api/function/template_preprocess_user_picture/7

catch’s picture

Status: Needs review » Needs work

// If file still user then 1 is enough to indicate.
This should be // If file is still used then 1 is enough to indicate this.
Otherwise RTBC.

andypost’s picture

Status: Needs work » Needs review
StatusFileSize
new1.97 KB

Patch with proposed comment

catch’s picture

Status: Needs review » Reviewed & tested by the community

This looks good. We should open a separate issue to change hook_file_references() documentation. Although there's also #353458: hook_file_references() was not designed for a highly flexible field storage.

rfay’s picture

I think we'll be accidentally rewriting this function forever if we don't change the signature of hook_file_references(). @catch, you're the keeper of this. Should hook_file_references() return a bool?

catch’s picture

I think it should, but I have zero knowledge of what that field storage issue is proposing and how that'd interact with the changes here.

dries’s picture

Status: Reviewed & tested by the community » Fixed

Sorry about that! I committed the updated patch to HEAD. Thanks.

andypost’s picture

Still nothing was commited?! http://drupal.org/project/cvs/3060

andypost’s picture

Status: Fixed » Reviewed & tested by the community
StatusFileSize
new1.97 KB

Re-roll last patch, back to RTBC because it was not commited

andypost’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)
Issue tags: -Quick fix

Automatically closed -- issue fixed for 2 weeks with no activity.