wordpress_import.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. // Start import
  65. $importOptions = [
  66. 'import_categories' => isset($_POST['import_categories']),
  67. 'import_users' => isset($_POST['import_users']),
  68. 'import_posts' => isset($_POST['import_posts']),
  69. 'import_comments' => isset($_POST['import_comments'])
  70. ];
  71. $importResults = $importer->importAll($importOptions);
  72. if ($importResults['success']) {
  73. $message = t('wordpress_import_success');
  74. } else {
  75. throw new Exception($importResults['error']);
  76. }
  77. }
  78. } catch (Exception $e) {
  79. $message = $e->getMessage();
  80. }
  81. }
  82. ?>
  83. <!DOCTYPE html>
  84. <html lang="<?php echo Translation::getCurrentLang(); ?>">
  85. <head>
  86. <meta charset="UTF-8">
  87. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  88. <title><?php echo t('wordpress_import'); ?> - <?php echo SITE_TITLE; ?></title>
  89. <link rel="stylesheet" href="../css/style.css">
  90. <link rel="stylesheet" href="../css/admin-import.css">
  91. </head>
  92. <body>
  93. <div class="admin-layout">
  94. <header class="admin-header">
  95. <div class="header-content">
  96. <h1><a href="/index.php"><?php echo SITE_TITLE; ?></a></h1>
  97. <nav class="admin-nav">
  98. <a href="index.php" class="nav-link"><?php echo t('admin_nav_dashboard'); ?></a>
  99. <a href="publications.php" class="nav-link"><?php echo t('admin_nav_publications'); ?></a>
  100. <a href="categories.php" class="nav-link"><?php echo t('admin_nav_categories'); ?></a>
  101. <a href="comments.php" class="nav-link"><?php echo t('admin_nav_comments'); ?></a>
  102. <a href="users.php" class="nav-link"><?php echo t('manage_users'); ?></a>
  103. <a href="wordpress_import.php" class="nav-link active"><?php echo t('wordpress_import'); ?></a>
  104. <?php if (LDAP_ENABLED): ?>
  105. <a href="ldap-users.php" class="nav-link"><?php echo t('admin_nav_ldap_users'); ?></a>
  106. <?php endif; ?>
  107. <a href="logout.php" class="nav-link"><?php echo t('admin_nav_logout'); ?></a>
  108. </nav>
  109. <div class="user-info">
  110. <?php echo t('welcome'); ?>, <?php echo htmlspecialchars($user['username']); ?>
  111. </div>
  112. <?php echo Translation::getLanguageSwitcher('wordpress_import.php'); ?>
  113. </div>
  114. </header>
  115. <main class="admin-main">
  116. <div class="page-header">
  117. <h2><?php echo t('wordpress_import'); ?></h2>
  118. <p class="page-description"><?php echo t('wordpress_import_description'); ?></p>
  119. </div>
  120. <?php if ($message): ?>
  121. <div class="alert alert-<?php echo strpos($message, 'Error') === false && strpos($message, 'failed') === false ? 'success' : 'error'; ?>">
  122. <?php echo $message; ?>
  123. </div>
  124. <?php endif; ?>
  125. <!-- Import Form -->
  126. <div class="import-form-container">
  127. <form method="post" class="import-form">
  128. <div class="form-section">
  129. <h3><?php echo t('wordpress_database_config'); ?></h3>
  130. <div class="form-row">
  131. <div class="form-group">
  132. <label for="wp_host"><?php echo t('database_host'); ?> *</label>
  133. <input type="text" id="wp_host" name="wp_host"
  134. value="<?php echo htmlspecialchars($_POST['wp_host'] ?? 'localhost'); ?>"
  135. placeholder="localhost" required>
  136. </div>
  137. <div class="form-group">
  138. <label for="wp_database"><?php echo t('database_name'); ?> *</label>
  139. <input type="text" id="wp_database" name="wp_database"
  140. value="<?php echo htmlspecialchars($_POST['wp_database'] ?? ''); ?>"
  141. placeholder="wordpress" required>
  142. </div>
  143. </div>
  144. <div class="form-row">
  145. <div class="form-group">
  146. <label for="wp_username"><?php echo t('database_username'); ?> *</label>
  147. <input type="text" id="wp_username" name="wp_username"
  148. value="<?php echo htmlspecialchars($_POST['wp_username'] ?? ''); ?>"
  149. placeholder="wp_user" required>
  150. </div>
  151. <div class="form-group">
  152. <label for="wp_password"><?php echo t('database_password'); ?></label>
  153. <input type="password" id="wp_password" name="wp_password"
  154. placeholder="<?php echo t('database_password_placeholder'); ?>">
  155. </div>
  156. </div>
  157. </div>
  158. <div class="form-section">
  159. <h3><?php echo t('import_options'); ?></h3>
  160. <div class="checkbox-group">
  161. <label class="checkbox-label">
  162. <input type="checkbox" name="import_categories" checked>
  163. <span class="checkmark"></span>
  164. <?php echo t('import_categories'); ?>
  165. </label>
  166. <label class="checkbox-label">
  167. <input type="checkbox" name="import_users" checked>
  168. <span class="checkmark"></span>
  169. <?php echo t('import_users'); ?>
  170. </label>
  171. <label class="checkbox-label">
  172. <input type="checkbox" name="import_posts" checked>
  173. <span class="checkmark"></span>
  174. <?php echo t('import_posts'); ?>
  175. </label>
  176. <label class="checkbox-label">
  177. <input type="checkbox" name="import_comments" checked>
  178. <span class="checkmark"></span>
  179. <?php echo t('import_comments'); ?>
  180. </label>
  181. </div>
  182. </div>
  183. <div class="form-actions">
  184. <button type="submit" name="test_connection" class="btn btn-secondary">
  185. <?php echo t('test_connection'); ?>
  186. </button>
  187. <button type="submit" name="start_import" class="btn btn-primary"
  188. onclick="return confirm('<?php echo t('wordpress_import_confirm'); ?>')">
  189. <?php echo t('start_import'); ?>
  190. </button>
  191. </div>
  192. </form>
  193. </div>
  194. <!-- Import Results -->
  195. <?php if ($importResults): ?>
  196. <div class="import-results">
  197. <h3><?php echo t('import_results'); ?></h3>
  198. <?php if ($importResults['success']): ?>
  199. <div class="success-summary">
  200. <p class="success-message"><?php echo t('wordpress_import_success'); ?></p>
  201. </div>
  202. <div class="results-details">
  203. <?php foreach ($importResults['results'] as $type => $result): ?>
  204. <div class="result-item">
  205. <h4><?php echo t('import_' . $type); ?></h4>
  206. <div class="result-stats">
  207. <span class="stat success">
  208. <?php echo t('imported'); ?>: <strong><?php echo $result['imported']; ?></strong>
  209. </span>
  210. <span class="stat warning">
  211. <?php echo t('skipped'); ?>: <strong><?php echo $result['skipped']; ?></strong>
  212. </span>
  213. </div>
  214. </div>
  215. <?php endforeach; ?>
  216. </div>
  217. <?php if (!empty($importResults['log'])): ?>
  218. <div class="import-log">
  219. <h4><?php echo t('import_log'); ?></h4>
  220. <div class="log-container">
  221. <?php foreach ($importResults['log'] as $logEntry): ?>
  222. <div class="log-entry log-<?php echo $logEntry['level']; ?>">
  223. <span class="log-time"><?php echo $logEntry['timestamp']; ?></span>
  224. <span class="log-message"><?php echo htmlspecialchars($logEntry['message']); ?></span>
  225. </div>
  226. <?php endforeach; ?>
  227. </div>
  228. </div>
  229. <?php endif; ?>
  230. <?php if (!empty($importResults['errors'])): ?>
  231. <div class="import-errors">
  232. <h4><?php echo t('import_errors'); ?></h4>
  233. <div class="error-list">
  234. <?php foreach ($importResults['errors'] as $error): ?>
  235. <div class="error-item">
  236. <?php echo htmlspecialchars($error); ?>
  237. </div>
  238. <?php endforeach; ?>
  239. </div>
  240. </div>
  241. <?php endif; ?>
  242. <?php else: ?>
  243. <div class="error-summary">
  244. <p class="error-message"><?php echo htmlspecialchars($importResults['error']); ?></p>
  245. </div>
  246. <?php endif; ?>
  247. </div>
  248. <?php endif; ?>
  249. <!-- Information Section -->
  250. <div class="info-section">
  251. <h3><?php echo t('wordpress_import_info'); ?></h3>
  252. <div class="info-content">
  253. <div class="info-item">
  254. <h4><?php echo t('what_gets_imported'); ?></h4>
  255. <ul>
  256. <li><?php echo t('import_posts_info'); ?></li>
  257. <li><?php echo t('import_categories_info'); ?></li>
  258. <li><?php echo t('import_users_info'); ?></li>
  259. <li><?php echo t('import_comments_info'); ?></li>
  260. </ul>
  261. </div>
  262. <div class="info-item">
  263. <h4><?php echo t('import_requirements'); ?></h4>
  264. <ul>
  265. <li><?php echo t('import_db_access'); ?></li>
  266. <li><?php echo t('import_wp_structure'); ?></li>
  267. <li><?php echo t('import_backup'); ?></li>
  268. </ul>
  269. </div>
  270. <div class="info-item">
  271. <h4><?php echo t('import_notes'); ?></h4>
  272. <ul>
  273. <li><?php echo t('import_existing_data'); ?></li>
  274. <li><?php echo t('import_content_processing'); ?></li>
  275. <li><?php echo t('import_user_mapping'); ?></li>
  276. </ul>
  277. </div>
  278. </div>
  279. </div>
  280. </main>
  281. </div>
  282. <script>
  283. // WordPress Import JavaScript
  284. document.addEventListener('DOMContentLoaded', function() {
  285. const form = document.querySelector('.import-form');
  286. const testBtn = document.querySelector('button[name="test_connection"]');
  287. const importBtn = document.querySelector('button[name="start_import"]');
  288. // Show loading state
  289. function setLoading(button, loading) {
  290. if (loading) {
  291. button.disabled = true;
  292. button.dataset.originalText = button.textContent;
  293. button.textContent = button.dataset.loadingText || '<?php echo t('loading'); ?>...';
  294. } else {
  295. button.disabled = false;
  296. button.textContent = button.dataset.originalText;
  297. }
  298. }
  299. // Test connection loading
  300. testBtn.addEventListener('click', function() {
  301. setLoading(this, true);
  302. this.dataset.loadingText = '<?php echo t('testing_connection'); ?>';
  303. });
  304. // Import loading
  305. importBtn.addEventListener('click', function() {
  306. setLoading(this, true);
  307. this.dataset.loadingText = '<?php echo t('importing'); ?>...';
  308. });
  309. // Reset loading on form submit
  310. form.addEventListener('submit', function() {
  311. setTimeout(() => {
  312. setLoading(testBtn, false);
  313. setLoading(importBtn, false);
  314. }, 1000);
  315. });
  316. // Auto-focus first empty field
  317. const firstEmpty = form.querySelector('input[value=""], input:not([value])');
  318. if (firstEmpty) {
  319. firstEmpty.focus();
  320. }
  321. });
  322. </script>
  323. </body>
  324. </html>