You are here

function instagram_feeds_queue in Instagram Feeds 7

Deletes old (not needed) Feeds URLs or create new needed Feeds URL (by cron).

Parameters

array $job: contains '#operation' and '#data' values;

4 string references to 'instagram_feeds_queue'
instagram_feeds_cron in ./instagram_feeds.module
Implements hook_cron().
instagram_feeds_cron_job_scheduler_info in ./instagram_feeds.module
Implements hook_cron_job_scheduler_info().
instagram_feeds_cron_queue_info in ./instagram_feeds.module
Implements hook_cron_queue_info().
instagram_feeds_import_tab_form_submit in includes/instagram_feeds.pages.inc
Submit handler for instagram_feeds_import_tab_form().

File

./instagram_feeds.module, line 494

Code

function instagram_feeds_queue($job, &$context = array()) {
  switch ($job['type']) {
    case 'delete_feeds_urls':
      $context['message'] = t('Removing unnecessary URLs');
      $data = $job['data'];
      node_delete_multiple($data);
      foreach ($data as $nid) {

        // Remove from schedule.
        $job = array(
          'type' => INSTAGRAM_FEEDS_FEED_ID,
          'id' => $nid,
        );
        JobScheduler::get('feeds_source_import')
          ->remove($job);
      }
      db_delete('feeds_source')
        ->condition('id', INSTAGRAM_FEEDS_FEED_ID)
        ->condition('feed_nid', $data, 'IN')
        ->execute();
      break;
    case 'delete_items':
      $context['message'] = t('Removing old Instagram Items');
      node_delete_multiple($job['data']);
      break;
    case 'delete_taxonomy_terms':
      $context['message'] = t('Remove unused users that are not in blacklist, and unused hash tags.');
      foreach ($job['data'] as $tid) {
        taxonomy_term_delete($tid);
      }
      break;
    case 'create_node':
      $data = $job['data'];
      if ($data->name) {
        $t_tokens = array(
          '@type' => t('Hash Tag'),
          '@name' => $data->name,
        );
      }
      else {
        $t_tokens = array(
          '@type' => t('Instagram User'),
          '@name' => $data->user_names,
        );
      }
      $existing = instagram_feeds_get_existing_feeds();
      if (!isset($existing[$data->source])) {
        $node = (object) array(
          'title' => 'Instagram URL / ' . ($data->name ? 'Tags / ' . $data->name : 'Users / ' . $data->user_names),
          'type' => INSTAGRAM_FEEDS_FEEDS_NODE_TYPE,
          'language' => LANGUAGE_NONE,
        );
        $context['message'] = t('Create URL for @type "@name"', $t_tokens);
        node_object_prepare($node);
        $node->uid = 1;
        $node->feeds['InstagramFeedsPluginsPager']['source'] = $data->source;
        node_save($node);
      }
      else {
        $context['message'] = t('URL for @type "@name" already created.', $t_tokens);
      }
      break;
    case 'import_attempt_images':
      if (variable_get('instagram_feeds_download_attempts', 0)) {
        $context['message'] = t('Attempt importing of missed images.');
        list($id, $feed_nid) = explode('__', $job['data']['id']);
        $source = feeds_source($id, $feed_nid);
        if (!lock_acquire("feeds_source_{$source->id}_{$source->feed_nid}", 60.0)) {
          throw new FeedsLockException(t('Cannot acquire lock for source @id / @feed_nid.', array(
            '@id' => $source->id,
            '@feed_nid' => $source->feed_nid,
          )));
        }
        try {
          $parser_result = new FeedsParserResult($job['data']['items']);
          $parser_result->link = $source->config['InstagramFeedsPluginsPager']['source'];

          // Process.
          $source->importer->processor
            ->process($source, $parser_result);
        } catch (Exception $e) {

          // Do nothing.
        }
        lock_release("feeds_source_{$source->id}_{$source->feed_nid}");
        unset($source);
        if (isset($e)) {
          throw $e;
        }
      }
      else {
        $context['message'] = t('Cleaning an array of missed images..');
      }
      break;
    case 'check_block_deltas':
      $context['message'] = t('Check whether there are the necessary blocks.');
      instagram_feeds_create_blocks($job['data']);
      break;
    default:
      if (function_exists($job['type'])) {
        call_user_func_array($job['type'], array(
          $job['data'],
          $context,
        ));
      }
      else {
        $args = array(
          '%function' => $job['type'],
        );
        $context['message'] = t('Can not perform the job %function.', $args);
        watchdog('instagram_feeds', 'Can not find a function %function to run job.', $args, WATCHDOG_NOTICE);
      }
      break;
  }
  if (drupal_is_cli() && isset($context['message']) && !empty($context['message'])) {
    echo 'Instagram Feeds: ', $context['message'], "\n";
  }
}