miniflux-legacy/app/models/item.php

386 lines
11 KiB
PHP
Raw Permalink Normal View History

<?php
2016-08-25 03:17:58 +02:00
namespace Miniflux\Model\Item;
2016-08-19 04:33:58 +02:00
use PicoDb\Database;
2016-08-25 03:17:58 +02:00
use Miniflux\Model\Group;
use Miniflux\Handler;
use Miniflux\Helper;
use PicoFeed\Parser\Parser;
const TABLE = 'items';
const STATUS_UNREAD = 'unread';
const STATUS_READ = 'read';
const STATUS_REMOVED = 'removed';
function change_item_status($user_id, $item_id, $status)
{
if (! in_array($status, array(STATUS_READ, STATUS_UNREAD, STATUS_REMOVED))) {
return false;
}
2015-08-15 03:33:39 +02:00
return Database::getInstance('db')
->table(TABLE)
->eq('user_id', $user_id)
->eq('id', $item_id)
->update(array('status' => $status));
}
function change_items_status($user_id, $current_status, $new_status, $before = null)
{
if (! in_array($new_status, array(STATUS_READ, STATUS_UNREAD, STATUS_REMOVED))) {
return false;
}
$query = Database::getInstance('db')
->table(TABLE)
->eq('user_id', $user_id)
->eq('status', $current_status);
if ($before !== null) {
$query->lte('updated', $before);
}
return $query->update(array('status' => $new_status));
}
function change_item_ids_status($user_id, array $item_ids, $status)
implement frontend autoupdate Only the unread counter is updated right know. The AutoUpdate Feature is designed on the premise of don't wasting resources. A distinction is made between updates when Miniflux is visible or hidden. To determine the visibility status, the Page Visibility API is used. The API is available starting with Chrome 33, Firefox 18 and IE10. [https://developer.mozilla.org/en-US/docs/Web/Guide/User_experience/Using_the_Page_Visibility_API] As IE9 returns an undefined, it doesn't break the compatibility at least. If Miniflux is visible, the unread counter on the web page is updated as soon as a mismatch between the counter and the number of unread articles in the database is found. If Miniflux is hidden, the timestamp of the most recent article from each feed is compared with the value from the last run. We have an update If the timestamp of the latest article is greater than the stored one and the latest article is unread. The web page title is updated with a ? symbol to notify the user and the update check pauses till Miniflux gets visible again. If Miniflux gets visible again, the number of unread articles is queried from the database, the unread counter on the web page is updated and finally the ? symbol is removed from the web page title. This way I can use my fever API client to read new articles (or at least the latest article) while Miniflux is hidden and as I've seen the new articles already a new articles notification is prevented. It's intentionally that the page does not reload automatically as long as articles are visible. If I'm in hurry, I only scroll through the articles to spot something interesting. Most of the time I don't reach the last article. If the page is reloaded while I'm away, I would have to scan from the top again. If we're on a nothing_to_read page and have unread articles in the database, a redirect to the unread page will be done. The default update check interval is 10 minutes and can be changed on the settings page. A zero value disables the update check entirely. fixes #213
2014-11-27 22:36:04 +01:00
{
if (! in_array($status, array(STATUS_READ, STATUS_UNREAD, STATUS_REMOVED))) {
return false;
}
if (empty($item_ids)) {
return false;
}
2015-08-15 03:33:39 +02:00
return Database::getInstance('db')
->table(TABLE)
->eq('user_id', $user_id)
->in('id', $item_ids)
->update(array('status' => $status));
implement frontend autoupdate Only the unread counter is updated right know. The AutoUpdate Feature is designed on the premise of don't wasting resources. A distinction is made between updates when Miniflux is visible or hidden. To determine the visibility status, the Page Visibility API is used. The API is available starting with Chrome 33, Firefox 18 and IE10. [https://developer.mozilla.org/en-US/docs/Web/Guide/User_experience/Using_the_Page_Visibility_API] As IE9 returns an undefined, it doesn't break the compatibility at least. If Miniflux is visible, the unread counter on the web page is updated as soon as a mismatch between the counter and the number of unread articles in the database is found. If Miniflux is hidden, the timestamp of the most recent article from each feed is compared with the value from the last run. We have an update If the timestamp of the latest article is greater than the stored one and the latest article is unread. The web page title is updated with a ? symbol to notify the user and the update check pauses till Miniflux gets visible again. If Miniflux gets visible again, the number of unread articles is queried from the database, the unread counter on the web page is updated and finally the ? symbol is removed from the web page title. This way I can use my fever API client to read new articles (or at least the latest article) while Miniflux is hidden and as I've seen the new articles already a new articles notification is prevented. It's intentionally that the page does not reload automatically as long as articles are visible. If I'm in hurry, I only scroll through the articles to spot something interesting. Most of the time I don't reach the last article. If the page is reloaded while I'm away, I would have to scan from the top again. If we're on a nothing_to_read page and have unread articles in the database, a redirect to the unread page will be done. The default update check interval is 10 minutes and can be changed on the settings page. A zero value disables the update check entirely. fixes #213
2014-11-27 22:36:04 +01:00
}
function update_feed_items($user_id, $feed_id, array $items, $rtl = false, array $ignore_urls = array())
{
$items_in_feed = array();
$db = Database::getInstance('db');
$db->startTransaction();
foreach ($items as $item) {
if ($item->getId() && $item->getUrl()) {
$item_id = get_item_id_from_checksum($feed_id, $item->getId());
$values = array(
'title' => $item->getTitle(),
'url' => $item->getUrl(),
'updated' => $item->getDate()->getTimestamp(),
'author' => $item->getAuthor(),
'content' => Helper\bool_config('nocontent') ? '' : $item->getContent(),
'enclosure_url' => $item->getEnclosureUrl(),
'enclosure_type' => $item->getEnclosureType(),
'language' => $item->getLanguage(),
'rtl' => $rtl || Parser::isLanguageRTL($item->getLanguage()) ? 1 : 0,
);
if ($item_id > 0) {
if (in_array($item->getUrl(), $ignore_urls)) {
unset($values['content']);
}
$db
->table(TABLE)
->eq('user_id', $user_id)
->eq('feed_id', $feed_id)
->eq('id', $item_id)
->update($values);
} else {
$values['checksum'] = $item->getId();
$values['user_id'] = $user_id;
$values['feed_id'] = $feed_id;
$values['status'] = STATUS_UNREAD;
$item_id = $db->table(TABLE)->persist($values);
}
$items_in_feed[] = $item_id;
}
}
cleanup_feed_items($feed_id, $items_in_feed);
$db->closeTransaction();
}
function cleanup_feed_items($feed_id, array $items_in_feed)
{
if (! empty($items_in_feed)) {
$db = Database::getInstance('db');
$removed_items = $db
->table(TABLE)
->columns('id')
->notIn('id', $items_in_feed)
->eq('status', STATUS_REMOVED)
->eq('feed_id', $feed_id)
->desc('updated')
->findAllByColumn('id');
// Keep a buffer of 2 items
// It's workaround for buggy feeds (cache issue with some Wordpress plugins)
if (is_array($removed_items)) {
$items_to_remove = array_slice($removed_items, 2);
if (! empty($items_to_remove)) {
// Handle the case when there is a huge number of items to remove
// Sqlite have a limit of 1000 sql variables by default
// Avoid the error message "too many SQL variables"
// We remove old items by batch of 500 items
$chunks = array_chunk($items_to_remove, 500);
foreach ($chunks as $chunk) {
$db->table(TABLE)
->in('id', $chunk)
->eq('status', STATUS_REMOVED)
->eq('feed_id', $feed_id)
->remove();
}
}
}
}
}
function get_item_id_from_checksum($feed_id, $checksum)
{
return (int) Database::getInstance('db')
->table(TABLE)
->eq('feed_id', $feed_id)
->eq('checksum', $checksum)
->findOneColumn('id');
}
function get_item($user_id, $item_id)
{
2015-08-15 03:33:39 +02:00
return Database::getInstance('db')
->table('items')
->eq('user_id', $user_id)
->eq('id', $item_id)
->findOne();
}
function get_item_nav($user_id, array $item, $status = array(STATUS_UNREAD), $bookmark = array(1, 0), $feed_id = null, $group_id = null)
{
2015-08-15 03:33:39 +02:00
$query = Database::getInstance('db')
->table(TABLE)
->columns('id', 'status', 'title', 'bookmark')
->neq('status', STATUS_REMOVED)
->eq('user_id', $user_id)
->orderBy('updated', Helper\config('items_sorting_direction'))
->desc('id')
;
2016-04-18 01:44:45 +02:00
if ($feed_id) {
$query->eq('feed_id', $feed_id);
}
2016-04-18 01:44:45 +02:00
if ($group_id) {
$query->in('feed_id', Group\get_feed_ids_by_group($group_id));
2016-04-18 01:44:45 +02:00
}
$items = $query->findAll();
$next_item = null;
$previous_item = null;
2016-05-03 10:45:07 +02:00
for ($i = 0, $ilen = count($items); $i < $ilen; ++$i) {
if ($items[$i]['id'] == $item['id']) {
if ($i > 0) {
$j = $i - 1;
while ($j >= 0) {
if (in_array($items[$j]['status'], $status) && in_array($items[$j]['bookmark'], $bookmark)) {
$previous_item = $items[$j];
break;
}
2016-05-03 10:45:07 +02:00
--$j;
}
}
if ($i < ($ilen - 1)) {
$j = $i + 1;
while ($j < $ilen) {
if (in_array($items[$j]['status'], $status) && in_array($items[$j]['bookmark'], $bookmark)) {
$next_item = $items[$j];
break;
}
2016-05-03 10:45:07 +02:00
++$j;
}
}
break;
}
}
return array(
'next' => $next_item,
'previous' => $previous_item
);
}
function get_items_by_status($user_id, $status, $feed_ids = array(), $offset = null, $limit = null, $order_column = 'updated', $order_direction = 'desc')
{
2015-08-15 03:33:39 +02:00
return Database::getInstance('db')
->table(TABLE)
->columns(
'items.id',
'items.checksum',
'items.title',
'items.updated',
'items.url',
'items.enclosure_url',
'items.enclosure_type',
'items.bookmark',
'items.feed_id',
'items.status',
'items.content',
'items.language',
'items.rtl',
'items.author',
'feeds.site_url',
'feeds.title AS feed_title'
)
->join('feeds', 'id', 'feed_id')
->eq('items.user_id', $user_id)
->eq('items.status', $status)
->in('items.feed_id', $feed_ids)
->orderBy($order_column, $order_direction)
->offset($offset)
->limit($limit)
->findAll();
}
function get_items($user_id, $since_id = null, array $item_ids = array(), $limit = 50)
{
$query = Database::getInstance('db')
->table(TABLE)
->columns(
'items.id',
'items.checksum',
'items.title',
'items.updated',
'items.url',
'items.enclosure_url',
'items.enclosure_type',
'items.bookmark',
'items.feed_id',
'items.status',
'items.content',
'items.language',
'items.rtl',
'items.author',
'feeds.site_url',
'feeds.title AS feed_title'
)
->join('feeds', 'id', 'feed_id')
->eq('items.user_id', $user_id)
->neq('items.status', STATUS_REMOVED)
->limit($limit)
->asc('items.id');
if ($since_id !== null) {
$query->gt('items.id', $since_id);
} elseif (! empty($item_ids)) {
$query->in('items.id', $item_ids);
}
return $query->findAll();
}
function get_item_ids_by_status($user_id, $status)
{
2015-08-15 03:33:39 +02:00
return Database::getInstance('db')
->table(TABLE)
->eq('user_id', $user_id)
->eq('status', $status)
2016-12-30 00:04:56 +01:00
->asc('id')
->findAllByColumn('id');
}
function get_item_urls($user_id, $feed_id)
{
return Database::getInstance('db')
->table(TABLE)
->eq('user_id', $user_id)
->eq('feed_id', $feed_id)
->findAllByColumn('url');
}
2016-12-26 21:07:18 +01:00
function get_latest_unread_items_timestamps($user_id)
{
2015-08-15 03:33:39 +02:00
return Database::getInstance('db')
2016-12-26 21:07:18 +01:00
->table(TABLE)
->columns(
2016-12-26 21:07:18 +01:00
'feed_id',
'MAX(updated) as updated'
)
2016-12-26 21:07:18 +01:00
->eq('user_id', $user_id)
->eq('status', STATUS_UNREAD)
->groupBy('feed_id')
->desc('updated')
->findAll();
}
function count_by_status($user_id, $status, $feed_ids = array())
{
$query = Database::getInstance('db')
->table(TABLE)
->eq('user_id', $user_id)
->in('feed_id', $feed_ids);
if (is_array($status)) {
$query->in('status', $status);
} else {
$query->eq('status', $status);
}
return $query->count();
}
function autoflush_read($user_id)
{
$autoflush = Helper\int_config('autoflush');
if ($autoflush > 0) {
2015-08-15 03:33:39 +02:00
Database::getInstance('db')
->table(TABLE)
->eq('user_id', $user_id)
->eq('bookmark', 0)
->eq('status', STATUS_READ)
->lt('updated', strtotime('-'.$autoflush.'day'))
->save(array('status' => STATUS_REMOVED, 'content' => ''));
2016-04-18 01:44:45 +02:00
} elseif ($autoflush === -1) {
2015-08-15 03:33:39 +02:00
Database::getInstance('db')
->table(TABLE)
->eq('user_id', $user_id)
->eq('bookmark', 0)
->eq('status', STATUS_READ)
->save(array('status' => STATUS_REMOVED, 'content' => ''));
}
}
function autoflush_unread($user_id)
{
$autoflush = Helper\int_config('autoflush_unread');
if ($autoflush > 0) {
2015-08-15 03:33:39 +02:00
Database::getInstance('db')
->table(TABLE)
->eq('user_id', $user_id)
->eq('bookmark', 0)
->eq('status', STATUS_UNREAD)
->lt('updated', strtotime('-'.$autoflush.'day'))
->save(array('status' => STATUS_REMOVED, 'content' => ''));
}
}