-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.php
More file actions
233 lines (193 loc) · 7.98 KB
/
install.php
File metadata and controls
233 lines (193 loc) · 7.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
require_once __DIR__ . '/config/env.php';
require_once __DIR__ . '/config/Logger.php';
require_once __DIR__ . '/config/Database.php';
putenv("APP_NAME=install");
$logger = Logger::getInstance();
$logger->log("Starting database installation...");
function loadSql($db, $dataFile, $logger)
{
$data = file_get_contents($dataFile);
// Split on semicolons but keep them in the statements
$statements = preg_split('/(?<=[;])/', $data, -1, PREG_SPLIT_NO_EMPTY);
foreach ($statements as $statement) {
$statement = trim($statement);
// echo $statement;
if (!empty($statement)) {
try {
$db->execute($statement);
} catch (Exception $e) {
echo "Error executing website data statement: " . $statement . "\n";
$logger->logError("Error executing website data statement: " . $statement, $e);
throw $e;
}
}
}
}
function loadCreateTableSql($db, $dataFile, $logger)
{
$data = file_get_contents($dataFile);
// Split on semicolons but keep them in the statements
$statements = preg_split('/(?<=[;])/', $data, -1, PREG_SPLIT_NO_EMPTY);
foreach ($statements as $statement) {
$statement = trim($statement);
if (!empty($statement)) {
// Execute CREATE TABLE and CREATE INDEX statements
if (preg_match('/^\s*CREATE\s+(?:TABLE|INDEX)/i', $statement)) {
try {
$db->execute($statement);
} catch (Exception $e) {
echo "Error executing schema statement: " . $statement . "\n";
$logger->logError("Error executing schema statement: " . $statement, $e);
throw $e;
}
}
}
}
}
function loadNonCreateSql($db, $dataFile, $logger)
{
$data = file_get_contents($dataFile);
// Split on semicolons but keep them in the statements
$statements = preg_split('/(?<=[;])/', $data, -1, PREG_SPLIT_NO_EMPTY);
foreach ($statements as $statement) {
$statement = trim($statement);
if (!empty($statement)) {
// Execute non-CREATE statements
if (!preg_match('/^\s*CREATE\s+(?:TABLE|INDEX)/i', $statement)) {
try {
$db->execute($statement);
} catch (Exception $e) {
echo "Error executing data statement: " . $statement . "\n";
$logger->logError("Error executing data statement: " . $statement, $e);
throw $e;
}
}
}
}
}
try {
// Ensure DB_PATH is set
if (!getenv('DB_PATH')) {
throw new Exception('DB_PATH environment variable is not set');
}
$dbPath = getenv('DB_PATH');
echo "Using database path: $dbPath\n";
$logger->log("Using database path: $dbPath");
// Ensure database directory exists
$dbDir = dirname($dbPath);
if (!is_dir($dbDir)) {
echo "Creating database directory: $dbDir\n";
$logger->log("Creating database directory: $dbDir");
mkdir($dbDir, 0777, true);
}
// Get database instance with explicit path
$db = Database::getInstance($dbPath);
// Drop existing tables in reverse order to handle foreign key constraints
echo "Dropping existing tables...\n";
$logger->log("Dropping existing tables...");
$tables = [
'certification_instructors',
'certification_details',
'team_members',
'meta',
'menu_categories',
'menu_items',
'config',
'sections',
'pages',
'sites'
];
foreach ($tables as $table) {
$db->execute("DROP TABLE IF EXISTS $table");
}
// First, create all tables from main schema file
$schemaFile = __DIR__ . '/schema.sql';
echo "Loading main schema from: $schemaFile\n";
$logger->logSqlFile($schemaFile, 'Loading main schema');
loadSql($db, $schemaFile, $logger);
// Load and execute all section schemas
echo "Loading Site schemas...\n";
$logger->log("Loading Site schemas...");
$sectionSchemas = glob(__DIR__ . '/www/*/schema.sql');
// First pass: Create all tables
foreach ($sectionSchemas as $schemaFile) {
//echo "Creating section schema: " . basename(dirname($schemaFile)) . "\n";
echo "Creating section schema: " . $schemaFile . "\n";
$logger->logSqlFile($schemaFile, 'Creating tables from section schema');
loadSql($db, $schemaFile, $logger);
}
// Verify site was created
$sites = $db->query("SELECT * FROM sites");
echo "Sites in database: " . count($sites) . "\n";
foreach ($sites as $site) {
echo "Site: {$site['name']} ({$site['domain']})\n";
}
// Verify pages were created
$pages = $db->query("SELECT p.*, s.domain FROM pages p JOIN sites s ON p.site_id = s.id");
echo "Pages in database: " . count($pages) . "\n";
foreach ($pages as $page) {
echo "Page: {$page['title']} (/{$page['slug']}) - {$page['status']}\n";
}
// Load and execute all section schemas
echo "Loading section schemas...\n";
$logger->log("Loading section schemas...");
$sectionSchemas = glob(__DIR__ . '/sections/*/schema.sql');
// First pass: Create all tables
foreach ($sectionSchemas as $schemaFile) {
//echo "Creating section schema: " . basename(dirname($schemaFile)) . "\n";
echo "Creating section schema: " . $schemaFile . "\n";
$logger->logSqlFile($schemaFile, 'Creating tables from section schema');
loadCreateTableSql($db, $schemaFile, $logger);
}
// Second pass: Execute all other statements from schemas
foreach ($sectionSchemas as $schemaFile) {
//echo "Loading section schema data: " . basename(dirname($schemaFile)) . "\n";
echo "Loading section data: " . $schemaFile . "\n";
$logger->logSqlFile($schemaFile, 'Loading section schema data');
$schema = file_get_contents($schemaFile);
loadNonCreateSql($db, $schemaFile, $logger);
}
// Load data from www/{domain}/sections/{section}/data.sql structure
echo "Loading website data...\n";
$logger->log("Loading website data...");
$wwwPath = __DIR__ . '/www';
// Get all domains
$domains = glob($wwwPath . '/*', GLOB_ONLYDIR);
foreach ($domains as $domainPath) {
$domain = basename($domainPath);
echo "Processing domain: $domain\n";
$logger->log("Processing domain: $domain");
// Get all section data files
$sectionDataFiles = glob($domainPath . '/sections/*/data.sql');
if (empty($sectionDataFiles)) {
echo "No page data found in directory: " . $domainPath . '/sections/*/data.sql' . "\n";
} else {
foreach ($sectionDataFiles as $dataFile) {
$section = $dataFile;// basename(dirname($dataFile));
echo "Loading data: $section\n";
$logger->logSqlFile($dataFile, "Loading data for domain '$domain' section '$section'");
loadSql($db, $dataFile, $logger);
}
}
// Load page data
$pageFiles = glob($domainPath . '/pages/*.sql');
if (empty($pageFiles)) {
echo "No page data found in directory: " . $domainPath . '/pages/*.sql' . "\n";
} else {
foreach ($pageFiles as $dataFile) {
// echo "Loading page data from: " . basename($pageFile) . "\n";
// $section = basename(dirname($dataFile));
$section = $dataFile;
echo "Loading data: $section\n";
$logger->logSqlFile($dataFile, "Loading data for domain '$domain' section '$section'");
loadSql($db, $dataFile, $logger);
}
}
}
echo "Database installation completed successfully at: " . $dbPath . "\n";
$logger->log("Database installation completed successfully");
} catch (Exception $e) {
echo "Installation failed: " . $e->getMessage() . "\n";
$logger->logError("Installation failed", $e);
}