Ver Fonte

Fixing WP comments import

svalavuo há 4 dias atrás
pai
commit
23ea577fe3
1 ficheiros alterados com 25 adições e 18 exclusões
  1. 25 18
      includes/wordpress_import.php

+ 25 - 18
includes/wordpress_import.php

@@ -437,33 +437,40 @@ class WordPressImport {
             $publicationMap = [];
             
             if (!empty($postIds)) {
-                // Test if wp_post_id column exists
+                // 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)];
                 }
                 
-                $placeholders = str_repeat('?,', count($postIds) - 1) . '?';
-                $this->log("Post IDs to map: " . implode(', ', $postIds));
-                $this->log("Placeholders: " . $placeholders);
-                $this->log("Parameter count: " . count($postIds));
-                $this->log("Executing publication mapping query with " . count($postIds) . " post IDs");
-                
-                // Use raw PDO for this query to avoid Database class parameter issues
-                $pdo = $this->targetDb->getConnection();
-                $pubStmt = $pdo->prepare("
-                    SELECT id, wp_post_id FROM publications 
-                    WHERE wp_post_id IN ($placeholders)
-                ");
-                $pubStmt->execute($postIds);
-                
-                foreach ($pubStmt->fetchAll() as $pub) {
-                    // Map WordPress post ID to publication ID
-                    $publicationMap[$pub['wp_post_id']] = $pub['id'];
+                // Try a simpler approach - query each post ID individually
+                $this->log("Building 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");