#!/usr/bin/env php getSystemUser(); if (!$system) { fwrite(STDERR, "No system user found\n"); exit(1); } $systemId = (int)$system['user_id']; /** The reporter a ticket certainly came from, or null. */ function detectSource(array $t): ?string { $title = (string)$t['title']; $description = (string)$t['description']; if (preg_match('/[┏┃┗━┣]/u', $description)) { return 'hwmonDaemon'; } if ( stripos($title, '[auto]') !== false && preg_match('/automatically by Gandalf|from the Gandalf monitor/i', $description) ) { return 'gandalf'; } return null; } $stmt = $conn->prepare( "SELECT ticket_id, title, description FROM tickets WHERE created_by = ? AND source IS NULL ORDER BY ticket_id" ); $stmt->bind_param('i', $systemId); $stmt->execute(); $rows = $stmt->get_result()->fetch_all(MYSQLI_ASSOC); $stmt->close(); $update = $conn->prepare("UPDATE tickets SET source = ? WHERE ticket_id = ? AND source IS NULL"); $audit = new AuditLogModel($conn); $counts = []; $unknown = []; foreach ($rows as $row) { $source = detectSource($row); if ($source === null) { $unknown[] = $row; continue; } $counts[$source] = ($counts[$source] ?? 0) + 1; if ($dryRun) { continue; } $update->bind_param('ss', $source, $row['ticket_id']); $update->execute(); if ($update->affected_rows > 0) { $audit->log($systemId, 'update', 'ticket', $row['ticket_id'], [ 'source' => ['from' => null, 'to' => $source], 'reason' => 'source backfilled from the description (tinker_tickets#123)', ]); } } foreach ($unknown as $row) { echo 'left unset ' . $row['ticket_id'] . ' ' . mb_substr((string)$row['title'], 0, 100) . "\n"; } ksort($counts); $summary = []; foreach ($counts as $source => $n) { $summary[] = "$n $source"; } printf( "%s%s %s, %d left unset\n", $dryRun ? '[dry run] ' : '', $dryRun ? 'would set' : 'set', $summary ? implode(', ', $summary) : 'none', count($unknown) );