You are here

function drush_feeds_clear in Feeds 7.2

Clears a Feeds importer.

Parameters

string $importer_id: The importer id to clean.

File

./feeds.drush.inc, line 544
Drush commands for Feeds module.

Code

function drush_feeds_clear($importer_id = NULL) {
  if (!strlen($importer_id)) {
    drush_set_error(dt("Please specify the importer to delete all imported items from. If the importer is attached to a content type, specify also the feed node with the option '--nid'."));
    return FALSE;
  }
  if (!($feed_nid = drush_get_option('nid'))) {
    $feed_nid = 0;
  }
  try {
    feeds_source($importer_id, $feed_nid)
      ->existing();
  } catch (FeedsNotExistingException $e) {
    $importer = feeds_importer_load($importer_id);
    if (!$importer) {
      drush_set_error(dt("The importer '@importer' does not exist.", array(
        '@importer' => $importer_id,
      )));
      return FALSE;
    }
    elseif (!$importer
      ->isEnabled()) {
      drush_set_error(dt("The importer '@importer' is not enabled. You can enable it with the command '@command'.", array(
        '@importer' => $importer_id,
        '@command' => 'drush feeds-enable ' . $importer_id,
      )));
      return FALSE;
    }
    if ($feed_nid == 0) {

      // Check if the importer is a standalone importer.
      if ($importer->config['content_type']) {
        drush_set_error(dt("The importer '@importer' is attached to a content type. Please specify the feed node to delete items from with the option '--nid'. To show a list of available feed nodes for this importer, use 'drush feeds-list-feeds @importer'.", array(
          '@importer' => $importer_id,
        )));
        return FALSE;
      }
    }
    elseif (!$importer->config['content_type']) {
      $message = dt("The importer '@importer' is not attached to a content type. Do you want to clear all items for this importer?", array(
        '@importer' => $importer_id,
      ));
      if (!drush_confirm($message)) {
        return drush_log(dt('Aborting.'), 'ok');
      }
      else {
        drush_set_option('nid', 0);

        // Avoid asking for confirmation twice.
        drush_set_option('feeds_clear_skip_confirm', 1);
        return drush_feeds_clear($importer_id);
      }
    }
    drush_set_error(dt("There is no feed node with ID @nid for importer '@importer'. To show a list of available feed nodes for this importer, use 'drush feeds-list-feeds @importer'.", array(
      '@importer' => $importer_id,
      '@nid' => $feed_nid,
    )));
    return FALSE;
  }

  // Only ask for confirmation if it wasn't already asked before. See above.
  if (!drush_get_option('feeds_clear_skip_confirm')) {
    if ($feed_nid) {
      $message = dt("All items imported with the importer '@importer' for the feed node @feed_nid will be deleted. Do you really want to continue?", array(
        '@importer' => $importer_id,
        '@feed_nid' => $feed_nid,
      ));
    }
    else {
      $message = dt("All items imported with the importer '@importer' will be deleted. Do you really want to continue?", array(
        '@importer' => $importer_id,
      ));
    }
    if (!drush_confirm($message)) {
      return drush_log(dt('Aborting.'), 'ok');
    }
  }
  $batch = array(
    'title' => dt('Clearing !importer', array(
      '!importer' => $importer_id,
    )),
    'operations' => array(
      array(
        'feeds_batch',
        array(
          'clear',
          $importer_id,
          $feed_nid,
        ),
      ),
    ),
  );
  batch_set($batch);
  drush_backend_batch_process();
}