You are here

function _s3fs_write_metadata in S3 File System 7.2

Same name and namespace in other branches
  1. 7.3 s3fs.module \_s3fs_write_metadata()
  2. 7 s3fs.module \_s3fs_write_metadata()

Writes metadata to the temp table in the database.

Parameters

array $file_metadata_list: An array passed by reference, which contains the current page of file metadata. This function empties out $file_metadata_list at the end.

array $folders: An associative array keyed by folder name, which is populated with the ancestor folders of each file in $file_metadata_list. Also emptied.

1 call to _s3fs_write_metadata()
_s3fs_refresh_cache in ./s3fs.module
Refreshes the metadata cache.

File

./s3fs.module, line 601
Hook implementations and other primary functionality for S3 File System.

Code

function _s3fs_write_metadata(&$file_metadata_list, &$folders) {
  if (!empty($file_metadata_list)) {
    $insert_query = db_insert('s3fs_file_temp')
      ->fields(array(
      'uri',
      'filesize',
      'timestamp',
      'dir',
      'version',
    ));
    foreach ($file_metadata_list as $key => $metadata) {
      $uri = drupal_dirname($metadata['uri']);

      // Write the file metadata to the DB.
      if (!$metadata['dir']) {
        $insert_query
          ->values($metadata);
      }
      else {
        $folders[$uri] = $metadata;

        // Remove for counting purposes.
        unset($file_metadata_list[$key]);
      }

      // Add the ancestor folders of this file to the $folders array.
      $root = file_uri_scheme($uri) . '://';

      // Loop through each ancestor folder until we get to the root uri.
      while ($uri != $root) {
        $folders[rtrim($uri, '/')] = TRUE;
        $uri = drupal_dirname($uri);
      }
    }
    $insert_query
      ->execute();
    if (function_exists('drush_log')) {
      drush_log('  ' . dt('Wrote @files file(s).', array(
        '@files' => count($file_metadata_list),
      )));
    }

    // Empty out the file array, so it can be re-filled by the next request.
    $file_metadata_list = array();
  }
  if (!empty($folders)) {
    $insert_query = db_insert('s3fs_folder_temp')
      ->fields(array(
      'uri',
      'filesize',
      'timestamp',
      'dir',
      'version',
    ));
    foreach ($folders as $folder_name => $folder_data) {
      if (is_bool($folder_data)) {
        $insert_query
          ->values(_s3fs_convert_metadata($folder_name, array()));
      }
      else {
        $insert_query
          ->values($folder_data);
      }
    }
    $insert_query
      ->execute();
    if (function_exists('drush_log')) {
      drush_log('  ' . dt('Wrote @folders folder(s).', array(
        '@folders' => count($folders),
      )));
    }

    // Empty out folders as well; consolidation will take care of duplicates.
    $folders = array();
  }
}