insert( Ailens_Schema::hits_table(), array( 'crawler_id' => $hit->crawlerId, 'category' => $hit->category, 'url_path' => $hit->urlPath, 'hit_at' => $hit->hitAt->format( 'Y-m-d H:i:s' ), 'verified' => $hit->verified ? 1 : 0, 'source' => $hit->source, 'ip_hash' => $hit->ipHash, ), array( '%s', '%s', '%s', '%s', '%d', '%s', '%s' ) ); } /** * 重複排除付き一括取り込み(ログインポート用)。 * 重複キー: crawler_id + url_path + hit_at(秒) + ip_hash(承認済み仕様)。 * * @param iterable $hits 取り込むヒット群(HitRecordのiterable)。 * @return int 実際に挿入した件数。 */ public function importHits( iterable $hits ): int { global $wpdb; $table = Ailens_Schema::hits_table(); $inserted = 0; foreach ( $hits as $hit ) { $hit_at = $hit->hitAt->format( 'Y-m-d H:i:s' ); if ( null === $hit->ipHash ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.InterpolatedNotPrepared $exists = $wpdb->get_var( $wpdb->prepare( "SELECT 1 FROM {$table} WHERE crawler_id = %s AND url_path = %s AND hit_at = %s AND ip_hash IS NULL LIMIT 1", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared $hit->crawlerId, $hit->urlPath, $hit_at ) ); } else { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.InterpolatedNotPrepared $exists = $wpdb->get_var( $wpdb->prepare( "SELECT 1 FROM {$table} WHERE crawler_id = %s AND url_path = %s AND hit_at = %s AND ip_hash = %s LIMIT 1", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared $hit->crawlerId, $hit->urlPath, $hit_at, $hit->ipHash ) ); } if ( $exists ) { continue; } $this->insertHit( $hit ); ++$inserted; } return $inserted; } }