Improve empty feeds detection

This commit is contained in:
Frederic Guillot 2013-07-12 20:26:47 -04:00
parent 5fc8ec2839
commit 69a37f02d7
6 changed files with 80 additions and 40 deletions

View File

@ -345,10 +345,19 @@ Router\get_action('refresh-all', function() {
Router\get_action('feeds', function() { Router\get_action('feeds', function() {
$empty_feeds = Model\get_empty_feeds(); $empty_feeds = Model\get_empty_feeds();
if (count($empty_feeds) > 0) {
$message = t('The following feeds are empty, there may be an error : ') . implode(', ', $empty_feeds); if (! empty($empty_feeds)) {
$listing = array();
foreach ($empty_feeds as &$feed) {
$listing[] = '"'.$feed['title'].'"';
}
$message = t('The following feeds are empty, there is maybe an error: %s', implode(', ', $listing));
Session\flash_error($message); Session\flash_error($message);
} }
Response\html(Template\layout('feeds', array( Response\html(Template\layout('feeds', array(
'feeds' => Model\get_feeds(), 'feeds' => Model\get_feeds(),
'nothing_to_read' => Request\int_param('nothing_to_read'), 'nothing_to_read' => Request\int_param('nothing_to_read'),

View File

@ -1,8 +1,8 @@
<?php <?php
return array( return array(
'The following feeds are empty, there may be an error : ' => 'The following feeds are empty, there is maybe an error: %s' =>
'Les abonnements suivants sont vides, il doit y avoir une erreur :', 'Les abonnements suivants sont vides, il y a peut-être une erreur : %s',
'Items per page' => 'Nombre d\'éléments par page', 'Items per page' => 'Nombre d\'éléments par page',
'Previous page' => 'Page précédente', 'Previous page' => 'Page précédente',
'Next page' => 'Page suivante', 'Next page' => 'Page suivante',

View File

@ -247,28 +247,25 @@ function get_feed($feed_id)
->findOne(); ->findOne();
} }
function get_empty_feeds() function get_empty_feeds()
{ {
$feeds = \PicoTools\singleton('db') $feeds = \PicoTools\singleton('db')
->table('feeds') ->table('feeds')
->gt('last_checked', 0) ->columns('feeds.id', 'feeds.title', 'COUNT(items.id) AS nb_items')
->asc('id') ->join('items', 'feed_id', 'id')
->listing('id', 'title'); ->isNull('feeds.last_checked')
$ids = array_keys($feeds); ->groupBy('items.feed_id')
->findAll();
// fake DISTINCT foreach ($feeds as $key => &$feed) {
$not_empty_feeds = \PicoTools\singleton('db')
->table('items')
->asc('feed_id')
->listing('feed_id', 'feed_id');
$not_empty_ids = array_keys($not_empty_feeds);
$diff = array_diff($ids, $not_empty_ids); if ($feed['nb_items'] > 0) {
$return = array(); unset($feeds[$key]);
foreach ($diff as $id) { }
$return[$id] = $feeds[$id];
} }
return $return;
return $feeds;
} }

View File

@ -38,7 +38,7 @@ class Database
public function getLogMessages() public function getLogMessages()
{ {
return implode(', ', $this->logs); return $this->logs;
} }

View File

@ -42,6 +42,7 @@ class Sqlite extends \PDO {
public function escapeIdentifier($value) public function escapeIdentifier($value)
{ {
if (strpos($value, '.') !== false) return $value;
return '"'.$value.'"'; return '"'.$value.'"';
} }
} }

View File

@ -14,6 +14,8 @@ class Table
private $is_or_condition = false; private $is_or_condition = false;
private $columns = array(); private $columns = array();
private $values = array(); private $values = array();
private $distinct = false;
private $group_by = array();
private $db; private $db;
@ -134,6 +136,15 @@ class Table
} }
public function findAllByColumn($column)
{
$rq = $this->db->execute($this->buildSelectQuery(), $this->values);
if (false === $rq) return false;
return $rq->fetchAll(\PDO::FETCH_COLUMN, 0);
}
public function findOne() public function findOne()
{ {
$this->limit(1); $this->limit(1);
@ -158,11 +169,13 @@ class Table
public function buildSelectQuery() public function buildSelectQuery()
{ {
return sprintf( return sprintf(
'SELECT %s FROM %s %s %s %s %s %s', 'SELECT %s %s FROM %s %s %s %s %s %s %s',
$this->distinct ? 'DISTINCT' : '',
empty($this->columns) ? '*' : implode(', ', $this->columns), empty($this->columns) ? '*' : implode(', ', $this->columns),
$this->db->escapeIdentifier($this->table_name), $this->db->escapeIdentifier($this->table_name),
implode(' ', $this->joins), implode(' ', $this->joins),
$this->conditions(), $this->conditions(),
empty($this->group_by) ? '' : 'GROUP BY '.implode(', ', $this->group_by),
$this->sql_order, $this->sql_order,
$this->sql_limit, $this->sql_limit,
$this->sql_offset $this->sql_offset
@ -286,6 +299,13 @@ class Table
} }
public function groupBy()
{
$this->group_by = \func_get_args();
return $this;
}
public function columns() public function columns()
{ {
$this->columns = \func_get_args(); $this->columns = \func_get_args();
@ -293,20 +313,23 @@ class Table
} }
public function distinct()
{
$this->columns = \func_get_args();
$this->distinct = true;
return $this;
}
public function __call($name, array $arguments) public function __call($name, array $arguments)
{ {
if (2 !== count($arguments)) {
throw new \LogicException('You must define a column and a value.');
}
$column = $arguments[0]; $column = $arguments[0];
$sql = ''; $sql = '';
switch ($name) { switch (strtolower($name)) {
case 'in': case 'in':
if (is_array($arguments[1])) { if (isset($arguments[1]) && is_array($arguments[1])) {
$sql = sprintf( $sql = sprintf(
'%s IN (%s)', '%s IN (%s)',
@ -317,7 +340,7 @@ class Table
break; break;
case 'notin': case 'notin':
if (is_array($arguments[1])) { if (isset($arguments[1]) && is_array($arguments[1])) {
$sql = sprintf( $sql = sprintf(
'%s NOT IN (%s)', '%s NOT IN (%s)',
@ -338,40 +361,50 @@ class Table
break; break;
case 'gt': case 'gt':
case 'greaterThan': case 'greaterthan':
$sql = sprintf('%s > ?', $this->db->escapeIdentifier($column)); $sql = sprintf('%s > ?', $this->db->escapeIdentifier($column));
break; break;
case 'lt': case 'lt':
case 'lowerThan': case 'lowerthan':
$sql = sprintf('%s < ?', $this->db->escapeIdentifier($column)); $sql = sprintf('%s < ?', $this->db->escapeIdentifier($column));
break; break;
case 'gte': case 'gte':
case 'greaterThanOrEquals': case 'greaterthanorequals':
$sql = sprintf('%s >= ?', $this->db->escapeIdentifier($column)); $sql = sprintf('%s >= ?', $this->db->escapeIdentifier($column));
break; break;
case 'lte': case 'lte':
case 'lowerThanOrEquals': case 'lowerthanorequals':
$sql = sprintf('%s <= ?', $this->db->escapeIdentifier($column)); $sql = sprintf('%s <= ?', $this->db->escapeIdentifier($column));
break; break;
case 'isnull':
$sql = sprintf('%s IS NULL', $this->db->escapeIdentifier($column));
break;
case 'notnull':
$sql = sprintf('%s IS NOT NULL', $this->db->escapeIdentifier($column));
break;
} }
if ('' !== $sql) { if ($sql !== '') {
$this->addCondition($sql); $this->addCondition($sql);
if (is_array($arguments[1])) { if (isset($arguments[1])) {
foreach ($arguments[1] as $value) { if (is_array($arguments[1])) {
$this->values[] = $value; foreach ($arguments[1] as $value) {
$this->values[] = $value;
}
} }
} else {
else {
$this->values[] = $arguments[1]; $this->values[] = $arguments[1];
}
} }
} }