maintenance.php: use ReportQueries

This commit is contained in:
Zankaria 2024-12-11 16:34:20 +01:00
parent 5b231099b0
commit d97c7f271e

View file

@ -3,57 +3,11 @@
* Performs maintenance tasks. Invoke this periodically if the auto_maintenance configuration option is turned off.
*/
use Vichan\Data\ReportQueries;
require dirname(__FILE__) . '/inc/cli.php';
function get_reports_by_board(): array {
$query = prepare("SELECT `board`, `post`, `id` FROM ``reports``");
$query->execute() or error(db_error($query));
return $query->fetchAll(PDO::FETCH_GROUP | PDO::FETCH_NUM);
}
function post_ids_by_board(array $reports): array {
$ret = [];
foreach ($reports as $board => $values) {
foreach ($values as $value) {
$ret[$board] ??= [];
$ret[$board][] = $value[0];
}
}
return $ret;
}
function filter_invalid_reports(array $board_post_ids, array $reports): array {
$invalid_reports = [];
foreach ($board_post_ids as $board => $post_ids) {
$query = query(sprintf('SELECT `id` FROM ``posts_%s`` WHERE `id` = ' . implode(' OR `id` = ', $post_ids), $board));
$existing_posts = $query->fetchAll(PDO::FETCH_COLUMN);
foreach ($reports[$board] as $values) {
list($post_id, $report_id) = $values;
if (!in_array($post_id, $existing_posts)) {
$invalid_reports[] = $report_id;
}
}
}
return $invalid_reports;
}
function get_report_ids(array $reports): array {
$ret = [];
foreach ($reports as $_board => $values) {
foreach ($values as $value) {
$report_id = $value[0];
$ret[] = $report_id;
}
}
return $ret;
}
function delete_reports(array $report_ids): int {
return query('DELETE FROM ``reports`` WHERE `id` = ' . implode(' OR `id` = ', $report_ids))->rowCount();
}
$ctx = Vichan\build_context($config);
echo "Clearing expired bans...\n";
$start = microtime(true);
@ -72,16 +26,9 @@ $time_tot = $delta;
$deleted_tot += $deleted_count;
echo "Clearing invalid reports...\n";
$report_queries = $ctx->get(ReportQueries::class);
$start = microtime(true);
$reports = get_reports_by_board();
$report_ids = get_report_ids($reports);
$board_post_ids = post_ids_by_board($reports);
$invalid_reports = filter_invalid_reports($board_post_ids, $reports);
if (!empty($invalid_reports)) {
$deleted_count = delete_reports($invalid_reports);
} else {
$deleted_count = 0;
}
$deleted_count = $report_queries->purge();
$delta = microtime(true) - $start;
echo "Deleted $deleted_count invalid reports in $delta seconds!\n";
$time_tot += $delta;