mode() ) { return RollbackPlanner::nextChunk( $job, $cursor, ChunkTuner::next( $job->id, $maxItems ) ); } return $this->readChunk( $job, $cursor, $maxItems ); } /** * Process one row (or one ledger row of a rollback preview). * * @param Job $job Job. * @param ItemRef $item Item. * @return ItemResult */ public function processItem( Job $job, ItemRef $item ): ItemResult { $t0 = microtime( true ); $p = JobParams::of( $job ); DryRunGuard::enter(); try { if ( 'rollback_preview' === $p->mode() ) { $out = RollbackPlanner::process( $job, $item ); } else { $out = $this->processRow( $job, $p, $item ); } } finally { DryRunGuard::leave(); } ChunkTuner::observe( $job->id, ( microtime( true ) - $t0 ) * 1000 ); return $out; } /** * A CSV row. * * @param Job $job Job. * @param JobParams $p Params. * @param ItemRef $item Item. * @return ItemResult */ private function processRow( Job $job, JobParams $p, ItemRef $item ): ItemResult { $ctx = RowParser::parse( (array) $item->payload['cells'], (int) $item->payload['row'], (int) $item->payload['off'], $p ); $plan = ( new RowPlanner() )->plan( $ctx, $p, ScanState::load( $p ), true, $job->id ); $res = $this->report( $plan ); $res->warnings = $plan->warnings; $out = $this->recordResult( $job, $ctx, $res ); // As the import records the row. if ( 'import_diff' === $p->mode() && ( RowPlan::UPDATE === $plan->kind || RowPlan::CREATE === $plan->kind ) && $plan->changes ) { $this->storeDiff( $job->id, $ctx->row, $ctx->offset, $plan ); } return $out; } /** * What the import would do, as a result object. * * @param RowPlan $plan Plan. * @return RowResult */ private function report( RowPlan $plan ): RowResult { switch ( $plan->kind ) { case RowPlan::FAIL: return RowResult::failed( $plan->code, $plan->detail ); case RowPlan::SKIP: return RowResult::skipped( $plan->code, $plan->detail ); case RowPlan::CREATE: return RowResult::of( RowResult::CREATED, 0, $plan->parentId ); case RowPlan::UPDATE: return RowResult::of( RowResult::UPDATED, $plan->product ? (int) $plan->product->get_id() : 0 ); } return RowResult::of( RowResult::UNCHANGED, $plan->product ? (int) $plan->product->get_id() : 0 ); } /** * Store the changed fields of a row (up to the cap, the rest is only counted). * * @param int $jobId Job. * @param int $row Row. * @param int $offset Offset. * @param RowPlan $plan Plan. * @return void */ private function storeDiff( int $jobId, int $row, int $offset, RowPlan $plan ): void { $cap = (int) apply_filters( 'catalogrelay_diff_cap', self::DIFF_CAP ); if ( JobRowsRepo::counter( $jobId, 'diff_rows' ) >= $cap ) { JobRowsRepo::bump( $jobId, 'diff_overflow' ); return; } $changes = array(); foreach ( $plan->changes as $c ) { $changes[] = array( 'f' => $c['f'], 'old' => self::clip( (string) $c['old'] ), 'new' => self::clip( (string) $c['new'] ), ); } // A replayed row (interrupted, or retried after an error) replaces the diff it stored, which may name other fields // when the products changed meanwhile: listed and counted once. $stored = JobRowsRepo::forget( $jobId, $row, array( 'changed' ) ); if ( JobRowsRepo::record( $jobId, $row, $offset, 'changed', RowPlan::CREATE === $plan->kind ? 'create' : 'update', '', $plan->product ? (int) $plan->product->get_id() : 0, $plan->fieldNames(), (string) wp_json_encode( $changes, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ) ) && 0 === $stored ) { JobRowsRepo::bump( $jobId, 'diff_rows' ); } } /** * Cut long values. * * @param string $v Value. * @return string */ private static function clip( string $v ): string { return function_exists( 'mb_substr' ) ? mb_substr( $v, 0, 200 ) : substr( $v, 0, 200 ); } /** * Row-by-row verdicts are reconciled by running the row again (read-only). * * @param Job $job Job. * @param ItemRef $item Item. * @return int */ public function reconcile( Job $job, ItemRef $item ): int { return \Rotosix\CatalogRelay\Vendor\Core\Jobs\ReconcileResult::NOT_APPLIED; } /** * The preview ended: totals, content hash, sealed plan. * * @param Job $job Job. * @param array $summary Summary. * @return void */ public function finish( Job $job, array $summary ): void { $p = JobParams::of( $job ); $totals = 'rollback_preview' === $p->mode() ? RollbackPlanner::totals( $job->id, $summary ) : self::previewTotals( $job->id, $summary ); JobRowsRepo::sentinel( $job->id, 'totals', $totals ); if ( ! in_array( $summary['status'], array( Job::COMPLETED, Job::COMPLETED_WITH_FAILURE ), true ) ) { return; } if ( 'rollback_preview' === $p->mode() && (int) ( $summary['failed'] ?? 0 ) > 0 ) { return; // Ledger rows the job engine failed on were never judged: such a preview cannot be confirmed (run it again). } $hash = self::contentHash( $job->id, $totals ); $meta = array( 'job_id' => $job->id, 'upload_ref' => $p->ref(), 'sha256' => $p->sha256(), 'config_hash' => $p->configHash(), 'delimiter' => $p->delimiter(), 'generated_at' => gmdate( 'Y-m-d H:i:s' ), 'source_job' => (int) ( $p->data['source_job'] ?? 0 ), ); $plan_id = PlanBuilder::sealPreview( 'catalogrelay.' . $p->mode(), $totals, $hash, $meta ); JobRowsRepo::sentinel( $job->id, 'plan', array( 'plan_id' => $plan_id, 'hash' => $hash, 'mode' => $p->mode(), ) + $meta ); } /** * Totals of a row preview. * * @param int $jobId Job. * @param array $summary Summary. * @return array */ public static function previewTotals( int $jobId, array $summary ): array { $base = ImportJobType::totals( $jobId, $summary ); $base['errors'] = JobRowsRepo::codeCounts( $jobId, 'failed' ); $base['warning_codes'] = JobRowsRepo::codeCounts( $jobId, 'warning' ); $base['diff_rows'] = JobRowsRepo::counter( $jobId, 'diff_rows' ); $base['diff_overflow'] = JobRowsRepo::counter( $jobId, 'diff_overflow' ); return $base; } /** * Hash over the totals and every stored finding in row order. * * @param int $jobId Job. * @param array $totals Totals. * @return string */ public static function contentHash( int $jobId, array $totals ): string { $h = hash_init( 'sha256' ); hash_update( $h, (string) wp_json_encode( $totals ) ); $after = 0; $id = 0; do { $page = JobRowsRepo::page( $jobId, array( 'after_row' => $after, 'after_id' => $id, 'limit' => 200, ) ); foreach ( $page['rows'] as $r ) { hash_update( $h, $r['row_no'] . '|' . $r['status'] . '|' . $r['code'] . '|' . $r['product_id'] . '|' . $r['fields'] . '|' . $r['diff'] . "\n" ); } $after = $page['next'] ? $page['next']['row'] : 0; $id = $page['next'] ? $page['next']['id'] : 0; } while ( $page['next'] ); return hash_final( $h ); } }