"Refreshes a feed based on its schedule.", 'arguments' => array( 'feed-name' => 'The name of the feed importer that will be refreshed. Mandatory.', ), ); $items['feeds-queue'] = array( 'description' => "Adds a scheduled feed to the drupal_queue.", 'arguments' => array( 'feed-name' => 'The name of the feed importer that will be added to the queue. Mandatory.', ), ); $items['feeds-config'] = array( 'description' => "Displays all active importers or displays the config of a given importer (passed as arg).", ); return $items; } /** * Implementation of hook_drush_help(). * * @param * A string with the help section (prepend with 'drush:') * * @return * A string with the help text for your command. */ function feeds_drush_help($section) { switch ($section) { case 'drush:feeds-refresh': return dt("Refreshes a feed based on its schedule."); case 'drush:feeds-queue': return dt("Adds a scheduled feed to the drupal_queue."); case 'drush:feeds-config': return dt("Displays all active importers or displays the config of a given importer (passed as arg)."); } } /** * Refresh a given feed_name based on its schedule. * The feed will not be refresh if it's not scheduled. * * @param string $feed_name */ function drush_feeds_refresh($feed_name) { if (!isset($feed_name) || $feed_name == "") { print 'The importer feed_name is required.'; return FALSE; } if (variable_get('feeds_scheduler_drush_refresh', FALSE)) { watchdog('Drush Feeds-Refresh', 'Last drush refresh did not finish.', array(), WATCHDOG_ERROR); } variable_set('feeds_scheduler_drush_refresh', TRUE); $num = variable_get('feeds_schedule_num', 5); $importer = feeds_importer($feed_name); foreach ($importer->getScheduleCallbacks() as $callback) { // Check whether jobs are scheduled. $period = $importer->getSchedulePeriod($callback); if ($period != FEEDS_SCHEDULE_NEVER) { $time = FEEDS_REQUEST_TIME - $period; $result = db_query_range('SELECT feed_nid, id, callback, last_executed_time FROM {feeds_schedule} WHERE id = "%s" AND callback = "%s" AND scheduled = 0 AND (last_executed_time < %d OR last_executed_time = 0) ORDER BY last_executed_time ASC', $importer->id, $callback, FEEDS_REQUEST_TIME - $period, 0, $num); while ($feed_info = db_fetch_array($result)) { feeds_scheduler_work($feed_info); feeds_scheduler()->add($feed_info['importer_id'], $feed_info['callback'], $feed_info['feed_nid']); //feeds_batch_set(t('Importing'), 'import', $feed_info['importer_id']); } } } // Unflag variable_set('feeds_scheduler_drush_refresh', FALSE); } /** * Add a scheduled feed to the drupal_queue. * * @param string $feed_name */ function drush_feeds_queue($feed_name) { if (!isset($feed_name) || $feed_name == "") { print 'The importer feed_name is required.'; return FALSE; } if (module_exists('drupal_queue')) { drupal_queue_include(); $queue = drupal_queue_get(FEEDS_SCHEDULER_QUEUE); $num = variable_get('feeds_schedule_queue_num', 200); $importer = feeds_importer($feed_name); foreach ($importer->getScheduleCallbacks() as $callback) { $period = $importer->getSchedulePeriod($callback); if ($period != FEEDS_SCHEDULE_NEVER) { $time = FEEDS_REQUEST_TIME - $period; $result = db_query_range('SELECT feed_nid, id, callback, last_executed_time FROM {feeds_schedule} WHERE id = "%s" AND callback = "%s" AND scheduled = 0 AND (last_executed_time < %d OR last_executed_time = 0) ORDER BY last_executed_time ASC', $importer->id, $callback, FEEDS_REQUEST_TIME - $period, 0, $num); while ($feed_info = db_fetch_array($result)) { if ($queue->createItem($feed_info)) { feeds_scheduler()->add($feed_info['importer_id'], $feed_info['callback'], $feed_info['feed_nid']); } else { watchdog('Drush Feeds-Queue', 'Error adding item to queue.', WATCHDOG_ALERT); } } } } } else { drush_print('Drupal_queue module is required for this feature.'); } } /** * Display the config of a given importer. * * @param string $feed_name */ function drush_feeds_config() { $args = func_get_args(); $feed_name = $args[0]; if (!isset($feed_name) || $feed_name == "") { $importers = feeds_importer_load_all(); drush_print("\nHere is a list of active importers:\n"); foreach ($importers as $importer) { drush_print($importer->config['name'] . " ({$importer->id})"); drush_print(" - " . $importer->config['description']); drush_print(" * " . $importer->config['fetcher']['plugin_key']); drush_print(" * " . $importer->config['parser']['plugin_key']); drush_print(" * " . $importer->config['processor']['plugin_key']); drush_print("\n\n"); } return FALSE; } $sql = "SELECT id, feed_nid, config, source FROM {feeds_source} WHERE id = '%s'"; $result = db_query_range($sql, $feed_name, 0, 1); $feed_info = db_fetch_array($result); if ($feed_info) { $importer = feeds_importer($feed_info['id']); drush_print($importer->config['name']); drush_print(" - " . $importer->config['description']); drush_print(" * " . $importer->config['fetcher']['plugin_key']); drush_print(" * " . $importer->config['parser']['plugin_key']); drush_print(" * " . $importer->config['processor']['plugin_key']); drush_print("\nFeed nid:"); print_r(unserialize($feed_info['feed_nid'])); drush_print("\nSource:"); print_r(unserialize($feed_info['source'])); drush_print("\nConfig:"); print_r(unserialize($feed_info['config'])); } else { drush_print("The importer '$feed_name' does not exist."); } return; } /** * Helper function to flag a feed scheduled. * * This function sets the feed's scheduled bit to 1 and updates * last_scheduled_time to FEEDS_REQUEST_TIME. * * @param $id * Id of the importer configuration. * @param $callback * Callback of the job. * @param $feed_nid * Identifier of the feed node. */ function _drush_feeds_flag($id, $callback, $feed_nid) { $save = array( 'id' => $id, 'callback' => $callback, 'feed_nid' => $feed_nid, 'last_scheduled_time' => FEEDS_REQUEST_TIME, 'scheduled' => 1, ); drupal_write_record('feeds_schedule', $save, array('id', 'callback', 'feed_nid')); }