function drush_feeds_import in Feeds 7.2
Imports a given importer/source.
Parameters
string $importer_id: (optional) The importer id to filter on.
File
- ./
feeds.drush.inc, line 295 - Drush commands for Feeds module.
Code
function drush_feeds_import($importer_id = NULL) {
if (!strlen($importer_id)) {
drush_set_error(dt("Please specify the importer to import items with. 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 {
$source = 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 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 (drush_get_option('file') || drush_get_option('url') || drush_get_option('stdin')) {
// Create a new source.
$source = feeds_source($importer_id);
$source
->save();
}
}
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);
}
}
if (empty($source)) {
// Only abort at this point when no source object exists. It can exist
// when the importer is not attached to a content type and a file, url or
// stdin is supplied.
if ($feed_nid == 0) {
drush_set_error(dt("No source exists yet for the importer '@importer'. There is nothing to import. You can import from a file or url using the options '--file' or '--url' or go to /import/@importer to configure the source to import. See 'drush help feeds-import' for more information.", array(
'@importer' => $importer_id,
)));
return FALSE;
}
else {
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')) {
$filepath = _drush_feeds_find_file($filename);
if (!is_file($filepath)) {
drush_set_error(dt("The file '@file' does not exist.", array(
'@file' => $filename,
)));
return FALSE;
}
else {
$filepath = realpath($filepath);
}
$result = new FeedsFileFetcherResult($filepath);
$messages[] = dt("The items will be imported from the file '@file'.", array(
'@file' => $filepath,
));
}
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.');
}
// 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');
}
}
// Start the import!
if ($result) {
try {
$source
->pushImport($result);
} catch (Exception $e) {
drush_set_error($e
->getMessage());
return FALSE;
}
}
else {
_drush_feeds_create_import_batch($importer_id, $feed_nid);
drush_backend_batch_process();
return;
}
}