header(); fwrite( $stream, "\xEF\xBB\xBF" ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fwrite CsvSafe::put( $stream, CsvSafe::row( array_merge( $header, self::EXTRA ) ), ',' ); if ( ! FileStore::hasSource( $p->ref() ) ) { return 0; } $reader = new CsvReader( $p->path(), $p->delimiter() ); $after = 0; $count = 0; $table = \Rotosix\CatalogRelay\Lifecycle\Schema::table(); $in = 'all' === $status ? "'failed', 'skipped'" : ( 'skipped' === $status ? "'skipped'" : "'failed'" ); do { $rows = (array) $wpdb->get_results( $wpdb->prepare( "SELECT row_no, byte_offset, code, message FROM %i WHERE job_id = %d AND status IN ($in) AND row_no > %d ORDER BY row_no LIMIT 500", $table, $rootId, $after ), ARRAY_A ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- fixed fragment. foreach ( $rows as $r ) { $after = (int) $r['row_no']; $cells = $reader->at( (int) $r['byte_offset'] ); if ( null === $cells ) { continue; } $cells = array_pad( array_map( 'strval', $cells ), count( $header ), '' ); CsvSafe::put( $stream, CsvSafe::row( array_merge( array_slice( $cells, 0, count( $header ) ), array( (string) $r['row_no'], (string) $r['code'], Messages::text( (string) $r['code'], (string) $r['message'] ) ) ) ), ',' ); ++$count; } $n_rows = count( $rows ); } while ( 500 === $n_rows ); return $count; } /** * Save the failed-rows CSV (done when a job ends), so it can still be downloaded once the import file is deleted * after its retention period (FR-004). It is kept as long as the row log. * * @param int $rootId Root job id. * @param JobParams $p Params. * @return int Rows. */ public static function writeFile( int $rootId, JobParams $p ): int { if ( ! FileStore::hasSource( $p->ref() ) ) { return 0; } if ( 0 === JobRowsRepo::failedCount( $rootId ) ) { $old = (string) FileStore::reportPath( $rootId ); if ( is_file( $old ) ) { wp_delete_file( $old ); // A retry fixed every row: no stale copy. } return 0; } $path = FileStore::reportPath( $rootId, true ); $stream = null === $path ? false : fopen( $path . '.part', 'wb' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen if ( ! $stream ) { return 0; } $n = self::failedRows( $rootId, $p, $stream ); fclose( $stream ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose // Written aside and moved in place: a process that dies while writing leaves the previous copy, not a cut one. rename( $path . '.part', $path ); // phpcs:ignore WordPress.WP.AlternativeFunctions.rename_rename return $n; } /** * Send the saved failed-rows CSV when the import file is gone (the rows can no longer be read back from it). * * @param int $rootId Root job id. * @param JobParams $p Params. * @param resource $stream Output. * @return bool Whether the saved copy was sent. */ public static function sendSaved( int $rootId, JobParams $p, $stream ): bool { $path = FileStore::reportPath( $rootId ); if ( FileStore::hasSource( $p->ref() ) || null === $path || ! is_file( $path ) ) { return false; } $in = fopen( $path, 'rb' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen if ( ! $in ) { return false; } stream_copy_to_stream( $in, $stream ); fclose( $in ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose return true; } /** * Issue list: every failed, warning and skipped row with code and text (no cell values). * * @param int $jobId Job. * @param resource $stream Output. * @return int */ public static function issues( int $jobId, $stream ): int { fwrite( $stream, "\xEF\xBB\xBF" ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fwrite CsvSafe::put( $stream, array( 'row', 'severity', 'code', 'message', 'product_id' ) ); $after = 0; $id = 0; $n = 0; do { $page = JobRowsRepo::page( $jobId, array( 'status' => array( 'failed', 'warning', 'skipped' ), 'after_row' => $after, 'after_id' => $id, 'limit' => 200, ) ); foreach ( $page['rows'] as $r ) { CsvSafe::put( $stream, CsvSafe::row( array( $r['row_no'], $r['status'], $r['code'], Messages::text( (string) $r['code'], (string) $r['message'] ), $r['product_id'] ) ) ); ++$n; } $after = $page['next'] ? $page['next']['row'] : 0; $id = $page['next'] ? $page['next']['id'] : 0; } while ( $page['next'] ); return $n; } /** * Diff export of a dry run: one line per changed field. * * @param int $jobId Job. * @param resource $stream Output. * @return int */ public static function diff( int $jobId, $stream ): int { fwrite( $stream, "\xEF\xBB\xBF" ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fwrite CsvSafe::put( $stream, array( 'row', 'product_id', 'field', 'old', 'new' ) ); $after = 0; $id = 0; $n = 0; do { $page = JobRowsRepo::page( $jobId, array( 'status' => array( 'changed' ), 'after_row' => $after, 'after_id' => $id, 'limit' => 200, ) ); foreach ( $page['rows'] as $r ) { $changes = json_decode( (string) $r['diff'], true ); foreach ( (array) $changes as $c ) { if ( ! is_array( $c ) ) { continue; } CsvSafe::put( $stream, CsvSafe::row( array( $r['row_no'], $r['product_id'], $c['f'] ?? '', $c['old'] ?? '', $c['new'] ?? '' ) ) ); ++$n; } } $after = $page['next'] ? $page['next']['row'] : 0; $id = $page['next'] ? $page['next']['id'] : 0; } while ( $page['next'] ); return $n; } /** * Conflict list of a rollback preview or a finished rollback. * * @param int $jobId Preview job. * @param resource $stream Output. * @return int */ public static function conflicts( int $jobId, $stream ): int { fwrite( $stream, "\xEF\xBB\xBF" ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fwrite CsvSafe::put( $stream, array( 'ledger_row', 'product_id', 'status', 'code', 'field', 'current', 'imported', 'before' ) ); $after = 0; $id = 0; $n = 0; do { $page = JobRowsRepo::page( $jobId, array( 'status' => array( 'conflict', 'skipped' ), 'after_row' => $after, 'after_id' => $id, 'limit' => 200, ) ); foreach ( $page['rows'] as $r ) { $d = json_decode( (string) $r['diff'], true ); $d = is_array( $d ) ? $d : array(); CsvSafe::put( $stream, CsvSafe::row( array( $r['row_no'], $r['product_id'], $r['status'], $r['code'], $r['fields'], self::flat( $d['cur'] ?? '' ), self::flat( $d['imported'] ?? '' ), self::flat( $d['snapshot'] ?? '' ) ) ) ); ++$n; } $after = $page['next'] ? $page['next']['row'] : 0; $id = $page['next'] ? $page['next']['id'] : 0; } while ( $page['next'] ); return $n; } /** * A preview value as one cell (prices are {reg, sale}). * * @param mixed $v Value. * @return string */ private static function flat( $v ): string { if ( is_array( $v ) ) { return 'regular ' . ( $v['reg'] ?? '' ) . ' / sale ' . ( $v['sale'] ?? '' ); } return (string) $v; } }