Use ORM in some more parts of 'update.php'.

This commit is contained in:
wn_ 2021-08-15 21:20:15 +00:00
parent 73d14338ab
commit cf0ec06b8c
1 changed files with 32 additions and 37 deletions

View File

@ -30,36 +30,31 @@
$days = (int) $days; $days = (int) $days;
if (Config::get(Config::DB_TYPE) == "pgsql") { if (Config::get(Config::DB_TYPE) == "pgsql") {
$interval_query = "date_updated < NOW() - INTERVAL '$days days'"; $interval_query = "e.date_updated < NOW() - INTERVAL '$days days'";
} else /*if (Config::get(Config::DB_TYPE) == "mysql") */ { } else /*if (Config::get(Config::DB_TYPE) == "mysql") */ {
$interval_query = "date_updated < DATE_SUB(NOW(), INTERVAL $days DAY)"; $interval_query = "e.date_updated < DATE_SUB(NOW(), INTERVAL $days DAY)";
} }
$tags_deleted = 0; $tags_deleted = 0;
$limit_part = 500;
$pdo = Db::pdo();
while ($limit > 0) { while ($limit > 0) {
$limit_part = 500; $tags = ORM::for_table('ttrss_tags')
->table_alias('t')
->select('t.id')
->join('ttrss_user_entries', ['ue.int_id', '=', 't.post_int_id'], 'ue')
->join('ttrss_entries', ['e.id', '=', 'ue.ref_id'], 'e')
->where_not_equal('ue.tag_cache', '')
->where_raw($interval_query)
->limit($limit_part)
->find_many();
$sth = $pdo->prepare("SELECT ttrss_tags.id AS id if (count($tags)) {
FROM ttrss_tags, ttrss_user_entries, ttrss_entries ORM::for_table('ttrss_tags')
WHERE post_int_id = int_id AND $interval_query AND ->where_id_in(array_column($tags->as_array(), 'id'))
ref_id = ttrss_entries.id AND tag_cache != '' LIMIT ?"); ->delete_many();
$sth->bindValue(1, $limit_part, PDO::PARAM_INT);
$sth->execute();
$ids = array(); $tags_deleted += ORM::get_last_statement()->rowCount();
while ($line = $sth->fetch()) {
array_push($ids, $line['id']);
}
if (count($ids) > 0) {
$ids = join(",", $ids);
$usth = $pdo->query("DELETE FROM ttrss_tags WHERE id IN ($ids)");
$tags_deleted = $usth->rowCount();
} else { } else {
break; break;
} }
@ -290,29 +285,29 @@
if (isset($options["gen-search-idx"])) { if (isset($options["gen-search-idx"])) {
echo "Generating search index (stemming set to English)...\n"; echo "Generating search index (stemming set to English)...\n";
$res = $pdo->query("SELECT COUNT(id) AS count FROM ttrss_entries WHERE tsvector_combined IS NULL"); $count = ORM::for_table('ttrss_entries')
$row = $res->fetch(); ->where_null('tsvector_combined')
$count = $row['count']; ->count();
print "Articles to process: $count.\n";
$limit = 500; $limit = 500;
$processed = 0; $processed = 0;
$sth = $pdo->prepare("SELECT id, title, content FROM ttrss_entries WHERE print "Articles to process: $count (will limit to $limit).\n";
tsvector_combined IS NULL ORDER BY id LIMIT ?");
$sth->execute([$limit]); $entries = ORM::for_table('ttrss_entries')
->select_many('id', 'title', 'content')
->where_null('tsvector_combined')
->order_by_asc('id')
->limit($limit)
->find_many();
$usth = $pdo->prepare("UPDATE ttrss_entries $usth = $pdo->prepare("UPDATE ttrss_entries
SET tsvector_combined = to_tsvector('english', ?) WHERE id = ?"); SET tsvector_combined = to_tsvector('english', ?) WHERE id = ?");
while (true) { while (true) {
foreach ($entries as $entry) {
while ($line = $sth->fetch()) { $tsvector_combined = mb_substr(strip_tags($entry->title . " " . $entry->content), 0, 1000000);
$tsvector_combined = mb_substr(strip_tags($line["title"] . " " . $line["content"]), 0, 1000000); $usth->execute([$tsvector_combined, $entry->id]);
$usth->execute([$tsvector_combined, $line['id']]);
$processed++; $processed++;
} }
@ -366,7 +361,7 @@
if (isset($options["user-list"])) { if (isset($options["user-list"])) {
$users = ORM::for_table('ttrss_users') $users = ORM::for_table('ttrss_users')
->order_by_expr('id') ->order_by_asc('id')
->find_many(); ->find_many();
foreach ($users as $user) { foreach ($users as $user) {