From a10565d738a3193c8c87ebc16606a1af50373864 Mon Sep 17 00:00:00 2001 From: Paul <1+paul@noreply.localhost> Date: Sat, 29 Aug 2026 04:40:48 +0000 Subject: [PATCH] =?UTF-8?q?T=C3=A9l=C3=A9verser=20les=20fichiers=20vers=20?= =?UTF-8?q?"/"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 43 +++- logo.png | Bin 0 -> 2300 bytes wordpress_import.php | 568 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 609 insertions(+), 2 deletions(-) create mode 100644 logo.png create mode 100644 wordpress_import.php diff --git a/README.md b/README.md index 9ecb5c5..144aa22 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,42 @@ -# PHPBoost-WordPress-Import +# Module WordPress 7.0 → PHPBoost Importer -Script de migration depuis WordPress 7.0 vers PHPBoost. \ No newline at end of file +**⚠️ WORK IN PROGRESS !** +**Ce script n'est pour le moment PAS 100% fonctionnel, à tester en environnement adéquat.** +**Développement en cours, il n'a pas encore été testé avec WordPress en version 7.0.** + +Ce module permet d'importer le contenu d'un site **WordPress 7.0** vers une installation PHPBoost. +Il importe les **utilisateurs**, **articles**, **catégories d'articles**, **pages**, **commentaires** et **médias** (images). + +--- + +## 📌 Prérequis + +1. **Accès aux bases de données** : + - Le script doit pouvoir se connecter aux bases de données **WordPress** et **PHPBoost** avec des droits en **lecture/écriture**. + - Les identifiants de connexion doivent être configurés dans le fichier `wordpress_import.php`. + +2. **Dossier de destination pour les médias** : + - Le dossier `PBOOST_UPLOAD_DIR` (ex: `/var/www/phpboost/upload/`) doit exister et être **accessible en écriture** par le script. + +3. **PHP 7.4 ou supérieur** : + - Le script utilise des fonctions modernes de PHP. + +--- + +## 🛠 Installation + +1. **Copier le script** `wordpress_import.php` à la racine de votre installation WordPress. +2. **Configurer le script** : + - Éditez le fichier `wordpress_import.php` et mettez à jour les constantes suivantes : + - `WP_DB_HOST`, `WP_DB_NAME`, `WP_DB_USER`, `WP_DB_PASS`, `WP_DB_PREFIX` (pour WordPress). + - `PBOOST_DB_HOST`, `PBOOST_DB_NAME`, `PBOOST_DB_USER`, `PBOOST_DB_PASS`, `PBOOST_DB_PREFIX` (pour PHPBoost). + - `WP_UPLOAD_DIR` : Chemin absolu vers le dossier `wp-content/uploads/` de WordPress. + - `PBOOST_UPLOAD_DIR` : Chemin absolu vers le dossier de destination des médias dans PHPBoost (ex: `/var/www/phpboost/upload/`). + +3. **Exécuter le script** : + - Via un navigateur : `https://votre-site-wordpress.fr/wordpress_import.php` + - Via la ligne de commande : `sudo -u www-data php /chemin/vers/wordpress_import.php` + +--- +## 📧 Infos +Pour toute question, [contacte-moi](https://geekonweb.fr/about) ! \ No newline at end of file diff --git a/logo.png b/logo.png new file mode 100644 index 0000000000000000000000000000000000000000..dda8fd9b100863ab3e95b2781a14ab861728cb7b GIT binary patch literal 2300 zcmVfsZ0DHFpKW_&_azSN>MV7@gkHG+QvjC{w0C%+jH);Sn zYydK6061#^KW?MP-}dF%(-%5#gPPH&GZSAN;>`OfS2X~gJaewuj6>V%oDm#Dgem#u-b)NH8EQ@!Pu z)b2&O<58N&w$tV~UxeM`=qy-&htBLAPkFK5@@S>a0BWzaRFx0_00;C*L_t(|ob8>7 zTIxC!g@J;$6$)jLg9CyS9_M*H9`E}<)oszMK-)CK-q}fS*8^z(uUTZTbSJ4)N|h?L zq=Umf>PJA;-tVs8J3OL(3^W}bhJLM9^TR9ZC&AJ1su%Oc@&?D$PlKRi->dzo|FTQ{ zK)~9432RI8dlzHshXU02BJ^vk8g#Fzp9@Ua;UK{mt6Q5;KN^T85lNQ9jaTZ&Q`YWA zn8`P{u$OMSM2ycp?*+F?Y(CkaCxYPCpOyg=p=W5E-?0PhSU!% zsNszOUo6i%pnhf{9SppDs1oyd@fs{164X^>z2~W?6_$tR9v;orz*X!pa z3ckIUJ`P`!u+eTmXy;SNND_qAYGe9KFW^O8szSk3M1M(2!*>R}B zfP93}Ys9eQfer(562^8s2euI|>$lwLRf13~S0QDj?@olhx17H(MHn-37IIfT48B_T_fI!T zP8lyr$USqQO!0%|jv#hNku+iS8hJh+BNSRW=y*_5k@m0yURJXOWGnro(8AmWT(3|gxwlnliGEE#g3K`4tm^; zhO05u3PL};5NQ@&UP`cmurJ*3ySN-=KB4DJG;J@mJ(x}?-FW}KENeEQA1ZWfe=V&W zP1t=`>$pGYdPWlh?3#$%0F5O~$w)GK-RK!g2)CJVa$+K(hqocr2tw3dga!~IZ_K7A z#NGr#O^Cmdhn7&sAs3W{dN$M%5{hReDUlIs+t^A(sBv>L385dJ=-bgsL8$kT0R4ns zSMSbp-GurN>(ER{aNvqoLXtysG!hb>w4#lW?C>gGgmedx=^>;%L{0}G?LmO}gw%(Q zpc9fG9D__a=rc;Rt$i>IhENIpkdcwro`NPEN45|N*@tl52Ci#4Au}Uwb~=t@H{qbq zDABfCf$P{!$o!79wm;{ynlOA}8L;!oXE7l&BRxCcfVL8bJ+=XzA6ZH`%P!HzcY*8J zPnfNpc!nr2;i3VHNO;-|oP-KQIJ^y3-(Rr^H{DJOUvUVxS_uOdiZIGZ8GHpHOgCV0 z2%j?ySQNsqj8s^{@qP}U?Swm7KcEF6j7qdzKAQCId5Ve=ERT7qj&?*T_!h}k~k`P=aVM&-!N%;RFY@y>tx@;2?y6AY7%1{6hoY3h! zqXSm%NeLYXdt1rqQNV>5gl-G>Ecl1E$Qwj?hi-6(cNYAwsn83=A&k7(*}>skyRVDA zKrBMX?KoTTe8Wp|J}km`s6IiT1-~w6KxD$0@fPw|^=#UZ0z@Q??C5Sn@r@)5sDUtg zjV%~E{*mTGCX5;P)pynWt%}YN8VToiRO36FD!f1)gpnN`g;I5NURS}>L+Cg`Yg-cV zLGlMwH(@kXKV%9y@e+-UFlH1k+5n)cQ8mUy-rV!@jDp(s55yrWSQOH65 zlu{ASa}}eIgPRVkb)I_hiN7aGh>?-2Mwc+ZsEsLCmBGx{VXY_ z9aW=+;6-d%TSyeeULaSAhZ1*NGIt@!i&@CYM|`R4f>}FBlo*+Ilv<`~Hzcnn+e&W7 z0}(S_Rq1P-}dzQxoD7=>J9M{Y)d`?`7C_66m3SPSI5 zpDo&TkUQ7;wqA%cGIJHrrzeQ}9lx}LGgRFUqmaw}dDwZ9;3IH?%E-B-91!d*ss&;HZu$#EqLdsR_1@g1U_7tQ<;}cS@ za)YLZLCC4ZQ8iGEpp8dL;@?UuCcwrcWt}1wa?D(%StPkiv#@fNYU2^0@N$)g9(hiY z@h`B&+)Dp0~G7C9FGm?xpQwuq?J>V2d z$yMynX%=d=@re0J)KV=R%Wp+SlF@&;%94yEqxtn1AlsqMRg|l&%1AOQ^7|`=rGEjh Wd?z4l6l<#h0000connect_error) { + die("ERREUR : Impossible de se connecter à la base WordPress : " . $wp_db->connect_error); +} + +$pboost_db = new mysqli(PBOOST_DB_HOST, PBOOST_DB_USER, PBOOST_DB_PASS, PBOOST_DB_NAME); +if ($pboost_db->connect_error) { + die("ERREUR : Impossible de se connecter à la base PHPBoost : " . $pboost_db->connect_error); +} + +// ============================================= +// 3. FONCTIONS UTILITAIRES +// ============================================= + +/** + * Nettoie un titre pour en faire un identifiant URL. + */ +function sanitizeTitle($title) { + $title = strtolower($title); + $title = preg_replace('/[^a-z0-9]+/', '-', $title); + $title = trim($title, '-'); + return $title; +} + +/** + * Vérifie si une colonne existe dans une table. + */ +function checkColumnExists($db, $table, $column) { + $query = "SHOW COLUMNS FROM $table LIKE '$column'"; + $result = $db->query($query); + return ($result && $result->num_rows > 0); +} + +/** + * Récupère les métadonnées d'un post WordPress. + */ +function getPostMeta($db, $post_id) { + $meta = []; + $query = "SELECT meta_key, meta_value FROM " . WP_DB_PREFIX . "postmeta WHERE post_id = $post_id"; + $result = $db->query($query); + if ($result) { + while ($row = $result->fetch_assoc()) { + $meta[$row['meta_key']] = $row['meta_value']; + } + } + return $meta; +} + +// ============================================= +// 4. RAPPORT D'IMPORT +// ============================================= + +$report = [ + 'users' => ['success' => 0, 'errors' => 0], + 'posts' => ['success' => 0, 'errors' => 0], + 'pages' => ['success' => 0, 'errors' => 0], + 'comments' => ['success' => 0, 'errors' => 0], + 'media' => ['success' => 0, 'errors' => 0, 'manual_copy' => []], +]; + +// ============================================= +// 5. IMPORT DES UTILISATEURS (version corrigée pour ta structure) +// ============================================= + +echo "

