From ba7e512bb3a0336793311060e37b9a9c445503a4 Mon Sep 17 00:00:00 2001 From: Matthias Lodner Date: Tue, 10 Jun 2025 02:31:23 +0200 Subject: [PATCH] Added new export record, path building and sql script to get data. --- seo_paths.sql | 10 +++++++++ src/main.rs | 59 +++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 55 insertions(+), 14 deletions(-) create mode 100644 seo_paths.sql diff --git a/seo_paths.sql b/seo_paths.sql new file mode 100644 index 0000000..a3f7c8d --- /dev/null +++ b/seo_paths.sql @@ -0,0 +1,10 @@ +-- SQL script to get shopware 5 seo paths with article number to later build new seo path for shopware 6 + +SELECT b.productID AS product_id, a.name, atri.attr1, LOWER(b.path) AS path, s.name AS shop_name, s.host, b.subshopID AS subshop_id +FROM s_articles as a +LEFT JOIN s_articles_attributes as atri ON a.id = atri.articleID +LEFT JOIN ( + SELECT seo.path, seo.org_path, SUBSTRING_INDEX(seo.org_path,'=',-1) AS productID, seo.subshopID + FROM `s_core_rewrite_urls` as seo + ) AS b ON a.id = b.productID +LEFT JOIN s_core_shops AS s on s.id = b.subshopID; diff --git a/src/main.rs b/src/main.rs index d3029b7..eb2af0f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,17 @@ use std::fs::File; use serde::{Deserialize, Serialize}; +#[derive(Deserialize, Serialize, Debug)] +struct ExportRecord { + product_id: String, + name: Option, + attr1: Option, + path: String, + shop_name: Option, + host: Option, + subshop_id: Option, +} + #[derive(Deserialize, Serialize, Debug)] struct Record { source_url: String, @@ -18,31 +29,51 @@ fn main() -> Result<(), csv::Error> { let mut records: Vec = Vec::new(); for result in rdr.deserialize() { - let mut record: Record = result?; - //println!("{:?}", record); + let record: ExportRecord = result?; + println!("{:?}", record); - let mut temp_target_url: Vec = record.source_url.split("/").map(|s| s.to_string()).collect(); + let mut temp_target_url: Vec = record.path.split("/").map(|s| s.to_string()).collect(); // Modify to new target url - temp_target_url.remove(temp_target_url.len() - 2); // Remove number before product name - + if temp_target_url[0].eq("gewuerze") { + temp_target_url.remove(0); + } + temp_target_url.insert(0, String::from("public/lgw")); // For testing environment + temp_target_url.remove(temp_target_url.len() - 2); // Remove number before product name + + if !record.attr1.clone().unwrap().eq("NULL") { + temp_target_url.push(record.attr1.unwrap()); // Add article number if not null + } + // Combine to new target url let target_url = temp_target_url.join("/"); - record.target_url = Some(target_url); - record.http_code = Some("301".to_string()); - record.enabled = Some("1".to_string()); - record.query_params_handling = Some("0".to_string()); - record.sales_channel_id = Some("".to_string()); - records.push(record) + // Build source url + let mut source_url_vec: Vec = Vec::new(); + source_url_vec.insert(0, String::from("public/lgw")); // For testing environment + source_url_vec.push(record.path); + + // Combine source url + let source_url = source_url_vec.join("/"); + + + records.push(Record{ + source_url, + target_url: Some(target_url), + http_code: Some("301".to_string()), + enabled: Some("1".to_string()), + query_params_handling: Some("".to_string()), + sales_channel_id: Some("".to_string()), + }) } - - let new_csv = File::create("redirects.csv")?; - let mut wtr = csv::Writer::from_writer(new_csv); + + let mut wtr = csv::WriterBuilder::new() + .delimiter(b';') + .from_writer(new_csv); for record in records { wtr.serialize(record)?;