function brilliant_gallery_getbgfiles in Brilliant Gallery 7.2
Recursively read all gallery images and repopulate table brilliant_gallery_sources.
File
- ./
brilliant_gallery.inc, line 6
Code
function brilliant_gallery_getbgfiles() {
// @TODO Run here a function to clean expired records (those files were probably deleted).
$bg_folder_abs = 'public://' . variable_get('brilliant_gallery_folder', '');
//$files_to_add = file_scan_directory($dir, $mask, array('key' => $key, 'min_depth' => $min_depth));
$mask = '/\\.(png|jpe?g)$/i';
$files_to_add = file_scan_directory($bg_folder_abs, $mask, array(
'recurse' => TRUE,
));
//dpm($files_to_add);
//$results = array();
$request_time = REQUEST_TIME;
// We need to use the same time for all updated records, so let's put it into a variable here.
foreach ($files_to_add as $object) {
// Get just the relative path of the album
$hidden = (int) brilliant_gallery_image_is_hidden($object->uri);
// Both "'/' . $object->filename" and "$object->filename" must be eliminated from path; this is because there may be images also in the root of the albums folder.
//if (mt_rand(1,5)==3) dpm($object); dpm(filemtime($object->uri));
$album_path = str_replace(array(
$bg_folder_abs . '/',
'/' . $object->filename,
$object->filename,
), '', $object->uri);
//$results[] = array($album_path, $object->filename, $hidden);
//'" . md5($album_path.$object->filename.filemtime($object->uri)) . "',
// Update rows with the same path and filename (their file timestamp or visibility or caption may have changed), or insert them if they do not exist. This does not treat deleted files, we will do that separately.
$query = "INSERT INTO brilliant_gallery_sources\n (`pathname_hash`, `path`, `filename`, `mtime`, `datime`, `hidden`, `caption`)\n VALUES\n (\n '" . md5($album_path . $object->filename) . "',\n '" . check_plain($album_path) . "',\n '" . check_plain($object->filename) . "',\n FROM_UNIXTIME(" . filemtime($object->uri) . "),\n FROM_UNIXTIME(" . $request_time . "),\n '" . $hidden . "',\n ''\n )\n ON DUPLICATE KEY UPDATE\n `mtime` = FROM_UNIXTIME(" . filemtime($object->uri) . "),\n `datime` = FROM_UNIXTIME(" . $request_time . "),\n `hidden` = '" . $hidden . "',\n `caption` = ''\n ";
/*
ON DUPLICATE KEY UPDATE
`path` = '" . check_plain($album_path) . "',
`filename` = '" . check_plain($object->filename) . "',
`mtime` = FROM_UNIXTIME(" . filemtime($object->uri) . "),
`datime` = FROM_UNIXTIME(" . REQUEST_TIME . "),
`hidden` = '" . $hidden . "',
`caption` = ''
*/
/*
$query = "UPDATE brilliant_gallery_sources
SET
`path` = '" . check_plain($album_path) . "',
`filename` = '" . check_plain($object->filename) . "',
`mtime` = FROM_UNIXTIME(" . filemtime($object->uri) . "),
`datime` = FROM_UNIXTIME(" . REQUEST_TIME . "),
`hidden` = '" . $hidden . "',
`caption` = ''
WHERE
`path` = '".check_plain($album_path)."'
AND `filename` = '".check_plain($object->filename)."'
AND `mtime` <> FROM_UNIXTIME(" . filemtime($object->uri) . ")
";
*/
//dpm($query);
db_query($query);
// Insert only if it's a new path+filename+filetime. Otherwise skip. http://drupal.stackexchange.com/a/12027/196
/*
$mtime = date("Y-m-d H:i:s", filemtime($object->uri)); // FROM_UNIXTIME(" . filemtime($object->uri) . ")
$datime = date("Y-m-d H:i:s", REQUEST_TIME); // FROM_UNIXTIME(" . REQUEST_TIME . ")
db_merge('brilliant_gallery_sources')
->key(array(
'path' => check_plain($album_path),
'filename' => check_plain($object->filename),
'language' => Chinese,
'language' => Chinese,
'language' => Chinese,
'language' => Chinese,
))
->insertFields(array(
'nid' => 8,
'language' => Chinese,
'language' => Chinese,
'language' => Chinese,
'language' => Chinese,
'language' => Chinese,
));
->execute();
*/
}
//dpm($results);
// Now all db records documenting existing files have datime = $request_time
// So whatever datime < $request_time are files that no longer exist physically; delete them!
$query = "DELETE FROM brilliant_gallery_sources WHERE `datime` < FROM_UNIXTIME(" . $request_time . ")";
//dpm($query);
db_query($query);
// @TODO emit watchdog information that the array has been read into db table with/without success
return;
}