Index: aggregator.module =================================================================== RCS file: /cvs/drupal/drupal/modules/aggregator.module,v retrieving revision 1.226 diff -u -F^f -r1.226 aggregator.module --- aggregator.module 17 Jan 2005 19:00:03 -0000 1.226 +++ aggregator.module 27 Jan 2005 05:44:01 -0000 @@ -122,12 +122,15 @@ function aggregator_menu($may_cache) { $items[] = array('path' => 'admin/aggregator/update', 'title' => t('update items'), 'callback' => 'aggregator_admin_refresh_feed', 'access' => $edit, 'type' => MENU_CALLBACK); - + $items[] = array('path' => 'admin/aggregator/list', 'title' => t('list'), 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10); $items[] = array('path' => 'admin/aggregator/add/feed', 'title' => t('add feed'), 'callback' => 'aggregator_admin_edit_feed', 'access' => $edit, 'type' => MENU_LOCAL_TASK); + $items[] = array('path' => 'admin/aggregator/add/opml', 'title' => t('import feeds'), + 'callback' => 'aggregator_admin_import_feed', 'access' => $edit, + 'type' => MENU_LOCAL_TASK); $items[] = array('path' => 'admin/aggregator/add/category', 'title' => t('add category'), 'callback' => 'aggregator_admin_edit_category', 'access' => $edit, 'type' => MENU_LOCAL_TASK); @@ -660,6 +663,12 @@ function aggregator_form_feed($edit = ar $form .= form_submit(t('Delete')); $form .= form_hidden('fid', $edit['fid']); } + + + $import_form = form_file(t('OPML File'), 'opml', 50, t('Upload an OPML file containing a list of newsfeeds to be imported.'), $required = TRUE); + + + return form($form); } @@ -805,6 +814,40 @@ function aggregator_admin_edit_category( } /** + * Menu callback; displays the feed import form. + * + * After importing, saves changes and redirects to the overview page. + */ +function aggregator_admin_import_feed($feed = 0) { + $edit = $_POST['edit']; + $op = $_POST['op']; + + $output = ""; + + switch ($op) { + case t('Submit'): + $file = file_check_upload('opml'); + if ($file=file($file->filepath)) { + $file = implode('',$file); + if (_aggregator_import($file)) { + drupal_set_message(t('Successfuly Imported Feeds from OPML')); + drupal_goto('admin/aggregator'); + } + else form_set_error('Bad File', t('Sorry, could not parse feeds')); + break; + } + else form_set_error('Bad File', t('Sorry, invalid / empty file')); + default: + $form = form_file(t('OPML File'), 'opml', 50, t('Upload an OPML file containing a list of newsfeeds to be imported.'), TRUE); + $form .= form_submit(t('Submit')); + $output .= form($form, 'POST', NULL, array('enctype' => 'multipart/form-data')); + } + + print theme('page', $output); +} + + +/** * Menu callback; displays the feed edit form. * * After editing, saves changes and redirects to the overview page. @@ -1030,6 +1073,45 @@ function aggregator_page_categories() { $output .= ''; print theme('page', $output); +} + +/** + * Imports from OPML + */ +function _aggregator_import($opml) { + + $feeds = array(); + $error = FALSE; + + $parser = xml_parser_create(); + + //Some OPML Files don't have the xml tag, which causes parsing to fail. Hence, using the appended version as a fallback parse + if (xml_parse_into_struct($parser,$opml,$vals,$index) || xml_parse_into_struct($parser,''.$opml,$vals,$index)) { + + foreach($vals as $entry) { + if ($entry['tag'] == 'OUTLINE') { + $feeds[] = $entry['attributes']; + } + } + + foreach($feeds as $n=>$feed) { + if ($feed['XMLURL']) { + $edit = array('title' => $feed['TITLE'], 'url' => $feed['XMLURL']); + + //Adding '@' to bypass the problem of duplicate INSERTS + @aggregator_save_feed($edit); + } + } + + } + else{ + + form_set_error('Bad format', t('Invalid Format')); + $error = TRUE; + + } + + return !$error; } /**