Merge pull request 'Use ORM more in 'classes/pref/feeds.php'.' (#43) from wn/tt-rss:feature/pref-feeds-idiorm into master

Reviewed-on: https://git.tt-rss.org/fox/tt-rss/pulls/43
This commit is contained in:
fox 2021-08-18 11:07:04 +03:00
commit 5ed108dce4
1 changed files with 190 additions and 195 deletions

View File

@ -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')
->order_by_asc('title')
->find_many();
while ($line = $sth->fetch()) { foreach ($feed_categories as $feed_category) {
$cat = [
$cat = array(); 'id' => 'CAT:' . $feed_category->id,
$cat['id'] = 'CAT:' . $line['id']; 'bare_id' => (int)$feed_category->id,
$cat['bare_id'] = (int)$line['id']; 'name' => $feed_category->title,
$cat['name'] = $line['title']; 'items' => $this->get_category_items($feed_category->id),
$cat['items'] = array(); 'checkbox' => false,
$cat['checkbox'] = false; 'type' => 'category',
$cat['type'] = 'category'; 'unread' => -1,
$cat['unread'] = -1; 'child_unread' => -1,
$cat['child_unread'] = -1; 'auxcounter' => -1,
$cat['auxcounter'] = -1; 'parent_id' => $cat_id,
$cat['parent_id'] = $cat_id; ];
$cat['items'] = $this->get_category_items($line['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);
} }
$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 cat_id = :cat AND ->where(['cat_id' => $cat_id, 'owner_uid' => $_SESSION['uid']])
owner_uid = :uid AND ->order_by_asc('order_id')
(:search = '' OR (LOWER(title) LIKE :search OR LOWER(feed_url) LIKE :search)) ->order_by_asc('title');
ORDER BY order_id, title");
$fsth->execute([":cat" => $cat_id, ":uid" => $_SESSION['uid'], ":search" => $search ? "%$search%" : ""]); if ($search) {
$feeds_obj->where_raw('(LOWER(title) LIKE ? OR LOWER(feed_url) LIKE LOWER(?))', ["%$search%", "%$search%"]);
}
while ($feed_line = $fsth->fetch()) { foreach ($feeds_obj->find_many() as $feed) {
$feed = array(); array_push($items, [
$feed['id'] = 'FEED:' . $feed_line['id']; 'id' => 'FEED:' . $feed->id,
$feed['bare_id'] = (int)$feed_line['id']; 'bare_id' => (int) $feed->id,
$feed['auxcounter'] = -1; 'auxcounter' => -1,
$feed['name'] = $feed_line['title']; 'name' => $feed->title,
$feed['checkbox'] = false; 'checkbox' => false,
$feed['unread'] = -1; 'unread' => -1,
$feed['error'] = $feed_line['last_error']; 'error' => $feed->last_error,
$feed['icon'] = Feeds::_get_icon($feed_line['id']); 'icon' => Feeds::_get_icon($feed->id),
$feed['param'] = TimeHelper::make_local_datetime( 'param' => TimeHelper::make_local_datetime($feed->last_updated, true),
$feed_line['last_updated'], true); 'updates_disabled' => (int)($feed->update_interval < 0),
$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) {
$label_id = Labels::label_to_feed_id($label->id);
$feed = $this->feedlist_init_feed($label_id, false, 0);
$feed['fg_color'] = $label->fg_color;
$feed['bg_color'] = $label->bg_color;
array_push($cat['items'], $feed);
}
$feed = $this->feedlist_init_feed($label_id, false, 0);
$feed['fg_color'] = $line['fg_color'];
$feed['bg_color'] = $line['bg_color'];
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')
->order_by_asc('order_id')
->order_by_asc('title')
->find_many();
while ($line = $sth->fetch()) { foreach ($feed_categories as $feed_category) {
$cat = array(); $cat = [
$cat['id'] = 'CAT:' . $line['id']; 'id' => 'CAT:' . $feed_category->id,
$cat['bare_id'] = (int)$line['id']; 'bare_id' => (int) $feed_category->id,
$cat['auxcounter'] = -1; 'auxcounter' => -1,
$cat['name'] = $line['title']; 'name' => $feed_category->title,
$cat['items'] = array(); 'items' => $this->get_category_items($feed_category->id),
$cat['checkbox'] = false; 'checkbox' => false,
$cat['type'] = 'category'; 'type' => 'category',
$cat['unread'] = -1; 'unread' => -1,
$cat['child_unread'] = -1; 'child_unread' => -1,
];
$cat['items'] = $this->get_category_items($line['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);
@ -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,
];
$cat = array(); $feeds_obj = ORM::for_table('ttrss_feeds')
$cat['id'] = 'CAT:0'; ->select_many('id', 'title', 'last_error', 'update_interval')
$cat['bare_id'] = 0; ->select_expr(SUBSTRING_FOR_DATE.'(last_updated,1,19)', 'last_updated')
$cat['auxcounter'] = -1; ->where('owner_uid', $_SESSION['uid'])
$cat['name'] = __("Uncategorized"); ->where_null('cat_id')
$cat['items'] = array(); ->order_by_asc('order_id')
$cat['type'] = 'category'; ->order_by_asc('title');
$cat['checkbox'] = false;
$cat['unread'] = -1;
$cat['child_unread'] = -1;
$fsth = $this->pdo->prepare("SELECT id, title,last_error, if ($search) {
".SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated, update_interval $feeds_obj->where_raw('(LOWER(title) LIKE ? OR LOWER(feed_url) LIKE LOWER(?))', ["%$search%", "%$search%"]);
FROM ttrss_feeds }
WHERE cat_id IS NULL AND
owner_uid = :uid AND
(:search = '' OR (LOWER(title) LIKE :search OR LOWER(feed_url) LIKE :search))
ORDER BY order_id, title");
$fsth->execute([":uid" => $_SESSION['uid'], ":search" => $search ? "%$search%" : ""]);
while ($feed_line = $fsth->fetch()) { foreach ($feeds_obj->find_many() as $feed) {
$feed = array(); array_push($cat['items'], [
$feed['id'] = 'FEED:' . $feed_line['id']; 'id' => 'FEED:' . $feed->id,
$feed['bare_id'] = (int)$feed_line['id']; 'bare_id' => (int) $feed->id,
$feed['auxcounter'] = -1; 'auxcounter' => -1,
$feed['name'] = $feed_line['title']; 'name' => $feed->title,
$feed['checkbox'] = false; 'checkbox' => false,
$feed['error'] = $feed_line['last_error']; 'error' => $feed->last_error,
$feed['icon'] = Feeds::_get_icon($feed_line['id']); 'icon' => Feeds::_get_icon($feed->id),
$feed['param'] = TimeHelper::make_local_datetime( 'param' => TimeHelper::make_local_datetime($feed->last_updated, true),
$feed_line['last_updated'], true); 'unread' => -1,
$feed['unread'] = -1; 'type' => 'feed',
$feed['type'] = 'feed'; 'updates_disabled' => (int)($feed->update_interval < 0),
$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()) { if ($search) {
$feed = array(); $feeds_obj->where_raw('(LOWER(title) LIKE ? OR LOWER(feed_url) LIKE LOWER(?))', ["%$search%", "%$search%"]);
$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); foreach ($feeds_obj->find_many() as $feed) {
array_push($cat['items'], [
'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),
]);
} }
$root['param'] = sprintf(_ngettext('(%d feed)', '(%d feeds)', count($root['items'])), count($root['items'])); $root['param'] = sprintf(_ngettext('(%d feed)', '(%d feeds)', count($root['items'])), count($root['items']));
} }
$fl = array(); return [
$fl['identifier'] = 'id'; 'identifier' => 'id',
$fl['label'] = 'name'; 'label' => 'name',
'items' => clean($_REQUEST['mode'] ?? 0) != 2 ? [$root] : $root['items'],
if (clean($_REQUEST['mode'] ?? 0) != 2) { ];
$fl['items'] = array($root);
} else {
$fl['items'] = $root['items'];
}
return $fl;
} }
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')
->group_by('f.id')
->group_by('f.site_url')
->group_by('f.feed_url')
->order_by_asc('last_article')
->find_array();
$rv = []; foreach ($inactive_feeds as $inactive_feed) {
$inactive_feed['last_article'] = TimeHelper::make_local_datetime($inactive_feed['last_article'], false);
while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
$row['last_article'] = TimeHelper::make_local_datetime($row['last_article'], false);
array_push($rv, $row);
} }
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) {