|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Automattic\WP_CLI\SQLite; |
| 4 | + |
| 5 | +use WP_CLI; |
| 6 | +use WP_CLI_Command; |
| 7 | + |
| 8 | +class SQLite_Command extends WP_CLI_Command { |
| 9 | + |
| 10 | + |
| 11 | + /** |
| 12 | + * Imports the database to SQLite from an MySQL dump file or from STDIN. |
| 13 | + * |
| 14 | + * ## OPTIONS |
| 15 | + * |
| 16 | + * <file> |
| 17 | + * : The name of the SQL file to import. If '-', then reads from STDIN. If omitted, it will look for '{dbname}.sql'. |
| 18 | + * |
| 19 | + * ## EXAMPLES |
| 20 | + * # Import the database from a file |
| 21 | + * $ wp sqlite import wordpress_dbase.sql |
| 22 | + * Success: Imported from 'import wordpress_dbase.sql'. |
| 23 | + */ |
| 24 | + public function import( $args, $assoc_args ) { |
| 25 | + |
| 26 | + if ( empty( $args[0] ) ) { |
| 27 | + WP_CLI::error( 'Please provide a file to import.' ); |
| 28 | + } |
| 29 | + |
| 30 | + if ( ! Import::get_sqlite_plugin_version() ) { |
| 31 | + WP_CLI::error( 'The SQLite integration plugin is not installed or activated.' ); |
| 32 | + } |
| 33 | + |
| 34 | + $import = new Import(); |
| 35 | + $import->run( $args[0], $assoc_args ); |
| 36 | + } |
| 37 | + |
| 38 | + |
| 39 | + /** |
| 40 | + * Exports the database from SQLite to a file or to STDOUT. |
| 41 | + * |
| 42 | + * ## OPTIONS |
| 43 | + * |
| 44 | + * [<file>] |
| 45 | + * : The name of the SQL file to export. If '-', then outputs to STDOUT. If |
| 46 | + * omitted, it will be '{dbname}-{Y-m-d}-{random-hash}.sql'. |
| 47 | + * |
| 48 | + * [--tables=<tables>] |
| 49 | + * : The tables to export. Separate multiple tables with a comma. If omitted, all tables will be exported. |
| 50 | + * |
| 51 | + * [--exclude_tables=<tables>] |
| 52 | + * : The comma separated list of specific tables that should be skipped from exporting. Excluding this parameter will export all tables in the database. |
| 53 | + * |
| 54 | + * [--porcelain] |
| 55 | + * : Output filename for the exported database. |
| 56 | + * |
| 57 | + * ## EXAMPLES |
| 58 | + * # Export the database to a file |
| 59 | + * $ wp sqlite export wordpress_dbase.sql |
| 60 | + * Success: Exported to 'wordpress_dbase.sql'. |
| 61 | + * |
| 62 | + * # Export the database to STDOUT |
| 63 | + * $ wp sqlite export - |
| 64 | + * -- Table structure for table wp_users |
| 65 | + * DROP TABLE IF EXISTS wp_users; |
| 66 | + * CREATE TABLE wp_users ( |
| 67 | + * ... |
| 68 | + * |
| 69 | + * # Export only specific tables |
| 70 | + * $ wp sqlite export --tables=wp_posts,wp_users |
| 71 | + * Success: Exported to 'wordpress_dbase.sql'. |
| 72 | + * |
| 73 | + * # Export all tables except specific tables |
| 74 | + * $ wp sqlite export --exclude_tables=wp_posts,wp_users |
| 75 | + * Success: Exported to 'wordpress_dbase.sql'. |
| 76 | + */ |
| 77 | + public function export( $args, $assoc_args ) { |
| 78 | + WP_CLI::success( 'Exporting database...' ); |
| 79 | + $export = new Export(); |
| 80 | + |
| 81 | + if ( ! empty( $args[0] ) ) { |
| 82 | + $result_file = $args[0]; |
| 83 | + } else { |
| 84 | + // phpcs:ignore WordPress.WP.AlternativeFunctions.rand_mt_rand -- WordPress is not loaded. |
| 85 | + $hash = substr( md5( mt_rand() ), 0, 7 ); |
| 86 | + $result_file = sprintf( '%s-%s.sql', date( 'Y-m-d' ), $hash ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date |
| 87 | + |
| 88 | + } |
| 89 | + |
| 90 | + $export->run( $result_file, $assoc_args ); |
| 91 | + } |
| 92 | +} |
0 commit comments