| Commit | Line | Data |
|---|---|---|
| adf581e4 | 1 | <?php |
| adf581e4 AN |
2 | |
| 3 | /** | |
| dd1d80c1 AN |
4 | * @file |
| 5 | * Drush commands for FeedAPI. | |
| 6 | */ | |
| 7 | ||
| 8 | /** | |
| adf581e4 AN |
9 | * Implementation of hook_drush_help(). |
| 10 | */ | |
| 11 | function feedapi_drush_help($section) { | |
| 12 | switch ($section) { | |
| 13 | case 'drush:feedapi create': | |
| 14 | return dt('Creates a feed.'); | |
| 15 | case 'drush:feedapi refresh': | |
| 16 | return dt('Refreshes a feed.'); | |
| 17 | case 'drush:feedapi delete': | |
| 18 | return dt('Deletes a feed.'); | |
| 19 | case 'drush:feedapi parse': | |
| 20 | return dt('Parses a feed and shows you the output.'); | |
| 21 | case 'drush:feedapi config': | |
| 22 | return dt('Shows the config of a parser of a processor.'); | |
| 0aa46c53 | 23 | |
| adf581e4 AN |
24 | } |
| 25 | } | |
| 26 | ||
| 27 | /** | |
| 28 | * Implementation of hook_drush_command(). | |
| 29 | */ | |
| 30 | function feedapi_drush_command() { | |
| 31 | $options['--type'] = 'The node type of the feed. Default value: feed, the built-in content-type of FeedAPI.'; | |
| 32 | $items['feedapi create'] = array( | |
| 33 | 'callback' => 'feedapi_drush_create', | |
| 34 | 'description' => dt('Creates a feed'), | |
| 35 | 'options' => $options, | |
| 36 | 'arguments' => array( | |
| 37 | 'url' => 'A feed URL. Mandatory.', | |
| 38 | ), | |
| 39 | ); | |
| 40 | $items['feedapi refresh'] = array( | |
| 41 | 'callback' => 'feedapi_drush_refresh', | |
| 42 | 'description' => dt('Refreshes a feed'), | |
| 43 | 'arguments' => array( | |
| 44 | 'feed' => 'A feed URL or node nid. Mandatory.', | |
| 45 | ), | |
| 46 | ); | |
| 47 | $items['feedapi parse'] = array( | |
| 48 | 'callback' => 'feedapi_drush_parse', | |
| 49 | 'description' => dt('Parses a feed and outputs the result to the stdout'), | |
| 50 | 'options' => $options, | |
| 51 | 'arguments' => array( | |
| 52 | 'url' => 'A feed URL. Mandatory.', | |
| 53 | 'parser' => 'The name of the parser. Mandatory.', | |
| 54 | ), | |
| 55 | ); | |
| 56 | $items['feedapi config'] = array( | |
| 57 | 'callback' => 'feedapi_drush_config', | |
| 58 | 'description' => dt('Shows the config of a given parser or processor'), | |
| 59 | 'options' => $options, | |
| 60 | 'arguments' => array( | |
| 61 | 'name' => 'The name of the parser or the processor. Mandatory.', | |
| 62 | ), | |
| 63 | ); | |
| 64 | return $items; | |
| 65 | } | |
| 66 | ||
| 67 | /** | |
| 68 | * Creates a feed | |
| 69 | * | |
| 70 | * @param $url | |
| 71 | */ | |
| 72 | function feedapi_drush_create($url) { | |
| 73 | $node_template = new stdClass(); | |
| 74 | $node_template->type = drush_get_option('type') ? drush_get_option('type') : 'feed'; | |
| 75 | feedapi_create_node($node_template, $url); | |
| 76 | } | |
| 77 | ||
| 78 | /** | |
| 79 | * Refreshes a feed. | |
| 80 | * When the feed URL is not enough to exactly define a feed, use the nid | |
| 81 | * | |
| 82 | * @param $feed | |
| 83 | * URL or nid | |
| 84 | */ | |
| 85 | function feedapi_drush_refresh($feed) { | |
| 86 | $nid = _feedapi_drush_get_nid($feed); | |
| 87 | if (is_numeric($nid)) { | |
| 88 | $node = node_load(array('nid' => $nid)); | |
| 89 | feedapi_invoke('refresh', $node->feed, FALSE); | |
| 90 | } | |
| 91 | } | |
| 92 | ||
| 93 | /** | |
| 94 | * Invokes the parser and displays the output | |
| 95 | */ | |
| 96 | function feedapi_drush_parse($url, $parser) { | |
| 97 | $type = drush_get_option('type') ? drush_get_option('type') : 'feed'; | |
| 98 | $settings = feedapi_get_settings($type); | |
| 99 | $feed = new stdClass(); | |
| 100 | $feed->url = $url; | |
| a1321cd7 | 101 | drush_print_r(_feedapi_call_parsers($feed, array($parser), $settings)); |
| adf581e4 AN |
102 | } |
| 103 | ||
| 104 | function feedapi_drush_config($name) { | |
| 105 | $type = drush_get_option('type') ? drush_get_option('type') : 'feed'; | |
| 106 | $settings = variable_get('feedapi_settings_'. $type, array()); | |
| 107 | if (isset($settings['parsers'][$name])) { | |
| a1321cd7 | 108 | drush_print_r($settings['parsers'][$name]); |
| adf581e4 AN |
109 | } |
| 110 | elseif (isset($settings['processors'][$name])) { | |
| a1321cd7 | 111 | drush_print_r($settings['processors'][$name]); |
| adf581e4 AN |
112 | } |
| 113 | } | |
| 114 | ||
| 115 | function _feedapi_drush_get_nid($feed) { | |
| f7163e9a | 116 | if (valid_url($feed, TRUE) || strpos($feed, '://')) { |
| adf581e4 AN |
117 | return db_result(db_query("SELECT nid FROM {feedapi} WHERE url = '%s'", $feed)); |
| 118 | } | |
| 119 | return $feed; | |
| 120 | } |