Skip to content

Commit 4f9f26d

Browse files
committed
Initial commit
0 parents  commit 4f9f26d

13 files changed

Lines changed: 786 additions & 0 deletions

File tree

‎.distignore‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
.DS_Store
2+
.git
3+
.gitignore
4+
.gitlab-ci.yml
5+
.editorconfig
6+
.travis.yml
7+
behat.yml
8+
circle.yml
9+
phpcs.xml.dist
10+
phpunit.xml.dist
11+
bin/
12+
features/
13+
utils/
14+
*.zip
15+
*.tar.gz

‎.editorconfig‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# This file is for unifying the coding style for different editors and IDEs
2+
# editorconfig.org
3+
4+
# WordPress Coding Standards
5+
# https://make.wordpress.org/core/handbook/coding-standards/
6+
7+
# From https://github.com/WordPress/wordpress-develop/blob/trunk/.editorconfig with a couple of additions.
8+
9+
root = true
10+
11+
[*]
12+
charset = utf-8
13+
end_of_line = lf
14+
insert_final_newline = true
15+
trim_trailing_whitespace = true
16+
indent_style = tab
17+
18+
[{*.yml,*.feature,.jshintrc,*.json}]
19+
indent_style = space
20+
indent_size = 2
21+
22+
[*.md]
23+
trim_trailing_whitespace = false
24+
25+
[{*.txt,wp-config-sample.php}]
26+
end_of_line = crlf

‎.github/workflows/code-quality.yml‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: Code Quality Checks
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- main
8+
- master
9+
10+
jobs:
11+
code-quality:
12+
uses: wp-cli/.github/.github/workflows/reusable-code-quality.yml@main

‎.gitignore‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.DS_Store
2+
.phpcs.xml
3+
wp-cli.local.yml
4+
node_modules/
5+
vendor/
6+
*.zip
7+
*.tar.gz
8+
composer.lock
9+
phpunit.xml
10+
phpcs.xml
11+
*.log
12+
.idea/

‎LICENSE‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (C) 2011-2018 WP-CLI Development Group (https://github.com/wp-cli/server-command/contributors)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

‎README.md‎

Whitespace-only changes.

‎command.php‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
if ( ! class_exists( 'WP_CLI' ) ) {
3+
return;
4+
}
5+
6+
$autoloader = __DIR__ . '/vendor/autoload.php';
7+
if ( file_exists( $autoloader ) ) {
8+
require_once $autoloader;
9+
}
10+
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+
) );

‎composer.json‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"name": "automattic/wp-cli-sqlite-command",
3+
"description": "A command for importing and exporting from an SQLite database",
4+
"type": "wp-cli-package",
5+
"license": "MIT",
6+
"autoload": {
7+
"files": [ "command.php" ],
8+
"classmap": ["src/" ]
9+
},
10+
"authors": [
11+
{
12+
"name": "Jeroen Pfeil",
13+
"email": "jeroen.pfeil@automattic.com"
14+
}
15+
],
16+
"require": {
17+
"php": ">=7.4",
18+
"wp-cli/wp-cli": "^2.5"
19+
},
20+
"require-dev": {
21+
"wp-cli/wp-cli-tests": "^4"
22+
},
23+
"scripts": {
24+
"behat": "run-behat-tests",
25+
"behat-rerun": "rerun-behat-tests",
26+
"lint": "run-linter-tests",
27+
"phpcs": "run-phpcs-tests",
28+
"phpcbf": "run-phpcbf-cleanup",
29+
"phpunit": "run-php-unit-tests",
30+
"prepare-tests": "install-package-tests",
31+
"test": [
32+
"@lint",
33+
"@phpcs",
34+
"@phpunit",
35+
"@behat"
36+
]
37+
},
38+
"config": {
39+
"process-timeout": 7200,
40+
"sort-packages": true,
41+
"allow-plugins": {
42+
"dealerdirect/phpcodesniffer-composer-installer": true,
43+
"johnpbloch/wordpress-core-installer": true
44+
}
45+
}
46+
}

