@@ -1247,6 +1247,101 @@ public function prefix() {
12471247 WP_CLI ::log ( $ wpdb ->prefix );
12481248 }
12491249
1250+ /**
1251+ * Displays a quick database status overview.
1252+ *
1253+ * Shows key database information including name, table count, size,
1254+ * prefix, engine, charset, collation, and health check status. This
1255+ * command is useful for getting a quick snapshot of database health
1256+ * without needing to run multiple separate commands.
1257+ *
1258+ * ## EXAMPLES
1259+ *
1260+ * $ wp db status
1261+ * Database Name: wp_cli_test
1262+ * Tables: 54
1263+ * Total Size: 312 KB
1264+ * Prefix: wp_
1265+ * Engine: InnoDB
1266+ * Charset: utf8mb4
1267+ * Collation: utf8mb4_unicode_ci
1268+ * Check Status: OK
1269+ *
1270+ * @when after_wp_load
1271+ */
1272+ public function status () {
1273+ global $ wpdb ;
1274+
1275+ // Get database name.
1276+ $ db_name = DB_NAME ;
1277+
1278+ // Get table count.
1279+ $ table_count = count ( Utils \wp_get_table_names ( [], [ 'scope ' => 'all ' ] ) );
1280+
1281+ // Get total database size.
1282+ $ db_size_bytes = $ wpdb ->get_var (
1283+ $ wpdb ->prepare (
1284+ 'SELECT SUM(data_length + index_length) FROM information_schema.TABLES where table_schema = %s GROUP BY table_schema; ' ,
1285+ DB_NAME
1286+ )
1287+ );
1288+
1289+ // Format size to human-readable.
1290+ $ size_key = floor ( log ( (float ) $ db_size_bytes ) / log ( 1000 ) );
1291+ $ sizes = [ 'B ' , 'KB ' , 'MB ' , 'GB ' , 'TB ' ];
1292+ $ size_format = isset ( $ sizes [ $ size_key ] ) ? $ sizes [ $ size_key ] : $ sizes [0 ];
1293+ $ divisor = pow ( 1000 , $ size_key );
1294+ $ db_size = round ( (int ) $ db_size_bytes / $ divisor , 2 ) . ' ' . $ size_format ;
1295+
1296+ // Get prefix.
1297+ $ prefix = $ wpdb ->prefix ;
1298+
1299+ // Get engine, charset, and collation from information_schema (using a common table).
1300+ $ table_info = $ wpdb ->get_row (
1301+ $ wpdb ->prepare (
1302+ 'SELECT ENGINE as engine, CCSA.character_set_name as charset, TABLE_COLLATION as collation '
1303+ . 'FROM information_schema.TABLES T '
1304+ . 'LEFT JOIN information_schema.COLLATION_CHARACTER_SET_APPLICABILITY CCSA '
1305+ . 'ON CCSA.collation_name = T.table_collation '
1306+ . 'WHERE T.table_schema = %s '
1307+ . 'AND T.table_name LIKE %s '
1308+ . 'LIMIT 1 ' ,
1309+ DB_NAME ,
1310+ $ wpdb ->esc_like ( $ prefix ) . '% '
1311+ )
1312+ );
1313+
1314+ $ engine = $ table_info && isset ( $ table_info ->engine ) ? $ table_info ->engine : 'N/A ' ;
1315+ $ charset = $ table_info && isset ( $ table_info ->charset ) ? $ table_info ->charset : 'N/A ' ;
1316+ $ collation = $ table_info && isset ( $ table_info ->collation ) ? $ table_info ->collation : 'N/A ' ;
1317+
1318+ // Run database check silently to get status.
1319+ $ check_args = [];
1320+ $ command = sprintf (
1321+ '/usr/bin/env %s%s %s ' ,
1322+ Utils \get_sql_check_command (),
1323+ $ this ->get_defaults_flag_string ( $ check_args ),
1324+ '%s '
1325+ );
1326+ list ( $ stdout , $ stderr , $ exit_code ) = self ::run (
1327+ Utils \esc_cmd ( $ command , DB_NAME ),
1328+ [ 'check ' => true ],
1329+ false
1330+ );
1331+
1332+ $ check_status = ( 0 === $ exit_code ) ? 'OK ' : 'Error ' ;
1333+
1334+ // Output formatted status.
1335+ WP_CLI ::log ( sprintf ( '%-18s %s ' , 'Database Name: ' , $ db_name ) );
1336+ WP_CLI ::log ( sprintf ( '%-18s %d ' , 'Tables: ' , $ table_count ) );
1337+ WP_CLI ::log ( sprintf ( '%-18s %s ' , 'Total Size: ' , $ db_size ) );
1338+ WP_CLI ::log ( sprintf ( '%-18s %s ' , 'Prefix: ' , $ prefix ) );
1339+ WP_CLI ::log ( sprintf ( '%-18s %s ' , 'Engine: ' , $ engine ) );
1340+ WP_CLI ::log ( sprintf ( '%-18s %s ' , 'Charset: ' , $ charset ) );
1341+ WP_CLI ::log ( sprintf ( '%-18s %s ' , 'Collation: ' , $ collation ) );
1342+ WP_CLI ::log ( sprintf ( '%-18s %s ' , 'Check Status: ' , $ check_status ) );
1343+ }
1344+
12501345 /**
12511346 * Finds a string in the database.
12521347 *
0 commit comments