@@ -456,6 +456,174 @@ public function tables( $args, $assoc_args ) {
456
456
}
457
457
}
458
458
459
+ /**
460
+ * Display the database name and size.
461
+ *
462
+ * Display the database name and size for `DB_NAME` specified in wp-config.php,
463
+ * defaults to a human-readable number.
464
+ *
465
+ * ## OPTIONS
466
+ *
467
+ * [--size_format]
468
+ * : Display only the database size as a number.
469
+ * ---
470
+ * default: b
471
+ * options:
472
+ * - b (bytes)
473
+ * - kb (kilobytes)
474
+ * - mb (megabytes)
475
+ * ---
476
+ *
477
+ * [--tables]
478
+ * : Display each table name and size instead of the database size.
479
+ *
480
+ * [--format]
481
+ * : table, csv, json
482
+ * ---
483
+ * default: table
484
+ * options:
485
+ * - table
486
+ * - csv
487
+ * - json
488
+ * ---
489
+ *
490
+ * [--scope=<scope>]
491
+ * : Can be all, global, ms_global, blog, or old tables. Defaults to all.
492
+ *
493
+ * [--network]
494
+ * : List all the tables in a multisite install. Overrides --scope=<scope>.
495
+ *
496
+ * [--all-tables-with-prefix]
497
+ * : List all tables that match the table prefix even if not registered on $wpdb. Overrides --network.
498
+ *
499
+ * [--all-tables]
500
+ * : List all tables in the database, regardless of the prefix, and even if not registered on $wpdb. Overrides --all-tables-with-prefix.
501
+ *
502
+ * ## EXAMPLES
503
+ *
504
+ * $ wp db size
505
+ * +-------------------+------+
506
+ * | Name | Size |
507
+ * +-------------------+------+
508
+ * | wordpress_default | 6 MB |
509
+ * +-------------------+------+
510
+ *
511
+ * $ wp db size --tables
512
+ * +-----------------------+-------+
513
+ * | Name | Size |
514
+ * +-----------------------+-------+
515
+ * | wp_users | 64 KB |
516
+ * | wp_usermeta | 48 KB |
517
+ * | wp_posts | 80 KB |
518
+ * | wp_comments | 96 KB |
519
+ * | wp_links | 32 KB |
520
+ * | wp_options | 32 KB |
521
+ * | wp_postmeta | 48 KB |
522
+ * | wp_terms | 48 KB |
523
+ * | wp_term_taxonomy | 48 KB |
524
+ * | wp_term_relationships | 32 KB |
525
+ * | wp_termmeta | 48 KB |
526
+ * | wp_commentmeta | 48 KB |
527
+ * +-----------------------+-------+
528
+ *
529
+ * $ wp db size --size_format=b
530
+ * 5865472
531
+ *
532
+ * $ wp db size --size_format=kb
533
+ * 5728
534
+ *
535
+ * $ wp db size --size_format=mb
536
+ * 6
537
+ */
538
+ public function size ( $ args , $ assoc_args ) {
539
+
540
+ @WP_CLI ::get_runner ()-> load_wordpress ();
541
+
542
+ global $ wpdb ;
543
+
544
+ $ format = WP_CLI \Utils \get_flag_value ( $ assoc_args , 'format ' );
545
+ $ size_format = WP_CLI \Utils \get_flag_value ( $ assoc_args , 'size_format ' );
546
+ $ tables = WP_CLI \Utils \get_flag_value ( $ assoc_args , 'tables ' );
547
+ $ tables = ! empty ( $ tables );
548
+
549
+ unset( $ assoc_args ['format ' ] );
550
+ unset( $ assoc_args ['size_format ' ] );
551
+ unset( $ assoc_args ['tables ' ] );
552
+
553
+ if ( empty ( $ args ) && empty ( $ assoc_args ) ) {
554
+ $ assoc_args ['scope ' ] = 'all ' ;
555
+ }
556
+
557
+ // Build rows for the formatter.
558
+ $ rows = array ();
559
+ $ fields = array ( 'Name ' , 'Size ' );
560
+
561
+ if ( $ tables ) {
562
+
563
+ // Add all of the table sizes
564
+ foreach ( WP_CLI \Utils \wp_get_table_names ( $ args , $ assoc_args ) as $ table_name ) {
565
+
566
+ // Get the table size.
567
+ $ table_bytes = $ wpdb ->get_var ( $ wpdb ->prepare (
568
+ "SELECT SUM(data_length + index_length) FROM information_schema.TABLES where table_schema = '%s' and Table_Name = '%s' GROUP BY Table_Name LIMIT 1 " ,
569
+ DB_NAME ,
570
+ $ table_name
571
+ )
572
+ );
573
+
574
+ // Add the table size to the list.
575
+ $ rows [] = array (
576
+ 'Name ' => $ table_name ,
577
+ 'Size ' => size_format ( $ table_bytes ),
578
+ );
579
+ }
580
+ } else {
581
+
582
+ // Get the database size.
583
+ $ db_bytes = $ wpdb ->get_var ( $ wpdb ->prepare (
584
+ "SELECT SUM(data_length + index_length) FROM information_schema.TABLES where table_schema = '%s' GROUP BY table_schema; " ,
585
+ DB_NAME
586
+ )
587
+ );
588
+
589
+ // Add the database size to the list.
590
+ $ rows [] = array (
591
+ 'Name ' => DB_NAME ,
592
+ 'Size ' => size_format ( $ db_bytes ),
593
+ );
594
+ }
595
+
596
+ if ( ! empty ( $ size_format ) && isset ( $ db_bytes ) && ! $ tables ) {
597
+
598
+ // Display the database size as a number.
599
+ switch ( $ size_format ) {
600
+ case 'mb ' :
601
+ $ divisor = MB_IN_BYTES ;
602
+ break ;
603
+
604
+ case 'kb ' :
605
+ $ divisor = KB_IN_BYTES ;
606
+ break ;
607
+
608
+ case 'b ' :
609
+ default :
610
+ $ divisor = 1 ;
611
+ break ;
612
+ }
613
+
614
+ WP_CLI ::Line ( ceil ( $ db_bytes / $ divisor ) );
615
+ } else {
616
+
617
+ // Display the rows.
618
+ $ args = array (
619
+ 'format ' => $ format ,
620
+ );
621
+
622
+ $ formatter = new \WP_CLI \Formatter ( $ args , $ fields );
623
+ $ formatter ->display_items ( $ rows );
624
+ }
625
+ }
626
+
459
627
private static function get_create_query () {
460
628
461
629
$ create_query = sprintf ( 'CREATE DATABASE `%s` ' , DB_NAME );
@@ -487,5 +655,4 @@ private static function run( $cmd, $assoc_args = array(), $descriptors = null )
487
655
$ final_args = array_merge ( $ assoc_args , $ required );
488
656
Utils \run_mysql_command ( $ cmd , $ final_args , $ descriptors );
489
657
}
490
-
491
658
}
0 commit comments