@@ -347,7 +347,7 @@ protected function sqlite_export( $file, $assoc_args ) {
347347 }
348348 }
349349
350- $ command = Utils \esc_cmd ( 'sqlite3 %s .dump > %s ' , $ temp_db, $ export_db );
350+ $ command = Utils \esc_cmd ( 'sqlite3 %s .dump ' , $ temp_db );
351351
352352 WP_CLI ::debug ( "Running shell command: {$ command }" , 'db ' );
353353
@@ -360,18 +360,15 @@ protected function sqlite_export( $file, $assoc_args ) {
360360 $ stdout = ( '- ' === $ file );
361361
362362 if ( $ stdout ) {
363- readfile ( $ export_db );
364- unlink ( $ export_db );
365- } elseif ( ! @rename ( $ export_db , $ file ) ) {
366- // Clean up temporary files and surface a clear error if the export cannot be moved.
367- if ( file_exists ( $ export_db ) ) {
368- unlink ( $ export_db );
369- }
363+ WP_CLI ::line ( $ result ->stdout );
364+ } elseif ( false === file_put_contents ( $ file , $ result ->stdout ) ) {
365+ // Clean up temporary file and surface a clear error if the export cannot be written.
370366 if ( file_exists ( $ temp_db ) ) {
371367 unlink ( $ temp_db );
372368 }
373- WP_CLI ::error ( "Could not move exported database to ' {$ file }'. Please check that the path is writable and on the same filesystem . " );
369+ WP_CLI ::error ( "Could not write exported database to ' {$ file }'. Please check that the path is writable. " );
374370 }
371+
375372 unlink ( $ temp_db );
376373
377374 if ( ! $ stdout ) {
@@ -421,26 +418,32 @@ protected function sqlite_import( $file, $assoc_args ) {
421418 $ import_file = tempnam ( sys_get_temp_dir (), 'temp.db ' );
422419 file_put_contents ( $ import_file , $ contents );
423420
424- // Build sqlite3 command as an argument array to avoid shell injection .
425- $ command = array ( 'sqlite3 ' ) ;
421+ // Build sqlite3 command.
422+ $ command_parts = [ 'sqlite3 ' ] ;
426423
427424 if ( ! Utils \get_flag_value ( $ assoc_args , 'skip-optimization ' ) ) {
428- $ command [] = '-cmd ' ;
429- $ command [] = 'PRAGMA foreign_keys=OFF; ' ;
430- $ command [] = '-cmd ' ;
431- $ command [] = 'PRAGMA ignore_check_constraints=ON; ' ;
432- $ command [] = '-cmd ' ;
433- $ command [] = 'PRAGMA synchronous=OFF; ' ;
434- $ command [] = '-cmd ' ;
435- $ command [] = 'PRAGMA journal_mode=MEMORY; ' ;
425+ $ command_parts [] = '-cmd ' ;
426+ $ command_parts [] = 'PRAGMA foreign_keys=OFF; ' ;
427+ $ command_parts [] = '-cmd ' ;
428+ $ command_parts [] = 'PRAGMA ignore_check_constraints=ON; ' ;
429+ $ command_parts [] = '-cmd ' ;
430+ $ command_parts [] = 'PRAGMA synchronous=OFF; ' ;
431+ $ command_parts [] = '-cmd ' ;
432+ $ command_parts [] = 'PRAGMA journal_mode=MEMORY; ' ;
436433 }
437434
438- $ command = implode ( ' ' , array_map ( 'escapeshellarg ' , $ command ) );
439- $ command .= ' ' . escapeshellarg ( $ db_path ) . ' < ' . escapeshellarg ( $ import_file );
435+ $ command_parts [] = $ db_path ;
436+
437+ $ command = Utils \esc_cmd (
438+ implode ( ' ' , array_fill ( 0 , count ( $ command_parts ), '%s ' ) ),
439+ ...$ command_parts
440+ );
441+
442+ $ command .= ' < ' . escapeshellarg ( $ import_file );
440443
441444 WP_CLI ::debug ( "Running shell command: {$ command }" , 'db ' );
442445
443- $ result = \WP_CLI \Process ::create ( $ command , null , null )->run ();
446+ $ result = \WP_CLI \Process ::create ( $ command , null , null )->run ( file_get_contents ( $ import_file ) );
444447
445448 unlink ( $ import_file );
446449
0 commit comments