Browse Source

Fixed WP import

svalavuo 4 ngày trước cách đây
mục cha
commit
ba43976894
1 tập tin đã thay đổi với 9 bổ sung54 xóa
  1. 9 54
      includes/wordpress_import.php

+ 9 - 54
includes/wordpress_import.php

@@ -406,26 +406,13 @@ class WordPressImport {
         $skipped = 0;
         
         try {
-            // Connect to WordPress database if not already connected
-            if ($this->wpDb === null) {
-                $this->connectWordPress();
-            }
-            
-            // Test basic WordPress comments query first
-            $testStmt = $this->wpDb->query("SELECT COUNT(*) FROM wp_comments");
-            $commentCount = $testStmt->fetchColumn();
-            $this->log("WordPress comments table has {$commentCount} total comments");
-            
-            // Get WordPress comments with post info in one query
+            // Get comments from WordPress
             $stmt = $this->wpDb->query("
-                SELECT c.comment_ID, c.comment_post_ID, c.comment_author, c.comment_author_email,
-                       c.comment_content, c.comment_date, c.comment_approved, c.comment_parent,
-                       p.post_name, p.post_title
+                SELECT c.*, p.post_title 
                 FROM wp_comments c
-                JOIN wp_posts p ON c.comment_post_ID = p.ID
-                WHERE p.post_type = 'post'
-                ORDER BY c.comment_date
-                LIMIT 2000
+                LEFT JOIN wp_posts p ON c.comment_post_ID = p.ID
+                WHERE c.comment_type = 'comment' AND c.comment_approved IN ('0', '1')
+                ORDER BY c.comment_date ASC
             ");
             
             $comments = $stmt->fetchAll();
@@ -437,49 +424,22 @@ class WordPressImport {
             $publicationMap = [];
             
             if (!empty($postIds)) {
-                // Test if wp_post_id column exists and has values
-                try {
-                    $testStmt = $this->targetDb->query("SELECT wp_post_id FROM publications LIMIT 1");
-                    $this->log("wp_post_id column exists in publications table");
-                    
-                    // Check if any publications have wp_post_id values
-                    $countStmt = $this->targetDb->query("SELECT COUNT(*) FROM publications WHERE wp_post_id IS NOT NULL");
-                    $wpPostIdCount = $countStmt->fetchColumn();
-                    $this->log("Found {$wpPostIdCount} publications with wp_post_id values");
-                    
-                    if ($wpPostIdCount == 0) {
-                        $this->log("No publications have wp_post_id values - comments cannot be mapped", 'error');
-                        return ['imported' => 0, 'skipped' => count($comments)];
-                    }
-                    
-                } catch (Exception $e) {
-                    $this->log("Error: wp_post_id column does not exist in publications table: " . $e->getMessage(), 'error');
-                    $this->log("Skipping comments import - publications table needs wp_post_id column", 'error');
-                    return ['imported' => 0, 'skipped' => count($comments)];
-                }
-                
-                // Try a simpler approach - query each post ID individually
-                $this->log("Building publication map using individual queries");
+                // Build publication map using individual queries
                 foreach ($postIds as $postId) {
                     try {
                         $pubStmt = $this->targetDb->query("SELECT id FROM publications WHERE wp_post_id = ?", [$postId]);
                         $pub = $pubStmt->fetch();
                         if ($pub) {
                             $publicationMap[$postId] = $pub['id'];
-                            $this->log("Mapped WordPress post ID {$postId} to publication ID {$pub['id']}");
                         }
                     } catch (Exception $e) {
                         $this->log("Error mapping post ID {$postId}: " . $e->getMessage(), 'error');
                     }
                 }
-                
-                $this->log("Found " . count($publicationMap) . " publication mappings for comments");
             }
             
-            foreach ($comments as $index => $wpComment) {
+            foreach ($comments as $wpComment) {
                 try {
-                    $this->log("Processing comment {$index}: '{$wpComment['comment_content']}' (Post ID: {$wpComment['comment_post_ID']})");
-                    
                     // Find corresponding publication using wp_post_id mapping
                     $publicationId = null;
                     $wpPostId = $wpComment['comment_post_ID'];
@@ -490,7 +450,6 @@ class WordPressImport {
                     
                     if (!$publicationId) {
                         $skipped++;
-                        $this->log("Comment skipped - no matching publication found for post ID {$wpComment['comment_post_ID']}");
                         continue;
                     }
                     
@@ -501,7 +460,6 @@ class WordPressImport {
                     $parentId = null;
                     
                     // Insert comment
-                    $this->log("Executing INSERT for comment: '{$wpComment['comment_content']}' (Post ID: {$wpPostId})");
                     $pdo = $this->targetDb->getConnection();
                     $commentStmt = $pdo->prepare("
                         INSERT INTO comments (publication_id, parent_id, name, email, content, status, created_at, admin_reply, wp_comment_id) 
@@ -515,18 +473,15 @@ class WordPressImport {
                         $wpComment['comment_content'],
                         $status,
                         $wpComment['comment_date'],
-                        0, // Use integer 0 for admin_reply instead of boolean false
+                        0, // Use integer 0 for admin_reply
                         $wpComment['comment_ID']
                     ]);
                     
-                    $commentId = $this->targetDb->getConnection()->lastInsertId();
-                    
                     $imported++;
-                    $this->log("Successfully imported comment: '{$wpComment['comment_content']}' (ID: {$wpComment['comment_ID']})");
                     
                 } catch (Exception $e) {
                     $skipped++;
-                    $this->log("Skipped comment '{$wpComment['comment_content']}' (ID: {$wpComment['comment_ID']}): " . $e->getMessage(), 'error');
+                    $this->log("Skipped comment ID {$wpComment['comment_ID']}: " . $e->getMessage(), 'error');
                 }
             }