diff --git a/feeds.drush.inc b/feeds.drush.inc new file mode 100644 index 0000000..4cadcbf --- /dev/null +++ b/feeds.drush.inc @@ -0,0 +1,327 @@ + "Displays all importers or displays the config of a given importer (passed as arg).", + 'examples' => array( + 'drush feeds-list', + ), + ); + + $items['feeds-import'] = array( + 'description' => "Import a feed.", + 'arguments' => array( + 'feeds-name' => 'The name of the feeds importer that will be refreshed. Mandatory.', + ), + 'options' => array( + 'nid' => 'The nid of the feeds importer that will be refreshed. Optional.', + 'file' => 'The file to import by the feed importer. Optional.', + 'http' => 'The source to import by the feed importer. Optional.', + ), + 'examples' => array( + 'drush feeds-import feed_name' => 'Import the feed feed_name', + 'drush feeds-import feed_name --nid=2' => 'Import feed_name associated with nid 2', + ), + ); + + $items['feeds-clear'] = array( + 'description' => "Clear a feed.", + 'arguments' => array( + 'feeds-names' => 'The name of the feeds importer that will be clear. Mandatory.', + ), + 'options' => array( + 'nid' => 'The nid of the feeds importer that will be clear. Optional.', + ), + 'examples' => array( + 'drush feeds-clear feed_name', + 'drush feeds-clear feed_name --nid=2', + ), + ); + + $items['feeds-enable'] = array( + 'description' => 'Enable feeds.', + 'arguments' => array( + 'feed-name' => 'A space delimited list of feeds importers. Mandatory.', + ), + 'examples' => array( + 'drush feeds-enable feed_name1 feed_name2', + ), + ); + + $items['feeds-disable'] = array( + 'description' => 'Disable feeds.', + 'arguments' => array( + 'feed-name' => 'A space delimited list of feeds importers. Mandatory.', + ), + 'examples' => array( + 'drush feeds-disable feed_name1 feed_name2', + ), + ); + + $items['feeds-delete'] = array( + 'description' => 'Delete feeds.', + 'arguments' => array( + 'feeds-names' => 'A space delimited list of feeds importers . Mandatory.', + ), + 'examples' => array( + 'drush feeds-delete feed_name1 feed_name2', + ), + ); + + $items['feeds-revert'] = array( + 'description' => 'Revert feeds.', + 'arguments' => array( + 'feed-name' => 'A space delimited list of feeds importers. Mandatory.', + ), + 'examples' => array( + 'drush feeds-revert feed_name1 feed_name2', + ), + ); + + return $items; +} + +/** + * Get a list of all feeds importers. + */ +function drush_feeds_list() { + $rows = array(array(dt('Name'), dt('Description'), dt('Attached to'), dt('Status'), dt('State'))); + + foreach (feeds_importer_load_all(TRUE) as $k => $i) { + if ($i->export_type == EXPORT_IN_CODE) { + $state = t('Default'); + } + else if ($i->export_type == EXPORT_IN_DATABASE) { + $state = t('Normal'); + } + else if ($i->export_type == (EXPORT_IN_CODE | EXPORT_IN_DATABASE)) { + $state = t('Overridden'); + } + + $rows[] = array( + ($i->config['name'] . '('. $i->id.')'), + $i->config['description'], + $i->config['content_type'] ? dt(node_get_types('name', $i->config['content_type'])) : dt('none'), + $i->disabled ? dt('Disabled') : dt('Enabled'), + $state + ); + } + drush_print_table($rows, TRUE); +} + +/** + * Import a given feed_name. + * The feed will be refreshed regardless of it's scheduled. + * + * @param string $feed_name + */ +function drush_feeds_import($feed_name) { + if (!isset($feed_name) || $feed_name == "") { + drush_print(dt('The importer feed_name is required.')); + return FALSE; + } + + if (!($feed_nid = drush_get_option('nid'))) { + $feed_nid = 0; + } + + $feedsSource = feeds_source($feed_name, $feed_nid); + $run_import = TRUE; + + // If a file is specified, set the feeds config to that file. + if ($filename = drush_get_option('file')) { + // Feeds public folder. + $feed_dir = variable_get('file_directory_path', 'sites/default/files') . '/feeds'; + file_create_path($feed_dir); + file_check_directory($feed_dir, FILE_MODIFY_PERMISSIONS | FILE_CREATE_DIRECTORY ); + $filename_path = $feed_dir . '/' . basename($filename); + if (!file_exists($filename_path)) { + drush_log(dt('Cannot import feed !feed: no such file: !file', array('!feed' => $feed_name, '!file' => $filename_path)), 'error'); + $run_import = FALSE; + } + else { + drush_log(dt('Importing !feed from !file', array('!feed' => $feed_name, '!file' => $filename_path)), 'notice'); + $config = $feedsSource->getConfig(); + $config['FeedsFileFetcher']['source'] = $filename_path; + $feedsSource->setConfig($config); + $feedsSource->save(); + } + } + + // If a file is specified, set the feeds config to that http source. + if ($filename = drush_get_option('http')) { + drush_log(dt('Importing !feed from !file', array('!feed' => $feed_name, '!file' => $filename)), 'notice'); + $config = $feedsSource->getConfig(); + $config['FeedsHTTPFetcher']['source'] = $filename; + $feedsSource->setConfig($config); + $feedsSource->save(); + } + + if ($run_import) { + // Set the batch operations. + $batch = array( + 'title' => dt('Importing !feed_name', array('!feed_name' => $feed_name)), + 'operations' => array( + array('feeds_batch', array('import', $feed_name, $feed_nid)), + ), + ); + batch_set($batch); + drush_backend_batch_process(); + } +} + +/** + * Clear a given feed_name. + * The feed will be clear regardless of it's scheduled. + * + * @param string $feed_name + */ +function drush_feeds_clear($feed_name) { + if (!isset($feed_name) || $feed_name == "") { + drush_print(dt('The importer feed_name is required.')); + return FALSE; + } + + if (!($feed_nid = drush_get_option('nid'))) { + $feed_nid = 0; + } + + $batch = array( + 'title' => dt('Clearing !feed_name', array('!feed_name' => $feed_name)), + 'operations' => array( + array('feeds_batch', array('clear', $feed_name, $feed_nid)), + ), + ); + batch_set($batch); + drush_backend_batch_process(); +} + +/** + * Delete a set of feeds. + * + * @param string $feed_name + */ +function drush_feeds_delete() { + $feed_names = func_get_args(); + $feed_importers = array_keys(feeds_importer_load_all(true)); + drush_print(dt('The following feeds will be deleted: !feed_names', array('!feed_names' => implode(', ', $feed_names)))); + if(!drush_confirm(dt('Do you really want to continue?'))) { + return drush_log(dt('Aborting.')); + } + foreach ($feed_names as $feed_name) { + if (in_array($feed_name, $feed_importers)) { + feeds_source($feed_name)->importer->delete(); + drush_log(dt('!feed_name was deleted successfully.', array('!feed_name' => $feed_name)), 'ok'); + } + else { + drush_log(dt('!feed_name wasn\'t found.', array('!feed_name' => $feed_name)), 'warning'); + } + } + + feeds_cache_clear(); +} + +/** + * Revert a set of feeds. + * Same workflow as deleting feeds, see @drush_feeds_delete. + * + * @param string $feed_name + */ +function drush_feeds_revert() { + $feed_names = func_get_args(); + $feed_importers = array_keys(feeds_importer_load_all(TRUE)); + drush_print(dt('The following feeds will be reverted: !feed_names', array('!feed_names' => implode(', ', $feed_names)))); + if(!drush_confirm(dt('Do you really want to continue?'))) { + return drush_log(dt('Aborting.')); + } + foreach ($feed_names as $feed_name) { + if (in_array($feed_name, $feed_importers)) { + feeds_source($feed_name)->importer->delete(); + drush_log(dt('!feed_name was reverted successfully.', array('!feed_name' => $feed_name)), 'ok'); + } + else { + drush_log(dt('!feed_name wasn\'t found.', array('!feed_name' => $feed_name)), 'warning'); + } + } + + feeds_cache_clear(); +} + +/** + * Enable a set of feeds. + * + * @param string $feed_name + */ +function drush_feeds_enable() { + $feed_names = func_get_args(); + $feed_importers = array_keys(feeds_importer_load_all(TRUE)); + drush_print(dt('The following feeds will be enabled: !feed_names', array('!feed_names' => implode(', ', $feed_names)))); + if(!drush_confirm(dt('Do you really want to continue?'))) { + return drush_log(dt('Aborting.')); + } + + $disabled = variable_get('default_feeds_importer'); + $feeds_to_enable = array_intersect($feed_names, $feed_importers); + foreach ($feeds_to_enable as $importer) { + if (isset($disabled[$importer])) { + unset($disabled[$importer]); + drush_log(dt('!feed_name has been enabled', array('!feed_name' => $importer)), 'ok'); + } + else { + drush_log(dt('!feed_name is not disabled and couldn\'t be enabled', array('!feed_name' => $importer)), 'warning'); + } + } + + variable_set('default_feeds_importer', $disabled); + + $not_existing_feeds = array_diff($feed_names, $feed_importers); + if (count($not_existing_feeds)) { + drush_log(dt('The following feeds weren\'t disabled because they don\'t exist: !feed_names', array('!feed_names' => implode(', ', $not_existing_feeds))), 'warning'); + } +} + +/** + * Disable a set of feeds. + * + * @param string $feed_name + */ +function drush_feeds_disable() { + $feed_names = func_get_args(); + $feed_importers = array_keys(feeds_importer_load_all(TRUE)); + drush_print(dt('The following feeds will be disabled: !feed_names', array('!feed_names' => implode(', ', $feed_names)))); + if(!drush_confirm(dt('Do you really want to continue?'))) { + return drush_log(dt('Aborting.')); + } + + $disabled = array(); + $feeds_to_disable = array_intersect($feed_names, $feed_importers); + foreach ($feeds_to_disable as $importer) { + $disabled[$importer] = TRUE; + } + + variable_set('default_feeds_importer', $disabled); + drush_print(dt('The following feeds have been disabled: !feed_names', array('!feed_names' => implode(', ', $feeds_to_disable)))); + + $not_existing_feeds = array_diff($feed_names, $feed_importers); + if (count($not_existing_feeds)) { + drush_log(dt('The following feeds weren\'t disabled because they don\'t exist: !feed_names', array('!feed_names' => implode(', ', $not_existing_feeds))), 'warning'); + } + + feeds_cache_clear(); +} \ No newline at end of file