4.php

main 133 lines · 3.8 KB Raw
Alex Alex Commit Initial commit 01/10/2026 09:20
1<?php
2/**
3 * Step 4 - import the ZnoteX schema.
4 *
5 * Only the znote_* tables. Every statement in znote_schema.sql is
6 * CREATE TABLE IF NOT EXISTS, so running this against a database that already
7 * has some of them adds the missing ones and leaves the rest alone.
8 */
9
10if (!defined('ZNOTE_INSTALL')) { http_response_code(403); die('Direct access denied.'); }
11
12$schemaFile = install_root() . '/SQL/znote_schema.sql';
13$report = null;
14
15if ($_SERVER['REQUEST_METHOD'] === 'POST') {
16
17 $link = install_connect($connectError);
18
19 if ($link === null) {
20 install_error('Lost the database connection: ' . ih((string)$connectError));
21 } elseif (!is_file($schemaFile)) {
22 install_error('SQL/znote_schema.sql is missing.');
23 } else {
24
25 $sql = (string)file_get_contents($schemaFile);
26 $sql = str_replace(array("\r\n", "\r"), "\n", $sql);
27
28 // Strip line comments, then split on ";" at end of line. The file is
29 // generated by us and has no procedures or triggers, so this is safe -
30 // a general-purpose SQL splitter would be overkill.
31 $sql = preg_replace('/^--.*$/m', '', $sql);
32
33 $created = 0;
34 $skipped = 0;
35 $errors = array();
36
37 foreach (array_filter(array_map('trim', explode(";\n", $sql))) as $statement) {
38 $statement = trim($statement, "; \t\n\r");
39 if ($statement === '') {
40 continue;
41 }
42
43 // Seed rows must not be duplicated on a re-run.
44 if (stripos($statement, 'INSERT INTO') === 0) {
45 if (preg_match('/INSERT INTO `([a-z_]+)`/i', $statement, $m)) {
46 $check = @$link->query('SELECT 1 FROM `' . $m[1] . '` LIMIT 1');
47 if ($check !== false && $check->num_rows > 0) {
48 $skipped++;
49 continue;
50 }
51 }
52 }
53
54 if (@$link->query($statement)) {
55 $created++;
56 } else {
57 $errors[] = $link->error . "\n in: "
58 . substr((string)preg_replace('/\s+/', ' ', $statement), 0, 90);
59 }
60 }
61
62 // Record the version, which the schema's own INSERT may have skipped.
63 @$link->query("UPDATE `znote` SET `version` = '2.0.1'");
64
65 $tablesNow = 0;
66 $result = @$link->query("SHOW TABLES LIKE 'znote%'");
67 if ($result !== false) {
68 $tablesNow = $result->num_rows;
69 }
70
71 $link->close();
72
73 $report = array(
74 'created' => $created,
75 'skipped' => $skipped,
76 'errors' => $errors,
77 'tables' => $tablesNow,
78 );
79
80 if (!$errors) {
81 install_state(array('schema_done' => true));
82 install_max_step(5);
83 } else {
84 install_error(count($errors) . ' statement(s) failed. The first was: '
85 . ih((string)$errors[0]));
86 }
87 }
88}
89?>
90<h1>Schema</h1>
91<p class="lead">
92 Creates the <code>znote_*</code> tables in
93 <code><?= ih(install_get('sqlDatabase')) ?></code>. Your server's own tables are not touched.
94</p>
95
96<?php if ($report === null): ?>
97
98 <div class="info">
99 Every statement is <code>CREATE TABLE IF NOT EXISTS</code>, and seed rows are skipped for
100 tables that already hold data &mdash; running this on a database that already has some
101 ZnoteX tables adds only what is missing.
102 </div>
103
104 <form method="post">
105 <div class="actions">
106 <button class="btn" type="submit">Import the schema</button>
107 <a class="btn ghost" href="<?= install_url(3) ?>">Back</a>
108 </div>
109 </form>
110
111<?php else: ?>
112
113 <?php if (!$report['errors']): ?>
114 <p class="good">
115 Done. <?= (int)$report['created'] ?> statement<?= $report['created'] === 1 ? '' : 's' ?> ran,
116 <?= (int)$report['skipped'] ?> skipped as already populated.
117 <?= (int)$report['tables'] ?> <code>znote_*</code> tables are now present.
118 </p>
119 <?php else: ?>
120 <p class="bad"><?= count($report['errors']) ?> statement(s) failed:</p>
121 <pre><?php foreach (array_slice($report['errors'], 0, 6) as $e) { echo ih($e), "\n"; } ?></pre>
122 <?php endif; ?>
123
124 <div class="actions">
125 <?php if (!$report['errors']): ?>
126 <a class="btn" href="<?= install_url(5) ?>">Continue</a>
127 <?php else: ?>
128 <a class="btn ghost" href="<?= install_url(4) ?>">Try again</a>
129 <?php endif; ?>
130 </div>
131
132<?php endif; ?>
133
Top