setup.sql 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. -- Web Publication System Complete Database Schema
  2. -- MariaDB/MySQL compatible
  3. -- Combined schema with all migrations for setup.php
  4. -- Note: Database creation and selection is handled by setup script
  5. -- Publications table
  6. CREATE TABLE publications (
  7. id INT AUTO_INCREMENT PRIMARY KEY,
  8. title VARCHAR(255) NOT NULL,
  9. slug VARCHAR(255),
  10. content TEXT NOT NULL,
  11. summary VARCHAR(500),
  12. author VARCHAR(100) NOT NULL,
  13. status ENUM('draft', 'published', 'archived') DEFAULT 'draft',
  14. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  15. updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  16. published_at TIMESTAMP NULL DEFAULT NULL,
  17. INDEX idx_publications_slug (slug)
  18. );
  19. -- Users table for admin authentication with LDAP support
  20. CREATE TABLE users (
  21. id INT AUTO_INCREMENT PRIMARY KEY,
  22. username VARCHAR(50) UNIQUE NOT NULL,
  23. password VARCHAR(255),
  24. email VARCHAR(255),
  25. role ENUM('admin', 'editor', 'user') DEFAULT 'editor',
  26. auth_type ENUM('local', 'ldap') DEFAULT 'local',
  27. ldap_dn VARCHAR(255),
  28. status ENUM('active', 'inactive') NOT NULL DEFAULT 'active',
  29. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  30. last_login TIMESTAMP NULL,
  31. updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  32. INDEX idx_users_role (role),
  33. INDEX idx_users_status (status)
  34. );
  35. -- Categories table
  36. CREATE TABLE categories (
  37. id INT AUTO_INCREMENT PRIMARY KEY,
  38. name VARCHAR(100) UNIQUE NOT NULL,
  39. description TEXT,
  40. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
  41. );
  42. -- Publication categories junction table
  43. CREATE TABLE publication_categories (
  44. publication_id INT,
  45. category_id INT,
  46. PRIMARY KEY (publication_id, category_id),
  47. FOREIGN KEY (publication_id) REFERENCES publications(id) ON DELETE CASCADE,
  48. FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE CASCADE
  49. );
  50. -- Images table for WYSIWYG editor
  51. CREATE TABLE IF NOT EXISTS images (
  52. id INT AUTO_INCREMENT PRIMARY KEY,
  53. filename VARCHAR(255) NOT NULL,
  54. original_name VARCHAR(255) NOT NULL,
  55. file_size INT NOT NULL,
  56. mime_type VARCHAR(100),
  57. uploaded_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  58. uploaded_by INT,
  59. INDEX idx_uploaded_at (uploaded_at),
  60. INDEX idx_uploaded_by (uploaded_by)
  61. );
  62. -- Add foreign key constraint for images table
  63. ALTER TABLE images ADD CONSTRAINT fk_images_user FOREIGN KEY (uploaded_by) REFERENCES users(id) ON DELETE SET NULL;
  64. -- Comments table for publication comments
  65. CREATE TABLE IF NOT EXISTS comments (
  66. id INT AUTO_INCREMENT PRIMARY KEY,
  67. publication_id INT NOT NULL,
  68. parent_id INT NULL DEFAULT NULL,
  69. name VARCHAR(100) NOT NULL,
  70. email VARCHAR(255),
  71. content TEXT NOT NULL,
  72. status ENUM('pending', 'approved', 'rejected') DEFAULT 'pending',
  73. ip_address VARCHAR(45),
  74. user_agent TEXT,
  75. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  76. updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  77. admin_reply BOOLEAN DEFAULT FALSE,
  78. replied_by INT NULL,
  79. wp_comment_id INT NULL,
  80. FOREIGN KEY (publication_id) REFERENCES publications(id) ON DELETE CASCADE,
  81. FOREIGN KEY (parent_id) REFERENCES comments(id) ON DELETE CASCADE,
  82. FOREIGN KEY (replied_by) REFERENCES users(id) ON DELETE SET NULL,
  83. INDEX idx_publication_id (publication_id),
  84. INDEX idx_parent_id (parent_id),
  85. INDEX idx_status (status),
  86. INDEX idx_created_at (created_at),
  87. INDEX idx_wp_comment_id (wp_comment_id)
  88. );
  89. -- Insert default admin user (password: admin123)
  90. INSERT INTO users (username, password, email, role, auth_type, status) VALUES
  91. ('admin', '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', 'admin@example.com', 'admin', 'local', 'active');
  92. -- Insert sample categories
  93. INSERT INTO categories (name, description) VALUES
  94. ('Technology', 'Articles about technology and programming'),
  95. ('News', 'Current events and news articles'),
  96. ('Tutorial', 'How-to guides and tutorials'),
  97. ('Opinion', 'Editorials and opinion pieces');
  98. -- Insert sample publications
  99. INSERT INTO publications (title, slug, content, summary, author, status, published_at) VALUES
  100. ('Welcome to Web Publication System', 'welcome-to-web-publication-system', 'This is a sample publication to demonstrate the system capabilities. You can edit or delete this publication from the admin panel.', 'A welcome message for new users', 'admin', 'published', NOW()),
  101. ('Getting Started with PHP', 'getting-started-with-php', 'PHP is a popular server-side scripting language designed for web development. This article covers the basics of getting started with PHP development.', 'Introduction to PHP programming', 'admin', 'published', NOW()),
  102. ('Database Design Best Practices', 'database-design-best-practices', 'Good database design is crucial for application performance and maintainability. This article covers essential principles and practices.', 'Tips for designing efficient databases', 'admin', 'draft', NULL);
  103. -- Update existing publications to generate slugs from titles (for any existing data)
  104. UPDATE publications
  105. SET slug = LOWER(REPLACE(REPLACE(REPLACE(title, '[^a-z0-9 ]', ''), ' ', '-'), '-', ''))
  106. WHERE slug IS NULL OR slug = '';
  107. -- Update published_at for existing published publications (for any existing data)
  108. UPDATE publications
  109. SET published_at = created_at
  110. WHERE status = 'published' AND published_at IS NULL;
  111. -- Update existing users to have default role and status (for any existing data)
  112. UPDATE users SET role = 'admin' WHERE id = 1; -- Make first user admin
  113. UPDATE users SET status = 'active' WHERE status IS NULL;