Use ORM more in 'classes/pref/feeds.php'.

master
wn_ 3 years ago
parent a73e3bec45
commit 992e9cd9e3

@ -12,19 +12,12 @@ class Pref_Feeds extends Handler_Protected {
} }
public static function get_ts_languages() { public static function get_ts_languages() {
$rv = []; if (Config::get(Config::DB_TYPE) == 'pgsql') {
return array_map('ucfirst',
if (Config::get(Config::DB_TYPE) == "pgsql") { array_column(ORM::for_table('pg_ts_config')->select('cfgname')->find_array(), 'cfgname'));
$dbh = Db::pdo();
$res = $dbh->query("SELECT cfgname FROM pg_ts_config");
while ($row = $res->fetch()) {
array_push($rv, ucfirst($row['cfgname']));
}
} }
return $rv; return [];
} }
function renameCat() { function renameCat() {
@ -51,61 +44,60 @@ class Pref_Feeds extends Handler_Protected {
$show_empty_cats = clean($_REQUEST['force_show_empty'] ?? false) || $show_empty_cats = clean($_REQUEST['force_show_empty'] ?? false) ||
(clean($_REQUEST['mode'] ?? 0) != 2 && !$search); (clean($_REQUEST['mode'] ?? 0) != 2 && !$search);
$items = array(); $items = [];
$sth = $this->pdo->prepare("SELECT id, title FROM ttrss_feed_categories $feed_categories = ORM::for_table('ttrss_feed_categories')
WHERE owner_uid = ? AND parent_cat = ? ORDER BY order_id, title"); ->select_many('id', 'title')
$sth->execute([$_SESSION['uid'], $cat_id]); ->where(['owner_uid' => $_SESSION['uid'], 'parent_cat' => $cat_id])
->order_by_asc('order_id')
while ($line = $sth->fetch()) { ->order_by_asc('title')
->find_many();
$cat = array();
$cat['id'] = 'CAT:' . $line['id']; foreach ($feed_categories as $feed_category) {
$cat['bare_id'] = (int)$line['id']; $cat = [
$cat['name'] = $line['title']; 'id' => 'CAT:' . $feed_category->id,
$cat['items'] = array(); 'bare_id' => (int)$feed_category->id,
$cat['checkbox'] = false; 'name' => $feed_category->title,
$cat['type'] = 'category'; 'items' => $this->get_category_items($feed_category->id),
$cat['unread'] = -1; 'checkbox' => false,
$cat['child_unread'] = -1; 'type' => 'category',
$cat['auxcounter'] = -1; 'unread' => -1,
$cat['parent_id'] = $cat_id; 'child_unread' => -1,
'auxcounter' => -1,
$cat['items'] = $this->get_category_items($line['id']); 'parent_id' => $cat_id,
];
$num_children = $this->calculate_children_count($cat); $num_children = $this->calculate_children_count($cat);
$cat['param'] = sprintf(_ngettext('(%d feed)', '(%d feeds)', (int) $num_children), $num_children); $cat['param'] = sprintf(_ngettext('(%d feed)', '(%d feeds)', (int) $num_children), $num_children);
if ($num_children > 0 || $show_empty_cats) if ($num_children > 0 || $show_empty_cats)
array_push($items, $cat); array_push($items, $cat);
}
$feeds_obj = ORM::for_table('ttrss_feeds')
->select_many('id', 'title', 'last_error', 'update_interval')
->select_expr(SUBSTRING_FOR_DATE.'(last_updated,1,19)', 'last_updated')
->where(['cat_id' => $cat_id, 'owner_uid' => $_SESSION['uid']])
->order_by_asc('order_id')
->order_by_asc('title');
if ($search) {
$feeds_obj->where_raw('(LOWER(title) LIKE ? OR LOWER(feed_url) LIKE ?)', ["%$search%", "%$search%"]);
} }
$fsth = $this->pdo->prepare("SELECT id, title, last_error, foreach ($feeds_obj->find_many() as $feed) {
".SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated, update_interval array_push($items, [
FROM ttrss_feeds 'id' => 'FEED:' . $feed->id,
WHERE cat_id = :cat AND 'bare_id' => (int) $feed->id,
owner_uid = :uid AND 'auxcounter' => -1,
(:search = '' OR (LOWER(title) LIKE :search OR LOWER(feed_url) LIKE :search)) 'name' => $feed->title,
ORDER BY order_id, title"); 'checkbox' => false,
'unread' => -1,
$fsth->execute([":cat" => $cat_id, ":uid" => $_SESSION['uid'], ":search" => $search ? "%$search%" : ""]); 'error' => $feed->last_error,
'icon' => Feeds::_get_icon($feed->id),
while ($feed_line = $fsth->fetch()) { 'param' => TimeHelper::make_local_datetime($feed->last_updated, true),
$feed = array(); 'updates_disabled' => (int)($feed->update_interval < 0),
$feed['id'] = 'FEED:' . $feed_line['id']; ]);
$feed['bare_id'] = (int)$feed_line['id'];
$feed['auxcounter'] = -1;
$feed['name'] = $feed_line['title'];
$feed['checkbox'] = false;
$feed['unread'] = -1;
$feed['error'] = $feed_line['last_error'];
$feed['icon'] = Feeds::_get_icon($feed_line['id']);
$feed['param'] = TimeHelper::make_local_datetime(
$feed_line['last_updated'], true);
$feed['updates_disabled'] = (int)($feed_line['update_interval'] < 0);
array_push($items, $feed);
} }
return $items; return $items;
@ -181,24 +173,22 @@ class Pref_Feeds extends Handler_Protected {
if (get_pref(Prefs::ENABLE_FEED_CATS)) { if (get_pref(Prefs::ENABLE_FEED_CATS)) {
$cat = $this->feedlist_init_cat(-2); $cat = $this->feedlist_init_cat(-2);
} else { } else {
$cat['items'] = array(); $cat['items'] = [];
} }
$num_labels = 0; $labels = ORM::for_table('ttrss_labels2')
while ($line = $sth->fetch()) { ->where('owner_uid', $_SESSION['uid'])
++$num_labels; ->find_many();
$label_id = Labels::label_to_feed_id($line['id']); if (count($labels)) {
foreach ($labels as $label) {
$feed = $this->feedlist_init_feed($label_id, false, 0); $label_id = Labels::label_to_feed_id($label->id);
$feed = $this->feedlist_init_feed($label_id, false, 0);
$feed['fg_color'] = $line['fg_color']; $feed['fg_color'] = $label->fg_color;
$feed['bg_color'] = $line['bg_color']; $feed['bg_color'] = $label->bg_color;
array_push($cat['items'], $feed);
array_push($cat['items'], $feed); }
}
if ($num_labels) {
if ($enable_cats) { if ($enable_cats) {
array_push($root['items'], $cat); array_push($root['items'], $cat);
} else { } else {
@ -211,23 +201,26 @@ class Pref_Feeds extends Handler_Protected {
$show_empty_cats = clean($_REQUEST['force_show_empty'] ?? false) || $show_empty_cats = clean($_REQUEST['force_show_empty'] ?? false) ||
(clean($_REQUEST['mode'] ?? 0) != 2 && !$search); (clean($_REQUEST['mode'] ?? 0) != 2 && !$search);
$sth = $this->pdo->prepare("SELECT id, title FROM ttrss_feed_categories $feed_categories = ORM::for_table('ttrss_feed_categories')
WHERE owner_uid = ? AND parent_cat IS NULL ORDER BY order_id, title"); ->select_many('id', 'title')
$sth->execute([$_SESSION['uid']]); ->where('owner_uid', $_SESSION['uid'])
->where_null('parent_cat')
while ($line = $sth->fetch()) { ->order_by_asc('order_id')
$cat = array(); ->order_by_asc('title')
$cat['id'] = 'CAT:' . $line['id']; ->find_many();
$cat['bare_id'] = (int)$line['id'];
$cat['auxcounter'] = -1; foreach ($feed_categories as $feed_category) {
$cat['name'] = $line['title']; $cat = [
$cat['items'] = array(); 'id' => 'CAT:' . $feed_category->id,
$cat['checkbox'] = false; 'bare_id' => (int) $feed_category->id,
$cat['type'] = 'category'; 'auxcounter' => -1,
$cat['unread'] = -1; 'name' => $feed_category->title,
$cat['child_unread'] = -1; 'items' => $this->get_category_items($feed_category->id),
'checkbox' => false,
$cat['items'] = $this->get_category_items($line['id']); 'type' => 'category',
'unread' => -1,
'child_unread' => -1,
];
$num_children = $this->calculate_children_count($cat); $num_children = $this->calculate_children_count($cat);
$cat['param'] = sprintf(_ngettext('(%d feed)', '(%d feeds)', (int) $num_children), $num_children); $cat['param'] = sprintf(_ngettext('(%d feed)', '(%d feeds)', (int) $num_children), $num_children);
@ -239,43 +232,44 @@ class Pref_Feeds extends Handler_Protected {
} }
/* Uncategorized is a special case */ /* Uncategorized is a special case */
$cat = [
'id' => 'CAT:0',
'bare_id' => 0,
'auxcounter' => -1,
'name' => __('Uncategorized'),
'items' => [],
'type' => 'category',
'checkbox' => false,
'unread' => -1,
'child_unread' => -1,
];
$feeds_obj = ORM::for_table('ttrss_feeds')
->select_many('id', 'title', 'last_error', 'update_interval')
->select_expr(SUBSTRING_FOR_DATE.'(last_updated,1,19)', 'last_updated')
->where('owner_uid', $_SESSION['uid'])
->where_null('cat_id')
->order_by_asc('order_id')
->order_by_asc('title');
$cat = array(); if ($search) {
$cat['id'] = 'CAT:0'; $feeds_obj->where_raw('(LOWER(title) LIKE ? OR LOWER(feed_url) LIKE ?)', ["%$search%", "%$search%"]);
$cat['bare_id'] = 0; }
$cat['auxcounter'] = -1;
$cat['name'] = __("Uncategorized"); foreach ($feeds_obj->find_many() as $feed) {
$cat['items'] = array(); array_push($cat['items'], [
$cat['type'] = 'category'; 'id' => 'FEED:' . $feed->id,
$cat['checkbox'] = false; 'bare_id' => (int) $feed->id,
$cat['unread'] = -1; 'auxcounter' => -1,
$cat['child_unread'] = -1; 'name' => $feed->title,
'checkbox' => false,
$fsth = $this->pdo->prepare("SELECT id, title,last_error, 'error' => $feed->last_error,
".SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated, update_interval 'icon' => Feeds::_get_icon($feed->id),
FROM ttrss_feeds 'param' => TimeHelper::make_local_datetime($feed->last_updated, true),
WHERE cat_id IS NULL AND 'unread' => -1,
owner_uid = :uid AND 'type' => 'feed',
(:search = '' OR (LOWER(title) LIKE :search OR LOWER(feed_url) LIKE :search)) 'updates_disabled' => (int)($feed->update_interval < 0),
ORDER BY order_id, title"); ]);
$fsth->execute([":uid" => $_SESSION['uid'], ":search" => $search ? "%$search%" : ""]);
while ($feed_line = $fsth->fetch()) {
$feed = array();
$feed['id'] = 'FEED:' . $feed_line['id'];
$feed['bare_id'] = (int)$feed_line['id'];
$feed['auxcounter'] = -1;
$feed['name'] = $feed_line['title'];
$feed['checkbox'] = false;
$feed['error'] = $feed_line['last_error'];
$feed['icon'] = Feeds::_get_icon($feed_line['id']);
$feed['param'] = TimeHelper::make_local_datetime(
$feed_line['last_updated'], true);
$feed['unread'] = -1;
$feed['type'] = 'feed';
$feed['updates_disabled'] = (int)($feed_line['update_interval'] < 0);
array_push($cat['items'], $feed);
} }
$cat['param'] = sprintf(_ngettext('(%d feed)', '(%d feeds)', count($cat['items'])), count($cat['items'])); $cat['param'] = sprintf(_ngettext('(%d feed)', '(%d feeds)', count($cat['items'])), count($cat['items']));
@ -287,46 +281,41 @@ class Pref_Feeds extends Handler_Protected {
$root['param'] = sprintf(_ngettext('(%d feed)', '(%d feeds)', (int) $num_children), $num_children); $root['param'] = sprintf(_ngettext('(%d feed)', '(%d feeds)', (int) $num_children), $num_children);
} else { } else {
$fsth = $this->pdo->prepare("SELECT id, title, last_error, $feeds_obj = ORM::for_table('ttrss_feeds')
".SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated, update_interval ->select_many('id', 'title', 'last_error', 'update_interval')
FROM ttrss_feeds ->select_expr(SUBSTRING_FOR_DATE.'(last_updated,1,19)', 'last_updated')
WHERE owner_uid = :uid AND ->where('owner_uid', $_SESSION['uid'])
(:search = '' OR (LOWER(title) LIKE :search OR LOWER(feed_url) LIKE :search)) ->order_by_asc('order_id')
ORDER BY order_id, title"); ->order_by_asc('title');
$fsth->execute([":uid" => $_SESSION['uid'], ":search" => $search ? "%$search%" : ""]);
while ($feed_line = $fsth->fetch()) {
$feed = array();
$feed['id'] = 'FEED:' . $feed_line['id'];
$feed['bare_id'] = (int)$feed_line['id'];
$feed['auxcounter'] = -1;
$feed['name'] = $feed_line['title'];
$feed['checkbox'] = false;
$feed['error'] = $feed_line['last_error'];
$feed['icon'] = Feeds::_get_icon($feed_line['id']);
$feed['param'] = TimeHelper::make_local_datetime(
$feed_line['last_updated'], true);
$feed['unread'] = -1;
$feed['type'] = 'feed';
$feed['updates_disabled'] = (int)($feed_line['update_interval'] < 0);
array_push($root['items'], $feed);
}
$root['param'] = sprintf(_ngettext('(%d feed)', '(%d feeds)', count($root['items'])), count($root['items'])); if ($search) {
} $feeds_obj->where_raw('(LOWER(title) LIKE ? OR LOWER(feed_url) LIKE ?)', ["%$search%", "%$search%"]);
}
$fl = array(); foreach ($feeds_obj->find_many() as $feed) {
$fl['identifier'] = 'id'; array_push($cat['items'], [
$fl['label'] = 'name'; 'id' => 'FEED:' . $feed->id,
'bare_id' => (int) $feed->id,
'auxcounter' => -1,
'name' => $feed->title,
'checkbox' => false,
'error' => $feed->last_error,
'icon' => Feeds::_get_icon($feed->id),
'param' => TimeHelper::make_local_datetime($feed->last_updated, true),
'unread' => -1,
'type' => 'feed',
'updates_disabled' => (int)($feed->update_interval < 0),
]);
}
if (clean($_REQUEST['mode'] ?? 0) != 2) { $root['param'] = sprintf(_ngettext('(%d feed)', '(%d feeds)', count($root['items'])), count($root['items']));
$fl['items'] = array($root);
} else {
$fl['items'] = $root['items'];
} }
return $fl; return [
'identifier' => 'id',
'label' => 'name',
'items' => clean($_REQUEST['mode'] ?? 0) != 2 ? [$root] : $root['items'],
];
} }
function catsortreset() { function catsortreset() {
@ -359,10 +348,14 @@ class Pref_Feeds extends Handler_Protected {
$parent_qpart = null; $parent_qpart = null;
} }
$sth = $this->pdo->prepare("UPDATE ttrss_feed_categories $feed_category = ORM::for_table('ttrss_feed_categories')
SET parent_cat = ? WHERE id = ? AND ->where('owner_uid', $_SESSION['uid'])
owner_uid = ?"); ->find_one($bare_item_id);
$sth->execute([$parent_qpart, $bare_item_id, $_SESSION['uid']]);
if ($feed_category) {
$feed_category->parent_cat = $parent_qpart;
$feed_category->save();
}
} }
$order_id = 1; $order_id = 1;
@ -380,22 +373,27 @@ class Pref_Feeds extends Handler_Protected {
if (strpos($id, "FEED") === 0) { if (strpos($id, "FEED") === 0) {
$cat_id = ($item_id != "root") ? $bare_item_id : null; $feed = ORM::for_table('ttrss_feeds')
->where('owner_uid', $_SESSION['uid'])
$sth = $this->pdo->prepare("UPDATE ttrss_feeds ->find_one($bare_id);
SET order_id = ?, cat_id = ?
WHERE id = ? AND owner_uid = ?");
$sth->execute([$order_id, $cat_id ? $cat_id : null, $bare_id, $_SESSION['uid']]);
if ($feed) {
$feed->order_id = $order_id;
$feed->cat_id = ($item_id != "root" && $bare_item_id) ? $bare_item_id : null;
$feed->save();
}
} else if (strpos($id, "CAT:") === 0) { } else if (strpos($id, "CAT:") === 0) {
$this->process_category_order($data_map, $item['_reference'], $item_id, $this->process_category_order($data_map, $item['_reference'], $item_id,
$nest_level+1); $nest_level+1);
$sth = $this->pdo->prepare("UPDATE ttrss_feed_categories $feed_category = ORM::for_table('ttrss_feed_categories')
SET order_id = ? WHERE id = ? AND ->where('owner_uid', $_SESSION['uid'])
owner_uid = ?"); ->find_one($bare_id);
$sth->execute([$order_id, $bare_id, $_SESSION['uid']]);
if ($feed_category) {
$feed_category->order_id = $order_id;
$feed_category->save();
}
} }
} }
@ -1120,41 +1118,38 @@ class Pref_Feeds extends Handler_Protected {
$interval_qpart = "DATE_SUB(NOW(), INTERVAL 3 MONTH)"; $interval_qpart = "DATE_SUB(NOW(), INTERVAL 3 MONTH)";
} }
$sth = $this->pdo->prepare("SELECT ttrss_feeds.title, ttrss_feeds.site_url, $inactive_feeds = ORM::for_table('ttrss_feeds')
ttrss_feeds.feed_url, ttrss_feeds.id, MAX(updated) AS last_article ->table_alias('f')
FROM ttrss_feeds, ttrss_entries, ttrss_user_entries WHERE ->select_many('f.id', 'f.title', 'f.site_url', 'f.feed_url')
(SELECT MAX(updated) FROM ttrss_entries, ttrss_user_entries WHERE ->select_expr('MAX(e.updated)', 'last_article')
ttrss_entries.id = ref_id AND ->join('ttrss_user_entries', [ 'ue.feed_id', '=', 'f.id'], 'ue')
ttrss_user_entries.feed_id = ttrss_feeds.id) < $interval_qpart ->join('ttrss_entries', ['e.id', '=', 'ue.ref_id'], 'e')
AND ttrss_feeds.owner_uid = ? AND ->where('f.owner_uid', $_SESSION['uid'])
ttrss_user_entries.feed_id = ttrss_feeds.id AND ->where_raw(
ttrss_entries.id = ref_id "(SELECT MAX(ttrss_entries.updated)
GROUP BY ttrss_feeds.title, ttrss_feeds.id, ttrss_feeds.site_url, ttrss_feeds.feed_url FROM ttrss_entries
ORDER BY last_article"); JOIN ttrss_user_entries ON ttrss_entries.id = ttrss_user_entries.ref_id
$sth->execute([$_SESSION['uid']]); WHERE ttrss_user_entries.feed_id = f.id) < $interval_qpart")
->group_by('f.title')
$rv = []; ->group_by('f.id')
->group_by('f.site_url')
while ($row = $sth->fetch(PDO::FETCH_ASSOC)) { ->group_by('f.feed_url')
$row['last_article'] = TimeHelper::make_local_datetime($row['last_article'], false); ->order_by_asc('last_article')
array_push($rv, $row); ->find_array();
foreach ($inactive_feeds as $inactive_feed) {
$inactive_feed['last_article'] = TimeHelper::make_local_datetime($inactive_feed['last_article'], false);
} }
print json_encode($rv); print json_encode($inactive_feeds);
} }
function feedsWithErrors() { function feedsWithErrors() {
$sth = $this->pdo->prepare("SELECT id,title,feed_url,last_error,site_url print json_encode(ORM::for_table('ttrss_feeds')
FROM ttrss_feeds WHERE last_error != '' AND owner_uid = ?"); ->select_many('id', 'title', 'feed_url', 'last_error', 'site_url')
$sth->execute([$_SESSION['uid']]); ->where_not_equal('last_error', '')
->where('owner_uid', $_SESSION['uid'])
$rv = []; ->find_array());
while ($row = $sth->fetch()) {
array_push($rv, $row);
}
print json_encode($rv);
} }
static function remove_feed($id, $owner_uid) { static function remove_feed($id, $owner_uid) {

Loading…
Cancel
Save