Importation des utilisateurs...

"; +$first_user_id = null; +$user_mapping = []; // WordPress User ID => PHPBoost User ID + +// Récupérer le premier utilisateur WordPress pour $first_user_id +$first_user_query = "SELECT ID FROM " . WP_DB_PREFIX . "users ORDER BY ID ASC LIMIT 1"; +$first_user_result = $wp_db->query($first_user_query); +if ($first_user_result && $first_user_result->num_rows > 0) { + $first_user = $first_user_result->fetch_assoc(); + $first_user_id = $first_user['ID']; +} else { + die("ERREUR : Aucune utilisateur trouvé dans WordPress."); +} + +// --- ADAPTE CES VALEURS SELON TA TABLE phpboost_member --- +// D'après ton retour, voici les noms de colonnes réels : +$login_column = 'display_name'; // Remplace par le nom exact (ex: display_name, user_login, pseudo) +$password_column = 'user_password'; // Remplace par le nom exact (ex: user_password, pass, mdp) +$email_column = 'email'; // Déjà correct +$name_column = 'display_name'; // Déjà correct +$level_column = 'level'; // Déjà correct +$reg_date_column = 'registration_date'; // Déjà correct + +$member_table = PBOOST_DB_PREFIX . "member"; + +// Vérifier que les colonnes existent +$required_columns = [$login_column, $password_column, $email_column, $name_column, $level_column, $reg_date_column]; +foreach ($required_columns as $column) { + if (!checkColumnExists($pboost_db, $member_table, $column)) { + die("ERREUR : La colonne '$column' n'existe pas dans $member_table. Vérifie les noms des colonnes."); + } +} + +echo "Colonnes utilisées pour phpboost_member :
"; +echo "- Login : $login_column
"; +echo "- Password : $password_column
"; +echo "- Email : $email_column
"; +echo "- Name : $name_column
"; +echo "- Level : $level_column
"; +echo "- Registration Date : $reg_date_column
"; + +// Importer tous les utilisateurs +$query = "SELECT * FROM " . WP_DB_PREFIX . "users ORDER BY ID ASC"; +$result = $wp_db->query($query); + +if ($result && $result->num_rows > 0) { + while ($user = $result->fetch_assoc()) { + $login = $pboost_db->real_escape_string($user['user_login']); + $email = $pboost_db->real_escape_string($user['user_email']); + $password = $user['user_pass']; // MD5, compatible avec PHPBoost + $name = $pboost_db->real_escape_string($user['display_name'] ?? $user['user_login']); + $level = 1; // Niveau par défaut (1 = membre) + + // Vérifier si l'utilisateur existe déjà + $check_query = "SELECT user_id FROM $member_table WHERE $login_column = '$login'"; + $check_result = $pboost_db->query($check_query); + + if ($check_result && $check_result->num_rows > 0) { + $existing_user = $check_result->fetch_assoc(); + $user_mapping[$user['ID']] = $existing_user['user_id']; + $report['users']['success']++; + } else { + // Insérer le nouvel utilisateur avec les bonnes colonnes + $insert_query = "INSERT INTO $member_table + ($login_column, $password_column, $email_column, $name_column, $level_column, $reg_date_column) + VALUES ('$login', '$password', '$email', '$name', $level, " . time() . ")"; + if ($pboost_db->query($insert_query)) { + $new_user_id = $pboost_db->insert_id; + $user_mapping[$user['ID']] = $new_user_id; + $report['users']['success']++; + } else { + $report['users']['errors']++; + echo "Erreur utilisateur (ID: {$user['ID']}) : " . $pboost_db->error . "
"; + } + } + } +} else { + echo "Aucun utilisateur trouvé dans WordPress.
"; +} + +echo "Utilisateurs importés : " . $report['users']['success'] . "
"; +flush(); + +// ============================================= +// 6. IMPORT DES ARTICLES +// ============================================= + +echo "

