diff --git a/feeds.drush.inc b/feeds.drush.inc index 0ccb19e..7da3655 100644 --- a/feeds.drush.inc +++ b/feeds.drush.inc @@ -243,35 +243,107 @@ function drush_feeds_import($importer_id = NULL) { $source = feeds_source($importer_id, $feed_nid)->existing(); } catch (FeedsNotExistingException $e) { - drush_set_error(dt('The feed @importer: @nid does not exist.', array('@importer' => $importer, '@nid' => $feed_nid))); + $importer = feeds_importer_load($importer_id); + if (!$importer) { + drush_set_error(dt("The importer '@importer' does not exist.", array( + '@importer' => $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 import items for 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 import 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_import_skip_confirm', 1); + return drush_feeds_import($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; } + // Propose confirmation message. + $messages = array(); + $vars = array( + '@importer' => $importer_id, + '@feed_nid' => $feed_nid, + ); + if ($feed_nid) { + $messages[] = dt("Items will be imported with the importer '@importer' for the feed node @feed_nid.", $vars); + } + else { + $messages[] = dt("Items will be imported with the importer '@importer'.", $vars); + } + + $result = NULL; if ($filename = drush_get_option('file')) { if (!is_file($filename)) { - drush_set_error(dt('The file @file does not exist.', array('@file' => $filename))); + drush_set_error(dt("The file '@file' does not exist.", array('@file' => $filename))); return FALSE; } $result = new FeedsFileFetcherResult($filename); + + $messages[] = dt("The items will be imported from the file '@file'.", array( + '@file' => $filename, + )); } elseif ($url = drush_get_option('url')) { $result = new FeedsHTTPFetcherResult($url); + + $messages[] = dt("The items will be imported from the url '@url'.", array( + '@url' => $url, + )); } elseif (drush_get_option('stdin')) { $result = new FeedsFetcherResult(file_get_contents('php://stdin')); + + $messages[] = dt('The items will be imported from stdin.'); } - else { - _drush_feeds_create_import_batch($importer, $feed_nid); - drush_backend_batch_process(); - return; + + // Only ask for confirmation if it wasn't already asked before. See above. + if (!drush_get_option('feeds_import_skip_confirm')) { + $messages[] = dt('Do you really want to continue?'); + $message = implode(' ', $messages); + if (!drush_confirm($message)) { + return drush_log(dt('Aborting.'), 'ok'); + } } - try { - $source->pushImport($result); + // Start the import! + if ($result) { + try { + $source->pushImport($result); + } + catch (Exception $e) { + drush_set_error($e->getMessage()); + return FALSE; + } } - catch (Exception $e) { - drush_print($e->getMessage()); - return FALSE; + else { + _drush_feeds_create_import_batch($importer_id, $feed_nid); + drush_backend_batch_process(); + return; } } @@ -356,10 +428,64 @@ function drush_feeds_clear($importer_id = NULL) { feeds_source($importer_id, $feed_nid)->existing(); } catch (FeedsNotExistingException $e) { - drush_set_error(dt('The feed @importer: @nid does not exist.', array('@importer' => $importer, '@nid' => $feed_nid))); + $importer = feeds_importer_load($importer_id); + if (!$importer) { + drush_set_error(dt("The importer '@importer' does not exist.", array( + '@importer' => $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( @@ -392,6 +518,9 @@ function drush_feeds_enable() { if ($to_enable) { drush_print(dt('The following importers will be enabled: !importers', array('!importers' => implode(', ', $to_enable)))); } + elseif (count(func_get_args()) == 0) { + return drush_set_error(dt('Please specify a space delimited list of importers to enable.')); + } else { return drush_print(dt('There are no importers to enable.')); } @@ -431,6 +560,9 @@ function drush_feeds_disable() { if ($to_disable) { drush_print(dt('The following importers will be disabled: !importers', array('!importers' => implode(', ', $to_disable)))); } + elseif (count(func_get_args()) == 0) { + return drush_set_error(dt('Please specify a space delimited list of importers to disable.')); + } else { return drush_print(dt('There are no importers to disable.')); } @@ -455,14 +587,30 @@ function drush_feeds_delete() { $all = feeds_importer_load_all(TRUE); $to_delete = array_intersect_key($all, array_flip(func_get_args())); $missing = array_diff(func_get_args(), array_keys($all)); + $cant_delete = array(); + + // Filter out default importers that are not overridden. + foreach ($to_delete as $delta => $importer) { + if ($importer->export_type === EXPORT_IN_CODE) { + unset($to_delete[$delta]); + $cant_delete[$importer->id] = $importer->id; + } + } if ($missing) { drush_print(dt('The following importers are missing: !importers', array('!importers' => implode(', ', $missing)))); } + if ($cant_delete) { + drush_print(dt('The following importers cannot be deleted because they only exist in code: !importers', array('!importers' => implode(', ', array_keys($cant_delete))))); + } + if ($to_delete) { drush_print(dt('The following importers will be deleted: !importers', array('!importers' => implode(', ', array_keys($to_delete))))); } + elseif (count(func_get_args()) == 0) { + return drush_set_error(dt('Please specify a space delimited list of importers to delete.')); + } else { return drush_print(dt('There are no importers to delete.')); } @@ -487,12 +635,18 @@ function drush_feeds_revert() { $missing = array_diff(func_get_args(), array_keys($all)); $to_revert = array_intersect_key($all, array_flip(func_get_args())); $cant_revert = array(); + $cant_revert_db = array(); // Filter out non-overridden importers. foreach ($to_revert as $delta => $importer) { if ($importer->export_type !== (EXPORT_IN_CODE | EXPORT_IN_DATABASE)) { unset($to_revert[$delta]); - $cant_revert[$importer->id] = $importer->id; + if ($importer->export_type == EXPORT_IN_DATABASE) { + $cant_revert_db[$importer->id] = $importer->id; + } + else { + $cant_revert[$importer->id] = $importer->id; + } } } @@ -501,12 +655,18 @@ function drush_feeds_revert() { } if ($cant_revert) { - drush_print(dt('The following importers cannot be reverted: !importers', array('!importers' => implode(', ', array_keys($cant_revert))))); + drush_print(dt('The following importers cannot be reverted because they are not overridden: !importers', array('!importers' => implode(', ', array_keys($cant_revert))))); + } + if ($cant_revert_db) { + drush_print(dt('The following importers cannot be reverted because they only exist in the database: !importers', array('!importers' => implode(', ', array_keys($cant_revert_db))))); } if ($to_revert) { drush_print(dt('The following importers will be reverted: !importers', array('!importers' => implode(', ', array_keys($to_revert))))); } + elseif (count(func_get_args()) == 0) { + return drush_set_error(dt('Please specify a space delimited list of importers to revert.')); + } else { return drush_print(dt('There are no importers to revert.')); }