wordpress_import.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. <?php
  2. /**
  3. * WordPress Database Import Tool
  4. * Imports posts, categories, users, and comments from WordPress to the publication system
  5. */
  6. class WordPressImport {
  7. private $wpDb;
  8. private $targetDb;
  9. private $wpConfig;
  10. private $importLog = [];
  11. private $errors = [];
  12. public function __construct($wpConfig) {
  13. error_log('WordPressImport constructor called');
  14. $this->wpConfig = $wpConfig;
  15. $this->targetDb = Database::getInstance();
  16. error_log('WordPressImport constructor completed');
  17. // Don't connect in constructor - connect on demand to avoid hanging
  18. }
  19. /**
  20. * Connect to WordPress database
  21. */
  22. private function connectWordPress() {
  23. try {
  24. // Set timeout options
  25. $options = [
  26. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  27. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  28. PDO::ATTR_TIMEOUT => 10, // 10 second timeout
  29. PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8mb4"
  30. ];
  31. $dsn = "mysql:host={$this->wpConfig['host']};dbname={$this->wpConfig['database']};charset=utf8mb4";
  32. $this->wpDb = new PDO($dsn, $this->wpConfig['username'], $this->wpConfig['password'], $options);
  33. // Test connection with a simple query
  34. $this->wpDb->query("SELECT 1");
  35. $this->log('Connected to WordPress database successfully');
  36. } catch (PDOException $e) {
  37. throw new Exception("Failed to connect to WordPress database: " . $e->getMessage());
  38. } catch (Exception $e) {
  39. throw new Exception("WordPress database connection error: " . $e->getMessage());
  40. }
  41. }
  42. /**
  43. * Test WordPress connection and verify structure
  44. */
  45. public function testConnection() {
  46. try {
  47. // Connect first
  48. $this->connectWordPress();
  49. // Check if WordPress tables exist
  50. $tables = ['wp_posts', 'wp_users', 'wp_terms', 'wp_term_taxonomy', 'wp_term_relationships', 'wp_comments'];
  51. $existingTables = [];
  52. foreach ($tables as $table) {
  53. $stmt = $this->wpDb->query("SHOW TABLES LIKE '{$table}'");
  54. if ($stmt->rowCount() > 0) {
  55. $existingTables[] = $table;
  56. }
  57. }
  58. if (count($existingTables) < 6) {
  59. throw new Exception("WordPress database structure incomplete. Missing tables: " . implode(', ', array_diff($tables, $existingTables)));
  60. }
  61. // Get basic stats
  62. $stats = [
  63. 'posts' => $this->getPostCount(),
  64. 'pages' => $this->getPageCount(),
  65. 'categories' => $this->getCategoryCount(),
  66. 'users' => $this->getUserCount(),
  67. 'comments' => $this->getCommentCount()
  68. ];
  69. return ['success' => true, 'stats' => $stats, 'tables' => $existingTables];
  70. } catch (Exception $e) {
  71. return ['success' => false, 'error' => $e->getMessage()];
  72. }
  73. }
  74. /**
  75. * Import all WordPress data
  76. */
  77. public function importAll($options = []) {
  78. error_log('importAll() called with options: ' . print_r($options, true));
  79. $results = [];
  80. try {
  81. // Connect to WordPress database first
  82. error_log('About to call connectWordPress()');
  83. $this->connectWordPress();
  84. error_log('connectWordPress() completed');
  85. // Start transaction
  86. error_log('About to start transaction');
  87. $this->targetDb->beginTransaction();
  88. error_log('Transaction started');
  89. // Import categories first (posts depend on them)
  90. if ($options['import_categories'] ?? true) {
  91. $results['categories'] = $this->importCategories();
  92. }
  93. // Import users
  94. if ($options['import_users'] ?? true) {
  95. $results['users'] = $this->importUsers();
  96. }
  97. // Import posts
  98. if ($options['import_posts'] ?? true) {
  99. $results['posts'] = $this->importPosts();
  100. }
  101. // Import comments
  102. if ($options['import_comments'] ?? true) {
  103. $results['comments'] = $this->importComments();
  104. }
  105. // Commit transaction
  106. $this->targetDb->commit();
  107. $this->log('Import completed successfully');
  108. return ['success' => true, 'results' => $results, 'log' => $this->importLog];
  109. } catch (Exception $e) {
  110. $this->targetDb->rollBack();
  111. $this->errors[] = $e->getMessage();
  112. $this->log('Import failed: ' . $e->getMessage(), 'error');
  113. return ['success' => false, 'error' => $e->getMessage(), 'log' => $this->importLog, 'errors' => $this->errors];
  114. }
  115. }
  116. /**
  117. * Import WordPress categories
  118. */
  119. public function importCategories() {
  120. $this->log('Starting categories import');
  121. $imported = 0;
  122. $skipped = 0;
  123. // Get WordPress categories
  124. $stmt = $this->wpDb->query("
  125. SELECT t.name, tt.description, tt.term_id
  126. FROM wp_terms t
  127. JOIN wp_term_taxonomy tt ON t.term_id = tt.term_id
  128. WHERE tt.taxonomy = 'category'
  129. ORDER BY t.name
  130. ");
  131. $categories = $stmt->fetchAll();
  132. foreach ($categories as $wpCategory) {
  133. try {
  134. // Check if category already exists
  135. $existing = $this->targetDb->fetch(
  136. "SELECT id FROM categories WHERE name = ?",
  137. [$wpCategory['name']]
  138. );
  139. if ($existing) {
  140. $skipped++;
  141. $this->log("Category '{$wpCategory['name']}' already exists, skipping");
  142. continue;
  143. }
  144. // Insert new category
  145. $this->targetDb->execute(
  146. "INSERT INTO categories (name, description, created_at) VALUES (?, ?, NOW())",
  147. [
  148. $wpCategory['name'],
  149. $wpCategory['description'] ?? ''
  150. ]
  151. );
  152. $imported++;
  153. $this->log("Imported category: {$wpCategory['name']}");
  154. } catch (Exception $e) {
  155. $this->errors[] = "Error importing category '{$wpCategory['name']}': " . $e->getMessage();
  156. $this->log("Error importing category '{$wpCategory['name']}': " . $e->getMessage(), 'error');
  157. }
  158. }
  159. $this->log("Categories import completed: {$imported} imported, {$skipped} skipped");
  160. return ['imported' => $imported, 'skipped' => $skipped];
  161. }
  162. /**
  163. * Import WordPress users
  164. */
  165. public function importUsers() {
  166. $this->log('Starting users import');
  167. $imported = 0;
  168. $skipped = 0;
  169. // Get WordPress users
  170. $stmt = $this->wpDb->query("
  171. SELECT ID, user_login, user_email, user_nicename, user_registered, display_name
  172. FROM wp_users
  173. WHERE user_status = 0
  174. ORDER BY ID
  175. ");
  176. $users = $stmt->fetchAll();
  177. foreach ($users as $wpUser) {
  178. try {
  179. // Check if user already exists
  180. $existing = $this->targetDb->fetch(
  181. "SELECT id FROM users WHERE username = ?",
  182. [$wpUser['user_login']]
  183. );
  184. if ($existing) {
  185. $skipped++;
  186. $this->log("User '{$wpUser['user_login']}' already exists, skipping");
  187. continue;
  188. }
  189. // Determine user role (WordPress usermeta table)
  190. $role = $this->getUserRole($wpUser['ID']);
  191. // Insert new user
  192. $this->targetDb->execute(
  193. "INSERT INTO users (username, email, role, auth_type, created_at) VALUES (?, ?, ?, 'wordpress', ?)",
  194. [
  195. $wpUser['user_login'],
  196. $wpUser['user_email'],
  197. $role,
  198. $wpUser['user_registered']
  199. ]
  200. );
  201. $imported++;
  202. $this->log("Imported user: {$wpUser['user_login']} (role: {$role})");
  203. } catch (Exception $e) {
  204. $this->errors[] = "Error importing user '{$wpUser['user_login']}': " . $e->getMessage();
  205. $this->log("Error importing user '{$wpUser['user_login']}': " . $e->getMessage(), 'error');
  206. }
  207. }
  208. $this->log("Users import completed: {$imported} imported, {$skipped} skipped");
  209. return ['imported' => $imported, 'skipped' => $skipped];
  210. }
  211. /**
  212. * Import WordPress posts
  213. */
  214. public function importPosts() {
  215. $this->log('Starting posts import');
  216. $imported = 0;
  217. $skipped = 0;
  218. try {
  219. // Get WordPress posts with author info in one query
  220. $stmt = $this->wpDb->query("
  221. SELECT p.ID, p.post_title, p.post_content, p.post_excerpt, p.post_date,
  222. p.post_modified, p.post_status, p.post_author, p.post_name,
  223. u.display_name as author_name
  224. FROM wp_posts p
  225. LEFT JOIN wp_users u ON p.post_author = u.ID
  226. WHERE p.post_type = 'post' AND p.post_status IN ('publish', 'draft')
  227. ORDER BY p.post_date
  228. LIMIT 1000
  229. ");
  230. $posts = $stmt->fetchAll();
  231. // Get all categories for all posts in one query
  232. $postIds = array_column($posts, 'ID');
  233. $categoriesMap = [];
  234. if (!empty($postIds)) {
  235. $placeholders = str_repeat('?,', count($postIds) - 1) . '?';
  236. $categoriesStmt = $this->wpDb->prepare("
  237. SELECT tr.object_id as post_id, t.name as category_name
  238. FROM wp_term_relationships tr
  239. JOIN wp_term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
  240. JOIN wp_terms t ON tt.term_id = t.term_id
  241. WHERE tt.taxonomy = 'category' AND tr.object_id IN ($placeholders)
  242. ");
  243. $categoriesStmt->execute($postIds);
  244. foreach ($categoriesStmt->fetchAll() as $cat) {
  245. $categoriesMap[$cat['post_id']][] = $cat['category_name'];
  246. }
  247. }
  248. foreach ($posts as $wpPost) {
  249. try {
  250. // Generate slug from post_name or title
  251. $slug = !empty($wpPost['post_name']) ? $wpPost['post_name'] : $this->generateSlug($wpPost['post_title']);
  252. // Map WordPress status to our status
  253. $status = ($wpPost['post_status'] === 'publish') ? 'published' : 'draft';
  254. // Use author name from query or fallback
  255. $author = $wpPost['author_name'] ?: 'Unknown Author';
  256. // Get categories from preloaded map
  257. $categories = $categoriesMap[$wpPost['ID']] ?? [];
  258. // Insert post
  259. $this->targetDb->execute(
  260. "INSERT INTO publications (title, slug, content, summary, author, status, created_at, updated_at, published_at)
  261. VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
  262. [
  263. $wpPost['post_title'],
  264. $slug,
  265. $this->processContent($wpPost['post_content']),
  266. $wpPost['post_excerpt'] ?? '',
  267. $author,
  268. $status,
  269. $wpPost['post_date'],
  270. $wpPost['post_modified'],
  271. ($status === 'published') ? $wpPost['post_date'] : null
  272. ]
  273. );
  274. $publicationId = $this->targetDb->lastInsertId();
  275. // Link categories
  276. if (!empty($categories)) {
  277. $this->linkPostCategories($publicationId, $categories);
  278. }
  279. $imported++;
  280. $this->log("Imported post: '{$wpPost['post_title']}'");
  281. } catch (Exception $e) {
  282. $this->errors[] = "Error importing post '{$wpPost['post_title']}': " . $e->getMessage();
  283. $this->log("Error importing post '{$wpPost['post_title']}': " . $e->getMessage(), 'error');
  284. }
  285. }
  286. } catch (Exception $e) {
  287. $this->errors[] = "Error in posts import: " . $e->getMessage();
  288. $this->log("Error in posts import: " . $e->getMessage(), 'error');
  289. }
  290. $this->log("Posts import completed: {$imported} imported, {$skipped} skipped");
  291. return ['imported' => $imported, 'skipped' => $skipped];
  292. }
  293. /**
  294. * Import WordPress comments
  295. */
  296. public function importComments() {
  297. $this->log('Starting comments import');
  298. $imported = 0;
  299. $skipped = 0;
  300. try {
  301. // Get WordPress comments with post info in one query
  302. $stmt = $this->wpDb->query("
  303. SELECT c.comment_ID, c.comment_post_ID, c.comment_author, c.comment_author_email,
  304. c.comment_content, c.comment_date, c.comment_approved, c.comment_parent,
  305. p.post_name, p.post_title
  306. FROM wp_comments c
  307. JOIN wp_posts p ON c.comment_post_ID = p.ID
  308. WHERE p.post_type = 'post'
  309. ORDER BY c.comment_date
  310. LIMIT 2000
  311. ");
  312. $comments = $stmt->fetchAll();
  313. // Build a map of post IDs to publication IDs
  314. $postIds = array_unique(array_column($comments, 'comment_post_ID'));
  315. $publicationMap = [];
  316. if (!empty($postIds)) {
  317. $placeholders = str_repeat('?,', count($postIds) - 1) . '?';
  318. $pubStmt = $this->targetDb->prepare("
  319. SELECT id, slug, title FROM publications
  320. WHERE slug IN ($placeholders) OR title IN ($placeholders)
  321. ");
  322. // Duplicate the post IDs for slug and title matching
  323. $allParams = array_merge($postIds, $postIds);
  324. $pubStmt->execute($allParams);
  325. foreach ($pubStmt->fetchAll() as $pub) {
  326. // Map both slug and title for easier lookup
  327. $publicationMap[strtolower($pub['slug'])] = $pub['id'];
  328. $publicationMap[strtolower($pub['title'])] = $pub['id'];
  329. }
  330. }
  331. foreach ($comments as $wpComment) {
  332. try {
  333. // Find corresponding publication using preloaded map
  334. $publicationId = null;
  335. $postSlug = !empty($wpComment['post_name']) ? strtolower($wpComment['post_name']) : null;
  336. $postTitle = strtolower($wpComment['post_title']);
  337. if ($postSlug && isset($publicationMap[$postSlug])) {
  338. $publicationId = $publicationMap[$postSlug];
  339. } elseif (isset($publicationMap[$postTitle])) {
  340. $publicationId = $publicationMap[$postTitle];
  341. }
  342. if (!$publicationId) {
  343. $skipped++;
  344. $this->log("Comment skipped - no matching publication found for post ID {$wpComment['comment_post_ID']}");
  345. continue;
  346. }
  347. // Map comment status
  348. $status = ($wpComment['comment_approved'] === '1') ? 'approved' : 'pending';
  349. // Handle parent comment (skip for now to avoid complexity)
  350. $parentId = null;
  351. // Insert comment
  352. $this->targetDb->execute(
  353. "INSERT INTO comments (publication_id, parent_id, name, email, content, status, created_at, admin_reply)
  354. VALUES (?, ?, ?, ?, ?, ?, ?, FALSE)",
  355. [
  356. $publicationId,
  357. $parentId,
  358. $wpComment['comment_author'],
  359. $wpComment['comment_author_email'],
  360. $wpComment['comment_content'],
  361. $status,
  362. $wpComment['comment_date']
  363. ]
  364. );
  365. $commentId = $this->targetDb->lastInsertId();
  366. // Store WordPress comment ID for parent mapping
  367. $this->targetDb->execute(
  368. "UPDATE comments SET wp_comment_id = ? WHERE id = ?",
  369. [$wpComment['comment_ID'], $commentId]
  370. );
  371. $imported++;
  372. $this->log("Imported comment for post ID {$wpComment['comment_post_ID']}");
  373. } catch (Exception $e) {
  374. $this->errors[] = "Error importing comment: " . $e->getMessage();
  375. $this->log("Error importing comment: " . $e->getMessage(), 'error');
  376. }
  377. }
  378. } catch (Exception $e) {
  379. $this->errors[] = "Error in comments import: " . $e->getMessage();
  380. $this->log("Error in comments import: " . $e->getMessage(), 'error');
  381. }
  382. $this->log("Comments import completed: {$imported} imported, {$skipped} skipped");
  383. return ['imported' => $imported, 'skipped' => $skipped];
  384. }
  385. /**
  386. * Helper methods
  387. */
  388. private function getUserRole($userId) {
  389. $stmt = $this->wpDb->prepare("
  390. SELECT meta_value FROM wp_usermeta
  391. WHERE user_id = ? AND meta_key = 'wp_capabilities'
  392. ");
  393. $stmt->execute([$userId]);
  394. $capabilities = $stmt->fetchColumn();
  395. if ($capabilities && strpos($capabilities, 'administrator') !== false) {
  396. return 'admin';
  397. }
  398. return 'editor'; // Default role
  399. }
  400. private function getAuthorName($authorId) {
  401. $stmt = $this->wpDb->prepare("SELECT display_name FROM wp_users WHERE ID = ?");
  402. $stmt->execute([$authorId]);
  403. $name = $stmt->fetchColumn();
  404. return $name ?: 'Unknown Author';
  405. }
  406. private function getPostCategories($postId) {
  407. $stmt = $this->wpDb->prepare("
  408. SELECT t.name FROM wp_terms t
  409. JOIN wp_term_taxonomy tt ON t.term_id = tt.term_id
  410. JOIN wp_term_relationships tr ON tt.term_taxonomy_id = tr.term_taxonomy_id
  411. WHERE tr.object_id = ? AND tt.taxonomy = 'category'
  412. ");
  413. $stmt->execute([$postId]);
  414. return $stmt->fetchAll(PDO::FETCH_COLUMN);
  415. }
  416. private function getPostSlugById($postId) {
  417. $stmt = $this->wpDb->prepare("SELECT post_name FROM wp_posts WHERE ID = ?");
  418. $stmt->execute([$postId]);
  419. return $stmt->fetchColumn() ?: '';
  420. }
  421. private function getPostTitleById($postId) {
  422. $stmt = $this->wpDb->prepare("SELECT post_title FROM wp_posts WHERE ID = ?");
  423. $stmt->execute([$postId]);
  424. return $stmt->fetchColumn() ?: '';
  425. }
  426. private function linkPostCategories($publicationId, $categories) {
  427. foreach ($categories as $categoryName) {
  428. $category = $this->targetDb->fetch(
  429. "SELECT id FROM categories WHERE name = ?",
  430. [$categoryName]
  431. );
  432. if ($category) {
  433. $this->targetDb->execute(
  434. "INSERT IGNORE INTO publication_categories (publication_id, category_id) VALUES (?, ?)",
  435. [$publicationId, $category['id']]
  436. );
  437. }
  438. }
  439. }
  440. private function processContent($content) {
  441. // Basic WordPress content processing
  442. // You can extend this to handle shortcodes, etc.
  443. $content = str_replace('[caption]', '', $content);
  444. $content = str_replace('[/caption]', '', $content);
  445. $content = preg_replace('/\[gallery.*?\]/', '', $content);
  446. return $content;
  447. }
  448. private function generateSlug($title) {
  449. $slug = strtolower($title);
  450. $slug = preg_replace('/[^a-z0-9]+/', '-', $slug);
  451. $slug = trim($slug, '-');
  452. return $slug;
  453. }
  454. private function log($message, $level = 'info') {
  455. $this->importLog[] = [
  456. 'timestamp' => date('Y-m-d H:i:s'),
  457. 'level' => $level,
  458. 'message' => $message
  459. ];
  460. }
  461. /**
  462. * Get statistics methods
  463. */
  464. public function getPostCount() {
  465. $stmt = $this->wpDb->query("SELECT COUNT(*) FROM wp_posts WHERE post_type = 'post'");
  466. return $stmt->fetchColumn();
  467. }
  468. public function getPageCount() {
  469. $stmt = $this->wpDb->query("SELECT COUNT(*) FROM wp_posts WHERE post_type = 'page'");
  470. return $stmt->fetchColumn();
  471. }
  472. public function getCategoryCount() {
  473. $stmt = $this->wpDb->query("
  474. SELECT COUNT(*) FROM wp_term_taxonomy
  475. WHERE taxonomy = 'category'
  476. ");
  477. return $stmt->fetchColumn();
  478. }
  479. public function getUserCount() {
  480. $stmt = $this->wpDb->query("SELECT COUNT(*) FROM wp_users");
  481. return $stmt->fetchColumn();
  482. }
  483. public function getCommentCount() {
  484. $stmt = $this->wpDb->query("SELECT COUNT(*) FROM wp_comments");
  485. return $stmt->fetchColumn();
  486. }
  487. /**
  488. * Get import log
  489. */
  490. public function getLog() {
  491. return $this->importLog;
  492. }
  493. /**
  494. * Get errors
  495. */
  496. public function getErrors() {
  497. return $this->errors;
  498. }
  499. }