Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Some code standard fixes
  • Loading branch information
jeroenpf committed Jul 19, 2024
commit f6b2da68d17f16b8ae0785e864a767d2bf93dc94
2 changes: 1 addition & 1 deletion src/DB_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ public function import( $args, $assoc_args ) {
}

// Check if SQLite is enabled and use it if it is.
if( WP_SQLite_Import::get_sqlite_version() ) {
if ( WP_SQLite_Import::get_sqlite_version() ) {
$importer = new WP_SQLite_Import();
$importer->run( $args[0], $args );
return;
Expand Down
12 changes: 6 additions & 6 deletions src/WP_SQLite_Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class WP_SQLite_Base {
*/
public static function get_sqlite_version() {
// Check if there is a db.php file in the wp-content directory.
if ( ! file_exists( ABSPATH . '/wp-content/db.php') ) {
if ( ! file_exists( ABSPATH . '/wp-content/db.php' ) ) {
return false;
}
// If the file is found, we need to check that it is the sqlite integration plugin.
Expand All @@ -27,20 +27,20 @@ protected function load_dependencies() {
}

// Load the translator class from the plugin.
if( ! defined( 'SQLITE_DB_DROPIN_VERSION' ) ) {
if ( ! defined( 'SQLITE_DB_DROPIN_VERSION' ) ) {
define( 'SQLITE_DB_DROPIN_VERSION', self::get_sqlite_version() );
}

# A hack to add the do_action and apply_filters functions to the global namespace.
# This is necessary because during the import WP has not been loaded, and we
# need to define these functions to avoid fatal errors.
if( ! function_exists( 'do_action') ) {
function do_action(){};
function apply_filters(){};
if ( ! function_exists( 'do_action' ) ) {
function do_action() {}
function apply_filters() {}
}

// We also need to selectively load the necessary classes from the plugin.
require_once $plugin_directory . '/php-polyfills.php';
require_once $plugin_directory . '/php-polyfills.php';
require_once $plugin_directory . '/constants.php';
require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-lexer.php';
require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-query-rewriter.php';
Expand Down
37 changes: 20 additions & 17 deletions src/WP_SQLite_Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,36 @@ public function run( $result_file, $args ) {
$this->load_dependencies();

$exclude_tables = isset( $args['exclude_tables'] ) ? explode( ',', $args['exclude_tables'] ) : [];
$exclude_tables = array_merge($exclude_tables , [
'_mysql_data_types_cache',
'sqlite_master',
'sqlite_sequence',
] );
$exclude_tables = array_merge(
$exclude_tables,
[
'_mysql_data_types_cache',
'sqlite_master',
'sqlite_sequence',
]
);

$include_tables = isset( $args['tables'] ) ? explode( ',', $args['tables'] ) : [];

$translator = new WP_SQLite_Translator();
$handle = fopen( 'export.sql', 'w' );

foreach ( $translator->query('SHOW TABLES') as $table ) {
foreach ( $translator->query( 'SHOW TABLES' ) as $table ) {

// Skip tables that are not in the include_tables list if the list is defined
if ( ! empty( $include_tables ) && ! in_array( $table->name, $include_tables ) ) {
if ( ! empty( $include_tables ) && ! in_array( $table->name, $include_tables, true ) ) {
continue;
}

// Skip tables that are in the exclude_tables list
if ( in_array( $table->name, $exclude_tables ) ) {
if ( in_array( $table->name, $exclude_tables, true ) ) {
continue;
}

fwrite($handle, "DROP TABLE IF EXISTS `" . $table->name ."`;\n");
fwrite( $handle, 'DROP TABLE IF EXISTS `' . $table->name . "`;\n" );
fwrite( $handle, $this->get_create_statement( $table, $translator ) . "\n" );

foreach( $this->get_insert_statements( $table, $translator->get_pdo() ) as $insert_statement ) {
foreach ( $this->get_insert_statements( $table, $translator->get_pdo() ) as $insert_statement ) {
fwrite( $handle, $insert_statement . "\n" );
}
}
Expand All @@ -66,15 +69,15 @@ public function run( $result_file, $args ) {
}

protected function get_create_statement( $table, $translator ) {
$create = $translator->query('SHOW CREATE TABLE ' . $table->name );
$create = $translator->query( 'SHOW CREATE TABLE ' . $table->name );
return $create[0]->{'Create Table'};
}

protected function get_insert_statements( $table, $pdo ) {
$stmt = $pdo->prepare('SELECT * FROM ' . $table->name );
$stmt = $pdo->prepare( 'SELECT * FROM ' . $table->name );
$stmt->execute();
while ( $row = $stmt->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT) ) {
yield sprintf("INSERT INTO `%1s` VALUES (%2s);", $table->name, $this->escape_values( $pdo, $row ));
while ( $row = $stmt->fetch( PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT ) ) {
yield sprintf( 'INSERT INTO `%1s` VALUES (%2s);', $table->name, $this->escape_values( $pdo, $row ) );
}
}

Expand All @@ -89,15 +92,15 @@ protected function get_insert_statements( $table, $pdo ) {
protected function escape_values( PDO $pdo, $values ) {
// Get a mysql PDO instance
$escaped_values = [];
foreach( $values as $value ) {
if( is_null( $value ) ) {
foreach ( $values as $value ) {
if ( is_null( $value ) ) {
$escaped_values[] = 'NULL';
} elseif ( is_numeric( $value ) ) {
$escaped_values[] = $value;
} else {
$escaped_values[] = $pdo->quote( $value );
}
}
return implode(",", $escaped_values );
return implode( ',', $escaped_values );
}
}
49 changes: 25 additions & 24 deletions src/WP_SQLite_Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public function run( $sql_file_path, $args ) {
$this->load_dependencies();
$translator = new WP_SQLite_Translator();
foreach ( $this->parse_statements( $sql_file_path ) as $statement ) {
$result = $translator->query($statement);
if ( $result === false ) {
WP_CLI::warning( "Could not execute statement: " . $statement );
$result = $translator->query( $statement );
if ( false === $result ) {
WP_CLI::warning( 'Could not execute statement: ' . $statement );
}
}
}
Expand All @@ -35,55 +35,56 @@ public function run( $sql_file_path, $args ) {
* @throws Exception
*/
public function parse_statements( $sql_file_path ) {
$handle = fopen($sql_file_path, 'r');
if (!$handle) {
throw new Exception("Unable to open file: $sql_file_path");
$handle = fopen( $sql_file_path, 'r' );
if ( ! $handle ) {
throw new Exception( "Unable to open file: $sql_file_path" );
}

$single_quotes = 0;
$double_quotes = 0;
$in_comment = false;
$buffer = '';
$in_comment = false;
$buffer = '';

while (($line = fgets($handle)) !== false) {
$line = trim($line);
while ( ( $line = fgets( $handle ) ) !== false ) {
$line = trim( $line );

// Skip empty lines and comments
if (empty($line) || strpos($line, '--') === 0 || strpos($line, '#') === 0) {
if ( empty( $line ) || strpos( $line, '--' ) === 0 || strpos( $line, '#' ) === 0 ) {
continue;
}

// Handle multi-line comments
if (! $in_comment && strpos($line, '/*') === 0) {
if ( ! $in_comment && strpos( $line, '/*' ) === 0 ) {
$in_comment = true;
}
if ($in_comment) {
if (strpos($line, '*/') !== false) {
if ( $in_comment ) {
if ( strpos( $line, '*/' ) !== false ) {
$in_comment = false;
}
continue;
}

for ($i = 0; $i < strlen($line); $i++) {
$ch = $line[$i];
$strlen = strlen( $line );
for ( $i = 0; $i < $strlen; $i++ ) {
$ch = $line[ $i ];

// Handle escaped characters
if ($i > 0 && $line[$i - 1] == '\\') {
if ( $i > 0 && '\\' === $line[ $i - 1 ] ) {
$buffer .= $ch;
continue;
}

// Handle quotes
if ($ch == "'" && $double_quotes == 0) {
if ( "'" === $ch && 0 === $double_quotes ) {
$single_quotes = 1 - $single_quotes;
}
if ($ch == '"' && $single_quotes == 0) {
if ( '"' === $ch && 0 === $single_quotes ) {
$double_quotes = 1 - $double_quotes;
}

// Process statement end
if ($ch == ';' && $single_quotes == 0 && $double_quotes == 0) {
yield trim($buffer);
if ( ';' === $ch && 0 === $single_quotes && 0 === $double_quotes ) {
yield trim( $buffer );
$buffer = '';
} else {
$buffer .= $ch;
Expand All @@ -92,10 +93,10 @@ public function parse_statements( $sql_file_path ) {
}

// Handle any remaining buffer content
if (!empty($buffer)) {
yield trim($buffer);
if ( ! empty( $buffer ) ) {
yield trim( $buffer );
}

fclose($handle);
fclose( $handle );
}
}