Importation des articles...

"; +$post_mapping = []; // WordPress Post ID => PHPBoost Article ID +$article_titles = []; // WordPress Post ID => Titre de l'article + +$query = "SELECT * FROM " . WP_DB_PREFIX . "posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date ASC"; +$result = $wp_db->query($query); + +if ($result && $result->num_rows > 0) { + while ($post = $result->fetch_assoc()) { + $title = $pboost_db->real_escape_string($post['post_title']); + $content = $pboost_db->real_escape_string($post['post_content']); + $creation_date = strtotime($post['post_date']); + $rewrited_title = sanitizeTitle($title); + $author_id = $user_mapping[$post['post_author']] ?? $user_mapping[$first_user_id] ?? 1; + + // Insérer l'article + $insert_query = "INSERT INTO " . PBOOST_DB_PREFIX . "articles + (title, author_user_id, creation_date, published, id_category) + VALUES ('$title', $author_id, $creation_date, 1, 0)"; + if ($pboost_db->query($insert_query)) { + $pboost_article_id = $pboost_db->insert_id; + $post_mapping[$post['ID']] = $pboost_article_id; + $article_titles[$post['ID']] = $title; + $report['posts']['success']++; + + // Mettre à jour les champs optionnels + $update_query = "UPDATE " . PBOOST_DB_PREFIX . "articles + SET content = '$content', + rewrited_title = '$rewrited_title', + views_number = 0, + update_date = $creation_date + WHERE id = $pboost_article_id"; + if (!$pboost_db->query($update_query)) { + $report['posts']['errors']++; + echo "Erreur mise à jour article (ID: $pboost_article_id) : " . $pboost_db->error . "
"; + } + } else { + $report['posts']['errors']++; + echo "Erreur article (ID: {$post['ID']}) : " . $pboost_db->error . "
"; + } + } +} else { + echo "Aucun article trouvé.
"; +} + +echo "Articles importés : " . $report['posts']['success'] . "
"; +flush(); + +// ============================================= +// 7. IMPORT DES PAGES +// ============================================= + +echo "

