You are here

function imageinfo_cache_shutdown_async in Imageinfo Cache 6.2

Same name and namespace in other branches
  1. 6 imageinfo_cache.module \imageinfo_cache_shutdown_async()

Function that gets called right after a file has been uploaded.

Parameters

$file: object File info

$op: string insert or delete

3 calls to imageinfo_cache_shutdown_async()
imageinfo_cache_file_delete in ./imageinfo_cache.module
Implements hook_file_delete().
imageinfo_cache_file_insert in ./imageinfo_cache.module
Implements hook_file_insert().
imageinfo_cache_nodeapi in ./imageinfo_cache.module
Implements hook_nodeapi().

File

./imageinfo_cache.module, line 152
Cache image info for theme_imagecache & theme_imagefield_image.

Code

function imageinfo_cache_shutdown_async($file = NULL, $op = NULL, $state = NULL) {
  global $base_path, $base_root;
  static $files = array();
  static $registered = FALSE;
  static $array_counter = 0;
  static $file_counter = 0;

  // Record file and op in static array & register shutdown function if needed.
  if (!empty($file) && !empty($op)) {

    // Make sure file has the correct info and it is an image.
    if (imageinfo_cache_check_file($file, $op) == FALSE) {
      return;
    }

    // Only send if file is missing from any one of the caches
    $missing = FALSE;
    $cid = 'theme_imagefield_' . md5($file->filepath);
    $cache = cache_get($cid, 'cache_imageinfo');
    if (empty($cache)) {
      $missing = TRUE;
    }

    // Build file info array.
    $file_info = array(
      'fid' => $file->fid,
      'filepath' => $file->filepath,
      'timestamp' => $file->timestamp,
      'op' => $op,
      'state' => $state,
    );
    if (!empty($file->field['type_name'])) {
      $file_info['field']['type_name'] = $file->field['type_name'];
    }
    if (!empty($file->field['field_name'])) {
      $file_info['field']['field_name'] = $file->field['field_name'];
    }

    // Do imagecache presets.
    if (!$missing && module_exists('imagecache') && function_exists('imagecache_generate_image') && variable_get('imageinfo_cache_imagecache_pregenerate', IMAGEINFO_CACHE_IMAGECACHE_PREGENERATE)) {

      // Check each imagecache preset
      foreach (imageinfo_cache_presets_to_generate($file_info) as $preset) {

        // see if a cache entry exists for that preset.
        $cid = 'imagecache_' . $preset . '_' . md5($file->filepath);
        $cache = cache_get($cid, 'cache_imageinfo');
        if (empty($cache)) {
          $missing = TRUE;
          break;
        }
      }
    }

    // All data for this image is cached for insert.
    // None of the data for this image is cached for delete.
    if (!$missing && $op == 'insert') {
      return;
    }

    // Only send 5 files at a time.
    $file_counter++;
    if ($file_counter > (int) variable_get('imageinfo_cache_async_max', IMAGEINFO_CACHE_ASYNC_MAX)) {
      $array_counter++;
      $file_counter = 0;
    }

    // Add info to static array.
    $files[$array_counter][$file->fid] = $file_info;
    if (!$registered) {
      register_shutdown_function(__FUNCTION__);
      $registered = TRUE;
    }
    return;
  }

  // Code below runs on shutdown.
  // Exit function if we have nothing to do.
  if (empty($files)) {
    return;
  }

  // URL key.
  $key = variable_get('imageinfo_cache_url_key', md5(drupal_get_private_key()));
  foreach ($files as $values) {
    $query['files'] = $values;
    $query['key'] = $key;

    // Setup request URL and headers.
    $query_string = http_build_query($query, '', '&');
    $ip = variable_get('imageinfo_cache_server_addr', FALSE);
    if (!empty($ip)) {
      $url = 'http://' . $ip . $base_path . 'imageinfo_cache_generate';
    }
    else {
      $url = httprl_build_url_self('imageinfo_cache_generate');
    }
    $headers = array(
      'Host' => $_SERVER['HTTP_HOST'],
      'Content-Type' => 'application/x-www-form-urlencoded',
    );
    $options = array(
      'headers' => $headers,
      'method' => 'POST',
      'data' => $query_string,
      'blocking' => variable_get('imageinfo_cache_httprl_mode', IMAGEINFO_CACHE_HTTPRL_MODE),
    );

    // Create imagecache presets generation requests.
    httprl_request($url, $options);
  }

  // Execute requests in parallel.
  $resposes = httprl_send_request();

  // If blocking then check the respose back
  if (variable_get('imageinfo_cache_httprl_mode', IMAGEINFO_CACHE_HTTPRL_MODE)) {
    foreach ($resposes as $url => $info) {

      // If async failed; block here and generate images and caches.
      if (strcmp(trim($info->data), $key) !== 0) {
        watchdog('imageinfo_cache', 'Asynchronous imageinfo cache primer failed %url. Using Synchronous mode. %key <br /> !data', array(
          '%url' => $url,
          '%key' => $key . ' ' . $info->data . ' ' . strcmp(trim($info->data), $key),
          '!data' => print_r($info, TRUE),
        ));
        imageinfo_cache_primer($query);
      }
    }
  }
}