Skip to main content
added 1197 characters in body
Source Link
artfulrobot
  • 4.1k
  • 7
  • 38
  • 74

To add a file_lock usage record for all existing files without one:

INSERT INTO file_usage (fid, module, `type`, id, `count`)
SELECT fid, 'file_lock' module, 'module' `type`, 0 id, 1 `count`
FROM file_managed 
WHERE status AND NOT EXISTS (
  SELECT id FROM file_usage
  WHERE file_managed.fid = file_usage.fid
        AND `type`='module'
        AND id=0
        AND `module` = 'file_lock'
        AND `count`>0
);

To add a file_lock record for all files in a directory that are NOT registered in the file_managed table

This would require looping theHere I loop through all likely image files in the directory, checking theand write them as file_managedpublic:// table for a reference (allowing for translation from stream wrappers - example), and then creating a file object, saving it to the database and then adding the file lock, e.g. with file_lock_add_lock($file);file_managed records if none exists.

  • preg_ls is not PHP/Drupal core - nicked it from PHP comment - v useful!

  • I exclude the styles/ directory which stores the variants made by different image styles. I also exclude the images/backup/ directory - this is particular to my needs, but I left the code in as you'll likely have something to exclude too.

  • This assumes you have no private:// files and that everything under sites/default/files is to be a public:// file. do not run this as it is if you use private files at all!

  • It also assumes that on file_save, file_lock will add its own lock (based on your config for file_lock).

(If I do this I'll post And now the code here.):

$dir = DRUPAL_ROOT . "/sites/default/files/";
foreach (preg_ls($dir, true, '/\.(jpe?g|gif|png|bmp)$/i') as $_) {

  $_ = substr($_, strlen($dir));
  if (preg_match('@^(styles/|images/backup/)@', $_)) {
    continue;
  }

  $uri = "public://$_";
  if (!db_query("SELECT fid FROM {file_managed} WHERE uri = :uri",[':uri' => $uri])->rowCount()) {
    $file = new stdClass();
    $file->fid = NULL;
    $file->uri = $uri;
    $file->filename = drupal_basename($uri);
    $file->filemime = file_get_mimetype($file->uri);
    $file->uid = $user->uid;
    $file->status = FILE_STATUS_PERMANENT;
    file_save($file);
  }
}

To add a file_lock usage record for all existing files without one:

INSERT INTO file_usage (fid, module, `type`, id, `count`)
SELECT fid, 'file_lock' module, 'module' `type`, 0 id, 1 `count`
FROM file_managed 
WHERE status AND NOT EXISTS (
  SELECT id FROM file_usage
  WHERE file_managed.fid = file_usage.fid
        AND `type`='module'
        AND id=0
        AND `module` = 'file_lock'
        AND `count`>0
);

To add a file_lock record for all files in a directory that are NOT registered in the file_managed table

This would require looping the files in the directory, checking the file_managed table for a reference (allowing for translation from stream wrappers - example), and then creating a file object, saving it to the database and then adding the file lock, e.g. with file_lock_add_lock($file);.

(If I do this I'll post the code here.)

To add a file_lock usage record for all existing files without one:

INSERT INTO file_usage (fid, module, `type`, id, `count`)
SELECT fid, 'file_lock' module, 'module' `type`, 0 id, 1 `count`
FROM file_managed 
WHERE status AND NOT EXISTS (
  SELECT id FROM file_usage
  WHERE file_managed.fid = file_usage.fid
        AND `type`='module'
        AND id=0
        AND `module` = 'file_lock'
        AND `count`>0
);

To add a file_lock record for all files in a directory that are NOT registered in the file_managed table

Here I loop through all likely image files in and write them as public:// file_managed records if none exists.

  • preg_ls is not PHP/Drupal core - nicked it from PHP comment - v useful!

  • I exclude the styles/ directory which stores the variants made by different image styles. I also exclude the images/backup/ directory - this is particular to my needs, but I left the code in as you'll likely have something to exclude too.

  • This assumes you have no private:// files and that everything under sites/default/files is to be a public:// file. do not run this as it is if you use private files at all!

  • It also assumes that on file_save, file_lock will add its own lock (based on your config for file_lock).

And now the code:

$dir = DRUPAL_ROOT . "/sites/default/files/";
foreach (preg_ls($dir, true, '/\.(jpe?g|gif|png|bmp)$/i') as $_) {

  $_ = substr($_, strlen($dir));
  if (preg_match('@^(styles/|images/backup/)@', $_)) {
    continue;
  }

  $uri = "public://$_";
  if (!db_query("SELECT fid FROM {file_managed} WHERE uri = :uri",[':uri' => $uri])->rowCount()) {
    $file = new stdClass();
    $file->fid = NULL;
    $file->uri = $uri;
    $file->filename = drupal_basename($uri);
    $file->filemime = file_get_mimetype($file->uri);
    $file->uid = $user->uid;
    $file->status = FILE_STATUS_PERMANENT;
    file_save($file);
  }
}
Source Link
artfulrobot
  • 4.1k
  • 7
  • 38
  • 74

To add a file_lock usage record for all existing files without one:

INSERT INTO file_usage (fid, module, `type`, id, `count`)
SELECT fid, 'file_lock' module, 'module' `type`, 0 id, 1 `count`
FROM file_managed 
WHERE status AND NOT EXISTS (
  SELECT id FROM file_usage
  WHERE file_managed.fid = file_usage.fid
        AND `type`='module'
        AND id=0
        AND `module` = 'file_lock'
        AND `count`>0
);

To add a file_lock record for all files in a directory that are NOT registered in the file_managed table

This would require looping the files in the directory, checking the file_managed table for a reference (allowing for translation from stream wrappers - example), and then creating a file object, saving it to the database and then adding the file lock, e.g. with file_lock_add_lock($file);.

(If I do this I'll post the code here.)