Importation des pages...

"; +$page_mapping = []; // WordPress Post ID => PHPBoost Page ID +$page_titles = []; // WordPress Post ID => Titre de la page + +$query = "SELECT * FROM " . WP_DB_PREFIX . "posts WHERE post_type = 'page' AND post_status = 'publish' ORDER BY post_date ASC"; +$result = $wp_db->query($query); + +if ($result && $result->num_rows > 0) { + while ($page = $result->fetch_assoc()) { + $title = $pboost_db->real_escape_string($page['post_title']); + $content = $pboost_db->real_escape_string($page['post_content']); + $creation_date = strtotime($page['post_date']); + $rewrited_title = sanitizeTitle($title); + $author_id = $user_mapping[$page['post_author']] ?? $user_mapping[$first_user_id] ?? 1; + + // Insérer la page + $insert_query = "INSERT INTO " . PBOOST_DB_PREFIX . "pages + (title, author_user_id, creation_date, published, id_category) + VALUES ('$title', $author_id, $creation_date, 1, 0)"; + if ($pboost_db->query($insert_query)) { + $pboost_page_id = $pboost_db->insert_id; + $page_mapping[$page['ID']] = $pboost_page_id; + $page_titles[$page['ID']] = $title; + $report['pages']['success']++; + + // Mettre à jour les champs optionnels + $update_query = "UPDATE " . PBOOST_DB_PREFIX . "pages + SET content = '$content', + rewrited_title = '$rewrited_title', + views_number = 0, + update_date = $creation_date, + i_order = 0, + thumbnail = '' + WHERE id = $pboost_page_id"; + if (!$pboost_db->query($update_query)) { + $report['pages']['errors']++; + echo "Erreur mise à jour page (ID: $pboost_page_id) : " . $pboost_db->error . "
"; + } + } else { + $report['pages']['errors']++; + echo "Erreur page (ID: {$page['ID']}) : " . $pboost_db->error . "
"; + } + } +} else { + echo "Aucune page trouvée.
"; +} + +echo "Pages importées : " . $report['pages']['success'] . "
"; +flush(); + +// ============================================= +// 8. CRÉATION DES SUJETS POUR ARTICLES ET PAGES +// ============================================= + +echo "

