Téléverser les fichiers vers "/"
This commit is contained in:
@@ -1,3 +1,42 @@
|
|||||||
# PHPBoost-WordPress-Import
|
# Module WordPress 7.0 → PHPBoost Importer
|
||||||
|
|
||||||
Script de migration depuis WordPress 7.0 vers PHPBoost.
|
**⚠️ 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) !
|
||||||
@@ -0,0 +1,568 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Script d'import WordPress (y compris 7.0) vers PHPBoost
|
||||||
|
* Adapté pour éviter les timeouts et gérer les erreurs.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// =============================================
|
||||||
|
// 1. CONFIGURATION
|
||||||
|
// =============================================
|
||||||
|
|
||||||
|
// --- Configuration WordPress ---
|
||||||
|
define('WP_DB_HOST', 'localhost'); // Hôte de la base WordPress
|
||||||
|
define('WP_DB_NAME', 'nom_base_wordpress'); // Nom de la base WordPress
|
||||||
|
define('WP_DB_USER', 'utilisateur_wordpress'); // Utilisateur WordPress
|
||||||
|
define('WP_DB_PASS', 'mot_de_passe_wordpress'); // Mot de passe WordPress
|
||||||
|
define('WP_DB_PREFIX', 'wp_'); // Préfixe des tables WordPress (ex: wp_)
|
||||||
|
define('WP_UPLOAD_DIR', '/var/www/geekonweb/wp-content/uploads/'); // Chemin absolu vers wp-content/uploads/
|
||||||
|
|
||||||
|
// --- Configuration PHPBoost ---
|
||||||
|
define('PBOOST_DB_HOST', 'localhost'); // Hôte de la base PHPBoost
|
||||||
|
define('PBOOST_DB_NAME', 'nom_base_phpboost'); // Nom de la base PHPBoost
|
||||||
|
define('PBOOST_DB_USER', 'utilisateur_phpboost'); // Utilisateur PHPBoost
|
||||||
|
define('PBOOST_DB_PASS', 'mot_de_passe_phpboost'); // Mot de passe PHPBoost
|
||||||
|
define('PBOOST_DB_PREFIX', 'phpboost_'); // Préfixe des tables PHPBoost
|
||||||
|
define('PBOOST_UPLOAD_DIR', '/var/www/geekonweb-phpboost/upload/'); // Chemin absolu vers le dossier upload de PHPBoost
|
||||||
|
|
||||||
|
// --- Paramètres généraux ---
|
||||||
|
set_time_limit(0); // Pas de limite de temps
|
||||||
|
ini_set('memory_limit', '512M'); // Mémoire suffisante
|
||||||
|
error_reporting(E_ALL);
|
||||||
|
ini_set('display_errors', 1);
|
||||||
|
|
||||||
|
// =============================================
|
||||||
|
// 2. CONNEXIONS À LA BASE DE DONNÉES
|
||||||
|
// =============================================
|
||||||
|
|
||||||
|
$wp_db = new mysqli(WP_DB_HOST, WP_DB_USER, WP_DB_PASS, WP_DB_NAME);
|
||||||
|
if ($wp_db->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 "<h2>Importation des utilisateurs...</h2>";
|
||||||
|
$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 :<br>";
|
||||||
|
echo "- Login : $login_column<br>";
|
||||||
|
echo "- Password : $password_column<br>";
|
||||||
|
echo "- Email : $email_column<br>";
|
||||||
|
echo "- Name : $name_column<br>";
|
||||||
|
echo "- Level : $level_column<br>";
|
||||||
|
echo "- Registration Date : $reg_date_column<br>";
|
||||||
|
|
||||||
|
// 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 . "<br>";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
echo "Aucun utilisateur trouvé dans WordPress.<br>";
|
||||||
|
}
|
||||||
|
|
||||||
|
echo "Utilisateurs importés : " . $report['users']['success'] . "<br>";
|
||||||
|
flush();
|
||||||
|
|
||||||
|
// =============================================
|
||||||
|
// 6. IMPORT DES ARTICLES
|
||||||
|
// =============================================
|
||||||
|
|
||||||
|
echo "<h2>Importation des articles...</h2>";
|
||||||
|
$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 . "<br>";
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$report['posts']['errors']++;
|
||||||
|
echo "Erreur article (ID: {$post['ID']}) : " . $pboost_db->error . "<br>";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
echo "Aucun article trouvé.<br>";
|
||||||
|
}
|
||||||
|
|
||||||
|
echo "Articles importés : " . $report['posts']['success'] . "<br>";
|
||||||
|
flush();
|
||||||
|
|
||||||
|
// =============================================
|
||||||
|
// 7. IMPORT DES PAGES
|
||||||
|
// =============================================
|
||||||
|
|
||||||
|
echo "<h2>Importation des pages...</h2>";
|
||||||
|
$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 . "<br>";
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$report['pages']['errors']++;
|
||||||
|
echo "Erreur page (ID: {$page['ID']}) : " . $pboost_db->error . "<br>";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
echo "Aucune page trouvée.<br>";
|
||||||
|
}
|
||||||
|
|
||||||
|
echo "Pages importées : " . $report['pages']['success'] . "<br>";
|
||||||
|
flush();
|
||||||
|
|
||||||
|
// =============================================
|
||||||
|
// 8. CRÉATION DES SUJETS POUR ARTICLES ET PAGES
|
||||||
|
// =============================================
|
||||||
|
|
||||||
|
echo "<h2>Création des sujets...</h2>";
|
||||||
|
$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.<br>";
|
||||||
|
flush();
|
||||||
|
|
||||||
|
// =============================================
|
||||||
|
// 9. IMPORT DES COMMENTAIRES
|
||||||
|
// =============================================
|
||||||
|
|
||||||
|
echo "<h2>Importation des commentaires...</h2>";
|
||||||
|
|
||||||
|
// 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') . ").<br>";
|
||||||
|
$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 . "<br>";
|
||||||
|
} 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...<br>";
|
||||||
|
flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Commit final
|
||||||
|
$pboost_db->commit();
|
||||||
|
$pboost_db->autocommit(true);
|
||||||
|
} else {
|
||||||
|
echo "Aucun commentaire trouvé dans WordPress.<br>";
|
||||||
|
}
|
||||||
|
|
||||||
|
echo "Commentaires importés : " . $report['comments']['success'] . "<br>";
|
||||||
|
flush();
|
||||||
|
|
||||||
|
// =============================================
|
||||||
|
// 10. IMPORT DES MÉDIAS
|
||||||
|
// =============================================
|
||||||
|
|
||||||
|
echo "<h2>Importation des médias...</h2>";
|
||||||
|
|
||||||
|
// 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 . "<br>";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 . "<br>";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} 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.<br>";
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$report['media']['errors']++;
|
||||||
|
$report['media']['manual_copy'][] = ['source' => $source_file, 'destination' => $new_file_path];
|
||||||
|
echo "Avertissement : Fichier source introuvable : $source_file.<br>";
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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...<br>";
|
||||||
|
flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Commit final
|
||||||
|
$pboost_db->commit();
|
||||||
|
$pboost_db->autocommit(true);
|
||||||
|
} else {
|
||||||
|
echo "Aucun média trouvé dans WordPress.<br>";
|
||||||
|
}
|
||||||
|
|
||||||
|
echo "Médias importés : " . $report['media']['success'] . "<br>";
|
||||||
|
if (!empty($report['media']['manual_copy'])) {
|
||||||
|
echo "<h3>Fichiers à copier manuellement :</h3>";
|
||||||
|
foreach ($report['media']['manual_copy'] as $file) {
|
||||||
|
echo "De : " . $file['source'] . " vers : " . $file['destination'] . "<br>";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
flush();
|
||||||
|
|
||||||
|
// =============================================
|
||||||
|
// 11. RÉSUMÉ FINAL
|
||||||
|
// =============================================
|
||||||
|
|
||||||
|
echo "<h2>Résumé de l'import</h2>";
|
||||||
|
echo "<pre>";
|
||||||
|
print_r($report);
|
||||||
|
echo "</pre>";
|
||||||
|
echo "<p>Import terminé !</p>";
|
||||||
|
|
||||||
|
// Fermer les connexions
|
||||||
|
$wp_db->close();
|
||||||
|
$pboost_db->close();
|
||||||
|
?>
|
||||||
Reference in New Issue
Block a user