|
| 1 | +<?php |
| 2 | +set_time_limit(0); |
| 3 | + |
| 4 | +function showHelp() { |
| 5 | + $help = '╔══════════════════════════════════════════════════════════════════════════════╗' . PHP_EOL; |
| 6 | + $help .= '║ 🦄 Ubilling Database Check tool 🦄 ║' . PHP_EOL; |
| 7 | + $help .= '╚══════════════════════════════════════════════════════════════════════════════╝' . PHP_EOL; |
| 8 | + $help .= 'Performs database integrity check using MySQL credentials.' . PHP_EOL . PHP_EOL; |
| 9 | + $help .= 'Usage:' . PHP_EOL; |
| 10 | + $help .= ' php dbcheck.php [options]' . PHP_EOL . PHP_EOL; |
| 11 | + $help .= 'Options:' . PHP_EOL; |
| 12 | + $help .= ' --repair Attempt to repair corrupted tables' . PHP_EOL; |
| 13 | + $help .= ' --optimize Optimize tables after check' . PHP_EOL; |
| 14 | + $help .= '╔══════════════════════════════════════════════════════════════════════════════╗' . PHP_EOL; |
| 15 | + $help .= '╚══════════════════════════════════════════════════════════════════════════════╝' . PHP_EOL; |
| 16 | + print($help); |
| 17 | +} |
| 18 | + |
| 19 | +function loadMysqlConfig() { |
| 20 | + $configFile = __DIR__ . '/../../config/mysql.ini'; |
| 21 | + if (!file_exists($configFile)) { |
| 22 | + die("❌ Error: MySQL config file not found: $configFile" . PHP_EOL); |
| 23 | + } |
| 24 | + |
| 25 | + $config = parse_ini_file($configFile); |
| 26 | + if (!$config || empty($config['server']) || empty($config['username']) || !isset($config['password']) || empty($config['db'])) { |
| 27 | + die("❌ Error: Invalid MySQL configuration file format" . PHP_EOL); |
| 28 | + } |
| 29 | + |
| 30 | + return $config; |
| 31 | +} |
| 32 | + |
| 33 | +function connectToDatabase($config) { |
| 34 | + $mysqli = new mysqli( |
| 35 | + $config['server'], |
| 36 | + $config['username'], |
| 37 | + $config['password'], |
| 38 | + $config['db'] |
| 39 | + ); |
| 40 | + |
| 41 | + if ($mysqli->connect_error) { |
| 42 | + die("❌ Error: Connection failed: " . $mysqli->connect_error . PHP_EOL); |
| 43 | + } |
| 44 | + |
| 45 | + print("🔌 Connected to {$config['db']}" . PHP_EOL); |
| 46 | + return $mysqli; |
| 47 | +} |
| 48 | + |
| 49 | +function checkTables($mysqli, $repair = false, $optimize = false) { |
| 50 | + $tables = array(); |
| 51 | + $result = $mysqli->query("SHOW TABLES"); |
| 52 | + if (!$result) { |
| 53 | + die("❌ Error: Cannot get table list: " . $mysqli->error . PHP_EOL); |
| 54 | + } |
| 55 | + |
| 56 | + while ($row = $result->fetch_row()) { |
| 57 | + $tables[] = $row[0]; |
| 58 | + } |
| 59 | + |
| 60 | + if (empty($tables)) { |
| 61 | + die("❌ Error: No tables found in database" . PHP_EOL); |
| 62 | + } |
| 63 | + |
| 64 | + print("🔍 Found " . count($tables) . " tables to check" . PHP_EOL); |
| 65 | + |
| 66 | + $hasErrors = false; |
| 67 | + foreach ($tables as $table) { |
| 68 | + print("🔍 Checking '$table'... "); |
| 69 | + |
| 70 | + $checkResult = $mysqli->query("CHECK TABLE `$table`"); |
| 71 | + if (!$checkResult) { |
| 72 | + print("❌ Check failed - " . $mysqli->error . PHP_EOL); |
| 73 | + $hasErrors = true; |
| 74 | + continue; |
| 75 | + } |
| 76 | + |
| 77 | + $row = $checkResult->fetch_assoc(); |
| 78 | + if ($row['Msg_type'] === 'error' || $row['Msg_type'] === 'warning') { |
| 79 | + print("⚠️ " . $row['Msg_text']); |
| 80 | + $hasErrors = true; |
| 81 | + |
| 82 | + if ($repair) { |
| 83 | + print(" [Repairing... "); |
| 84 | + $repairResult = $mysqli->query("REPAIR TABLE `$table`"); |
| 85 | + if (!$repairResult) { |
| 86 | + print("❌ Failed]"); |
| 87 | + } else { |
| 88 | + $repairRow = $repairResult->fetch_assoc(); |
| 89 | + print($repairRow['Msg_type'] === 'status' ? "✅]" : "⚠️]"); |
| 90 | + } |
| 91 | + } |
| 92 | + print(PHP_EOL); |
| 93 | + } else { |
| 94 | + print("✅ OK"); |
| 95 | + |
| 96 | + if ($optimize) { |
| 97 | + print(" [Optimizing... "); |
| 98 | + $optimizeResult = $mysqli->query("OPTIMIZE TABLE `$table`"); |
| 99 | + if (!$optimizeResult) { |
| 100 | + print("❌]"); |
| 101 | + } else { |
| 102 | + $optimizeRow = $optimizeResult->fetch_assoc(); |
| 103 | + print($optimizeRow['Msg_type'] === 'status' ? "✅]" : "⚠️]"); |
| 104 | + } |
| 105 | + } |
| 106 | + print(PHP_EOL); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return $hasErrors; |
| 111 | +} |
| 112 | + |
| 113 | +function formatDuration($seconds) { |
| 114 | + if ($seconds < 60) { |
| 115 | + return round($seconds, 2) . "s"; |
| 116 | + } elseif ($seconds < 3600) { |
| 117 | + $minutes = floor($seconds / 60); |
| 118 | + $remainingSeconds = $seconds % 60; |
| 119 | + return $minutes . "m " . round($remainingSeconds, 2) . "s"; |
| 120 | + } else { |
| 121 | + $hours = floor($seconds / 3600); |
| 122 | + $minutes = floor(($seconds % 3600) / 60); |
| 123 | + $remainingSeconds = $seconds % 60; |
| 124 | + return $hours . "h " . $minutes . "m " . round($remainingSeconds, 2) . "s"; |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +function main() { |
| 129 | + global $argv; |
| 130 | + showHelp(); |
| 131 | + |
| 132 | + $repair = in_array('--repair', $argv); |
| 133 | + $optimize = in_array('--optimize', $argv); |
| 134 | + |
| 135 | + $startTime = microtime(true); |
| 136 | + $config = loadMysqlConfig(); |
| 137 | + $mysqli = connectToDatabase($config); |
| 138 | + $hasErrors = checkTables($mysqli, $repair, $optimize); |
| 139 | + $mysqli->close(); |
| 140 | + |
| 141 | + $executionTime = microtime(true) - $startTime; |
| 142 | + print("⏱️ Completed in " . formatDuration($executionTime) . PHP_EOL); |
| 143 | + |
| 144 | + if ($hasErrors) { |
| 145 | + print("⚠️ Check completed with issues" . PHP_EOL); |
| 146 | + exit(1); |
| 147 | + } else { |
| 148 | + print("✅ All tables OK" . PHP_EOL); |
| 149 | + exit(0); |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +main(); |
0 commit comments