Skip to content

Commit 2f4801d

Browse files
authored
Merge pull request #1 from Automattic/fix/linting-issues
Fix linting issues, fix namespaces
2 parents 4f9f26d + 8d340f7 commit 2f4801d

File tree

8 files changed

+156
-108
lines changed

8 files changed

+156
-108
lines changed

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
automattic/wp-cli-sqlite-command
2+
================================
3+
4+
Imports and exports SQLite databases using WP-CLI.
5+
6+
## Using
7+
8+
### wp sqlite import
9+
10+
Imports a MySQL compatible file into an SQLite database.
11+
12+
```
13+
$ wp sqlite import <file>
14+
```
15+
16+
**OPTIONS**
17+
18+
<file>
19+
The path to the MySQL compatible dump file to import. When passing `-` as the file argument, the SQL commands are read from standard input.
20+
21+
### wp sqlite export
22+
23+
Exports an SQLite database to a MySQL compatible file.
24+
25+
```
26+
$ wp sqlite export [<file>] [--tables=<tables>] [--exclude-tables] [--porcelain]
27+
```
28+
29+
**OPTIONS**
30+
31+
[<file>]
32+
Path to the file to write the MySQL compatible dump to. If not provided, the SQL commands are written to standard output.
33+
34+
[--tables=<tables>]
35+
List of tables to export. Use commas to separate multiple table names.
36+
37+
[--exclude-tables]
38+
Exclude certain tables from the export. Use commas to separate multiple table names.
39+
40+
[--porcelain]
41+
Output just the SQL commands, without any comments.
42+

command.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
return;
44
}
55

6-
$autoloader = __DIR__ . '/vendor/autoload.php';
7-
if ( file_exists( $autoloader ) ) {
8-
require_once $autoloader;
6+
$automattic_wpcli_sqlite_autoloader = __DIR__ . '/vendor/autoload.php';
7+
if ( file_exists( $automattic_wpcli_sqlite_autoloader ) ) {
8+
require_once $automattic_wpcli_sqlite_autoloader;
99
}
1010

11-
WP_CLI::add_command( 'sqlite', 'Sqlite_Command', array(
12-
'before_invoke' => function() {
13-
// Load the SQLite driver
14-
$min_version = '7.4';
15-
if ( version_compare( PHP_VERSION, $min_version, '<' ) ) {
16-
WP_CLI::error( "The `wp server` command requires PHP {$min_version} or newer." );
17-
}
18-
}
19-
) );
11+
WP_CLI::add_command(
12+
'sqlite',
13+
'\Automattic\WP_CLI\SQLite\SQLite_Command',
14+
array(
15+
'before_invoke' => function () {
16+
$min_version = '7.4';
17+
if ( version_compare( PHP_VERSION, $min_version, '<' ) ) {
18+
WP_CLI::error( "The `wp server` command requires PHP {$min_version} or newer." );
19+
}
20+
},
21+
)
22+
);

phpcs.xml.dist

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,9 @@
4545
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
4646
<properties>
4747
<property name="prefixes" type="array">
48-
<element value="WP_CLI\Router"/><!-- Namespaces. -->
49-
<element value="wpcli_server"/><!-- Global variables and such. -->
48+
<element value="Automattic\WP_Cli\SQLite"/><!-- Namespaces. -->
49+
<element value="automattic_wpcli_sqlite"/><!-- Global variables and such. -->
5050
</property>
5151
</properties>
5252
</rule>
53-
54-
<!-- Exclude existing classes from the prefix rule as it would break BC to prefix them now. -->
55-
<rule ref="WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound">
56-
<exclude-pattern>*/src/Server_Command\.php$</exclude-pattern>
57-
</rule>
58-
</ruleset>
53+
</ruleset>

src/Base.php

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
<?php
2-
namespace WP_CLI\SQLite;
2+
namespace Automattic\WP_CLI\SQLite;
33

44
use WP_CLI;
55

66
class Base {
77

8-
protected $unsupported_arguments = [];
9-
108
/**
119
* Get the version of the SQLite integration plugin if it is installed
1210
* and activated.
@@ -85,31 +83,11 @@ protected function load_dependencies() {
8583
}
8684

8785
// We also need to selectively load the necessary classes from the plugin.
88-
require_once $plugin_directory . '/php-polyfills.php';
8986
require_once $plugin_directory . '/constants.php';
9087
require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-lexer.php';
9188
require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-query-rewriter.php';
9289
require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-translator.php';
9390
require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-token.php';
9491
require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-pdo-user-defined-functions.php';
9592
}
96-
97-
/**
98-
* Check if the arguments passed to the command are supported.
99-
*
100-
* @param $args
101-
*
102-
* @return void
103-
* @throws WP_CLI\ExitException
104-
*/
105-
protected function check_arguments( $args ) {
106-
if ( array_intersect_key( $args, array_flip( $this->unsupported_arguments ) ) ) {
107-
WP_CLI::error(
108-
sprintf(
109-
'The following arguments are not supported by SQLite exports: %s',
110-
implode( ', ', $this->unsupported_arguments )
111-
)
112-
);
113-
}
114-
}
11593
}

src/Export.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
namespace WP_CLI\SQLite;
2+
namespace Automattic\WP_CLI\SQLite;
33

44
use Exception;
55
use PDO;
@@ -8,18 +8,6 @@
88

99
class Export extends Base {
1010

11-
/**
12-
* List of arguments that are not supported by the export command.
13-
* @var string[]
14-
*/
15-
protected $unsupported_arguments = [
16-
'fields',
17-
'include-tablespaces',
18-
'defaults',
19-
'dbuser',
20-
'dbpass',
21-
];
22-
2311
protected $translator;
2412
protected $args = array();
2513
protected $is_stdout = false;
@@ -40,7 +28,6 @@ public function __construct() {
4028
*/
4129
public function run( $result_file, $args ) {
4230
$this->args = $args;
43-
$this->check_arguments( $args );
4431

4532
$handle = $this->open_output_stream( $result_file );
4633

src/Import.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
namespace WP_CLI\SQLite;
2+
namespace Automattic\WP_CLI\SQLite;
33

44
use Exception;
55
use Generator;
@@ -8,14 +8,6 @@
88

99
class Import extends Base {
1010

11-
protected $unsupported_arguments = [
12-
'skip-optimization',
13-
'defaults',
14-
'fields',
15-
'dbuser',
16-
'dbpass',
17-
];
18-
1911
protected $translator;
2012
protected $args;
2113

@@ -34,7 +26,6 @@ public function __construct() {
3426
*/
3527
public function run( $sql_file_path, $args ) {
3628
$this->args = $args;
37-
$this->check_arguments( $args );
3829

3930
$is_stdin = '-' === $sql_file_path;
4031
$import_file = $is_stdin ? 'php://stdin' : $sql_file_path;
@@ -58,7 +49,7 @@ protected function execute_statements( $import_file ) {
5849
$result = $this->translator->query( $statement );
5950
if ( false === $result ) {
6051
WP_CLI::warning( 'Could not execute statement: ' . $statement );
61-
echo $this->translator->get_error_message(); die();
52+
echo $this->translator->get_error_message();
6253
}
6354
}
6455
}

src/SQLite_Command.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
}

src/Sqlite_Command.php

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)