Skip to content

Commit

Permalink
blog MDL-25341 added removal of deleted external blog posts
Browse files Browse the repository at this point in the history
  • Loading branch information
andyjdavis committed Dec 20, 2010
1 parent cc52e53 commit 50ff50d
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion blog/lib.php
Expand Up @@ -137,7 +137,8 @@ function blog_remove_associations_for_course($courseid) {


/** /**
* Given a record in the {blog_external} table, checks the blog's URL * Given a record in the {blog_external} table, checks the blog's URL
* for new entries not yet copied into Moodle. * for new entries not yet copied into Moodle.
* Also attempts to identify and remove deleted blog entries
* *
* @param object $externalblog * @param object $externalblog
* @return boolean False if the Feed is invalid * @return boolean False if the Feed is invalid
Expand All @@ -164,7 +165,9 @@ function blog_sync_external_entries($externalblog) {
return null; return null;
} }


//used to identify blog posts that have been deleted from the source feed
$oldesttimestamp = null; $oldesttimestamp = null;
$uniquehashes = array();


foreach ($rss->get_items() as $entry) { foreach ($rss->get_items() as $entry) {
// If filtertags are defined, use them to filter the entries by RSS category // If filtertags are defined, use them to filter the entries by RSS category
Expand All @@ -185,6 +188,8 @@ function blog_sync_external_entries($externalblog) {
continue; continue;
} }
} }

$uniquehashes[] = $entry->get_permalink();


$newentry = new stdClass(); $newentry = new stdClass();
$newentry->userid = $externalblog->userid; $newentry->userid = $externalblog->userid;
Expand Down Expand Up @@ -249,6 +254,18 @@ function blog_sync_external_entries($externalblog) {
$DB->update_record('post', $newentry); $DB->update_record('post', $newentry);
} }
} }

//Look at the posts we have in the database to check if any of them have been deleted from the feed.
//Only checking posts within the time frame returned by the rss feed. Older items may have been deleted or
//may just not be returned anymore. We cant tell the difference so we leave older posts alone.
$dbposts = $DB->get_records_select('post', 'created > :ts', array('ts'=>$oldesttimestamp), '', 'id, uniquehash');
$todelete = array();
foreach($dbposts as $dbpost) {
if (!in_array($dbpost->uniquehash,$uniquehashes)) {
$todelete[] = $dbpost->id;
}
}
$DB->delete_records_list('post', 'id', $todelete);


$DB->update_record('blog_external', array('id' => $externalblog->id, 'timefetched' => mktime())); $DB->update_record('blog_external', array('id' => $externalblog->id, 'timefetched' => mktime()));
} }
Expand Down

0 comments on commit 50ff50d

Please sign in to comment.