5 * OPML import and export for FeedAPI
9 * OPML Feed import form, also allows setting defaults to be applied to each feed
11 function feedapi_import_opml($form_state) {
12 $form['opml'] = array(
14 '#title' => t('OPML File'),
16 '#description' => t('Upload an OPML file containing a list of newsfeeds to be imported.'),
18 $form['feed_type'] = array(
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(),
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'),
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'),
35 $form['#attributes']['enctype'] = 'multipart/form-data';
36 $form['submit'] = array(
38 '#value' => t('Import'),
40 if (module_exists('og')) {
41 og_form_add_og_audience($form, $form_state);
47 * Handle the submission of the OPML import form
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);
53 $result = _feedapi_import_opml($file, $form_state['values']);
54 if ($result['added']) {
56 format_plural($result['added'],
57 'Successfuly imported 1 feed from OPML',
58 'Successfuly imported @count feeds from OPML',
63 elseif ($result['dup']) {
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.',
73 drupal_set_message(t('Feed list could not be imported. Please check that this is a valid OPML file.'), 'error');
77 drupal_set_message(t('Data could not be retrieved, invalid or empty file.'), 'error');
79 $form_state['redirect'] = 'admin/content/feed';
84 * Generates an OPML representation of all feeds.
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'));
89 while ($feed = db_fetch_object($result)) {
92 return theme('feedapi_export_opml', $feeds);
96 * Theme the OPML feed output.
99 * An array of the feeds to theme.
102 function theme_feedapi_export_opml($feeds) {
103 drupal_set_header('Content-Type: text/xml; charset=utf-8');
105 $output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
106 $output .
= "<opml version=\"1.1\">\n";
107 $output .
= "<head>\n";
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";
116 $output .
= "</body>\n";
117 $output .
= "</opml>\n";
122 * Imports from OPML XML file.
124 function _feedapi_import_opml($opml, $args = array()) {
126 $dup_count = $count = 0;
127 $parser = drupal_xml_parser_create($opml);
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'];
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']));
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'];
150 if ($args['override_body'] && isset($feed['TEXT'])) {
151 $node->body
= $feed['TEXT'];
153 $node->og_groups
= $args['og_groups'];
154 $node->og_public
= (int) $args['og_public'];
155 $node = feedapi_create_node($node, $feed['XMLURL']);
164 return array('added' => $count, 'dup' => $dup_count);