Skip to content

Commit 8556960

Browse files
authored
Merge pull request #15 from Automattic/update/speed-up-encoding-check-as-fallback
Speedup encoding check by executing it as a fallback on execute_statements
2 parents 5d7507f + cfdd79c commit 8556960

1 file changed

Lines changed: 15 additions & 10 deletions

File tree

‎src/Import.php‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,21 @@ public function run( $sql_file_path, $args ) {
5151
*/
5252
protected function execute_statements( $import_file ) {
5353
foreach ( $this->parse_statements( $import_file ) as $statement ) {
54-
$result = $this->driver->query( $statement );
55-
if ( false === $result ) {
56-
WP_CLI::warning( 'Could not execute statement: ' . $statement );
57-
echo $this->driver->get_error_message();
54+
try {
55+
$this->driver->query( $statement );
56+
} catch ( Exception $e ) {
57+
try {
58+
// Try converting encoding and retry
59+
$detected_encoding = mb_detect_encoding( $statement, mb_list_encodings(), true );
60+
if ( $detected_encoding && 'UTF-8' !== $detected_encoding ) {
61+
$converted_statement = mb_convert_encoding( $statement, 'UTF-8', $detected_encoding );
62+
echo 'Converted ecoding for statement: ' . $converted_statement . PHP_EOL;
63+
$this->driver->query( $converted_statement );
64+
}
65+
} catch ( Exception $e ) {
66+
WP_CLI::warning( 'Could not execute statement: ' . $statement );
67+
echo $e->getMessage();
68+
}
5869
}
5970
}
6071
}
@@ -79,12 +90,6 @@ public function parse_statements( $sql_file_path ) {
7990

8091
// phpcs:ignore
8192
while ( ( $line = fgets( $handle ) ) !== false ) {
82-
// Detect and convert encoding to UTF-8
83-
$detected_encoding = mb_detect_encoding( $line, mb_list_encodings(), true );
84-
if ( $detected_encoding && 'UTF-8' !== $detected_encoding ) {
85-
$line = mb_convert_encoding( $line, 'UTF-8', $detected_encoding );
86-
}
87-
8893
$strlen = strlen( $line );
8994
for ( $i = 0; $i < $strlen; $i++ ) {
9095
$ch = $line[ $i ];

0 commit comments

Comments
 (0)