wordpress_import.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <?php
  2. // Start session for language preference
  3. if (session_status() === PHP_SESSION_NONE) {
  4. session_start();
  5. }
  6. require_once '../includes/config.php';
  7. require_once '../includes/database.php';
  8. require_once '../includes/auth.php';
  9. require_once '../includes/wordpress_import.php';
  10. require_once '../includes/translation.php';
  11. // Translation system is auto-initialized when translation.php is included
  12. $auth = new Auth();
  13. $auth->requireAuth();
  14. $user = $auth->getUser();
  15. $message = '';
  16. $importResults = null;
  17. $connectionTest = null;
  18. // Handle form submission
  19. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  20. try {
  21. // Debug: Log form submission
  22. error_log('WordPress import form submitted');
  23. // Get WordPress database configuration
  24. $wpConfig = [
  25. 'host' => trim($_POST['wp_host'] ?? ''),
  26. 'database' => trim($_POST['wp_database'] ?? ''),
  27. 'username' => trim($_POST['wp_username'] ?? ''),
  28. 'password' => $_POST['wp_password'] ?? ''
  29. ];
  30. // Debug: Log config (without password)
  31. error_log('WP Config: ' . print_r([
  32. 'host' => $wpConfig['host'],
  33. 'database' => $wpConfig['database'],
  34. 'username' => $wpConfig['username'],
  35. 'password' => !empty($wpConfig['password']) ? '[SET]' : '[EMPTY]'
  36. ], true));
  37. // Validate required fields
  38. if (empty($wpConfig['host']) || empty($wpConfig['database']) || empty($wpConfig['username'])) {
  39. throw new Exception(t('wordpress_import_config_required'));
  40. }
  41. // Debug: About to create importer
  42. error_log('About to create WordPressImport instance');
  43. // Create import instance
  44. $importer = new WordPressImport($wpConfig);
  45. // Debug: Importer created successfully
  46. error_log('WordPressImport instance created successfully');
  47. if (isset($_POST['test_connection'])) {
  48. // Debug: Testing connection
  49. error_log('About to test WordPress connection');
  50. // Test connection only
  51. $connectionTest = $importer->testConnection();
  52. // Debug: Connection test result
  53. error_log('Connection test result: ' . print_r($connectionTest, true));
  54. if ($connectionTest['success']) {
  55. $message = t('wordpress_connection_success') . ': ' .
  56. t('posts') . ': ' . $connectionTest['stats']['posts'] . ', ' .
  57. t('categories') . ': ' . $connectionTest['stats']['categories'] . ', ' .
  58. t('users') . ': ' . $connectionTest['stats']['users'] . ', ' .
  59. t('comments') . ': ' . $connectionTest['stats']['comments'];
  60. } else {
  61. throw new Exception($connectionTest['error']);
  62. }
  63. } elseif (isset($_POST['start_import'])) {
  64. // Debug: Starting import
  65. error_log('About to start WordPress import');
  66. // Start import
  67. $importOptions = [
  68. 'import_categories' => isset($_POST['import_categories']),
  69. 'import_users' => isset($_POST['import_users']),
  70. 'import_posts' => isset($_POST['import_posts']),
  71. 'import_comments' => isset($_POST['import_comments'])
  72. ];
  73. error_log('Import options: ' . print_r($importOptions, true));
  74. // Debug: About to call importAll
  75. error_log('About to call importer->importAll()');
  76. $importResults = $importer->importAll($importOptions);
  77. // Debug: Import completed
  78. error_log('importAll() completed');
  79. if ($importResults['success']) {
  80. $message = t('wordpress_import_success');
  81. error_log('Import successful: ' . print_r($importResults['results'], true));
  82. } else {
  83. error_log('Import failed: ' . $importResults['error']);
  84. throw new Exception($importResults['error']);
  85. }
  86. }
  87. } catch (Exception $e) {
  88. $message = $e->getMessage();
  89. }
  90. }
  91. ?>
  92. <!DOCTYPE html>
  93. <html lang="<?php echo Translation::getCurrentLang(); ?>">
  94. <head>
  95. <meta charset="UTF-8">
  96. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  97. <title><?php echo t('wordpress_import'); ?> - <?php echo SITE_TITLE; ?></title>
  98. <link rel="stylesheet" href="../css/style.css">
  99. <link rel="stylesheet" href="../css/admin-import.css">
  100. </head>
  101. <body>
  102. <div class="admin-layout">
  103. <header class="admin-header">
  104. <div class="header-content">
  105. <h1><a href="/index.php"><?php echo SITE_TITLE; ?></a></h1>
  106. <nav class="admin-nav">
  107. <a href="index.php" class="nav-link"><?php echo t('admin_nav_dashboard'); ?></a>
  108. <a href="publications.php" class="nav-link"><?php echo t('admin_nav_publications'); ?></a>
  109. <a href="categories.php" class="nav-link"><?php echo t('admin_nav_categories'); ?></a>
  110. <a href="comments.php" class="nav-link"><?php echo t('admin_nav_comments'); ?></a>
  111. <a href="users.php" class="nav-link"><?php echo t('manage_users'); ?></a>
  112. <a href="wordpress_import.php" class="nav-link active"><?php echo t('wordpress_import'); ?></a>
  113. <?php if (LDAP_ENABLED): ?>
  114. <a href="ldap-users.php" class="nav-link"><?php echo t('admin_nav_ldap_users'); ?></a>
  115. <?php endif; ?>
  116. <a href="logout.php" class="nav-link"><?php echo t('admin_nav_logout'); ?></a>
  117. </nav>
  118. <div class="user-info">
  119. <?php echo t('welcome'); ?>, <?php echo htmlspecialchars($user['username']); ?>
  120. </div>
  121. <?php echo Translation::getLanguageSwitcher('wordpress_import.php'); ?>
  122. </div>
  123. </header>
  124. <main class="admin-main">
  125. <div class="page-header">
  126. <h2><?php echo t('wordpress_import'); ?></h2>
  127. <p class="page-description"><?php echo t('wordpress_import_description'); ?></p>
  128. </div>
  129. <?php if ($message): ?>
  130. <div class="alert alert-<?php echo strpos($message, 'Error') === false && strpos($message, 'failed') === false ? 'success' : 'error'; ?>">
  131. <?php echo $message; ?>
  132. </div>
  133. <?php endif; ?>
  134. <!-- Import Form -->
  135. <div class="import-form-container">
  136. <form method="post" class="import-form">
  137. <div class="form-section">
  138. <h3><?php echo t('wordpress_database_config'); ?></h3>
  139. <div class="form-row">
  140. <div class="form-group">
  141. <label for="wp_host"><?php echo t('database_host'); ?> *</label>
  142. <input type="text" id="wp_host" name="wp_host"
  143. value="<?php echo htmlspecialchars($_POST['wp_host'] ?? 'localhost'); ?>"
  144. placeholder="localhost" required>
  145. </div>
  146. <div class="form-group">
  147. <label for="wp_database"><?php echo t('database_name'); ?> *</label>
  148. <input type="text" id="wp_database" name="wp_database"
  149. value="<?php echo htmlspecialchars($_POST['wp_database'] ?? ''); ?>"
  150. placeholder="wordpress" required>
  151. </div>
  152. </div>
  153. <div class="form-row">
  154. <div class="form-group">
  155. <label for="wp_username"><?php echo t('database_username'); ?> *</label>
  156. <input type="text" id="wp_username" name="wp_username"
  157. value="<?php echo htmlspecialchars($_POST['wp_username'] ?? ''); ?>"
  158. placeholder="wp_user" required>
  159. </div>
  160. <div class="form-group">
  161. <label for="wp_password"><?php echo t('database_password'); ?></label>
  162. <input type="password" id="wp_password" name="wp_password"
  163. placeholder="<?php echo t('database_password_placeholder'); ?>">
  164. </div>
  165. </div>
  166. </div>
  167. <div class="form-section">
  168. <h3><?php echo t('import_options'); ?></h3>
  169. <div class="checkbox-group">
  170. <label class="checkbox-label">
  171. <input type="checkbox" name="import_categories" checked>
  172. <span class="checkmark"></span>
  173. <?php echo t('import_categories'); ?>
  174. </label>
  175. <label class="checkbox-label">
  176. <input type="checkbox" name="import_users" checked>
  177. <span class="checkmark"></span>
  178. <?php echo t('import_users'); ?>
  179. </label>
  180. <label class="checkbox-label">
  181. <input type="checkbox" name="import_posts" checked>
  182. <span class="checkmark"></span>
  183. <?php echo t('import_posts'); ?>
  184. </label>
  185. <label class="checkbox-label">
  186. <input type="checkbox" name="import_comments" checked>
  187. <span class="checkmark"></span>
  188. <?php echo t('import_comments'); ?>
  189. </label>
  190. </div>
  191. </div>
  192. <div class="form-actions">
  193. <button type="submit" name="test_connection" class="btn btn-secondary">
  194. <?php echo t('test_connection'); ?>
  195. </button>
  196. <button type="submit" name="start_import" class="btn btn-primary"
  197. onclick="return confirm('<?php echo t('wordpress_import_confirm'); ?>')">
  198. <?php echo t('start_import'); ?>
  199. </button>
  200. </div>
  201. </form>
  202. </div>
  203. <!-- Import Results -->
  204. <?php if ($importResults): ?>
  205. <div class="import-results">
  206. <h3><?php echo t('import_results'); ?></h3>
  207. <?php if ($importResults['success']): ?>
  208. <div class="success-summary">
  209. <p class="success-message"><?php echo t('wordpress_import_success'); ?></p>
  210. </div>
  211. <div class="results-details">
  212. <?php foreach ($importResults['results'] as $type => $result): ?>
  213. <div class="result-item">
  214. <h4><?php echo t('import_' . $type); ?></h4>
  215. <div class="result-stats">
  216. <span class="stat success">
  217. <?php echo t('imported'); ?>: <strong><?php echo $result['imported']; ?></strong>
  218. </span>
  219. <span class="stat warning">
  220. <?php echo t('skipped'); ?>: <strong><?php echo $result['skipped']; ?></strong>
  221. </span>
  222. </div>
  223. </div>
  224. <?php endforeach; ?>
  225. </div>
  226. <?php if (!empty($importResults['log'])): ?>
  227. <div class="import-log">
  228. <h4><?php echo t('import_log'); ?></h4>
  229. <div class="log-container">
  230. <?php foreach ($importResults['log'] as $logEntry): ?>
  231. <div class="log-entry log-<?php echo $logEntry['level']; ?>">
  232. <span class="log-time"><?php echo $logEntry['timestamp']; ?></span>
  233. <span class="log-message"><?php echo htmlspecialchars($logEntry['message']); ?></span>
  234. </div>
  235. <?php endforeach; ?>
  236. </div>
  237. </div>
  238. <?php endif; ?>
  239. <?php if (!empty($importResults['errors'])): ?>
  240. <div class="import-errors">
  241. <h4><?php echo t('import_errors'); ?></h4>
  242. <div class="error-list">
  243. <?php foreach ($importResults['errors'] as $error): ?>
  244. <div class="error-item">
  245. <?php echo htmlspecialchars($error); ?>
  246. </div>
  247. <?php endforeach; ?>
  248. </div>
  249. </div>
  250. <?php endif; ?>
  251. <?php else: ?>
  252. <div class="error-summary">
  253. <p class="error-message"><?php echo htmlspecialchars($importResults['error']); ?></p>
  254. </div>
  255. <?php endif; ?>
  256. </div>
  257. <?php endif; ?>
  258. <!-- Information Section -->
  259. <div class="info-section">
  260. <h3><?php echo t('wordpress_import_info'); ?></h3>
  261. <div class="info-content">
  262. <div class="info-item">
  263. <h4><?php echo t('what_gets_imported'); ?></h4>
  264. <ul>
  265. <li><?php echo t('import_posts_info'); ?></li>
  266. <li><?php echo t('import_categories_info'); ?></li>
  267. <li><?php echo t('import_users_info'); ?></li>
  268. <li><?php echo t('import_comments_info'); ?></li>
  269. </ul>
  270. </div>
  271. <div class="info-item">
  272. <h4><?php echo t('import_requirements'); ?></h4>
  273. <ul>
  274. <li><?php echo t('import_db_access'); ?></li>
  275. <li><?php echo t('import_wp_structure'); ?></li>
  276. <li><?php echo t('import_backup'); ?></li>
  277. </ul>
  278. </div>
  279. <div class="info-item">
  280. <h4><?php echo t('import_notes'); ?></h4>
  281. <ul>
  282. <li><?php echo t('import_existing_data'); ?></li>
  283. <li><?php echo t('import_content_processing'); ?></li>
  284. <li><?php echo t('import_user_mapping'); ?></li>
  285. </ul>
  286. </div>
  287. </div>
  288. </div>
  289. </main>
  290. </div>
  291. <script>
  292. // WordPress Import JavaScript
  293. document.addEventListener('DOMContentLoaded', function() {
  294. const form = document.querySelector('.import-form');
  295. const testBtn = document.querySelector('button[name="test_connection"]');
  296. const importBtn = document.querySelector('button[name="start_import"]');
  297. // Show loading state
  298. function setLoading(button, loading) {
  299. if (loading) {
  300. button.disabled = true;
  301. button.dataset.originalText = button.textContent;
  302. button.textContent = button.dataset.loadingText || '<?php echo t('loading'); ?>...';
  303. } else {
  304. button.disabled = false;
  305. button.textContent = button.dataset.originalText;
  306. }
  307. }
  308. // Test connection loading
  309. testBtn.addEventListener('click', function() {
  310. setLoading(this, true);
  311. this.dataset.loadingText = '<?php echo t('testing_connection'); ?>';
  312. });
  313. // Import loading
  314. importBtn.addEventListener('click', function() {
  315. setLoading(this, true);
  316. this.dataset.loadingText = '<?php echo t('importing'); ?>...';
  317. });
  318. // Reset loading on form submit
  319. form.addEventListener('submit', function() {
  320. setTimeout(() => {
  321. setLoading(testBtn, false);
  322. setLoading(importBtn, false);
  323. }, 1000);
  324. });
  325. // Auto-focus first empty field
  326. const firstEmpty = form.querySelector('input[value=""], input:not([value])');
  327. if (firstEmpty) {
  328. firstEmpty.focus();
  329. }
  330. });
  331. </script>
  332. </body>
  333. </html>