connect_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(); ?>