wordpress_import.php 22 KB

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