system_update_files_database
- Versions
- 7
system_update_files_database(&$files, $type)
Updates the records in the system table based on the files array.
Parameters
$files An array of files.
$type The type of the files.
Code
modules/system/system.module, line 2032
<?php
function system_update_files_database(&$files, $type) {
$result = db_query("SELECT * FROM {system} WHERE type = :type", array(':type' => $type));
// Add all files that need to be deleted to a DatabaseCondition.
$delete = db_or();
foreach ($result as $file) {
if (isset($files[$file->name]) && is_object($files[$file->name])) {
// Keep the old filename from the database in case the file has moved.
$old_filename = $file->filename;
$updated_fields = array();
// Handle info specially, compare the serialized value.
$serialized_info = serialize($files[$file->name]->info);
if ($serialized_info != $file->info) {
$updated_fields['info'] = $serialized_info;
}
unset($file->info);
// Scan remaining fields to find only the updated values.
foreach ($file as $key => $value) {
if (isset($files[$file->name]->$key) && $files[$file->name]->$key != $value) {
$updated_fields[$key] = $files[$file->name]->$key;
}
}
// Update the record.
if (count($updated_fields)) {
db_update('system')
->fields($updated_fields)
->condition('filename', $old_filename)
->execute();
}
// Indiciate that the file exists already.
$files[$file->name]->exists = TRUE;
}
else {
// File is not found in file system, so delete record from the system table.
$delete->condition('filename', $file->filename);
}
}
if (count($delete) > 0) {
// Delete all missing files from the system table
db_delete('system')
->condition($delete)
->execute();
}
// All remaining files are not in the system table, so we need to add them.
$query = db_insert('system')->fields(array('filename', 'name', 'type', 'owner', 'info'));
foreach($files as &$file) {
if (isset($file->exists)) {
unset($file->exists);
}
else {
$query->values(array(
'filename' => $file->uri,
'name' => $file->name,
'type' => $type,
'owner' => isset($file->owner) ? $file->owner : '',
'info' => serialize($file->info),
));
$file->type = $type;
$file->status = 0;
$file->schema_version = -1;
}
}
$query->execute();
}
?>Login or register to post comments 