| Commit | Line | Data |
|---|---|---|
| b8051ca0 | 1 | <?php |
| b8051ca0 AN |
2 | |
| 3 | /** | |
| 4 | * @file | |
| 5 | * OPML import and export for FeedAPI | |
| 6 | */ | |
| 7 | ||
| 8 | /** | |
| 9 | * OPML Feed import form, also allows setting defaults to be applied to each feed | |
| 10 | */ | |
| 11 | function feedapi_import_opml($form_state) { | |
| 12 | $form['opml'] = array( | |
| 13 | '#type' => 'file', | |
| 14 | '#title' => t('OPML File'), | |
| 15 | '#size' => 50, | |
| 16 | '#description' => t('Upload an OPML file containing a list of newsfeeds to be imported.'), | |
| 17 | ); | |
| 18 | $form['feed_type'] = array( | |
| 19 | '#type' => 'select', | |
| 20 | '#title' => t('Feed Type'), | |
| 21 | '#description' => t("The type of feed you would like to associate this import with."), | |
| 22 | '#options' => feedapi_get_types(), | |
| 23 | '#required' => TRUE, | |
| 24 | ); | |
| 25 | $form['override_title'] = array( | |
| 26 | '#type' => 'checkbox', | |
| 27 | '#title' => t('Use TITLE attribute of OPML entries as feed title'), | |
| 28 | '#description' => t('If checked feed title will be overriden with the information from OPML file'), | |
| 29 | ); | |
| 30 | $form['override_body'] = array( | |
| 31 | '#type' => 'checkbox', | |
| 32 | '#title' => t('Use TEXT attribute of OPML entries as feed description'), | |
| 33 | '#description' => t('If checked feed description will be overriden with the information from OPML file'), | |
| 34 | ); | |
| 35 | $form['#attributes']['enctype'] = 'multipart/form-data'; | |
| 4f95f0ab AN |
36 | $form['submit'] = array( |
| 37 | '#type' => 'submit', | |
| 38 | '#value' => t('Import'), | |
| 39 | ); | |
| b8051ca0 AN |
40 | if (module_exists('og')) { |
| 41 | og_form_add_og_audience($form, $form_state); | |
| 42 | } | |
| 43 | return $form; | |
| 44 | } | |
| 45 | ||
| 46 | /** | |
| 47 | * Handle the submission of the OPML import form | |
| 48 | */ | |
| 49 | function feedapi_import_opml_submit($form, &$form_state) { | |
| 50 | $file = file_save_upload('opml'); | |
| 51 | if ($file = file($file->filepath)) { | |
| 52 | $file = implode('', $file); | |
| 9d5fe37f AN |
53 | $result = _feedapi_import_opml($file, $form_state['values']); |
| 54 | if ($result['added']) { | |
| 55 | drupal_set_message( | |
| 56 | format_plural($result['added'], | |
| 57 | 'Successfuly imported 1 feed from OPML', | |
| 58 | 'Successfuly imported @count feeds from OPML', | |
| 59 | array() | |
| 60 | ) | |
| 61 | ); | |
| 62 | } | |
| 63 | elseif ($result['dup']) { | |
| 64 | drupal_set_message( | |
| 65 | format_plural($result['dup'], | |
| 66 | 'All of the feed URLs already exist in the FeedAPI table. 1 URL was skipped.', | |
| 67 | 'All of the feed URLs already exist in the FeedAPI table. @count URLs were skipped.', | |
| 68 | array() | |
| 69 | ) | |
| 70 | ); | |
| b8051ca0 AN |
71 | } |
| 72 | else { | |
| 73 | drupal_set_message(t('Feed list could not be imported. Please check that this is a valid OPML file.'), 'error'); | |
| 74 | } | |
| 75 | } | |
| 76 | else { | |
| 77 | drupal_set_message(t('Data could not be retrieved, invalid or empty file.'), 'error'); | |
| 78 | } | |
| 79 | $form_state['redirect'] = 'admin/content/feed'; | |
| 80 | } | |
| 81 | ||
| 82 | ||
| 83 | /** | |
| 84 | * Generates an OPML representation of all feeds. | |
| 85 | */ | |
| 86 | function feedapi_export_opml() { | |
| 87 | $result = db_query(db_rewrite_sql('SELECT n.title, f.url FROM {feedapi} f INNER JOIN {node} n ON f.nid = n.nid ORDER BY n.title ASC')); | |
| 88 | $feeds = array(); | |
| 89 | while ($feed = db_fetch_object($result)) { | |
| 90 | $feeds[] = $feed; | |
| 91 | } | |
| 92 | return theme('feedapi_export_opml', $feeds); | |
| 93 | } | |
| 94 | ||
| 95 | /** | |
| 96 | * Theme the OPML feed output. | |
| 97 | * | |
| 98 | * @param $feeds | |
| 99 | * An array of the feeds to theme. | |
| 100 | * @ingroup themeable | |
| 101 | */ | |
| 102 | function theme_feedapi_export_opml($feeds) { | |
| 103 | drupal_set_header('Content-Type: text/xml; charset=utf-8'); | |
| 104 | ||
| 105 | $output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"; | |
| 106 | $output .= "<opml version=\"1.1\">\n"; | |
| 107 | $output .= "<head>\n"; | |
| 108 | ||
| 109 | $output .= '<title>'. check_plain(variable_get('site_name', 'Drupal')) ."</title>\n"; | |
| 110 | $output .= '<dateModified>'. gmdate('r') ."</dateModified>\n"; | |
| 111 | $output .= "</head>\n"; | |
| 112 | $output .= "<body>\n"; | |
| 113 | foreach ($feeds as $feed) { | |
| 114 | $output .= '<outline text="'. check_plain($feed->title) .'" xmlUrl="'. check_url($feed->url) ."\" />\n"; | |
| 115 | } | |
| 116 | $output .= "</body>\n"; | |
| 117 | $output .= "</opml>\n"; | |
| 118 | print $output; | |
| 119 | } | |
| 120 | ||
| 121 | /** | |
| 122 | * Imports from OPML XML file. | |
| 123 | */ | |
| 124 | function _feedapi_import_opml($opml, $args = array()) { | |
| 125 | $feeds = array(); | |
| 9d5fe37f | 126 | $dup_count = $count = 0; |
| b8051ca0 AN |
127 | $parser = drupal_xml_parser_create($opml); |
| 128 | ||
| 129 | //Some OPML Files don't have the xml tag, which causes parsing to fail. Hence, using the appended version as a fallback parse | |
| 130 | if (xml_parse_into_struct($parser, $opml, $vals, $index) || xml_parse_into_struct($parser, '<?xml version="1.0"?>'. $opml, $vals, $index)) { | |
| 131 | foreach ($vals as $entry) { | |
| 132 | if ($entry['tag'] == 'OUTLINE') { | |
| 133 | $feeds[] = $entry['attributes']; | |
| 134 | } | |
| 135 | } | |
| 4f95f0ab | 136 | |
| b8051ca0 AN |
137 | foreach ($feeds as $feed) { |
| 138 | if (strlen($feed['XMLURL']) > 1) { | |
| 139 | // check if feed url is already in the list | |
| 140 | $dupe = db_result(db_query("SELECT nid FROM {feedapi} WHERE url = '%s'", $feed['XMLURL'])); | |
| 141 | ||
| 142 | // If the feed is not already in the list, add it | |
| 143 | if ($dupe === FALSE) { | |
| 144 | // Generate a feed structure | |
| 145 | $node = new stdClass(); | |
| 146 | $node->type = $args['feed_type']; | |
| 147 | if ($args['override_title'] && isset($feed['TITLE'])) { | |
| 148 | $node->title = $feed['TITLE']; | |
| 149 | } | |
| 150 | if ($args['override_body'] && isset($feed['TEXT'])) { | |
| 151 | $node->body = $feed['TEXT']; | |
| 152 | } | |
| 153 | $node->og_groups = $args['og_groups']; | |
| 154 | $node->og_public = (int) $args['og_public']; | |
| 155 | $node = feedapi_create_node($node, $feed['XMLURL']); | |
| 156 | $count++; | |
| 157 | } | |
| 9d5fe37f AN |
158 | else { |
| 159 | $dup_count++; | |
| 160 | } | |
| b8051ca0 AN |
161 | } |
| 162 | } | |
| 163 | } | |
| 9d5fe37f | 164 | return array('added' => $count, 'dup' => $dup_count); |
| b8051ca0 | 165 | } |