Skip to content

Commit

Permalink
Merge pull request #690 from PhrozenByte/enhancement/ImproveLastEntry
Browse files Browse the repository at this point in the history
Use the date specified by the items as the date of the last entry
  • Loading branch information
SSilence committed Jul 28, 2015
2 parents 1270597 + f5498b6 commit 18a1053
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
10 changes: 5 additions & 5 deletions daos/mysql/Sources.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,20 @@ public function error($id, $error) {
*
* @return void
* @param int $id the source id
* @param boolean newPostAdded true or false depending on whether a post was added
* @param int $lastEntry timestamp of the newest item or NULL when no items were added
*/
public function saveLastUpdate($id, $newPostAdded) {
public function saveLastUpdate($id, $lastEntry) {
\F3::get('db')->exec('UPDATE '.\F3::get('db_prefix').'sources SET lastupdate=:lastupdate WHERE id=:id',
array(
':id' => $id,
':lastupdate' => time()
));

if ($newPostAdded == TRUE) {
if ($lastEntry !== null) {
\F3::get('db')->exec('UPDATE '.\F3::get('db_prefix').'sources SET lastentry=:lastentry WHERE id=:id',
array(
':id' => $id,
':lastentry' => time()
':lastentry' => $lastEntry
));
}

Expand All @@ -134,7 +134,7 @@ public function saveLastUpdate($id, $newPostAdded) {
* @return mixed all sources
*/
public function getByLastUpdate() {
$ret = \F3::get('db')->exec('SELECT id, title, tags, spout, params, filter, error, lastupdate FROM '.\F3::get('db_prefix').'sources ORDER BY lastupdate ASC');
$ret = \F3::get('db')->exec('SELECT id, title, tags, spout, params, filter, error, lastupdate, lastentry FROM '.\F3::get('db_prefix').'sources ORDER BY lastupdate ASC');
$spoutLoader = new \helpers\SpoutLoader();
for($i=0;$i<count($ret);$i++)
$ret[$i]['spout_obj'] = $spoutLoader->get( $ret[$i]['spout'] );
Expand Down
17 changes: 8 additions & 9 deletions helpers/ContentLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ public function update() {
*/
public function fetch($source) {

$newPostAdded = FALSE;
$lastEntry = $source['lastentry'];

// at least 20 seconds wait until next update of a given source
$this->updateSource($source, $newPostAdded);
$this->updateSource($source, null);
if(time() - $source['lastupdate'] < 20)
return;

Expand Down Expand Up @@ -188,15 +188,15 @@ public function fetch($source) {
\F3::get('logger')->log('Memory usage: '.memory_get_usage(), \DEBUG);
\F3::get('logger')->log('Memory peak usage: '.memory_get_peak_usage(), \DEBUG);

$newPostAdded = TRUE;
$lastEntry = max($lastEntry, $itemDate->getTimestamp());
}

// destroy feed object (prevent memory issues)
\F3::get('logger')->log('destroy spout object', \DEBUG);
$spout->destroy();

// remove previous errors and set last update timestamp
$this->updateSource($source, $newPostAdded);
$this->updateSource($source, $lastEntry);
}

/**
Expand Down Expand Up @@ -379,16 +379,15 @@ protected function cleanupFiles($type) {
/**
* Update source (remove previous errors, update last update)
*
* @param $source source object
* @param $newPostAdded boolean true or false depending on if a new post was found
*
* @param mixed $source source object
* @param int $lastEntry timestamp of the newest item or NULL when no items were added
*/
protected function updateSource($source, $newPostAdded) {
protected function updateSource($source, $lastEntry) {
// remove previous error
if ( !is_null($source['error']) ) {
$this->sourceDao->error($source['id'], '');
}
// save last update
$this->sourceDao->saveLastUpdate($source['id'], $newPostAdded);
$this->sourceDao->saveLastUpdate($source['id'], $lastEntry);
}
}

0 comments on commit 18a1053

Please sign in to comment.