Création des sujets...

"; +$topic_mapping = []; // WordPress Post ID => PHPBoost Topic ID +$comments_topic_table = PBOOST_DB_PREFIX . 'comments_topic'; + +// Vérifier que la table existe +$check_table_query = "SHOW TABLES LIKE '$comments_topic_table'"; +$check_table_result = $pboost_db->query($check_table_query); +if (!$check_table_result || $check_table_result->num_rows == 0) { + die("ERREUR : La table $comments_topic_table n'existe pas. Crée-la avant de continuer."); +} + +// Vérifier que les colonnes nécessaires existent +$required_columns = ['id_topic', 'module_id', 'id_in_module', 'topic_identifier']; +foreach ($required_columns as $column) { + if (!checkColumnExists($pboost_db, $comments_topic_table, $column)) { + die("ERREUR : La colonne '$column' n'existe pas dans $comments_topic_table."); + } +} + +// Créer des sujets pour les articles +foreach ($post_mapping as $wp_post_id => $pboost_article_id) { + $title = $pboost_db->real_escape_string($article_titles[$wp_post_id] ?? 'Sans titre'); + $topic_identifier = sanitizeTitle($title) . '-' . $pboost_article_id; + + $check_topic_query = "SELECT id_topic FROM $comments_topic_table + WHERE module_id = 'article' AND id_in_module = $pboost_article_id"; + $check_topic_result = $pboost_db->query($check_topic_query); + + if ($check_topic_result && $check_topic_result->num_rows > 0) { + $existing_topic = $check_topic_result->fetch_assoc(); + $topic_mapping[$wp_post_id] = $existing_topic['id_topic']; + } else { + $insert_topic_query = "INSERT INTO $comments_topic_table + (module_id, id_in_module, topic_identifier) + VALUES ('article', $pboost_article_id, '$topic_identifier')"; + if (!$pboost_db->query($insert_topic_query)) { + die("ERREUR : Impossible de créer un sujet pour l'article $pboost_article_id : " . $pboost_db->error); + } + $topic_mapping[$wp_post_id] = $pboost_db->insert_id; + } +} + +// Créer des sujets pour les pages +foreach ($page_mapping as $wp_page_id => $pboost_page_id) { + $title = $pboost_db->real_escape_string($page_titles[$wp_page_id] ?? 'Sans titre'); + $topic_identifier = sanitizeTitle($title) . '-' . $pboost_page_id; + + $check_topic_query = "SELECT id_topic FROM $comments_topic_table + WHERE module_id = 'page' AND id_in_module = $pboost_page_id"; + $check_topic_result = $pboost_db->query($check_topic_query); + + if ($check_topic_result && $check_topic_result->num_rows > 0) { + $existing_topic = $check_topic_result->fetch_assoc(); + $topic_mapping[$wp_page_id] = $existing_topic['id_topic']; + } else { + $insert_topic_query = "INSERT INTO $comments_topic_table + (module_id, id_in_module, topic_identifier) + VALUES ('page', $pboost_page_id, '$topic_identifier')"; + if (!$pboost_db->query($insert_topic_query)) { + die("ERREUR : Impossible de créer un sujet pour la page $pboost_page_id : " . $pboost_db->error); + } + $topic_mapping[$wp_page_id] = $pboost_db->insert_id; + } +} + +echo "Sujets créés pour " . count($topic_mapping) . " contenus.
"; +flush(); + +// ============================================= +// 9. IMPORT DES COMMENTAIRES +// ============================================= + +echo "

