This repository was archived by the owner on Nov 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.php
More file actions
358 lines (268 loc) · 6.62 KB
/
database.php
File metadata and controls
358 lines (268 loc) · 6.62 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
<?php
/**
* TODO:
*
* - Database locks. These are important!
*/
if (!defined("APP_LOADED")) {
die();
}
$gDatabasePath = "../../$gAppName/db/";
class RDBObject {
/**
* RevisionDB object container
*/
public $revisions;
public $rdb_;
function __construct(object $source) {
// Check if RevisionDB is already supported. If not enable it.
if (property_exists($source, "rdb_")) {
$this->revisions = $source->revisions;
$this->rdb_ = 1;
}
else {
$this->revisions = array($source);
$this->rdb_ = 1;
}
}
function at(int $index = -1) {
/**
* Return the revision at the given index. If index is -1, then the newest
* revision is returned.
*/
if ($index == -1) {
$index = sizeof($this->revisions) - 1;
}
return $this->revisions[$index];
}
function top() {
/**
* Return the top (current) revision
*/
return $this->at();
}
function add($item) {
/**
* Add a new revision.
*/
$this->revisions[] = $item;
}
function revert(int $index) {
/**
* Revert to the revision at index.
*/
$this->add($this->at($index));
}
function size() {
/**
* Return the number of revisions.
*/
return sizeof($this->revisions);
}
}
class Database {
/**
* "Connection" to the database.
*/
public $path;
function __construct(string $name) {
global $gDatabasePath;
$this->path = $gDatabasePath . $name . "/";
// Make database folder if it doesn't exist
if (!file_exists($this->path)) {
mkdir($this->path, 0777, true);
}
// Force RevisionDB
}
function get_item_path(string $item) : string {
/**
* Get the path to the file in the database.
*/
return $this->path . str_replace("/", ".", $item);
}
function load(string $item) : object | array | null {
/**
* Load a database object
*/
$path = $this->get_item_path($item);
$file = fopen($path, "r");
// Need to clear cache to make sure things work; otherwise there are bugs.
clearstatcache();
$json_data = fread($file, filesize($path));
$data = json_decode($json_data);
fclose($file);
return $data;
}
function save(string $item, $data) : void {
/**
* Save a database object
*/
$file = fopen($this->get_item_path($item), "w");
fwrite($file, json_encode($data));
fclose($file);
}
function delete(string $item) : void {
/**
* Remove a database object
*/
unlink($this->get_item_path($item));
}
function has(string $item) : bool {
$path = $this->get_item_path($item);
return file_exists($path) && !is_dir($path);
}
function enumerate() : array {
$array = scandir($this->get_item_path(""));
array_shift($array);
array_shift($array);
return $array;
}
function where_one(array $query) : ?string {
/**
* Return the first entry that has matched all key => value pairs in
* $query
*
* TODO: Switch to a real document database so it actually preforms okay
*/
$items = $this->enumerate();
foreach ($items as $item) {
$obj = $this->load($item);
$okay = true;
foreach ($query as $key => $value) {
if (!property_exists($obj, $key) || $obj->$key != $value) {
$okay = false;
}
}
if ($okay) {
return $item;
}
}
return null;
}
}
class RevisionDB {
/**
* A database which supports revisioing. It can be used as a drop in replacement
* for the normal database.
*/
public $db;
function __construct(string $name) {
$this->db = new Database($name);
}
function load(string $item, int $index = -1) {
/**
* Load a revision (or the latest if not specified)
*/
$data = $this->db->load($item);
// RDBObject's constructor will take care of the case that this isn't
// already an RDBObject for us :)
$obj = new RDBObject($data);
return $obj->at($index);
}
function save(string $item, $newdata) : void {
/**
* Save a new revision
*/
// Load current version info
$data = null;
if ($this->db->has($item)) {
$data = $this->db->load($item);
}
else {
$data = new stdClass();
$data->rdb_ = 1;
$data->revisions = array();
}
// Constuct the RDB Object
// We can do this transparently since RDBObject will auto-create itself
// if this isn't already an RDBObject
$obj = new RDBObject($data);
// Add the new revision
$obj->add($newdata);
// Save the new RDBObject
$this->db->save($item, $obj);
}
function delete(string $item) : void {
$this->db->delete($item);
}
function has(string $item) : bool {
return $this->db->has($item);
}
function enumerate() : array {
return $this->db->enumerate();
}
function history(string $item) : array {
/**
* Return the revision history for a given item as
*
* array(
* object(
* author: string,
* updated: int,
* reason: string
* ),
* ...
* )
*/
$data = $this->db->load($item);
// Array to hold history info
$history = array();
$obj = new RDBObject($data);
for ($i = 0; $i < $obj->size(); $i++) {
$revision = $obj->at($i);
// Check if there is an author feild, if so use it instead
$author = "Unknown author";
if (property_exists($revision, "author")) {
$author = $revision->author;
}
// Check if there is an updated feild, and use if so
$updated = 0;
if (property_exists($revision, "updated")) {
$updated = $revision->updated;
}
// Check if there is a reason feild, and use if so
$reason = "No reason given";
if (property_exists($revision, "reason")) {
$reason = $revision->reason;
}
// Create the info object
$info = new stdClass();
$info->author = $author;
$info->updated = $updated;
$info->reason = $reason;
$history[] = $info;
}
return $history;
}
}
function enumerate_stores() {
/**
* Enumerate available database stores
*/
global $gDatabasePath;
$array = scandir($gDatabasePath);
array_shift($array);
array_shift($array);
return $array;
}
function backup_database() : string {
/**
* Back up the databse to a zip file
*/
global $gDatabasePath;
$zip = new ZipArchive();
$filename = "../../$gAppName/store/backup_" . date("Y-m-d_His", time()) . ".zip";
$zip->open($filename, ZIPARCHIVE::CREATE);
$zip->addEmptyDir("db");
$stores = enumerate_stores();
for ($j = 0; $j < sizeof($stores); $j++) {
$db = new Database($stores[$j]);
$files = $db->enumerate();
$zip->addEmptyDir("db/" . $stores[$j]);
for ($i = 0; $i < sizeof($files); $i++) {
$zip->addFile($gDatabasePath . "/" . $stores[$j] . "/" . $files[$i], "db/" . $stores[$j] . "/" . $files[$i]);
}
}
$zip->close();
return $filename;
}