‎phpcs.xml.dist‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="WP-CLI-server">
3+
<description>Custom ruleset for WP-CLI server-command</description>
4+
5+
<!--
6+
#############################################################################
7+
COMMAND LINE ARGUMENTS
8+
For help understanding this file: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml
9+
For help using PHPCS: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Usage
10+
#############################################################################
11+
-->
12+
13+
<!-- What to scan. -->
14+
<file>.</file>
15+
16+
<!-- Show progress. -->
17+
<arg value="p"/>
18+
19+
<!-- Strip the filepaths down to the relevant bit. -->
20+
<arg name="basepath" value="./"/>
21+
22+
<!-- Check up to 8 files simultaneously. -->
23+
<arg name="parallel" value="8"/>
24+
25+
<!--
26+
#############################################################################
27+
USE THE WP_CLI_CS RULESET
28+
#############################################################################
29+
-->
30+
31+
<rule ref="WP_CLI_CS"/>
32+
33+
<!--
34+
#############################################################################
35+
PROJECT SPECIFIC CONFIGURATION FOR SNIFFS
36+
#############################################################################
37+
-->
38+
39+
<!-- For help understanding the `testVersion` configuration setting:
40+
https://github.com/PHPCompatibility/PHPCompatibility#sniffing-your-code-for-compatibility-with-specific-php-versions -->
41+
<config name="testVersion" value="5.6-"/>
42+
43+
<!-- Verify that everything in the global namespace is either namespaced or prefixed.
44+
See: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties#naming-conventions-prefix-everything-in-the-global-namespace -->
45+
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
46+
<properties>
47+
<property name="prefixes" type="array">
48+
<element value="WP_CLI\Router"/><!-- Namespaces. -->
49+
<element value="wpcli_server"/><!-- Global variables and such. -->
50+
</property>
51+
</properties>
52+
</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>

‎src/Base.php‎

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
namespace WP_CLI\SQLite;
3+
4+
use WP_CLI;
5+
6+
class Base {
7+
8+
protected $unsupported_arguments = [];
9+
10+
/**
11+
* Get the version of the SQLite integration plugin if it is installed
12+
* and activated.
13+
*
14+
* @return false|string The version of the SQLite integration plugin or false if not found/activated.
15+
*/
16+
public static function get_sqlite_plugin_version() {
17+
// Check if there is a db.php file in the wp-content directory.
18+
if ( ! file_exists( ABSPATH . '/wp-content/db.php' ) ) {
19+
return false;
20+
}
21+
22+
// If the file is found, we need to check that it is the sqlite integration plugin.
23+
$plugin_file = file_get_contents( ABSPATH . '/wp-content/db.php' );
24+
if ( ! preg_match( '/define\( \'SQLITE_DB_DROPIN_VERSION\', \'([0-9.]+)\' \)/', $plugin_file ) ) {
25+
return false;
26+
}
27+
28+
$plugin_path = self::get_plugin_directory();
29+
if ( ! $plugin_path ) {
30+
return false;
31+
}
32+
33+
// Try to get the version number from readme.txt
34+
$plugin_file = file_get_contents( $plugin_path . '/readme.txt' );
35+
36+
preg_match( '/^Stable tag:\s*?(.+)$/m', $plugin_file, $matches );
37+
38+
return isset( $matches[1] ) ? trim( $matches[1] ) : false;
39+
}
40+
41+
/**
42+
* Find the directory where the SQLite integration plugin is installed.
43+
*
44+
* @return string|null The directory where the SQLite integration plugin is installed or null if not found.
45+
*/
46+
protected static function get_plugin_directory() {
47+
$plugin_folders = [
48+
ABSPATH . '/wp-content/plugins/sqlite-database-integration',
49+
ABSPATH . '/wp-content/mu-plugins/sqlite-database-integration',
50+
];
51+
52+
foreach ( $plugin_folders as $folder ) {
53+
if ( file_exists( $folder ) && is_dir( $folder ) ) {
54+
return $folder;
55+
}
56+
}
57+
58+
return null;
59+
}
60+
61+
/**
62+
* Load the necessary classes from the SQLite integration plugin.
63+
*
64+
* @return void
65+
* @throws WP_CLI\ExitException
66+
*/
67+
protected function load_dependencies() {
68+
$plugin_directory = self::get_plugin_directory();
69+
if ( ! $plugin_directory ) {
70+
WP_CLI::error( 'Could not locate the SQLite integration plugin.' );
71+
}
72+
73+
$sqlite_plugin_version = self::get_sqlite_plugin_version();
74+
if ( ! $sqlite_plugin_version ) {
75+
WP_CLI::error( 'Could not determine the version of the SQLite integration plugin.' );
76+
}
77+
78+
if ( version_compare( $sqlite_plugin_version, '2.1.11', '<' ) ) {
79+
WP_CLI::error( 'The SQLite integration plugin must be version 2.1.11 or higher.' );
80+
}
81+
82+
// Load the translator class from the plugin.
83+
if ( ! defined( 'SQLITE_DB_DROPIN_VERSION' ) ) {
84+
define( 'SQLITE_DB_DROPIN_VERSION', $sqlite_plugin_version ); // phpcs:ignore
85+
}
86+
87+
// We also need to selectively load the necessary classes from the plugin.
88+
require_once $plugin_directory . '/php-polyfills.php';
89+
require_once $plugin_directory . '/constants.php';
90+
require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-lexer.php';
91+
require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-query-rewriter.php';
92+
require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-translator.php';
93+
require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-token.php';
94+
require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-pdo-user-defined-functions.php';
95+
}
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+
}
115+
}

0 commit comments

Comments
 (0)