You are here

function media_youtube_cron in Media: YouTube 6

Implementation of hook_cron().

File

./media_youtube.module, line 89
Embedded Video Field provider file for YouTube.com.

Code

function media_youtube_cron() {

  // Recheck the status of any unavailable videos.
  // See if we need to check video status.
  $status_update_frequency = media_youtube_variable_get('status_update_frequency');
  if ($status_update_frequency != MEDIA_YOUTUBE_STATUS_UPDATE_NONE) {

    // Include the _media_youtube_save_status_data() function.
    module_load_include('inc', 'media_youtube', 'includes/media_youtube.api');

    // Build our SQL for the query of metadata.
    $sql = "SELECT value, status, last_touched FROM {media_youtube_metadata}";

    // Only get items that haven't been updated in awhile.
    $where = array(
      "last_touched < %d",
    );
    $arguments = array(
      time() - media_youtube_variable_get('cron_time') * 60,
    );

    // If we only care about unavailable videos, then filter out the rest.
    if ($status_update_frequency == MEDIA_YOUTUBE_STATUS_UPDATE_FROM_UNAVAILABLE) {
      $where[] = "status = %d";
      $arguments[] = EMFIELD_STATUS_UNAVAILABLE;
    }

    // We'll either grab a range or all required items.
    if ($limit = media_youtube_variable_get('cron_limit')) {
      $results = db_query_range($sql . ' WHERE ' . implode(' AND ', $where) . ' ORDER BY last_touched ASC', $arguments, 0, $limit);
    }
    else {
      $results = db_query($sql . ' WHERE ' . implode(' AND ', $where) . ' ORDER BY last_touched ASC', $arguments);
    }
    while ($media = db_fetch_array($results)) {

      // See if the status has changed.
      $status = media_youtube_check_status($media['value']);
      if ($status != $media['status']) {
        $media['status'] = $status;

        // Write the new metadata.
        drupal_write_record('media_youtube_metadata', $media, 'value');

        // Now record each node that uses this video.
        $data = db_query("SELECT value, vid, field_name, delta FROM {media_youtube_node_data} WHERE value = '%s'", $media['value']);
        while ($item = db_fetch_array($data)) {
          $node = node_load(array(
            'vid' => $item['vid'],
          ));
          $node->{$item['field_name']}[$item['delta']]['status'] = $media['status'];
          node_save($node);
        }
      }
    }
  }
}