From fb644cee255929d58a9e18ef9120dd910804d43f Mon Sep 17 00:00:00 2001 From: Antonio Sejas Date: Mon, 25 Nov 2024 06:52:36 +0000 Subject: [PATCH 1/8] Add SQLite tabels command supporting list and csv --- src/SQLite_Command.php | 54 +++++++++++++++++++++++++++++ src/Tables.php | 79 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 src/Tables.php diff --git a/src/SQLite_Command.php b/src/SQLite_Command.php index 77f29c7..4081e15 100644 --- a/src/SQLite_Command.php +++ b/src/SQLite_Command.php @@ -4,6 +4,7 @@ use WP_CLI; use WP_CLI_Command; +use PDO; class SQLite_Command extends WP_CLI_Command { @@ -97,4 +98,57 @@ public function export( $args, $assoc_args ) { $export->run( $result_file, $assoc_args ); } + + /** + * Lists the database tables. + * + * Defaults to all tables in the SQLite database. + * + * ## OPTIONS + * + * [...] + * : List tables based on wildcard search, e.g. 'wp_*_options' or 'wp_post?'. + * + * [--format=] + * : Render output in a particular format. + * --- + * default: list + * options: + * - list + * - csv + * --- + * + * ## EXAMPLES + * + * # List all tables in the database + * $ wp sqlite tables + * wp_commentmeta + * wp_comments + * wp_links + * wp_options + * wp_postmeta + * wp_posts + * wp_terms + * wp_termmeta + * wp_term_relationships + * wp_term_taxonomy + * wp_usermeta + * wp_users + * + * # List all tables matching a wildcard + * $ wp sqlite tables wp_post* + * wp_postmeta + * wp_posts + * + * @when before_wp_load + */ + public function tables($args, $assoc_args) { + if (!Base::get_sqlite_plugin_version()) { + WP_CLI::error('The SQLite integration plugin is not installed or activated.'); + } + + $tables = new Tables(); + $pattern = !empty($args) ? $args[0] : null; + $tables->run($pattern, $assoc_args); + } } diff --git a/src/Tables.php b/src/Tables.php new file mode 100644 index 0000000..e908fb6 --- /dev/null +++ b/src/Tables.php @@ -0,0 +1,79 @@ +load_dependencies(); + $this->translator = new WP_SQLite_Translator(); + } + + /** + * Get the PDO instance. + * + * @return PDO + */ + protected function get_pdo() { + return $this->translator->get_pdo(); + } + + /** + * Lists all tables in the SQLite database. + * + * @param string|null $pattern Optional wildcard pattern to filter tables. + * @param array $assoc_args Associative array of options. + * @return void + */ + public function run($pattern = null, $assoc_args = []) { + $pdo = $this->get_pdo(); + + // Get all tables + $stmt = $pdo->query("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'"); + $tables = $stmt->fetchAll(PDO::FETCH_COLUMN); + + // Filter tables if wildcard pattern is provided + if ($pattern) { + $sql_pattern = str_replace('?', '_', $pattern); + $sql_pattern = str_replace('*', '%', $sql_pattern); + + $tables = array_filter($tables, function($table) use ($pattern) { + return fnmatch($pattern, $table, FNM_CASEFOLD); + }); + } + + // Remove system tables + $tables_to_exclude = array('_mysql_data_types_cache'); + $tables = array_diff($tables, $tables_to_exclude); + + if (empty($tables)) { + if ($pattern) { + WP_CLI::error("No tables found matching: " . $pattern); + } else { + WP_CLI::error("No tables found in the database."); + } + } + + $format = \WP_CLI\Utils\get_flag_value( $assoc_args, 'format' ); + + if ( 'csv' === $format ) { + WP_CLI::line( implode( ',', $tables ) ); + } else { + foreach ( $tables as $table ) { + WP_CLI::line( $table ); + } + } + } +} From ce86994923060257bf669d576856218566fd47b4 Mon Sep 17 00:00:00 2001 From: Antonio Sejas Date: Mon, 25 Nov 2024 07:06:16 +0000 Subject: [PATCH 2/8] format php files --- src/SQLite_Command.php | 12 ++++---- src/Tables.php | 63 ++++++++++++++++++++++-------------------- 2 files changed, 39 insertions(+), 36 deletions(-) diff --git a/src/SQLite_Command.php b/src/SQLite_Command.php index 4081e15..7574f3c 100644 --- a/src/SQLite_Command.php +++ b/src/SQLite_Command.php @@ -142,13 +142,13 @@ public function export( $args, $assoc_args ) { * * @when before_wp_load */ - public function tables($args, $assoc_args) { - if (!Base::get_sqlite_plugin_version()) { - WP_CLI::error('The SQLite integration plugin is not installed or activated.'); + public function tables( $args, $assoc_args ) { + if ( ! Base::get_sqlite_plugin_version() ) { + WP_CLI::error( 'The SQLite integration plugin is not installed or activated.' ); } - $tables = new Tables(); - $pattern = !empty($args) ? $args[0] : null; - $tables->run($pattern, $assoc_args); + $tables = new Tables(); + $pattern = ! empty( $args ) ? $args[0] : null; + $tables->run( $pattern, $assoc_args ); } } diff --git a/src/Tables.php b/src/Tables.php index e908fb6..161ec57 100644 --- a/src/Tables.php +++ b/src/Tables.php @@ -30,41 +30,44 @@ protected function get_pdo() { return $this->translator->get_pdo(); } - /** - * Lists all tables in the SQLite database. - * - * @param string|null $pattern Optional wildcard pattern to filter tables. - * @param array $assoc_args Associative array of options. - * @return void - */ - public function run($pattern = null, $assoc_args = []) { - $pdo = $this->get_pdo(); + /** + * Lists all tables in the SQLite database. + * + * @param string|null $pattern Optional wildcard pattern to filter tables. + * @param array $assoc_args Associative array of options. + * @return void + */ + public function run( $pattern = null, $assoc_args = [] ) { + $pdo = $this->get_pdo(); - // Get all tables - $stmt = $pdo->query("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'"); - $tables = $stmt->fetchAll(PDO::FETCH_COLUMN); + // Get all tables + $stmt = $pdo->query( "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'" ); + $tables = $stmt->fetchAll( PDO::FETCH_COLUMN ); - // Filter tables if wildcard pattern is provided - if ($pattern) { - $sql_pattern = str_replace('?', '_', $pattern); - $sql_pattern = str_replace('*', '%', $sql_pattern); + // Filter tables if wildcard pattern is provided + if ( $pattern ) { + $sql_pattern = str_replace( '?', '_', $pattern ); + $sql_pattern = str_replace( '*', '%', $sql_pattern ); - $tables = array_filter($tables, function($table) use ($pattern) { - return fnmatch($pattern, $table, FNM_CASEFOLD); - }); - } + $tables = array_filter( + $tables, + function ( $table ) use ( $pattern ) { + return fnmatch( $pattern, $table, FNM_CASEFOLD ); + } + ); + } // Remove system tables - $tables_to_exclude = array('_mysql_data_types_cache'); - $tables = array_diff($tables, $tables_to_exclude); + $tables_to_exclude = array( '_mysql_data_types_cache' ); + $tables = array_diff( $tables, $tables_to_exclude ); - if (empty($tables)) { - if ($pattern) { - WP_CLI::error("No tables found matching: " . $pattern); - } else { - WP_CLI::error("No tables found in the database."); - } - } + if ( empty( $tables ) ) { + if ( $pattern ) { + WP_CLI::error( 'No tables found matching: ' . $pattern ); + } else { + WP_CLI::error( 'No tables found in the database.' ); + } + } $format = \WP_CLI\Utils\get_flag_value( $assoc_args, 'format' ); @@ -75,5 +78,5 @@ public function run($pattern = null, $assoc_args = []) { WP_CLI::line( $table ); } } - } + } } From 4dea7a62f7afcc09d0e07f8a76f048679edf106a Mon Sep 17 00:00:00 2001 From: Antonio Sejas Date: Mon, 25 Nov 2024 07:13:04 +0000 Subject: [PATCH 3/8] Add Readme --- README.md | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/README.md b/README.md index cebf4b8..fbbfe80 100644 --- a/README.md +++ b/README.md @@ -42,3 +42,52 @@ $ wp sqlite export [] [--tables=] [--exclude-tables] [--porcelain] [--porcelain] Output filename for the exported database. +### wp sqlite tables + +Lists the SQLite database tables. + +``` +$ wp sqlite tables [--pattern=] [--format=] +``` + +**OPTIONS** + + [--pattern=] + Filter the list of tables by a wildcard pattern. + + [--format=] + Render output in a specific format. + --- + Default: list + Options: + - list + - csv + --- + +**EXAMPLES** + +``` + # List all tables + $ wp sqlite tables + wp_users + wp_usermeta + wp_termmeta + wp_terms + wp_term_taxonomy + wp_term_relationships + wp_commentmeta + wp_comments + wp_links + wp_options + wp_postmeta + wp_posts + + # List all tables matching a wildcard + $ wp sqlite tables "wp_post*" + wp_postmeta + wp_posts + + * List all tables in CSV format + $ wp sqlite tables --format=csv + wp_users,wp_usermeta,wp_termmeta,wp_terms,wp_term_taxonomy,wp_term_relationships,wp_commentmeta,wp_comments,wp_links,wp_options,wp_postmeta,wp_posts +``` From 958e454b4f53dd0ce849b0e86b66785b0aef3cbd Mon Sep 17 00:00:00 2001 From: Antonio Sejas Date: Mon, 25 Nov 2024 07:15:00 +0000 Subject: [PATCH 4/8] upgrade GH actions --- .github/workflows/build.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d99664a..dc641a2 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -8,7 +8,7 @@ jobs: build: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: Setup PHP uses: shivammathur/setup-php@v2 @@ -45,7 +45,7 @@ jobs: run: rm -rf temp_dist version - name: Upload distribution as artifact - uses: actions/upload-artifact@v2 + uses: actions/upload-artifact@v4 with: name: wp-cli-sqlite-command-${{ env.RELEASE_VERSION }} path: wp-cli-sqlite-command-${{ env.RELEASE_VERSION }}.zip From ba78e0d35593cb6fe2caf2898ee6f3976a8cfdca Mon Sep 17 00:00:00 2001 From: Fredrik Rombach Ekelund Date: Mon, 25 Nov 2024 16:15:41 +0100 Subject: [PATCH 5/8] Support multiple table patterns --- src/SQLite_Command.php | 11 ++++++----- src/Tables.php | 25 +++++++++++-------------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/SQLite_Command.php b/src/SQLite_Command.php index 7574f3c..4788c6b 100644 --- a/src/SQLite_Command.php +++ b/src/SQLite_Command.php @@ -135,8 +135,10 @@ public function export( $args, $assoc_args ) { * wp_usermeta * wp_users * - * # List all tables matching a wildcard - * $ wp sqlite tables wp_post* + * # List all tables matching wildcards + * $ wp sqlite tables wp_comment* wp_post* + * wp_commentmeta + * wp_comments * wp_postmeta * wp_posts * @@ -147,8 +149,7 @@ public function tables( $args, $assoc_args ) { WP_CLI::error( 'The SQLite integration plugin is not installed or activated.' ); } - $tables = new Tables(); - $pattern = ! empty( $args ) ? $args[0] : null; - $tables->run( $pattern, $assoc_args ); + $tables = new Tables(); + $tables->run( $args, $assoc_args ); } } diff --git a/src/Tables.php b/src/Tables.php index 161ec57..c1a7023 100644 --- a/src/Tables.php +++ b/src/Tables.php @@ -33,11 +33,11 @@ protected function get_pdo() { /** * Lists all tables in the SQLite database. * - * @param string|null $pattern Optional wildcard pattern to filter tables. + * @param array|null $patterns Optional wildcard patterns to filter tables. * @param array $assoc_args Associative array of options. * @return void */ - public function run( $pattern = null, $assoc_args = [] ) { + public function run( ?array $patterns = null, $assoc_args = [] ) { $pdo = $this->get_pdo(); // Get all tables @@ -45,16 +45,13 @@ public function run( $pattern = null, $assoc_args = [] ) { $tables = $stmt->fetchAll( PDO::FETCH_COLUMN ); // Filter tables if wildcard pattern is provided - if ( $pattern ) { - $sql_pattern = str_replace( '?', '_', $pattern ); - $sql_pattern = str_replace( '*', '%', $sql_pattern ); - - $tables = array_filter( - $tables, - function ( $table ) use ( $pattern ) { - return fnmatch( $pattern, $table, FNM_CASEFOLD ); - } - ); + if ( ! empty( $patterns ) ) { + foreach ( $patterns as $pattern ) { + $tables = array_filter( + $tables, + fn ( $table ) => fnmatch( $pattern, $table, FNM_CASEFOLD ) + ); + } } // Remove system tables @@ -62,8 +59,8 @@ function ( $table ) use ( $pattern ) { $tables = array_diff( $tables, $tables_to_exclude ); if ( empty( $tables ) ) { - if ( $pattern ) { - WP_CLI::error( 'No tables found matching: ' . $pattern ); + if ( ! empty( $patterns ) ) { + WP_CLI::error( 'No tables found.' ); } else { WP_CLI::error( 'No tables found in the database.' ); } From 34cf6e195a99e1c608678b4c4907e3a332d0dbff Mon Sep 17 00:00:00 2001 From: Fredrik Rombach Ekelund Date: Mon, 25 Nov 2024 16:17:16 +0100 Subject: [PATCH 6/8] Remove pattern option --- src/SQLite_Command.php | 13 ++----------- src/Tables.php | 19 ++----------------- 2 files changed, 4 insertions(+), 28 deletions(-) diff --git a/src/SQLite_Command.php b/src/SQLite_Command.php index 4788c6b..744aff4 100644 --- a/src/SQLite_Command.php +++ b/src/SQLite_Command.php @@ -106,11 +106,9 @@ public function export( $args, $assoc_args ) { * * ## OPTIONS * - * [
...] - * : List tables based on wildcard search, e.g. 'wp_*_options' or 'wp_post?'. - * * [--format=] * : Render output in a particular format. + * * --- * default: list * options: @@ -135,13 +133,6 @@ public function export( $args, $assoc_args ) { * wp_usermeta * wp_users * - * # List all tables matching wildcards - * $ wp sqlite tables wp_comment* wp_post* - * wp_commentmeta - * wp_comments - * wp_postmeta - * wp_posts - * * @when before_wp_load */ public function tables( $args, $assoc_args ) { @@ -150,6 +141,6 @@ public function tables( $args, $assoc_args ) { } $tables = new Tables(); - $tables->run( $args, $assoc_args ); + $tables->run( $assoc_args ); } } diff --git a/src/Tables.php b/src/Tables.php index c1a7023..366d90d 100644 --- a/src/Tables.php +++ b/src/Tables.php @@ -33,37 +33,22 @@ protected function get_pdo() { /** * Lists all tables in the SQLite database. * - * @param array|null $patterns Optional wildcard patterns to filter tables. * @param array $assoc_args Associative array of options. * @return void */ - public function run( ?array $patterns = null, $assoc_args = [] ) { + public function run( $assoc_args = [] ) { $pdo = $this->get_pdo(); // Get all tables $stmt = $pdo->query( "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'" ); $tables = $stmt->fetchAll( PDO::FETCH_COLUMN ); - // Filter tables if wildcard pattern is provided - if ( ! empty( $patterns ) ) { - foreach ( $patterns as $pattern ) { - $tables = array_filter( - $tables, - fn ( $table ) => fnmatch( $pattern, $table, FNM_CASEFOLD ) - ); - } - } - // Remove system tables $tables_to_exclude = array( '_mysql_data_types_cache' ); $tables = array_diff( $tables, $tables_to_exclude ); if ( empty( $tables ) ) { - if ( ! empty( $patterns ) ) { - WP_CLI::error( 'No tables found.' ); - } else { - WP_CLI::error( 'No tables found in the database.' ); - } + WP_CLI::error( 'No tables found in the database.' ); } $format = \WP_CLI\Utils\get_flag_value( $assoc_args, 'format' ); From 16be7f2b2ec5b3b714c65357c0143750ec0ef27c Mon Sep 17 00:00:00 2001 From: Fredrik Rombach Ekelund Date: Tue, 26 Nov 2024 13:21:20 +0100 Subject: [PATCH 7/8] Tweaks --- README.md | 10 +--------- src/SQLite_Command.php | 1 - src/Tables.php | 5 ----- 3 files changed, 1 insertion(+), 15 deletions(-) diff --git a/README.md b/README.md index fbbfe80..c1eb583 100644 --- a/README.md +++ b/README.md @@ -47,14 +47,11 @@ $ wp sqlite export [] [--tables=] [--exclude-tables] [--porcelain] Lists the SQLite database tables. ``` -$ wp sqlite tables [--pattern=] [--format=] +$ wp sqlite tables [--format=] ``` **OPTIONS** - [--pattern=] - Filter the list of tables by a wildcard pattern. - [--format=] Render output in a specific format. --- @@ -82,11 +79,6 @@ $ wp sqlite tables [--pattern=] [--format=] wp_postmeta wp_posts - # List all tables matching a wildcard - $ wp sqlite tables "wp_post*" - wp_postmeta - wp_posts - * List all tables in CSV format $ wp sqlite tables --format=csv wp_users,wp_usermeta,wp_termmeta,wp_terms,wp_term_taxonomy,wp_term_relationships,wp_commentmeta,wp_comments,wp_links,wp_options,wp_postmeta,wp_posts diff --git a/src/SQLite_Command.php b/src/SQLite_Command.php index 744aff4..860f6ba 100644 --- a/src/SQLite_Command.php +++ b/src/SQLite_Command.php @@ -4,7 +4,6 @@ use WP_CLI; use WP_CLI_Command; -use PDO; class SQLite_Command extends WP_CLI_Command { diff --git a/src/Tables.php b/src/Tables.php index 366d90d..0bf2165 100644 --- a/src/Tables.php +++ b/src/Tables.php @@ -6,15 +6,10 @@ use PDO; use WP_SQLite_Translator; -/** - * Class Tables - * Handles listing tables in the SQLite database. - */ class Tables extends Base { protected $translator; protected $args = array(); - protected $is_stdout = false; public function __construct() { $this->load_dependencies(); From f6dce6fc9d28a9651107a42235735940232f9551 Mon Sep 17 00:00:00 2001 From: Fredrik Rombach Ekelund Date: Tue, 26 Nov 2024 14:36:04 +0100 Subject: [PATCH 8/8] Tweak --- src/Tables.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Tables.php b/src/Tables.php index 0bf2165..5dbd283 100644 --- a/src/Tables.php +++ b/src/Tables.php @@ -9,7 +9,6 @@ class Tables extends Base { protected $translator; - protected $args = array(); public function __construct() { $this->load_dependencies();