Working on #322287: Add file.module to provide UI for managing files it because apparent that it'd be really useful to see the actual context where modules are using a file. Knowing that imagefield is using a file is helpful but it's much more helpful to know that it's being used on node/1025.

Sometimes having path is overkill, e.g. during deletion you really just want a "yes, someone is using this file" / "no, it's unused" answer, so it would seem beneficial to have two modes: boolean and menu paths.

This would complicate the calling of the hook so it seems that it would be helpful to add a new file_usage() function that would be responsible for calling hook_file_references(). The upside is that we could fix one annoyance in hook_file_references(). Currently the module implementing hook_file_references() is responsible for keying it's references with the module name. If there's a function for checking usage then it can replicate some of the module_invoke_all code and key the results by module leaving the modules to just return their usage as either a number or array of links.

I'm thinking of doing something along the lines of this--totally untested--bit of code:

/**
 * Determine if a file is in use.
 *
 * @param $file
 *   A file object.
 * @param $mode
 *   The type of value to return. Allowed values are: 'boolean', 'path'.
 * @return
 *   Mixed depending on the value of $mode.
 *     - 'boolean' returns a simple yes/no answer.
 *     - 'path' returns an array of paths to instances where the file is used.
 */
function file_usage($file, $mode = 'path') {
  $file = (object)$file;

  $return = array();
  foreach (module_implements('file_references') as $name) {
    $function = $name . '_' . $hook;
    $result = $function($file, $mode);
    if (isset($result)) {
      $return[$name] = $result;
    }
  }
  return $return;
}

Code that just wants the yes/no like file_delete() would call file_usage($file, 'boolean');

CommentFileSizeAuthor
#4 file_329311.patch21.94 KBdrewish
#3 file_329311.patch17.11 KBdrewish
#1 file_329311.patch2.81 KBdrewish

Comments

drewish’s picture

Status: Active » Needs work
StatusFileSize
new2.81 KB

the more i mess with it the less i'm convinced that there should be two modes. file_delete() is the only case that would just need a yes/no answer but it turns around and passes the results back to the caller if the file is in use... delete's happen infrequently so if we spend a little longer figuring out where the files are used that's not a huge burden.

the attached returns an array of arrays like:

array(
  'upload' => array(
    array('title of the page', 'node/1'),
    array('different one', 'node/99'),
  ),
);

this gives you something to display for a list of links to usages.

drewish’s picture

after talking with earl i think that a better approach is forcing modules to store their usage in a table. that way views can query it and we can easily delete multiple files without having to continually call the hook.

drewish’s picture

StatusFileSize
new17.11 KB

i should probably just close this issue and open a new one but since it's here i'll post this work in progress to.

drewish’s picture

StatusFileSize
new21.94 KB

now with unit tests.

drewish’s picture

Status: Needs work » Closed (won't fix)