Importation des commentaires...

"; + +// Pré-charger les types de posts pour éviter les requêtes répétées +$comment_post_ids = []; +$comment_query = "SELECT DISTINCT comment_post_ID FROM " . WP_DB_PREFIX . "comments WHERE comment_approved = '1'"; +$comment_result = $wp_db->query($comment_query); +if ($comment_result) { + while ($row = $comment_result->fetch_assoc()) { + $comment_post_ids[] = $row['comment_post_ID']; + } +} + +$post_type_mapping = []; // WordPress Post ID => post_type +if (!empty($comment_post_ids)) { + $post_types_query = "SELECT ID, post_type FROM " . WP_DB_PREFIX . "posts WHERE ID IN (" . implode(',', $comment_post_ids) . ")"; + $post_types_result = $wp_db->query($post_types_query); + if ($post_types_result) { + while ($row = $post_types_result->fetch_assoc()) { + $post_type_mapping[$row['ID']] = $row['post_type']; + } + } +} + +// Importer les commentaires par lots +$batch_size = 100; +$batch_count = 0; +$pboost_db->autocommit(false); + +$query = "SELECT * FROM " . WP_DB_PREFIX . "comments WHERE comment_approved = '1' ORDER BY comment_date ASC"; +$result = $wp_db->query($query); + +if ($result && $result->num_rows > 0) { + while ($comment = $result->fetch_assoc()) { + $wp_post_id = $comment['comment_post_ID']; + + // Ignorer les commentaires sur des contenus non importés (médias, CPT, etc.) + if (!isset($topic_mapping[$wp_post_id])) { + $report['comments']['errors']++; + if (!isset($warned_missing_topics[$wp_post_id])) { + echo "Avertissement : Aucun sujet trouvé pour le post ID $wp_post_id (type: " . ($post_type_mapping[$wp_post_id] ?? 'inconnu') . ").
"; + $warned_missing_topics[$wp_post_id] = true; + } + continue; + } + + $id_topic = $topic_mapping[$wp_post_id]; + $content = $pboost_db->real_escape_string(preg_replace('/[^\x20-\x7E]/u', '', $comment['comment_content'])); + $date = strtotime($comment['comment_date']); + $wp_user_id = $comment['user_id']; + $author_id = $user_mapping[$wp_user_id] ?? $user_mapping[$first_user_id] ?? 1; + $pseudo = $pboost_db->real_escape_string($comment['comment_author'] ?? 'Anonyme'); + $visitor_email = $pboost_db->real_escape_string($comment['comment_author_email'] ?? ''); + $user_ip = $pboost_db->real_escape_string($comment['comment_author_IP'] ?? ''); + + $insert_query = "INSERT INTO " . PBOOST_DB_PREFIX . "comments + (id_topic, message, user_id, pseudo, visitor_email, user_ip, timestamp) + VALUES ($id_topic, '$content', $author_id, '$pseudo', '$visitor_email', '$user_ip', $date)"; + + if (!$pboost_db->query($insert_query)) { + $report['comments']['errors']++; + echo "Erreur commentaire (Post ID: $wp_post_id) : " . $pboost_db->error . "
"; + } else { + $report['comments']['success']++; + } + + // Commit par lots + $batch_count++; + if ($batch_count % $batch_size === 0) { + $pboost_db->commit(); + $pboost_db->autocommit(false); + echo "Traités : $batch_count commentaires...
"; + flush(); + } + } + + // Commit final + $pboost_db->commit(); + $pboost_db->autocommit(true); +} else { + echo "Aucun commentaire trouvé dans WordPress.
"; +} + +echo "Commentaires importés : " . $report['comments']['success'] . "
"; +flush(); + +// ============================================= +// 10. IMPORT DES MÉDIAS +// ============================================= + +echo "

