wordpress_import.php 16 KB

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