2013-12-23 02:55:53 +01:00
|
|
|
<?php
|
|
|
|
|
2016-08-25 03:17:58 +02:00
|
|
|
namespace Miniflux\Model\Feed;
|
2013-12-23 02:55:53 +01:00
|
|
|
|
2016-12-26 22:54:44 +01:00
|
|
|
use Miniflux\Model\Favicon;
|
2016-08-25 03:17:58 +02:00
|
|
|
use Miniflux\Model\Item;
|
|
|
|
use Miniflux\Model\Group;
|
2014-02-08 20:13:14 +01:00
|
|
|
use PicoDb\Database;
|
2016-12-26 15:44:53 +01:00
|
|
|
use PicoFeed\Parser\Feed;
|
2013-12-23 02:55:53 +01:00
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
const STATUS_ACTIVE = 1;
|
|
|
|
const STATUS_INACTIVE = 0;
|
|
|
|
const TABLE = 'feeds';
|
2013-12-23 02:55:53 +01:00
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
function create($user_id, Feed $feed, $etag, $last_modified, $rtl = false, $scraper = false, $cloak_referrer = false)
|
2013-12-23 02:55:53 +01:00
|
|
|
{
|
2015-08-15 03:33:39 +02:00
|
|
|
$db = Database::getInstance('db');
|
2013-12-23 02:55:53 +01:00
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
if ($db->table('feeds')->eq('user_id', $user_id)->eq('feed_url', $feed->getFeedUrl())->exists()) {
|
|
|
|
return -1;
|
2015-04-10 23:34:07 +02:00
|
|
|
}
|
2013-12-23 02:55:53 +01:00
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
$feed_id = $db
|
|
|
|
->table(TABLE)
|
|
|
|
->persist(array(
|
|
|
|
'user_id' => $user_id,
|
|
|
|
'title' => $feed->getTitle(),
|
|
|
|
'site_url' => $feed->getSiteUrl(),
|
|
|
|
'feed_url' => $feed->getFeedUrl(),
|
|
|
|
'download_content' => $scraper ? 1 : 0,
|
|
|
|
'rtl' => $rtl ? 1 : 0,
|
|
|
|
'etag' => $etag,
|
|
|
|
'last_modified' => $last_modified,
|
|
|
|
'last_checked' => time(),
|
|
|
|
'cloak_referrer' => $cloak_referrer ? 1 : 0,
|
|
|
|
));
|
2015-04-10 23:34:07 +02:00
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
if ($feed_id !== false) {
|
|
|
|
Item\update_feed_items($user_id, $feed_id, $feed->getItems(), $rtl);
|
2013-12-23 02:55:53 +01:00
|
|
|
}
|
|
|
|
|
2015-04-10 23:34:07 +02:00
|
|
|
return $feed_id;
|
2013-12-23 02:55:53 +01:00
|
|
|
}
|
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
function get_feeds($user_id)
|
2013-12-23 02:55:53 +01:00
|
|
|
{
|
2015-12-24 15:34:55 +01:00
|
|
|
return Database::getInstance('db')
|
2016-12-26 15:44:53 +01:00
|
|
|
->table(TABLE)
|
|
|
|
->eq('user_id', $user_id)
|
2016-12-26 21:07:18 +01:00
|
|
|
->desc('parsing_error')
|
|
|
|
->desc('enabled')
|
2013-12-23 02:55:53 +01:00
|
|
|
->asc('title')
|
|
|
|
->findAll();
|
|
|
|
}
|
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
function get_feeds_with_items_count($user_id)
|
2014-02-22 20:00:49 +01:00
|
|
|
{
|
2016-12-26 21:07:18 +01:00
|
|
|
$feeds_count = array();
|
|
|
|
$feeds = get_feeds($user_id);
|
|
|
|
$feed_items = Database::getInstance('db')
|
|
|
|
->table(Item\TABLE)
|
|
|
|
->columns('feed_id', 'status', 'count(*) as nb')
|
|
|
|
->eq('user_id', $user_id)
|
|
|
|
->neq('status', Item\STATUS_REMOVED)
|
|
|
|
->groupBy('feed_id', 'status')
|
2014-02-23 01:45:02 +01:00
|
|
|
->findAll();
|
2016-12-26 21:07:18 +01:00
|
|
|
|
|
|
|
foreach ($feed_items as $row) {
|
|
|
|
if (! isset($feeds_count[$row['feed_id']])) {
|
|
|
|
$feeds_count[$row['feed_id']] = array('unread' => 0, 'total' => 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($row['status'] === 'unread') {
|
|
|
|
$feeds_count[$row['feed_id']]['unread'] = $row['nb'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$feeds_count[$row['feed_id']]['total'] += $row['nb'];
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($feeds as &$feed) {
|
|
|
|
$feed['items_unread'] = isset($feeds_count[$feed['id']]) ? $feeds_count[$feed['id']]['unread'] : 0;
|
|
|
|
$feed['items_total'] = isset($feeds_count[$feed['id']]) ? $feeds_count[$feed['id']]['total'] : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $feeds;
|
2014-02-22 20:00:49 +01:00
|
|
|
}
|
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
function get_feed_ids($user_id, $limit = null)
|
2014-02-22 20:00:49 +01:00
|
|
|
{
|
2016-12-26 15:44:53 +01:00
|
|
|
$query = Database::getInstance('db')
|
|
|
|
->table(TABLE)
|
|
|
|
->eq('user_id', $user_id)
|
|
|
|
->eq('enabled', STATUS_ACTIVE)
|
|
|
|
->asc('last_checked')
|
|
|
|
->asc('id');
|
2014-02-23 01:45:02 +01:00
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
if ($limit !== null) {
|
|
|
|
$query->limit($limit);
|
2014-02-22 20:00:49 +01:00
|
|
|
}
|
2014-02-23 01:45:02 +01:00
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
return $query->findAllByColumn('id');
|
2014-02-22 20:00:49 +01:00
|
|
|
}
|
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
function get_feed($user_id, $feed_id)
|
2013-12-23 02:55:53 +01:00
|
|
|
{
|
2015-12-24 15:34:55 +01:00
|
|
|
return Database::getInstance('db')
|
2016-12-26 15:44:53 +01:00
|
|
|
->table(TABLE)
|
|
|
|
->eq('user_id', $user_id)
|
2013-12-23 02:55:53 +01:00
|
|
|
->eq('id', $feed_id)
|
|
|
|
->findOne();
|
|
|
|
}
|
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
function update_feed($user_id, $feed_id, array $values)
|
2013-12-23 02:55:53 +01:00
|
|
|
{
|
2016-12-26 15:44:53 +01:00
|
|
|
$db = Database::getInstance('db');
|
|
|
|
$db->startTransaction();
|
2013-12-23 02:55:53 +01:00
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
$feed = $values;
|
|
|
|
unset($feed['id']);
|
|
|
|
unset($feed['group_name']);
|
|
|
|
unset($feed['feed_group_ids']);
|
|
|
|
|
|
|
|
$result = Database::getInstance('db')
|
|
|
|
->table('feeds')
|
|
|
|
->eq('user_id', $user_id)
|
|
|
|
->eq('id', $feed_id)
|
|
|
|
->save($feed);
|
|
|
|
|
|
|
|
if ($result) {
|
|
|
|
if (isset($values['feed_group_ids']) && isset($values['group_name']) &&
|
|
|
|
! Group\update_feed_groups($user_id, $values['id'], $values['feed_group_ids'], $values['group_name'])) {
|
|
|
|
$db->cancelTransaction();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$db->closeTransaction();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$db->cancelTransaction();
|
|
|
|
return false;
|
2013-12-23 02:55:53 +01:00
|
|
|
}
|
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
function change_feed_status($user_id, $feed_id, $status = STATUS_ACTIVE)
|
2013-12-23 02:55:53 +01:00
|
|
|
{
|
2016-12-26 15:44:53 +01:00
|
|
|
return Database::getInstance('db')
|
|
|
|
->table(TABLE)
|
|
|
|
->eq('user_id', $user_id)
|
2013-12-23 02:55:53 +01:00
|
|
|
->eq('id', $feed_id)
|
2016-12-26 15:44:53 +01:00
|
|
|
->save((array('enabled' => $status)));
|
2013-12-23 02:55:53 +01:00
|
|
|
}
|
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
function remove_feed($user_id, $feed_id)
|
2013-12-23 02:55:53 +01:00
|
|
|
{
|
2016-12-26 22:54:44 +01:00
|
|
|
$result = Database::getInstance('db')
|
2016-12-26 15:44:53 +01:00
|
|
|
->table(TABLE)
|
|
|
|
->eq('user_id', $user_id)
|
|
|
|
->eq('id', $feed_id)
|
|
|
|
->remove();
|
2016-12-26 22:54:44 +01:00
|
|
|
|
|
|
|
if ($result) {
|
|
|
|
Favicon\purge_favicons();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
2013-12-23 02:55:53 +01:00
|
|
|
}
|
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
function count_failed_feeds($user_id)
|
2013-12-23 02:55:53 +01:00
|
|
|
{
|
2016-12-26 15:44:53 +01:00
|
|
|
return Database::getInstance('db')
|
|
|
|
->table(TABLE)
|
|
|
|
->eq('user_id', $user_id)
|
|
|
|
->eq('parsing_error', 1)
|
|
|
|
->count();
|
2013-12-23 02:55:53 +01:00
|
|
|
}
|
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
function count_feeds($user_id)
|
2013-12-23 02:55:53 +01:00
|
|
|
{
|
2016-12-26 15:44:53 +01:00
|
|
|
return Database::getInstance('db')
|
|
|
|
->table(TABLE)
|
|
|
|
->eq('user_id', $user_id)
|
|
|
|
->count();
|
2013-12-23 02:55:53 +01:00
|
|
|
}
|
|
|
|
|