maintenance.php: use tabs

This commit is contained in:
Zankaria 2024-11-12 13:47:21 +01:00
parent d7db185129
commit 82391e0f83

View file

@ -6,53 +6,53 @@
require dirname(__FILE__) . '/inc/cli.php'; require dirname(__FILE__) . '/inc/cli.php';
function get_reports_by_board(): array { function get_reports_by_board(): array {
$query = prepare("SELECT `board`, `post`, `id` FROM ``reports``"); $query = prepare("SELECT `board`, `post`, `id` FROM ``reports``");
$query->execute() or error(db_error($query)); $query->execute() or error(db_error($query));
return $query->fetchAll(PDO::FETCH_GROUP | PDO::FETCH_NUM); return $query->fetchAll(PDO::FETCH_GROUP | PDO::FETCH_NUM);
} }
function post_ids_by_board(array $reports): array { function post_ids_by_board(array $reports): array {
$ret = []; $ret = [];
foreach ($reports as $board => $values) { foreach ($reports as $board => $values) {
foreach ($values as $value) { foreach ($values as $value) {
$ret[$board] ??= []; $ret[$board] ??= [];
$ret[$board][] = $value[0]; $ret[$board][] = $value[0];
} }
} }
return $ret; return $ret;
} }
function filter_invalid_reports(array $board_post_ids, array $reports): array { function filter_invalid_reports(array $board_post_ids, array $reports): array {
$invalid_reports = []; $invalid_reports = [];
foreach ($board_post_ids as $board => $post_ids) { foreach ($board_post_ids as $board => $post_ids) {
$query = query(sprintf('SELECT `id` FROM ``posts_%s`` WHERE `id` = ' . implode(' OR `id` = ', $post_ids), $board)); $query = query(sprintf('SELECT `id` FROM ``posts_%s`` WHERE `id` = ' . implode(' OR `id` = ', $post_ids), $board));
$existing_posts = $query->fetchAll(PDO::FETCH_COLUMN); $existing_posts = $query->fetchAll(PDO::FETCH_COLUMN);
foreach ($reports[$board] as $values) { foreach ($reports[$board] as $values) {
list($post_id, $report_id) = $values; list($post_id, $report_id) = $values;
if (!in_array($post_id, $existing_posts)) { if (!in_array($post_id, $existing_posts)) {
$invalid_reports[] = $report_id; $invalid_reports[] = $report_id;
} }
} }
} }
return $invalid_reports; return $invalid_reports;
} }
function get_report_ids(array $reports): array { function get_report_ids(array $reports): array {
$ret = []; $ret = [];
foreach ($reports as $_board => $values) { foreach ($reports as $_board => $values) {
foreach ($values as $value) { foreach ($values as $value) {
$report_id = $value[0]; $report_id = $value[0];
$ret[] = $report_id; $ret[] = $report_id;
} }
} }
return $ret; return $ret;
} }
function delete_reports(array $report_ids): int { function delete_reports(array $report_ids): int {
return query('DELETE FROM ``reports`` WHERE `id` = ' . implode(' OR `id` = ', $report_ids))->rowCount(); return query('DELETE FROM ``reports`` WHERE `id` = ' . implode(' OR `id` = ', $report_ids))->rowCount();
} }
echo "Clearing expired bans...\n"; echo "Clearing expired bans...\n";
@ -78,9 +78,9 @@ $report_ids = get_report_ids($reports);
$board_post_ids = post_ids_by_board($reports); $board_post_ids = post_ids_by_board($reports);
$invalid_reports = filter_invalid_reports($board_post_ids, $reports); $invalid_reports = filter_invalid_reports($board_post_ids, $reports);
if (!empty($invalid_reports)) { if (!empty($invalid_reports)) {
$deleted_count = delete_reports($invalid_reports); $deleted_count = delete_reports($invalid_reports);
} else { } else {
$deleted_count = 0; $deleted_count = 0;
} }
$delta = microtime(true) - $start; $delta = microtime(true) - $start;
echo "Deleted $deleted_count invalid reports in $delta seconds!\n"; echo "Deleted $deleted_count invalid reports in $delta seconds!\n";