/wp-content * 2. drop a copy of MapgieRSS into /wp-content * 3. update wp-agg.ini * 4. install a cron job to run periodically, * 5. alternately browse to http:///wp-content/wp-rss-aggregate.php */ define('MAGPIE_DIR', 'magpierss/'); require_once MAGPIE_DIR.'rss_fetch.inc'; require_once MAGPIE_DIR.'rss_utils.inc'; require_once '../wp-config.php'; require_once '../wp-includes/wp-db.php'; $feeds = parse_config(); $total_new = 0; foreach ($feeds as $f) { $new_count = feed2wp($wpdb, $f); $total_new = $total_new + $new_count; } echo "Added $total_new new posts"; function feed2wp($wpdb, $f) { $feed = fetch_rss($f['url']); $new_count = 0; foreach ($feed->items as $item) { $post = item_to_post($wpdb, $item, $f); // echo "
"; var_dump($post); echo "
"; $new = add_post($wpdb, $post); if ( $new ) { $new_count++; } } return $new_count; } function item_to_post($wpdb, $item, $f) { $post = array(); $post['post_title'] = $wpdb->escape($item['title']); $post['post_author'] = ($item['dc']['creator']) ? $item['dc']['creator'] : $item['dc']['contributor']; $post['post_author'] = author_to_id($wpdb, $post['post_author']); if ( isset($item['content']['encoded']) and $item['content']['encoded'] ) { $post['post_content'] = $wpdb->escape($item['content']['encoded']); } else { $post['post_content'] = $wpdb->escape($item['description']); } $post['post_name'] = sanitize_title($post_title); $epoch = parse_w3cdtf($item['dc']['date']); $date = date('Y-m-d H:i:s', $epoch); $gmt = gmdate('Y-m-d H:i:s', $epoch); $post['post_date'] = $date; $post['post_modified'] = $date; $post['post_date_gmt'] = $gmt; $post['post_modified_gmt'] = $gmt; $post['wp_agg_rss_id'] = $item['link']; $post['cats'] = $f['cats']; if ( $item['dc']['subject'] ) { if ( strpos($f['url'], 'del.icio.us') !== false ) { $post['cats'] = array_merge($post['cats'], explode(' ', $item['dc']['subject'])); } else { $post['cats'][] = $item['dc']['subject']; } } return $post; } function add_post($wpdb, $post) { $result = $wpdb->get_var( "SELECT meta_id FROM $wpdb->postmeta WHERE meta_key='wp_agg_rss_id' AND meta_value='$post[wp_agg_rss_id]'"); // if we've already added this item, then skip if ($result) { return false; } $postquery = sprintf( "INSERT INTO $wpdb->posts ( ID, post_author, post_date, post_date_gmt, post_content, post_title, post_name, post_modified, post_modified_gmt) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", '0', $post['post_author'], $post['post_date'], $post['post_date_gmt'], $post['post_content'], $post['post_title'], $post['post_name'], $post['post_modified'], $post['post_modified_gmt'] ); error_log("post:$postquery"); $result = $wpdb->query($postquery); $postId = mysql_insert_id ($wpdb->dbh); $cat_ids = lookup_categories($wpdb, $post['cats']); add_to_category($wpdb, $postId, $cat_ids); add_rss_id($wpdb, $postId, $post['wp_agg_rss_id']); return true; } # function add_to_category($wpdb, $postId, $post_categories) { if (!$post_categories) $post_categories[] = 1; foreach ($post_categories as $post_category) { // Double check it's not there already $exists = $wpdb->get_row( "SELECT * FROM $wpdb->post2cat WHERE post_id = $postId AND category_id = $post_category"); if (!$exists) { $wpdb->query(" INSERT INTO $wpdb->post2cat (post_id, category_id) VALUES ($postId, $post_category) "); } } } function add_rss_id($wpdb, $postId, $id) { $result = $wpdb->query(" INSERT INTO $wpdb->postmeta (post_id,meta_key,meta_value) VALUES ('$postId','wp_agg_rss_id','$id')"); } // look up (and create) an author function author_to_id($wpdb, $author) { $id = $wpdb->get_var( "SELECT ID from $wpdb->users WHERE user_login = '$author' OR user_firstname = '$author' OR user_nickname = '$author' OR user_description = '$author' OR user_nicename = '$author'"); if ($id) { return $id; } $wpdb->query( "INSERT INTO $wpdb->users (ID,user_login,user_firstname,user_nickname) VALUES ('0', '$author','$author','$author')"); $authorId = mysql_insert_id ($wpdb->dbh); return $authorId; } // look up (and create) category ids from a list of categories function lookup_categories ($wpdb, $cats) { if ( !count($cats) ) { return array(); } # i'd kill for a decent map function in PHP # but that would require functiosn to be first class object, or at least # coderef support $cat_strs = array(); foreach ( $cats as $c ) { $c = $wpdb->escape($c); $c = "'$c'"; $cat_strs[] = $c; } $cat_sql = join(',', $cat_strs); $sql = "SELECT cat_ID,cat_name from $wpdb->categories WHERE cat_name IN ($cat_sql)"; $results = $wpdb->get_results($sql); $cat_ids = array(); $cat_found = array(); foreach ( $results as $row ) { $cat_ids[] = $row->cat_ID; $cat_found[] = $row->cat_name; } $cat_unknown = array_diff($cats, $cat_found); if ( count($cat_unknown) ) { $sql = "INSERT INTO $wpdb->categories (cat_name, category_nicename) VALUES ('%s', '%s')"; foreach ( $cat_unknown as $new_cat ) { $nice_cat = sanitize_title($new_cat); $wpdb->query(sprintf($sql, $new_cat, $nice_cat)); $cat_ids[] = mysql_insert_id ($wpdb->dbh); } } return $cat_ids; } # use ini files because i'm lazy # # example config. source name is ignored # cats are split on ':' # # [source name] # url = http://laughingmeme.org/index.rdf # cats = LM # # [source name2] # url = http://del.icio.us/tag/coffee # cats = addiction:caffeine:seattle function parse_config($file='wp-agg.ini') { $parsed = parse_ini_file($file, true); $feeds = array(); foreach ($parsed as $name => $sec) { if (!$sec['url']) { continue; } $sec['cats'] = explode(':', $sec['cats']); $feeds[] = $sec; } return $feeds; } ?>