Importation des médias...

"; + +// Vérifier que le dossier de destination existe +if (!file_exists(PBOOST_UPLOAD_DIR)) { + if (!mkdir(PBOOST_UPLOAD_DIR, 0755, true)) { + die("ERREUR : Impossible de créer le dossier " . PBOOST_UPLOAD_DIR . ". Vérifie les permissions."); + } +} + +$query = "SELECT * FROM " . WP_DB_PREFIX . "posts WHERE post_type = 'attachment' ORDER BY ID ASC"; +$result = $wp_db->query($query); + +if ($result && $result->num_rows > 0) { + $media_batch_count = 0; + $pboost_db->autocommit(false); + + while ($media = $result->fetch_assoc()) { + $meta = getPostMeta($wp_db, $media['ID']); + $file_path = $meta['_wp_attached_file'] ?? ''; + + if (empty($file_path)) { + $report['media']['errors']++; + continue; + } + + // Construire le chemin source (avec et sans sous-dossier Y/m/) + $date_path = date('Y/m', strtotime($media['post_date'])); + $source_file_1 = WP_UPLOAD_DIR . $date_path . '/' . $file_path; + $source_file_2 = WP_UPLOAD_DIR . $file_path; + + $source_file = file_exists($source_file_1) ? $source_file_1 : $source_file_2; + $new_file_path = PBOOST_UPLOAD_DIR . basename($file_path); + + if (file_exists($source_file)) { + if (copy($source_file, $new_file_path)) { + $report['media']['success']++; + $old_url = '/wp-content/uploads/' . $date_path . '/' . $file_path; + $new_url = '/upload/' . basename($file_path); + + // Échapper les URLs + $old_url_escaped = $pboost_db->real_escape_string($old_url); + $new_url_escaped = $pboost_db->real_escape_string($new_url); + + // Mettre à jour dans les articles + foreach ($post_mapping as $wp_id => $pboost_id) { + $update_query = "UPDATE " . PBOOST_DB_PREFIX . "articles + SET content = REPLACE(content, '$old_url_escaped', '$new_url_escaped') + WHERE id = $pboost_id"; + if (!$pboost_db->query($update_query)) { + echo "Erreur mise à jour URL article (ID: $pboost_id) : " . $pboost_db->error . "
"; + } + } + + // Mettre à jour dans les pages + foreach ($page_mapping as $wp_id => $pboost_id) { + $update_query = "UPDATE " . PBOOST_DB_PREFIX . "pages + SET content = REPLACE(content, '$old_url_escaped', '$new_url_escaped') + WHERE id = $pboost_id"; + if (!$pboost_db->query($update_query)) { + echo "Erreur mise à jour URL page (ID: $pboost_id) : " . $pboost_db->error . "
"; + } + } + } else { + $report['media']['errors']++; + $report['media']['manual_copy'][] = ['source' => $source_file, 'destination' => $new_file_path]; + echo "Erreur : Impossible de copier $source_file vers $new_file_path.
"; + } + } else { + $report['media']['errors']++; + $report['media']['manual_copy'][] = ['source' => $source_file, 'destination' => $new_file_path]; + echo "Avertissement : Fichier source introuvable : $source_file.
"; + } + + // Commit par lots + $media_batch_count++; + if ($media_batch_count % 50 === 0) { + $pboost_db->commit(); + $pboost_db->autocommit(false); + echo "Médias traités : $media_batch_count...
"; + flush(); + } + } + + // Commit final + $pboost_db->commit(); + $pboost_db->autocommit(true); +} else { + echo "Aucun média trouvé dans WordPress.
"; +} + +echo "Médias importés : " . $report['media']['success'] . "
"; +if (!empty($report['media']['manual_copy'])) { + echo "

Fichiers à copier manuellement :

"; + foreach ($report['media']['manual_copy'] as $file) { + echo "De : " . $file['source'] . " vers : " . $file['destination'] . "
"; + } +} + +flush(); + +// ============================================= +// 11. RÉSUMÉ FINAL +// ============================================= + +echo "

Résumé de l'import

"; +echo "
";
+print_r($report);
+echo "
"; +echo "

Import terminé !

"; + +// Fermer les connexions +$wp_db->close(); +$pboost_db->close(); +?> \ No newline at end of file