/
SpotStruct_abs.php
executable file
·630 lines (528 loc) · 31.6 KB
/
SpotStruct_abs.php
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
<?php
abstract class SpotStruct_abs {
protected $_spotdb;
protected $_dbcon;
public function __construct(SpotDb $spotdb) {
$this->_spotdb = $spotdb;
$this->_dbcon = $spotdb->getDbHandle();
} # __construct
/*
* optimaliseer/analyseer een aantal tables welke veel veranderen,
* deze functie wijzigt geen data!
*/
abstract function analyze();
/* converteert een "spotweb" datatype naar een mysql datatype */
abstract function swDtToNative($colType);
/* converteert een mysql datatype naar een "spotweb" datatype */
abstract function nativeDtToSw($colInfo);
/*
* Add an index, kijkt eerst wel of deze index al bestaat,
* $idxType kan danwel 'UNIQUE' danwel 'FULLTEXT' zijn
*/
abstract function addIndex($idxname, $idxType, $tablename, $colList);
/* dropt een index als deze bestaat */
abstract function dropIndex($idxname, $tablename);
/* voegt een column toe, kijkt wel eerst of deze nog niet bestaat */
abstract function addColumn($colName, $tablename, $colType, $colDefault, $notNull, $collation);
/* wijzigt een column - controleert *niet* of deze voldoet aan het prototype */
abstract function modifyColumn($colName, $tablename, $colType, $colDefault, $notNull, $collation, $what);
/* dropt een kolom (mits db dit ondersteunt) */
abstract function dropColumn($colName, $tablename);
/* controleert of een index bestaat */
abstract function indexExists($idxname, $tablename);
/* controleert of een kolom bestaat */
abstract function columnExists($tablename, $colname);
/* controleert of een tabel bestaat */
abstract function tableExists($tablename);
/* controleert of een full text index bestaat */
abstract function ftsExists($ftsname, $tablename, $colList);
/* maakt een full text index aan */
abstract function createFts($ftsname, $tablename, $colList);
/* dropt en fulltext index */
abstract function dropFts($ftsname, $tablename, $colList);
/* geeft FTS info terug */
abstract function getFtsInfo($ftsname, $tablename, $colList);
/* ceeert een lege tabel met enkel een ID veld, collation kan UTF8 of ASCII zijn */
abstract function createTable($tablename, $collation);
/* creeert een foreign key constraint */
abstract function addForeignKey($tablename, $colname, $reftable, $refcolumn, $action);
/* dropped een foreign key constraint */
abstract function dropForeignKey($tablename, $colname, $reftable, $refcolumn, $action);
/* verandert een storage engine (concept dat enkel mysql kent :P ) */
abstract function alterStorageEngine($tablename, $engine);
/* drop een table */
abstract function dropTable($tablename);
/* rename een table */
abstract function renameTable($tablename, $newTableName);
/* Geeft, in een afgesproken formaat, de index informatie terug */
abstract function getIndexInfo($idxname, $tablename);
/* Geeft, in een afgesproken formaat, de index formatie terug */
abstract function getColumnInfo($tablename, $colname);
/* controleert of de index structuur hetzelfde is als de gewenste, zo niet, maak hem opnieuw aan */
function validateIndex($idxname, $type, $tablename, $colList) {
echo "\tValidating index " . $idxname . PHP_EOL;
if (!$this->compareIndex($idxname, $type, $tablename, $colList)) {
# Drop de index
if ($this->indexExists($idxname, $tablename)) {
echo "\t\tDropping index " . $idxname . PHP_EOL;
$this->dropIndex($idxname, $tablename);
} # if
echo "\t\tAdding index " . $idxname . PHP_EOL;
# en creeer hem opnieuw
$this->addIndex($idxname, $type, $tablename, $colList);
} # if
} # validateIndex
/* controleert of de fulltext structuur hetzelfde is als de gewenste, zo niet, maak hem opnieuw aan */
function validateFts($ftsname, $tablename, $colList) {
echo "\tValidating FTS " . $ftsname . PHP_EOL;
if (!$this->compareFts($ftsname, $tablename, $colList)) {
# Drop de FTS
if ($this->ftsExists($ftsname, $tablename, $colList)) {
echo "\t\tDropping FTS " . $ftsname . PHP_EOL;
$this->dropFts($ftsname, $tablename, $colList);
} # if
echo "\t\tAdding FTS " . $ftsname . PHP_EOL;
# en creeer hem opnieuw
$this->createFts($ftsname, $tablename, $colList);
} # if
} # validateFts
/* controleert of de index structuur hetzelfde is als de gewenste, zo niet, maak hem opnieuw aan */
function validateColumn($colName, $tablename, $colType, $colDefault, $notNull, $collation) {
echo "\tValidating " . $tablename . "(" . $colName . ")" . PHP_EOL;
$compResult = $this->compareColumn($colName, $tablename, $colType, $colDefault, $notNull, $collation);
if ($compResult !== true) {
if ($this->columnExists($tablename, $colName)) {
echo "\t\tModifying column " . $colName . " (" . $compResult . ") on " . $tablename . PHP_EOL;
$this->modifyColumn($colName, $tablename, $colType, $colDefault, $notNull, $collation, $compResult);
} else {
echo "\t\tAdding column " . $colName . "(" . $colType . ") to " . $tablename . PHP_EOL;
$this->addColumn($colName, $tablename, $colType, $colDefault, $notNull, $collation);
} # else
} # if
} # validateColumn
/* vergelijkt een column met de gewenste structuur */
function compareColumn($colName, $tablename, $colType, $colDefault, $notNull, $collation) {
# Vraag nu de column informatie op
$q = $this->getColumnInfo($tablename, $colName);
# Als de column helemaal niet gevonden wordt..
if (empty($q)) {
return false;
} # if
# controleer het type
if (strtolower($q['COLUMN_TYPE']) != strtolower($this->swDtToNative($colType))) {
#var_dump($q);
#var_dump($colType);
#var_dump($this->swDtToNative($colType));
#die();
return 'type';
} # if
# controleer default
if (strtolower($q['COLUMN_DEFAULT']) != strtolower($colDefault)) {
return 'default';
} # if
# controleer NOT NULL setting
if (strtolower($q['NOTNULL']) != $notNull) {
return 'not null';
} # if
# controleer NOT NULL setting
if ((strtolower($q['CHARACTER_SET_NAME']) != $collation) && ($q['CHARACTER_SET_NAME'] != null)) {
return 'charset';
} # if
return true;
} # compareColumn
/* vergelijkt een index met de gewenste structuur */
function compareIndex($idxname, $type, $tablename, $colList) {
# Vraag nu de index informatie op
$q = $this->getIndexInfo($idxname, $tablename);
# Als het aantal kolommen niet gelijk is
if (count($q) != count($colList)) {
return false;
} # if
# we loopen vervolgens door elke index kolom heen, en vergelijken
# dan of ze in dezelfde volgorde staan en dezelfde eigenschappen hebben
for($i = 0; $i < count($colList); $i++) {
$same = true;
if ($colList[$i] != $q[$i]['column_name']) {
$same = false;
} # if
if ($same) {
switch(strtolower($type)) {
case 'fulltext' : $same = (strtolower($q[$i]['index_type']) == 'fulltext'); break;
case 'unique' : $same = ($q[$i]['non_unique'] == 0); break;
case '' : $same = (strtolower($q[$i]['index_type']) != 'fulltext') && ($q[$i]['non_unique'] == 1);
} # switch
} # if
if (!$same) {
#var_dump($q[$i]);
#var_dump($type);
#var_dump($colList);
#die();
return false;
} # if
} # for
return true;
} # compareIndex
/* vergelijkt een FTS met de gewenste structuur */
function compareFts($ftsname, $tablename, $colList) {
# Vraag nu de FTS informatie op
$q = $this->getFtsInfo($ftsname, $tablename, $colList);
# Als het aantal kolommen niet gelijk is
if (count($q) != count($colList)) {
return false;
} # if
# we loopen vervolgens door elke index kolom heen, en vergelijken
# dan of ze in dezelfde volgorde staan en dezelfde eigenschappen hebben
for($i = 0; $i < count($colList); $i++) {
if ($colList[$i + 1] != $q[$i]['column_name']) {
return false;
} # if
} # for
return true;
} # compareFts
function updateSchema() {
# drop eventueel FTS indexes op de spotsfull tabel
$this->dropIndex("idx_spotsfull_fts_1", "spotsfull");
$this->dropIndex("idx_spotsfull_fts_2", "spotsfull");
$this->dropIndex("idx_spotsfull_fts_3", "spotsfull");
# relaties wissen
$this->dropForeignKey('spotsfull', 'messageid', 'spots', 'messageid', 'ON DELETE CASCADE ON UPDATE CASCADE');
$this->dropForeignKey('spotstatelist', 'messageid', 'spots', 'messageid', 'ON DELETE CASCADE ON UPDATE CASCADE');
$this->dropForeignKey('commentsposted', 'inreplyto', 'spots', 'messageid', 'ON DELETE CASCADE ON UPDATE CASCADE');
$this->dropForeignKey('commentsposted', 'messageid', 'spots', 'messageid', 'ON DELETE CASCADE ON UPDATE CASCADE');
$this->dropForeignKey('commentsxover', 'messageid', 'spots', 'messageid', 'ON DELETE CASCADE ON UPDATE CASCADE');
$this->dropForeignKey('commentsfull', 'messageid', 'spots', 'messageid', 'ON DELETE CASCADE ON UPDATE CASCADE');
$this->dropForeignKey('reportsposted', 'inreplyto', 'spots', 'messageid', 'ON DELETE CASCADE ON UPDATE CASCADE');
$this->dropForeignKey('reportsposted', 'messageid', 'spots', 'messageid', 'ON DELETE CASCADE ON UPDATE CASCADE');
##############################################################################################
# Opschonen data #############################################################################
##############################################################################################
if (($this instanceof SpotStruct_mysql) && (false)) {
echo "Cleaning up old data..." . PHP_EOL;
if ($this->tableExists('usersettings') && $this->tableExists('users')) {
$this->_dbcon->rawExec("DELETE usersettings FROM usersettings LEFT JOIN users ON usersettings.userid=users.id WHERE users.id IS NULL");
} # if
if ($this->tableExists('sessions') && $this->tableExists('users')) {
$this->_dbcon->rawExec("DELETE sessions FROM sessions LEFT JOIN users ON sessions.userid=users.id WHERE users.id IS NULL");
} # if
if ($this->tableExists('spotstatelist') && $this->tableExists('users')) {
$this->_dbcon->rawExec("DELETE spotstatelist FROM spotstatelist LEFT JOIN users ON spotstatelist.ouruserid=users.id WHERE users.id IS NULL");
} # if
if ($this->tableExists('usergroups') && $this->tableExists('users')) {
$this->_dbcon->rawExec("DELETE usergroups FROM usergroups LEFT JOIN users ON usergroups.userid=users.id WHERE users.id IS NULL");
} # if
if ($this->tableExists('usergroups') && $this->tableExists('securitygroups')) {
$this->_dbcon->rawExec("DELETE usergroups FROM usergroups LEFT JOIN securitygroups ON usergroups.groupid=securitygroups.id WHERE securitygroups.id IS NULL");
} # if
if ($this->tableExists('grouppermissions') && $this->tableExists('securitygroups')) {
$this->_dbcon->rawExec("DELETE grouppermissions FROM grouppermissions LEFT JOIN securitygroups ON grouppermissions.groupid=securitygroups.id WHERE securitygroups.id IS NULL");
} # if
if ($this->tableExists('commentsposted') && $this->tableExists('users')) {
$this->_dbcon->rawExec("DELETE commentsposted FROM commentsposted LEFT JOIN users ON commentsposted.ouruserid=users.id WHERE users.id IS NULL");
} # if
if ($this->tableExists('commentsposted') && $this->tableExists('spots')) {
$this->_dbcon->rawExec("DELETE commentsposted FROM commentsposted LEFT JOIN spots ON commentsposted.inreplyto=spots.messageid WHERE spots.messageid IS NULL");
} # if
if ($this->tableExists('commentsfull') && $this->tableExists('commentsxover')) {
$this->_dbcon->rawExec("DELETE commentsfull FROM commentsfull LEFT JOIN commentsxover ON commentsfull.messageid=commentsxover.messageid WHERE commentsxover.messageid IS NULL");
} # if
if ($this->tableExists('spotsfull') && $this->tableExists('spots')) {
$this->_dbcon->rawExec("DELETE spotsfull FROM spotsfull LEFT JOIN spots ON spotsfull.messageid=spots.messageid WHERE spots.messageid IS NULL");
} # if
if ($this->tableExists('spotstatelist') && $this->tableExists('spots')) {
$this->_dbcon->rawExec("DELETE spotstatelist FROM spotstatelist LEFT JOIN spots ON spotstatelist.messageid=spots.messageid WHERE spots.messageid IS NULL");
} # if
if ($this->tableExists('reportsposted') && $this->tableExists('users')) {
$this->_dbcon->rawExec("DELETE reportsposted FROM reportsposted LEFT JOIN users ON reportsposted.ouruserid=users.id WHERE users.id IS NULL");
} # if
if ($this->tableExists('reportsposted') && $this->tableExists('spots')) {
$this->_dbcon->rawExec("DELETE reportsposted FROM reportsposted LEFT JOIN spots ON reportsposted.inreplyto=spots.messageid WHERE spots.messageid IS NULL");
} # if
} # if
# ---- spots table ---- #
$this->createTable('spots', "utf8");
$this->validateColumn('messageid', 'spots', 'VARCHAR(128)', "''", true, 'ascii');
$this->validateColumn('poster', 'spots', 'VARCHAR(128)', NULL, false, 'utf8');
$this->validateColumn('title', 'spots', 'VARCHAR(128)', NULL, false, 'utf8');
$this->validateColumn('tag', 'spots', 'VARCHAR(128)', NULL, false, 'utf8');
$this->validateColumn('category', 'spots', 'INTEGER', NULL, false, '');
$this->validateColumn('subcata', 'spots', 'VARCHAR(64)', NULL, false, 'utf8'); # FIXME: charset kan ook ascii worden
$this->validateColumn('subcatb', 'spots', 'VARCHAR(64)', NULL, false, 'utf8'); # FIXME: charset kan ook ascii worden
$this->validateColumn('subcatc', 'spots', 'VARCHAR(64)', NULL, false, 'utf8'); # FIXME: charset kan ook ascii worden
$this->validateColumn('subcatd', 'spots', 'VARCHAR(64)', NULL, false, 'utf8'); # FIXME: charset kan ook ascii worden
$this->validateColumn('subcatz', 'spots', 'VARCHAR(64)', NULL, false, 'utf8'); # FIXME: charset kan ook ascii worden
$this->validateColumn('stamp', 'spots', 'UNSIGNED INTEGER', NULL, false, '');
$this->validateColumn('reversestamp', 'spots', 'INTEGER', "0", false, '');
$this->validateColumn('filesize', 'spots', 'UNSIGNED BIGINTEGER', "0", true, '');
$this->validateColumn('moderated', 'spots', 'BOOLEAN', NULL, false, '');
$this->validateColumn('commentcount', 'spots', 'INTEGER', "0", false, '');
$this->validateColumn('spotrating', 'spots', 'INTEGER', "0", false, '');
$this->validateColumn('reportcount', 'spots', 'INTEGER', "0", false, '');
$this->alterStorageEngine("spots", "MyISAM");
# ---- spotsfull table ---- #
$this->createTable('spotsfull', "utf8");
$this->validateColumn('messageid', 'spotsfull', 'VARCHAR(128)', "''", true, 'ascii');
$this->validateColumn('userid', 'spotsfull', 'VARCHAR(32)', NULL, false, 'utf8'); # FIXME: charset kan ook ascii worden
$this->validateColumn('verified', 'spotsfull', 'BOOLEAN', NULL, false, '');
$this->validateColumn('usersignature', 'spotsfull', 'VARCHAR(255)', NULL, false, 'ascii');
$this->validateColumn('userkey', 'spotsfull', 'VARCHAR(512)', NULL, false, 'ascii');
$this->validateColumn('xmlsignature', 'spotsfull', 'VARCHAR(255)', NULL, false, 'ascii');
$this->validateColumn('fullxml', 'spotsfull', 'TEXT', NULL, false, 'utf8');
$this->alterStorageEngine("spotsfull", "InnoDB");
# ---- nntp table ---- #
$this->createTable('nntp', "utf8");
$this->validateColumn('server', 'nntp', 'VARCHAR(128)', "''", true, 'utf8'); # FIXME: charset kan ook ascii worden
$this->validateColumn('maxarticleid', 'nntp', 'INTEGER', NULL, false, '');
$this->validateColumn('nowrunning', 'nntp', 'INTEGER', "0", false, '');
$this->validateColumn('lastrun', 'nntp', 'INTEGER', "0", false, '');
$this->validateColumn('serverdatelastrun', 'nntp', 'VARCHAR(14)', "00000000000000", false, 'ascii');
$this->alterStorageEngine("nntp", "InnoDB");
# ---- commentsxover table ---- #
$this->createTable('commentsxover', "ascii");
$this->validateColumn('messageid', 'commentsxover', 'VARCHAR(128)', "''", true, 'ascii');
$this->validateColumn('nntpref', 'commentsxover', 'VARCHAR(128)', "''", true, 'ascii');
$this->validateColumn('spotrating', 'commentsxover', 'INTEGER', "0", false, '');
$this->alterStorageEngine("commentsxover", "InnoDB");
# ---- reportsxover table ---- #
$this->createTable('reportsxover', "ascii");
$this->validateColumn('messageid', 'reportsxover', 'VARCHAR(128)', "''", true, 'ascii');
$this->validateColumn('fromhdr', 'reportsxover', 'VARCHAR(128)', "''", true, 'ascii');
$this->validateColumn('keyword', 'reportsxover', 'VARCHAR(128)', "''", true, 'ascii');
$this->validateColumn('nntpref', 'reportsxover', 'VARCHAR(128)', "''", true, 'ascii');
$this->alterStorageEngine("reportsxover", "InnoDB");
# ---- spotstatelist table ---- #
$this->createTable('spotstatelist', "ascii");
$this->validateColumn('messageid', 'spotstatelist', 'VARCHAR(128)', "''", true, 'ascii');
$this->validateColumn('ouruserid', 'spotstatelist', 'INTEGER', "0", false, '');
$this->validateColumn('download', 'spotstatelist', 'INTEGER', NULL, false, '');
$this->validateColumn('watch', 'spotstatelist', 'INTEGER', NULL, false, '');
$this->validateColumn('seen', 'spotstatelist', 'INTEGER', NULL, false, '');
$this->alterStorageEngine("spotstatelist", "InnoDB");
# ---- commentsfull table ---- #
$this->createTable('commentsfull', "ascii");
$this->validateColumn('messageid', 'commentsfull', 'VARCHAR(128)', "''", true, 'ascii');
$this->validateColumn('fromhdr', 'commentsfull', 'VARCHAR(128)', NULL, false, 'utf8');
$this->validateColumn('stamp', 'commentsfull', 'INTEGER', NULL, false, '');
$this->validateColumn('usersignature', 'commentsfull', 'VARCHAR(255)', NULL, false, 'ascii');
$this->validateColumn('userkey', 'commentsfull', 'VARCHAR(512)', NULL, false, 'ascii');
$this->validateColumn('userid', 'commentsfull', 'VARCHAR(32)', NULL, false, 'ascii');
$this->validateColumn('hashcash', 'commentsfull', 'VARCHAR(255)', NULL, false, 'ascii');
$this->validateColumn('body', 'commentsfull', 'TEXT', NULL, false, 'utf8');
$this->validateColumn('verified', 'commentsfull', 'BOOLEAN', NULL, false, '');
$this->alterStorageEngine("commentsfull", "InnoDB");
# ---- settings table ---- #
$this->createTable('settings', "ascii");
$this->validateColumn('name', 'settings', 'VARCHAR(128)', "''", true, 'ascii');
$this->validateColumn('value', 'settings', 'TEXT', NULL, false, 'utf8');
$this->validateColumn('serialized', 'settings', 'boolean', NULL, false, '');
$this->alterStorageEngine("settings", "InnoDB");
# ---- commentsposted table ---- #
$this->createTable('commentsposted', "ascii");
$this->validateColumn('ouruserid', 'commentsposted', 'INTEGER', "0", true, '');
$this->validateColumn('messageid', 'commentsposted', 'VARCHAR(128)', "''", true, 'ascii');
$this->validateColumn('inreplyto', 'commentsposted', 'VARCHAR(128)', "''", true, 'ascii');
$this->validateColumn('randompart', 'commentsposted', 'VARCHAR(32)', "''", true, 'ascii');
$this->validateColumn('rating', 'commentsposted', 'INTEGER', 0, true, '');
$this->validateColumn('body', 'commentsposted', 'TEXT', NULL, false, 'utf8');
$this->validateColumn('stamp', 'commentsposted', 'INTEGER', "0", true, '');
$this->alterStorageEngine("commentsposted", "InnoDB");
# ---- reportsposted table ---- #
$this->createTable('reportsposted', "ascii");
$this->validateColumn('ouruserid', 'reportsposted', 'INTEGER', "0", true, '');
$this->validateColumn('messageid', 'reportsposted', 'VARCHAR(128)', "''", true, 'ascii');
$this->validateColumn('inreplyto', 'reportsposted', 'VARCHAR(128)', "''", true, 'ascii');
$this->validateColumn('randompart', 'reportsposted', 'VARCHAR(32)', "''", true, 'ascii');
$this->validateColumn('body', 'reportsposted', 'TEXT', NULL, false, 'utf8');
$this->validateColumn('stamp', 'reportsposted', 'INTEGER', "0", true, '');
$this->alterStorageEngine("reportsposted", "InnoDB");
# ---- usersettings table ---- #
$this->createTable('usersettings', "utf8");
$this->validateColumn('userid', 'usersettings', 'INTEGER', '0', true, '');
$this->validateColumn('privatekey', 'usersettings', "TEXT", NULL, false, 'ascii');
$this->validateColumn('publickey', 'usersettings', "TEXT", NULL, false, 'ascii');
$this->validateColumn('otherprefs', 'usersettings', "TEXT", NULL, false, 'utf8');
$this->alterStorageEngine("usersettings", "InnoDB");
# ---- users table ---- #
$this->createTable('users', "utf8");
$this->validateColumn('username', 'users', "VARCHAR(128)", "''", true, 'utf8');
$this->validateColumn('firstname', 'users', "VARCHAR(128)", "''", true, 'utf8');
$this->validateColumn('passhash', 'users', "VARCHAR(40)", "''", true, 'ascii');
$this->validateColumn('lastname', 'users', "VARCHAR(128)", "''", true, 'utf8');
$this->validateColumn('mail', 'users', "VARCHAR(128)", "''", true, 'utf8');
$this->validateColumn('apikey', 'users', "VARCHAR(32)", "''", true, 'ascii');
$this->validateColumn('lastlogin', 'users', "INTEGER", "0", true, '');
$this->validateColumn('lastvisit', 'users', "INTEGER", "0", true, '');
$this->validateColumn('lastread', 'users', "INTEGER", "0", true, '');
$this->validateColumn('lastapiusage', 'users', "INTEGER", "0", true, '');
$this->validateColumn('deleted', 'users', "BOOLEAN", 'false', true, '');
$this->alterStorageEngine("users", "InnoDB");
# ---- sessions ---- #
$this->createTable('sessions', "ascii");
$this->validateColumn('sessionid', 'sessions', 'VARCHAR(128)', NULL, false, 'ascii');
$this->validateColumn('userid', 'sessions', 'INTEGER', NULL, false, '');
$this->validateColumn('hitcount', 'sessions', 'INTEGER', NULL, false, '');
$this->validateColumn('lasthit', 'sessions', 'INTEGER', NULL, false, '');
$this->alterStorageEngine("sessions", "InnoDB");
# ---- securitygroups ----
$this->createTable('securitygroups', "ascii");
$this->validateColumn('name', 'securitygroups', 'VARCHAR(128)', NULL, false, 'ascii');
$this->alterStorageEngine("securitygroups", "InnoDB");
# ---- grouppermissions ----
$this->createTable('grouppermissions', "ascii");
$this->validateColumn('groupid', 'grouppermissions', 'INTEGER', "0", true, '');
$this->validateColumn('permissionid', 'grouppermissions', 'INTEGER', "0", true, '');
$this->validateColumn('objectid', 'grouppermissions', "VARCHAR(128)", "''", true, 'ascii');
$this->validateColumn('deny', 'grouppermissions', "BOOLEAN", 'false', true, '');
$this->alterStorageEngine("grouppermissions", "InnoDB");
# ---- usergroups ----
$this->createTable('usergroups', "ascii");
$this->validateColumn('userid', 'usergroups', 'INTEGER', "0", true, '');
$this->validateColumn('groupid', 'usergroups', 'INTEGER', "0", true, '');
$this->validateColumn('prio', 'usergroups', 'INTEGER', '1', true, '');
$this->alterStorageEngine("usergroups", "InnoDB");
# ---- notifications ----
$this->createTable('notifications', "ascii");
$this->validateColumn('userid', 'notifications', 'INTEGER', "0", true, '');
$this->validateColumn('stamp', 'notifications', 'INTEGER', "0", true, '');
$this->validateColumn('objectid', 'notifications', 'VARCHAR(128)', "''", true, 'ascii');
$this->validateColumn('type', 'notifications', 'VARCHAR(128)', "''", true, 'ascii');
$this->validateColumn('title', 'notifications', 'VARCHAR(128)', "''", true, 'utf8');
$this->validateColumn('body', 'notifications', 'TEXT', NULL, false, 'utf8');
$this->validateColumn('sent', 'notifications', 'BOOLEAN', 'false', true, '');
$this->alterStorageEngine("notifications", "InnoDB");
# ---- filters ----
$this->createTable('filters', "utf8");
$this->validateColumn('userid', 'filters', 'INTEGER', "0", true, '');
$this->validateColumn('filtertype', 'filters', 'VARCHAR(128)', "''", true, 'ascii');
$this->validateColumn('title', 'filters', 'VARCHAR(128)', "''", true, 'utf8');
$this->validateColumn('icon', 'filters', 'VARCHAR(128)', "''", true, 'utf8');
$this->validateColumn('torder', 'filters', 'INTEGER', "0", true, '');
$this->validateColumn('tparent', 'filters', 'INTEGER', "0", true, '');
$this->validateColumn('tree', 'filters', 'TEXT', NULL, false, 'ascii');
$this->validateColumn('valuelist', 'filters', 'TEXT', NULL, false, 'utf8');
$this->validateColumn('sorton', 'filters', 'VARCHAR(128)', NULL, false, 'ascii');
$this->validateColumn('sortorder', 'filters', 'VARCHAR(128)', NULL, false, 'ascii');
$this->alterStorageEngine("filters", "InnoDB");
##############################################################################################
### deprecation van oude Spotweb versies #####################################################
##############################################################################################
if ($this->_spotdb->getSchemaVer() > 0.00 && ($this->_spotdb->getSchemaVer() < 0.30)) {
throw new Exception("Je huidige Spotweb database installatie is te oud om in een keer te upgraden naar deze versie." . PHP_EOL .
"Download een eerdere versie van spotweb (https://github.com/spotweb/spotweb/zipball/da6ba29071c49ae88823cccfefc39375b37e9bee), " . PHP_EOL .
"draai daarmee upgrade-db.php en als die succesvol is, start dan nogmaals de upgrade via deze versie.");
} # if
# Tabellen terug samenvoegen in een MyISAM tabel
if (($this->_spotdb->getSchemaVer() < 0.34) && ($this->tableExists('spottexts'))) {
$this->_dbcon->rawExec("CREATE TABLE spotstmp(id INTEGER PRIMARY KEY AUTO_INCREMENT,
messageid varchar(128) CHARACTER SET ascii NOT NULL,
poster varchar(128),
title varchar(128),
tag varchar(128),
category INTEGER,
subcata VARCHAR(64),
subcatb VARCHAR(64),
subcatc VARCHAR(64),
subcatd VARCHAR(64),
subcatz VARCHAR(64),
stamp INTEGER(10) UNSIGNED,
reversestamp INTEGER DEFAULT 0,
filesize BIGINT UNSIGNED NOT NULL DEFAULT 0,
moderated BOOLEAN,
commentcount INTEGER DEFAULT 0,
spotrating INTEGER DEFAULT 0) ENGINE = MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci");
# Copieer de data uit de andere tabellen
$this->_dbcon->rawExec("INSERT INTO spotstmp(messageid, poster, title, tag, category,
subcata, subcatb, subcatc, subcatd,
subcatz, stamp, reversestamp, filesize,
moderated, commentcount, spotrating)
(SELECT s.messageid, t.poster, t.title, t.tag,
s.category,
s.subcata, s.subcatb, s.subcatc,
s.subcatd, s.subcatz, s.stamp,
s.reversestamp, s.filesize, s.moderated,
s.commentcount, s.spotrating
FROM spots s
JOIN spottexts t ON (s.messageid = t.messageid))");
# drop de 'oude' tabellen
$this->dropTable('spots');
$this->dropTable('spottexts');
# rename deze tabel
$this->renameTable('spotstmp', 'spots');
} # if
# En creeer de diverse indexen
# ---- Indexen op spots -----
$this->validateIndex("idx_spots_1", "UNIQUE", "spots", array("messageid"));
$this->validateIndex("idx_spots_2", "", "spots", array("stamp"));
$this->validateIndex("idx_spots_3", "", "spots", array("reversestamp"));
$this->validateIndex("idx_spots_4", "", "spots", array("category", "subcata", "subcatb", "subcatc", "subcatd", "subcatz"));
$this->validateFts("idx_fts_spots", "spots",
array(1 => "poster",
2 => 'title',
3 => 'tag'));
# ---- Indexen op nntp ----
$this->validateIndex("idx_nntp_1", "UNIQUE", "nntp", array("server"));
# ---- Indexen op spotsfull ----
$this->validateIndex("idx_spotsfull_1", "UNIQUE", "spotsfull", array("messageid"));
$this->validateIndex("idx_spotsfull_2", "", "spotsfull", array("userid"));
# ---- Indexen op commentsfull ----
$this->validateIndex("idx_commentsfull_1", "UNIQUE", "commentsfull", array("messageid"));
# ---- Indexen op commentsxover ----
$this->validateIndex("idx_commentsxover_1", "UNIQUE", "commentsxover", array("messageid"));
$this->validateIndex("idx_commentsxover_2", "", "commentsxover", array("nntpref"));
# ---- Indexen op reportsxover ----
$this->validateIndex("idx_reportsxover_1", "UNIQUE", "reportsxover", array("messageid"));
$this->validateIndex("idx_reportsxover_2", "", "reportsxover", array("nntpref"));
# ---- Indexen op reportsposted ----
$this->validateIndex("idx_reportsposted_1", "UNIQUE", "reportsposted", array("messageid"));
$this->validateIndex("idx_reportspostedrel_1", "", "reportsposted", array("ouruserid"));
# ---- Indexen op commentsposted ----
$this->validateIndex("idx_commentsposted_1", "UNIQUE", "commentsposted", array("messageid"));
$this->validateIndex("idx_commentspostedrel_1", "", "commentsposted", array("ouruserid"));
# ---- Indexen op settings ----
$this->validateIndex("idx_settings_1", "UNIQUE", "settings", array("name"));
# ---- Indexen op usersettings ----
$this->validateIndex("idx_usersettings_1", "UNIQUE", "usersettings", array("userid"));
# ---- Indexen op users ----
$this->validateIndex("idx_users_1", "UNIQUE", "users", array("username"));
$this->validateIndex("idx_users_2", "UNIQUE", "users", array("mail"));
$this->validateIndex("idx_users_3", "", "users", array("deleted"));
$this->validateIndex("idx_users_4", "UNIQUE", "users", array("apikey"));
# ---- Indexen op sessions
$this->validateIndex("idx_sessions_1", "UNIQUE", "sessions", array("sessionid"));
$this->validateIndex("idx_sessions_2", "", "sessions", array("lasthit"));
$this->validateIndex("idx_sessions_3", "", "sessions", array("sessionid", "userid"));
$this->validateIndex("idx_sessionsrel_1", "", "sessions", array("userid"));
# ---- Indexen op spotstatelist ----
$this->validateIndex("idx_spotstatelist_1", "UNIQUE", "spotstatelist", array("messageid", "ouruserid"));
$this->validateIndex("idx_spotstatelistrel_1", "", "spotstatelist", array("ouruserid"));
# ---- Indexen op securitygroups ----
$this->validateIndex("idx_securitygroups_1", "UNIQUE", "securitygroups", array("name"));
# ---- Indexen op grouppermissions ----
$this->validateIndex("idx_grouppermissions_1", "UNIQUE", "grouppermissions", array("groupid", "permissionid", "objectid"));
# ---- Indexen op usergroups ----
$this->validateIndex("idx_usergroups_1", "UNIQUE", "usergroups", array("userid", "groupid"));
$this->validateIndex("idx_usergroupsrel_1", "", "usergroups", array("groupid"));
# ---- Indexen op notifications ----
$this->validateIndex("idx_notifications_1", "", "notifications", array("userid"));
$this->validateIndex("idx_notifications_2", "", "notifications", array("sent"));
# ---- Indexen op filters ----
$this->validateIndex("idx_filters_1", "", "filters", array("userid", "filtertype", 'tparent', 'torder'));
# leg foreign keys aan
$this->addForeignKey('usersettings', 'userid', 'users', 'id', 'ON DELETE CASCADE ON UPDATE CASCADE');
$this->addForeignKey('sessions', 'userid', 'users', 'id', 'ON DELETE CASCADE ON UPDATE CASCADE');
$this->addForeignKey('spotstatelist', 'ouruserid', 'users', 'id', 'ON DELETE CASCADE ON UPDATE CASCADE');
$this->addForeignKey('usergroups', 'userid', 'users', 'id', 'ON DELETE CASCADE ON UPDATE CASCADE');
$this->addForeignKey('usergroups', 'groupid', 'securitygroups', 'id', 'ON DELETE CASCADE ON UPDATE CASCADE');
$this->addForeignKey('grouppermissions', 'groupid', 'securitygroups', 'id', 'ON DELETE CASCADE ON UPDATE CASCADE');
$this->addForeignKey('commentsfull', 'messageid', 'commentsxover', 'messageid', 'ON DELETE CASCADE ON UPDATE CASCADE');
$this->addForeignKey('notifications', 'userid', 'users', 'id', 'ON DELETE CASCADE ON UPDATE CASCADE');
$this->addForeignKey('commentsposted', 'ouruserid', 'users', 'id', 'ON DELETE CASCADE ON UPDATE CASCADE');
$this->addForeignKey('reportsposted', 'ouruserid', 'users', 'id', 'ON DELETE CASCADE ON UPDATE CASCADE');
$this->addForeignKey('filters', 'userid', 'users', 'id', 'ON DELETE CASCADE ON UPDATE CASCADE');
##############################################################################################
# Hier droppen we kolommen ###################################################################
##############################################################################################
$this->dropColumn('filesize', 'spotsfull');
# voeg het database schema versie nummer toe
$this->_spotdb->updateSetting('schemaversion', SPOTDB_SCHEMA_VERSION);
} # updateSchema
} # class