wordpress_import.php 24 KB

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