-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlib_sqlitedb.php
executable file
·685 lines (581 loc) · 16.3 KB
/
lib_sqlitedb.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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
<?php
define ( 'DIR', dirname ( __FILE__ ) );
class sqlitedb extends SQLite3
{
var $dbfile = '/data/filmdb.sqlite';
var $structure = '/data/filmdb.sql';
function __construct()
{
$this -> open ( DIR . $this -> dbfile );
//$this -> updateDBSchema();
}
function updateDBSchema()
{
$schema_queries = explode ( ';', file_get_contents ( DIR . $this -> structure ) );
foreach ( $schema_queries as $query )
{
try {
$this -> exec ( $query );
} catch ( Exception $e ) { }
}
}
/**
* darf der Nutzer editieren?
*
* @todo konfigurierbar machen
* @return Boolean
*/
public function isAdmin()
{
return ( $_SERVER [ 'REMOTE_ADDR' ] == '192.168.0.2' );
}
/**
* gibt die ID eines Cast-Eintrags zurück, entweder eine
* existierende ID wenn es den Eintrag schon gibt, oder
* eine neue ID, nachdem dieser angelegt wurde
*
* @param String $name vollständiger Name eines Cast-Mitglieds
* @return Integer ID
*/
private function getCastId ( $name )
{
$query = $this -> prepare ( 'SELECT "cast_id" FROM "cast" WHERE "cast"=:name' );
$query -> bindValue ( ':name', $name, SQLITE3_TEXT );
$res = $query -> execute();
if ( $data = $res -> fetchArray ( SQLITE3_ASSOC ) )
return $data [ 'cast_id' ];
else
{
$query = $this -> prepare ( 'INSERT INTO "cast"("cast") VALUES(:name)' );
$query -> bindValue ( ':name', $name, SQLITE3_TEXT );
$res = $query -> execute();
return $this -> lastInsertRowID();
}
}
/**
* speichert den aktuellen Cast eines Films, entfernt dazu ggf. den bestehenden
*
* @param String $table cast2movie | director2movie
* @param Integer $imdb_id IMDb-ID
* @param Array $arr_cast Liste von Cast-IDs
*/
private function saveCast ( $table, $imdb_id, $arr_cast )
{
$this -> exec ( 'DELETE FROM ' . $table . ' WHERE imdb_id=' . $imdb_id );
$sql = 'INSERT INTO ' . $table . ' ( cast_id, imdb_id, sort ) VALUES ';
$sort = 0;
$values = array();
$arr_cast = array_unique ( $arr_cast );
// damit die Statements nicht zu groß werden, nur 100 pro Schwung
while ( $cast_id = array_shift ( $arr_cast ) )
{
$values[] = '(' . $cast_id . ', ' . $imdb_id . ', ' . $sort++ . ')';
if ( count ( $values ) < 100 )
continue;
else
{
$this -> exec ( $sql . implode ( ',', $values ) );
$values = array();
}
}
// die restlichen
$this -> exec ( $sql . implode ( ',', $values ) );
}
/**
* speichert die aktuelle Liste der Genres zu einem Film, entfernt ggf. die bestehenden
*
* @param Integer $imdb_id IMDb-ID
* @param Array $arr_genres Liste von Genre-IDs
*/
private function saveGenre ( $imdb_id, $arr_genres )
{
$this -> exec ( 'DELETE FROM genre2movie WHERE imdb_id=' . $imdb_id );
$sql = 'INSERT INTO genre2movie ( genre_id, imdb_id ) VALUES ';
$values = array();
foreach ( $arr_genres as $genre_id )
$values[] = '(' . $genre_id . ', ' . $imdb_id . ')';
$sql .= implode ( ',', $values );
$this -> exec ( $sql );
}
/**
* gibt die ID eines Genre-Eintrags zurück, entweder eine
* existierende ID wenn es den Eintrag schon gibt, oder
* eine neue ID, nachdem dieser angelegt wurde
*
* @param String $name Name eines Genres
* @return Integer ID
*/
private function getGenreId ( $name )
{
$query = $this -> prepare ( 'SELECT "genre_id" FROM "genre" WHERE "genre"=:name' );
$query -> bindValue ( ':name', $name, SQLITE3_TEXT );
$res = $query -> execute();
if ( $data = $res -> fetchArray ( SQLITE3_ASSOC ) )
return $data [ 'genre_id' ];
else
{
$query = $this -> prepare ( 'INSERT INTO "genre"("genre") VALUES(:name)' );
$query -> bindValue ( ':name', $name, SQLITE3_TEXT );
$res = $query -> execute();
return $this -> lastInsertRowID();
}
}
/**
* speichert einen Film in der Datenbank, mitsamt aller Zusatzinfos, Cast, Genres etc.
*
* @param Array $data Film-Daten
*/
public function saveMovie ( $data )
{
$this -> results ( 'REPLACE INTO movie(
imdb_id,
imdb_photo,
imdb_plot,
imdb_rating,
imdb_top250,
imdb_runtime,
imdb_title_deu,
imdb_title_eng,
imdb_title_orig,
imdb_year,
imdb_type,
language_deu,
language_eng,
language_omu,
custom_rating,
custom_notes,
custom_quality,
bechdel_id,
bechdel_rating,
bechdel_dubious,
metacritic,
rottentomatoes
) VALUES (
:imdb_id,
:imdb_photo,
:imdb_plot,
:imdb_rating,
:imdb_top250,
:imdb_runtime,
:imdb_title_deu,
:imdb_title_eng,
:imdb_title_orig,
:imdb_year,
:imdb_type,
:language_deu,
:language_eng,
:language_omu,
:custom_rating,
:custom_notes,
:custom_quality,
:bechdel_id,
:bechdel_rating,
:bechdel_dubious,
:metacritic,
:rottentomatoes
)', $data );
// Cast
if ( isset ( $data [ 'cast' ] ) )
{
$actors = array();
foreach ( $data [ 'cast' ] as $actor )
$actors[] = $this -> getCastId ( $actor );
$this -> saveCast ( 'cast2movie', $data ['@imdb_id' ], $actors );
}
// Director
if ( isset ( $data [ 'director' ] ) )
{
$directors = array();
foreach ( $data [ 'director' ] as $director )
$directors[] = $this -> getCastId ( $director );
$this -> saveCast ( 'director2movie', $data ['@imdb_id' ], $directors );
}
// Genres
$genres = array();
foreach ( $data [ 'genres' ] as $genre )
$genres[] = $this -> getGenreId ( $genre );
$this -> saveGenre ( $data [ '@imdb_id' ], $genres );
$this -> updateFulltext ( $data [ '@imdb_id' ] );
}
/**
* aktualisiert die IMDb-unabhängigen Filmdaten
*
* @param array $data Filmdaten aus dem Edit-Formular
*/
public function updateMovie ( $data )
{
$movie = array (
'@imdb_id' => $data [ 'imdb_id' ],
'@language_deu' => $data [ 'language_deu' ],
'@language_eng' => $data [ 'language_eng' ],
'@language_omu' => $data [ 'language_omu' ],
'@custom_rating' => $data [ 'custom_rating' ],
'$custom_notes' => $data [ 'custom_notes' ],
'$custom_quality' => $data [ 'custom_quality' ]
);
// und aktualisieren
$this -> results ( 'UPDATE movie
SET
language_deu = :language_deu,
language_eng = :language_eng,
language_omu = :language_omu,
custom_rating = :custom_rating,
custom_notes = :custom_notes,
custom_quality = :custom_quality
WHERE imdb_id = :imdb_id', $movie );
$this -> updateFulltext ( $data [ 'imdb_id' ] );
}
/**
* wieviele in der DB vorhandene Filme hat ein Cast-Mitglied gedreht?
*
* @param String $cast_id ID des Cast-Mitglieds
* @param String $table director2movie | cast2movie
* @return Integer Anzahl Filme
*/
public function castMovieCount ( $cast_id, $table )
{
$moviecount = $this -> results (
'SELECT COUNT(cast_id) AS cnt
FROM ' . $table . '
WHERE cast_id=:cast_id',
array ( '@cast_id' => $cast_id )
);
return $moviecount [ 0 ][ 'cnt' ];
}
/**
* Filter aus REQUEST erzeugen
*
* @param Array $form Formulardaten aus einem REQUEST
* @return Array JOINs und WHERE-Klausel (jeweils Arrays) für DB-Abfrage
*/
public function getFilters ( $form )
{
$joins = array();
$where = array();
// Volltextsuche
if ( !empty ( $form [ 'fulltext' ] ) )
{
if ( is_numeric ( $form [ 'fulltext' ] ) )
$where[] = 'm.imdb_id=' . intval ( $form [ 'fulltext' ] );
else
{
$terms = explode ( ' ', $this -> _transliterate ( $form [ 'fulltext' ] ) );
foreach ( $terms as $term )
$where[] = 'm.fulltext LIKE "%' . $term . '%"';
}
}
// Sprachfilter (ODER)
if ( !empty ( $form [ 'lang' ] ) && is_array ( $form [ 'lang' ] ) )
{
$lang_where = array();
foreach ( $form [ 'lang' ] as $lang => $value )
$lang_where[] = $lang .'=1';
$where [ 'lang' ] = '(' . implode ( ' OR ', $lang_where ) . ')';
}
// Regiefilter
if ( !empty ( $form [ 'director' ] ) && is_array ( $form [ 'director' ] ) )
{
foreach ( $form [ 'director' ] as $value )
$dir_where[] = 'd2m.cast_id=' . intval ( $value );
$joins[] = 'LEFT JOIN director2movie d2m ON d2m.imdb_id=m.imdb_id';
$where[] = '(' . implode ( ' OR ', $dir_where ) . ')';
}
// Cast-Filter
if ( !empty ( $form [ 'cast' ] ) && is_array ( $form [ 'cast' ] ) )
{
$idx = 0;
foreach ( $form [ 'cast' ] as $value )
{
$joins[] = 'LEFT JOIN cast2movie a2m_'.$idx.' ON a2m_'.$idx.'.imdb_id=m.imdb_id';
$where[] = 'a2m_'.$idx.'.cast_id=' . intval ( $value );
$idx++;
}
}
// Genre-Filter (UND)
if ( !empty ( $form [ 'genre' ] ) && is_array ( $form [ 'genre' ] ) )
{
$idx = 0;
foreach ( $form [ 'genre' ] as $value )
{
if ( $value > 0 )
{
$joins[] = 'LEFT JOIN genre2movie g2m_'.$idx.' ON g2m_'.$idx.'.imdb_id=m.imdb_id';
$where[] = 'g2m_'.$idx.'.genre_id=' . intval ( $value );
}
else
{
$joins[] = 'LEFT JOIN genre2movie g2m_'.$idx.' ON g2m_'.$idx.'.imdb_id=m.imdb_id AND g2m_'.$idx.'.genre_id=' . abs ( intval ( $value ) );
$where[] = 'g2m_'.$idx.'.genre_id IS NULL';
}
$idx++;
}
}
// Typ-Filter (ODER)
if ( !empty ( $form [ 'type' ] ) && is_array ( $form [ 'type' ] ) )
{
$type_where = [];
$types = $this -> getTypeList();
foreach ( $form [ 'type' ] as $value )
{
if ( $value >= 0 )
$type_where[] = 'm.imdb_type="' . $types [ $value -1 ][ 'name' ] . '"';
else
$where[] = 'm.imdb_type<>"' . $types [ abs ( $value ) -1 ][ 'name' ] . '"';
}
if ( !empty ( $type_where ) )
$where[] = '(' . implode ( ' OR ', $type_where ) . ')';
}
return array ( 'joins' => $joins, 'where' => $where );
}
/**
* Liste von Filmen
*
* @param Array $filters REQUEST-Suchfilter
* @return Array Filme
* @todo Sortierungsmöglichkeiten
*/
function getMovieList ( $filters = null )
{
$sql = 'SELECT DISTINCT
m.imdb_id,
m.imdb_title_orig,
m.imdb_photo,
m.custom_rating,
m.imdb_year
FROM movie m
%JOIN
%WHERE
%ORDER';
$joins = array();
$where[] = '1=1';
$filters [ 'where' ][] = '1=1';
if ( empty ( $filters [ 'joins' ] ) )
$filters [ 'joins' ] = array();
$sql = str_replace ( '%JOIN', implode ( ' ', $filters [ 'joins' ] ), $sql );
$sql = str_replace ( '%WHERE', 'WHERE ' . implode ( ' AND ', $filters [ 'where' ] ), $sql );
$sql = str_replace ( '%ORDER', 'ORDER BY imdb_year DESC, m.imdb_id DESC', $sql );
return $this -> results ( $sql );
}
/**
* Detail-Informationen eines Films
*
* @param Integer $imdb_id IMDb-ID
* @return Array Filmdaten
*/
function getSingleMovie ( $imdb_id )
{
$movies = $this -> results (
'SELECT * FROM movie WHERE imdb_id=:imdb_id',
array ( '@imdb_id' => $imdb_id )
);
$movie = $movies [ 0 ];
// Directors
$movie [ 'director' ] = $this -> results (
'SELECT
c.cast_id AS id,
"cast" AS "name"
FROM "cast" c
LEFT JOIN director2movie d2m
ON d2m.cast_id=c.cast_id
WHERE imdb_id=:imdb_id',
array ( '@imdb_id' => $imdb_id )
);
// Cast
$movie [ 'cast' ] = $this -> results (
'SELECT
c.cast_id AS id,
"cast" AS "name"
FROM "cast" c
LEFT JOIN cast2movie c2m
ON c2m.cast_id=c.cast_id
WHERE imdb_id=:imdb_id
ORDER BY c2m.sort ASC',
array ( '@imdb_id' => $imdb_id )
);
// Genres
$movie [ 'genres' ] = $this -> results (
'SELECT
g.genre_id AS id,
genre AS "name"
FROM genre g
LEFT JOIN genre2movie g2m
ON g2m.genre_id=g.genre_id
WHERE imdb_id=:imdb_id',
array ( '@imdb_id' => $imdb_id )
);
return $movie;
}
/**
* besorgt eine Liste aller in der DB vorhandenen Genres
* mit zugehöriger Anzahl der Filme dieses Genres
*
* @return Array Genreliste
*/
public function getGenreList()
{
return $this -> results ( 'SELECT
g.genre_id,
genre,
COUNT(g2m.imdb_id) AS cnt
FROM genre g
LEFT JOIN genre2movie g2m ON g2m.genre_id=g.genre_id
GROUP BY g2m.genre_id
ORDER BY COUNT(g2m.imdb_id) DESC' );
}
/**
* besorgt eine Liste aller in der DB vorhandenen Genres
* mit zugehöriger Anzahl der Filme dieses Genres
*
* @return Array Genreliste
*/
public function getTypeList()
{
$types = $this -> results ( 'SELECT
imdb_type AS name,
COUNT(imdb_id) AS cnt
FROM movie
GROUP BY imdb_type
ORDER BY imdb_type' );
foreach ( $types as $index => &$type )
$type [ 'value' ] = $index + 1;
return $types;
}
/**
* besorgt eine Liste der 25 aktivsten Darsteller
* (Kurz- und Animationsfilme ausgenommen)
* mit zugehöriger Anzahl der Filme
*
* @return Array Darsteller-Liste
*/
public function getCastList()
{
return $this -> results ( 'SELECT
c.cast_id,
"cast",
COUNT(c2m.imdb_id) AS cnt
FROM "cast" c
LEFT JOIN cast2movie c2m ON c2m.cast_id=c.cast_id
LEFT JOIN genre2movie g2m_1 ON c2m.imdb_id=g2m_1.imdb_id AND g2m_1.genre_id = 3
LEFT JOIN genre2movie g2m_2 ON c2m.imdb_id=g2m_2.imdb_id AND g2m_2.genre_id = 6
WHERE g2m_1.genre_id IS NULL
AND g2m_2.genre_id IS NULL
GROUP BY c2m.cast_id
ORDER BY
COUNT(c2m.imdb_id) DESC,
AVG(c2m.sort) ASC
LIMIT 25' );
}
/**
* besorgt eine Liste der 25 aktivsten Regisseure
* (Kurzfilme ausgenommen)
* mit zugehöriger Anzahl der Filme
*
* @return Array Cast-Liste
*/
public function getDirectorList()
{
return $this -> results ( 'SELECT
c.cast_id,
"cast",
COUNT(d2m.imdb_id) AS cnt
FROM "cast" c
LEFT JOIN director2movie d2m ON d2m.cast_id=c.cast_id
LEFT JOIN genre2movie g2m ON d2m.imdb_id=g2m.imdb_id AND g2m.genre_id = 3
WHERE g2m.genre_id IS NULL
GROUP BY d2m.cast_id
ORDER BY
COUNT(d2m.imdb_id) DESC,
AVG(d2m.sort) ASC
LIMIT 25' );
}
/**
* SQLite-Wrapper für Queries mit Parametern
*
* @param String $sql SQL-Query mit ggf. Platzhaltern
* @param Array $placeholders assoziatives Array mit Platzhaltern und deren Datentypen
* @return Array Ergebnis der SQL-Abfrage
*/
public function results ( $sql, $placeholders = array() )
{
$query = $this -> prepare ( $sql );
foreach ( $placeholders as $key => $value )
{
if ( empty ( $value ) && $value != 0 )
$type = SQLITE3_NULL;
else switch ( $key[0] )
{
case '@': $type = SQLITE3_INTEGER; break;
case '#': $type = SQLITE3_FLOAT; break;
case '$':
default: $type = SQLITE3_TEXT; break;
}
$query -> bindValue ( ':'.substr ( $key, 1 ), $value, $type );
}
$res = $query -> execute();
$data = array();
if ( $res )
while ( $ds = $res -> fetchArray ( SQLITE3_ASSOC ) )
$data[] = $ds;
return $data;
}
/**
* aktualisiert die Volltextinformationen eines Films in der Datenbank
*
* @param Integer $imdb_id IMDb-ID
*/
private function updateFulltext ( $imdb_id )
{
$movie = $this -> getSingleMovie ( $imdb_id );
$fulltext = $this -> getFulltext ( $movie );
$this -> results ( 'UPDATE movie SET fulltext = :fulltext WHERE imdb_id = :imdb_id',
array (
'$fulltext' => $fulltext,
'@imdb_id' => $imdb_id
)
);
}
/**
* Volltextindex eines Films erzeugen
*
* @param Array $movie MongoDB-Filmdaten
*/
private function getFulltext ( &$movie )
{
// alle zu indizierenden Felder zusammensuchen
$fulltext[] = $movie [ 'imdb_title_orig' ];
$fulltext[] = $movie [ 'imdb_title_deu' ];
$fulltext[] = $movie [ 'imdb_title_eng' ];
$fulltext[] = $movie [ 'custom_notes' ];
// Genres
foreach ( $movie [ 'genres' ] as $genre )
$fulltext[] = $genre [ 'name' ];
// Cast
foreach ( $movie [ 'cast' ] as $cast )
$fulltext[] = $cast [ 'name' ];
// Director
foreach ( $movie [ 'director' ] as $director )
$fulltext[] = $director [ 'name' ];
// jedes Wort nur einmal
$fulltext = array_unique ( $fulltext );
// Top250?
if ( !empty ( $movie [ 'imdb_top250' ] ) )
$fulltext[] = 'Top250';
// Worte normalisieren
$fulltext = $this -> _transliterate ( implode ( ' ', $fulltext ) );
return $fulltext;
}
/**
* Zeichenkette transliterieren
*
* @param String $string Zeichenkette
* @return String normalisierte Zeichenkette
*/
private function _transliterate ( $string )
{
setlocale ( LC_ALL, 'de_DE' );
$string = iconv ( 'utf-8', 'ASCII//TRANSLIT', $string );
$string = preg_replace ( '~[^\w ]~', '', $string );
$string = preg_replace ( '~[\s]+~', ' ', $string );
return strtolower ( $string );
}
}