1 system.module 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.

File

core/modules/system/system.module, line 2879
Configuration system that lets administrators modify the workings of the site.

Code

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();
      }

      // Indicate 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, but only if the plugin
    // has never been installed.
    db_delete('system')
      ->condition($delete)
      ->condition('schema_version', -1)
      ->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();

  // If any module or theme was moved to a new location, we need to reset the
  // system_list() cache or we will continue to load the old copy, look for
  // schema updates in the wrong place, etc.
  // We avoid system_list_reset() since this resets the static storage in
  // system_rebuild_module_data(), which itself calls this function.
  backdrop_static_reset('system_list');
  backdrop_static_reset('list_themes');
  cache('bootstrap')->deleteMultiple(array('bootstrap_modules', 'system_list'));
}