This commit is contained in:
2026-04-30 21:33:58 +02:00
parent 2639242ca6
commit 5a3948fee5
11 changed files with 635 additions and 113 deletions

View File

@@ -18,11 +18,12 @@ class XmlFeedImporter
* @param int $client_id
* @return array raport z polami: feed_url, fetched, updated, inserted, skipped, errors, peak_memory_mb, duration_ms
*/
static public function import_for_client( $client_id )
static public function import_for_client( $client_id, array $options = [] )
{
global $mdb;
$client_id = (int) $client_id;
$debug_offer_id = trim( (string) ( $options['debug_offer_id'] ?? '' ) );
$report = [
'feed_url' => '',
'fetched' => 0,
@@ -33,6 +34,18 @@ class XmlFeedImporter
'peak_memory_mb' => 0,
'duration_ms' => 0,
];
if ( $debug_offer_id !== '' )
{
$report['debug_offer'] = [
'offer_id' => $debug_offer_id,
'found_in_feed' => false,
'custom_label_1_raw_present' => false,
'custom_label_1_value' => '',
'title_present' => false,
'matched_existing_rows' => 0,
'updated_rows' => 0,
];
}
if ( $client_id <= 0 )
{
@@ -105,6 +118,22 @@ class XmlFeedImporter
continue;
}
if ( $debug_offer_id !== '' && (string) $item['offer_id'] === $debug_offer_id )
{
$report['debug_offer']['found_in_feed'] = true;
$report['debug_offer']['custom_label_1_raw_present'] = !empty( $item['custom_label_1_raw_present'] );
$report['debug_offer']['custom_label_1_value'] = (string) ( $item['custom_label_1'] ?? '' );
$report['debug_offer']['title_present'] = trim( (string) ( $item['title'] ?? '' ) ) !== '';
if ( !$report['debug_offer']['custom_label_1_raw_present'] )
{
$report['debug_offer']['missing_reason'] = 'custom_label_1_missing_in_feed';
}
else if ( trim( (string) $item['custom_label_1'] ) === '' )
{
$report['debug_offer']['missing_reason'] = 'custom_label_1_empty_in_feed';
}
}
$report['fetched']++;
$batch[] = $item;
@@ -134,6 +163,11 @@ class XmlFeedImporter
$reader -> close();
@unlink( $tmp_file );
if ( $debug_offer_id !== '' && empty( $report['debug_offer']['found_in_feed'] ) )
{
$report['debug_offer']['missing_reason'] = 'offer_id_missing_in_feed';
}
$mdb -> update( 'clients', [ 'xml_feed_last_sync_at' => date( 'Y-m-d H:i:s' ) ], [ 'id' => $client_id ] );
$report['peak_memory_mb'] = round( memory_get_peak_usage( true ) / 1024 / 1024, 1 );
@@ -233,7 +267,22 @@ class XmlFeedImporter
$description = trim( (string) $sxe -> description );
}
$custom_label_1 = isset( $g -> custom_label_1 ) ? trim( (string) $g -> custom_label_1 ) : '';
$custom_label_1_raw_present = isset( $g -> custom_label_1 );
$custom_label_1 = $custom_label_1_raw_present ? trim( (string) $g -> custom_label_1 ) : '';
if ( !$custom_label_1_raw_present && isset( $sxe -> custom_label_1 ) )
{
$custom_label_1_raw_present = true;
$custom_label_1 = trim( (string) $sxe -> custom_label_1 );
}
if ( !$custom_label_1_raw_present )
{
$custom_label_1_fallback = self::get_text_by_local_name( $doc, 'custom_label_1' );
if ( $custom_label_1_fallback !== null )
{
$custom_label_1_raw_present = true;
$custom_label_1 = trim( (string) $custom_label_1_fallback );
}
}
$price = null;
if ( isset( $g -> price ) )
@@ -251,10 +300,25 @@ class XmlFeedImporter
'title' => self::truncate( $title, 255 ),
'description' => $description,
'custom_label_1' => self::truncate( $custom_label_1, 255 ),
'custom_label_1_raw_present' => $custom_label_1_raw_present,
'price' => $price,
];
}
static private function get_text_by_local_name( \DOMDocument $doc, $local_name )
{
$local_name = (string) $local_name;
foreach ( $doc -> getElementsByTagName( '*' ) as $node )
{
if ( $node -> localName === $local_name )
{
return $node -> textContent;
}
}
return null;
}
static private function parse_price( $raw )
{
if ( $raw === '' )
@@ -341,15 +405,22 @@ class XmlFeedImporter
$updated_count = 0;
$inserted_count = 0;
$debug_offer_id = (string) ( $report['debug_offer']['offer_id'] ?? '' );
foreach ( $batch as $item )
{
$title = $item['title'] !== '' ? $item['title'] : null;
$desc = $item['description'] !== '' ? $item['description'] : null;
$cl1 = $item['custom_label_1'] !== '' ? $item['custom_label_1'] : null;
$price = $item['price'];
$is_debug_offer = $debug_offer_id !== '' && (string) $item['offer_id'] === $debug_offer_id;
if ( !empty( $existing[ $item['offer_id'] ] ) )
{
if ( $is_debug_offer )
{
$report['debug_offer']['matched_existing_rows'] = count( $existing[ $item['offer_id'] ] );
}
// aktualizujemy WSZYSTKIE legacy duplikaty (utrzymujemy spojnosc danych)
foreach ( $existing[ $item['offer_id'] ] as $row_id )
{
@@ -361,6 +432,10 @@ class XmlFeedImporter
':id' => $row_id,
] );
$updated_count++;
if ( $is_debug_offer )
{
$report['debug_offer']['updated_rows']++;
}
}
}
else
@@ -374,6 +449,10 @@ class XmlFeedImporter
':price' => $price,
] );
$inserted_count++;
if ( $is_debug_offer )
{
$report['debug_offer']['inserted'] = true;
}
}
}