forked from RomanSixty/Feed-on-Feeds
-
Notifications
You must be signed in to change notification settings - Fork 3
/
fof-db.php
executable file
·2209 lines (1754 loc) · 64.1 KB
/
fof-db.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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
* This file is part of FEED ON FEEDS - http://feedonfeeds.com/
*
* fof-db.php - all of the DB specific code
*
*
* Copyright (C) 2004-2007 Stephen Minutillo
* steve@minutillo.com - http://minutillo.com/steve/
*
* Distributed under the GPL - see LICENSE
*
*/
require_once 'classes/pdolog.php';
/* these may be overridden in fof-config.php, but generally oughtn't be */
defined('FOF_DB_PREFIX') || define('FOF_DB_PREFIX', 'fof_');
defined('FOF_FEED_TABLE') || define('FOF_FEED_TABLE', FOF_DB_PREFIX . 'feed');
defined('FOF_ITEM_TABLE') || define('FOF_ITEM_TABLE', FOF_DB_PREFIX . 'item');
defined('FOF_ITEM_TAG_TABLE') || define('FOF_ITEM_TAG_TABLE', FOF_DB_PREFIX . 'item_tag');
defined('FOF_SUBSCRIPTION_TABLE') || define('FOF_SUBSCRIPTION_TABLE', FOF_DB_PREFIX . 'subscription');
defined('FOF_TAG_TABLE') || define('FOF_TAG_TABLE', FOF_DB_PREFIX . 'tag');
defined('FOF_USER_TABLE') || define('FOF_USER_TABLE', FOF_DB_PREFIX . 'user');
defined('FOF_VIEW_TABLE') || define('FOF_VIEW_TABLE', FOF_DB_PREFIX . 'view');
defined('FOF_VIEW_STATE_TABLE') || define('FOF_VIEW_STATE_TABLE', FOF_DB_PREFIX . 'view_state');
if (defined('USE_MYSQL')) {
defined('MYSQL_ENGINE') || define('MYSQL_ENGINE', 'InnoDB');
}
$FOF_FEED_TABLE = FOF_FEED_TABLE;
$FOF_ITEM_TABLE = FOF_ITEM_TABLE;
$FOF_ITEM_TAG_TABLE = FOF_ITEM_TAG_TABLE;
$FOF_SUBSCRIPTION_TABLE = FOF_SUBSCRIPTION_TABLE;
$FOF_TAG_TABLE = FOF_TAG_TABLE;
$FOF_USER_TABLE = FOF_USER_TABLE;
$FOF_VIEW_TABLE = FOF_VIEW_TABLE;
$FOF_VIEW_STATE_TABLE = FOF_VIEW_STATE_TABLE;
if (defined('USE_SQLITE')) {
/* sqlite uses an additional table */
defined('FOF_USER_LEVELS_TABLE') || define('FOF_USER_LEVELS_TABLE', FOF_USER_TABLE . "_levels");
$FOF_USER_LEVELS_TABLE = FOF_USER_LEVELS_TABLE;
}
/*
Non-local function dependencies:
fof_prefs()
fof_log()
fof_stacktrace()
fof_todays_date()
fof_multi_sort()
Non-local variable dependencies:
$fof_connection
$fof_user_id
$fof_user_name
$fof_user_level
*/
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
/** Callback for logging database queries via PDOLog class.
*/
function fof_db_query_log_cb($query_string, $elapsed_time, $result, $rows_affected = null, $parameters = null) {
$msg = '';
if (defined('FOF_QUERY_LOG_TRACE')) {
$msg .= fof_stacktrace(2, false);
$msg .= ' result:' . $result . ' ';
} /* FOF_QUERY_LOG_TRACE */
$msg .= $query_string;
if (!empty($parameters)) {
$msg .= ' {';
foreach ($parameters as $k => $v) {
$msg .= " '$k':'$v'";
}
$msg .= ' }';
}
if (!is_null($rows_affected)) {
$msg .= sprintf(' (%d rows affected)', $rows_affected);
}
$msg .= sprintf(' %.3f', $elapsed_time);
fof_log($msg, 'query');
}
/* set up a db connection, creating the db if it doesn't exist */
function fof_db_connect($create = false) {
global $fof_connection;
/* It would be nice to actually use prepared statements by setting
PDO::ATTR_EMULATE_PREPARES => false
but it seems that however PDO translates its named parameters to the
MySQL bindings doesn't work when a parameter is repeated in a query..
Leaving the emulation on is easier than changing the sql.
*/
$pdo_options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
if (defined('USE_MYSQL')) {
if (!defined('FOF_DB_HOST') || !defined('FOF_DB_DBNAME')) {
throw new Exception('Missing MySQL configuration; make sure FOF_DB_HOST and FOF_DB_DBNAME are set');
}
$dsn = "mysql:host=" . FOF_DB_HOST . ($create ? '' : (';dbname=' . FOF_DB_DBNAME)) . ";charset=utf8mb4";
} else if (defined('USE_SQLITE')) {
if (!defined('FOF_DATA_PATH') || !defined('FOF_DB_FILENAME')) {
throw new Exception('Missing SQLite configuration; make sure FOF_DATA_PATH and FOF_DB_FILENAME are set');
}
$sqlite_db = FOF_DATA_PATH . DIRECTORY_SEPARATOR . FOF_DB_FILENAME;
$dsn = "sqlite:$sqlite_db";
} else {
throw new Exception('Missing database configuration; make sure USE_SQLITE or USE_MYSQL is set');
}
fof_log("Connecting to '$dsn'...");
if (defined('USE_SQLITE')) {
// sqlite doesn't use these variables
defined('FOF_DB_USER') || define('FOF_DB_USER', '');
defined('FOF_DB_PASS') || define('FOF_DB_PASS', '');
}
$fof_connection = new PDOLog($dsn, FOF_DB_USER, FOF_DB_PASS, $pdo_options);
PDOLog::$logfn = 'fof_db_query_log_cb';
if (defined('USE_MYSQL') && $create) {
if ($fof_connection) {
$fof_connection->exec("CREATE DATABASE IF NOT EXISTS " . FOF_DB_DBNAME);
$fof_connection->exec("USE " . FOF_DB_DBNAME);
}
}
return $fof_connection;
}
function fof_db_get_row($statement, $key = NULL, $nomore = FALSE) {
if (($row = $statement->fetch(PDO::FETCH_ASSOC)) === FALSE) {
return FALSE;
}
if ($nomore) {
$statement->closeCursor();
}
if (isset($key)) {
return (isset($row[$key]) ? $row[$key] : NULL);
}
return $row;
}
function fof_db_optimize() {
global $FOF_FEED_TABLE, $FOF_ITEM_TABLE, $FOF_ITEM_TAG_TABLE, $FOF_SUBSCRIPTION_TABLE, $FOF_TAG_TABLE, $FOF_USER_TABLE;
global $fof_connection;
if (defined('USE_MYSQL')) {
$query = "OPTIMIZE TABLE $FOF_FEED_TABLE, $FOF_ITEM_TABLE, $FOF_ITEM_TAG_TABLE, $FOF_SUBSCRIPTION_TABLE, $FOF_TAG_TABLE, $FOF_USER_TABLE";
} else if (defined('USE_SQLITE')) {
$query = "VACUUM";
} else {
throw new Exception("missing implementation");
}
$result = $fof_connection->exec($query);
return $result;
}
////////////////////////////////////////////////////////////////////////////////
// Feed level stuff
////////////////////////////////////////////////////////////////////////////////
/** Store the current timestamp as when a feed was last successfully fetched and parsed.
*/
function fof_db_feed_mark_cached($feed_id) {
global $FOF_FEED_TABLE;
global $fof_connection;
fof_trace();
$query = "UPDATE $FOF_FEED_TABLE SET feed_cache_date = :cache_date WHERE feed_id = :feed_id";
$statement = $fof_connection->prepare($query);
$statement->bindValue(':cache_date', time());
$statement->bindValue(':feed_id', $feed_id);
$result = $statement->execute();
$statement->closeCursor();
return $result;
}
/** Store the current timestamp as when a feed was last attempted to be fetched.
*/
function fof_db_feed_mark_attempted_cache($feed_id) {
global $FOF_FEED_TABLE;
global $fof_connection;
fof_trace();
$query = "UPDATE $FOF_FEED_TABLE SET feed_cache_attempt_date = :cache_date WHERE feed_id = :feed_id";
$statement = $fof_connection->prepare($query);
$statement->bindValue(':cache_date', time());
$statement->bindValue(':feed_id', $feed_id);
$result = $statement->execute();
$statement->closeCursor();
return $result;
}
/** Store the status of the most recent update of a feed.
*/
function fof_db_feed_update_attempt_status($feed_id, $status) {
global $FOF_FEED_TABLE;
global $fof_connection;
fof_trace();
$query = "UPDATE $FOF_FEED_TABLE SET feed_cache_last_attempt_status = :status WHERE feed_id = :feed_id";
$statement = $fof_connection->prepare($query);
$statement->bindValue(':feed_id', $feed_id);
if (empty($status)) {
$statement->bindValue(':status', NULL, PDO::PARAM_NULL);
} else {
$statement->bindValue(':status', $status);
}
$result = $statement->execute();
$statement->closeCursor();
return $result;
}
/** Store the various data which describes a feed.
*/
function fof_db_feed_update_metadata($feed_id, $title, $link, $feed_url, $description, $image, $image_cache_date) {
global $FOF_FEED_TABLE;
global $fof_connection;
fof_trace();
$query = "UPDATE $FOF_FEED_TABLE SET feed_title = :title, feed_link = :link, feed_url = :feed_url, feed_description = :description, feed_image = :image, feed_image_cache_date = :image_cache_date WHERE feed_id = :feed_id";
$statement = $fof_connection->prepare($query);
$statement->bindValue(':title', empty($title) ? "[no title]" : $title);
$statement->bindValue(':link', empty($link) ? "[no link]" : $link);
$statement->bindValue(':feed_url', $feed_url);
$statement->bindValue(':description', empty($description) ? "[no description]" : $description);
if (!empty($image)) {
$statement->bindValue(':image', $image);
} else {
$statement->bindValue(':image', null, PDO::PARAM_NULL);
}
$statement->bindValue(':image_cache_date', empty($image_cache_date) ? time() : $image_cache_date);
$statement->bindValue(':feed_id', $feed_id);
$result = $statement->execute();
$statement->closeCursor();
return $result;
}
/** Update a feed's WebSub subscription.
*/
function fof_db_feed_update_websub($feed_id, $hub, $secret, $lease = null) {
global $FOF_FEED_TABLE;
global $fof_connection;
fof_trace();
$query = "UPDATE $FOF_FEED_TABLE SET feed_websub_hub = :hub";
if ($lease) {
$query .= ", feed_websub_lease = :lease";
}
$query .= ", feed_websub_secret = :secret WHERE feed_id = :feed_id";
$statement = $fof_connection->prepare($query);
$statement->bindValue(':feed_id', $feed_id);
$statement->bindValue(':hub', $hub);
if ($lease) {
$statement->bindValue(':lease', $lease);
}
$statement->bindValue(':secret', $secret);
$result = $statement->execute();
$statement->closeCursor();
return $result;
}
/** Return a row iterator of the most-recent items, per feed_id.
*/
function fof_db_get_latest_item_age($user_id = null, $feed_id = null) {
global $FOF_ITEM_TABLE, $FOF_SUBSCRIPTION_TABLE;
global $fof_connection;
fof_trace();
if ($user_id || $feed_id) {
$where = 'WHERE ';
if ($user_id) {
$where .= 's.user_id = :user_id ';
}
if ($user_id && $feed_id) {
$where .= 'AND ';
}
if ($feed_id) {
$where .= 'i.feed_id = :feed_id ';
}
} else {
$where = '';
}
$query = "SELECT max(i.item_cached) AS max_date, i.feed_id " .
"FROM $FOF_ITEM_TABLE i " .
($user_id ? "JOIN $FOF_SUBSCRIPTION_TABLE s ON i.feed_id = s.feed_id " : '') .
$where .
"GROUP BY i.feed_id";
$statement = $fof_connection->prepare($query);
if ($user_id) {
$statement->bindValue(':user_id', $user_id);
}
if ($feed_id) {
$statement->bindValue(':feed_id', $feed_id);
}
$result = $statement->execute();
return $statement;
}
/** Return a summary of the total number of items in a feed, the number of
items with tags, and the count of items per tag.
*/
function fof_db_feed_counts($user_id, $feed_id) {
global $FOF_ITEM_TABLE, $FOF_ITEM_TAG_TABLE, $FOF_SUBSCRIPTION_TABLE, $FOF_TAG_TABLE;
global $fof_connection;
static $system_tags = array('unread', 'star', 'folded'); /* won't tally these under 'tagged' */
$counts = array();
$tagged = 0;
/* always want system tags to have a count */
foreach ($system_tags as $t) {
$counts[$t] = 0;
}
/* get total items */
$query = "SELECT count(DISTINCT i.item_id) AS total " .
"FROM $FOF_ITEM_TABLE i " .
"LEFT JOIN $FOF_SUBSCRIPTION_TABLE s ON s.feed_id = i.feed_id " .
"WHERE i.feed_id = :feed_id AND s.user_id = :user_id";
$statement = $fof_connection->prepare($query);
$statement->bindValue(':user_id', $user_id);
$statement->bindValue(':feed_id', $feed_id);
$result = $statement->execute();
$total = fof_db_get_row($statement, 'total', TRUE);
/* get counts per tag */
$query = "SELECT t.tag_id, t.tag_name, COUNT(t.tag_name) AS tag_count " .
"FROM $FOF_ITEM_TABLE i " .
"LEFT JOIN $FOF_ITEM_TAG_TABLE it ON it.item_id = i.item_id " .
"LEFT JOIN $FOF_TAG_TABLE t ON t.tag_id = it.tag_id " .
"WHERE i.feed_id = :feed_id AND it.user_id = :user_id " .
"GROUP BY t.tag_id";
$statement = $fof_connection->prepare($query);
$statement->bindValue(':user_id', $user_id);
$statement->bindValue(':feed_id', $feed_id);
$result = $statement->execute();
while (($row = fof_db_get_row($statement)) !== false) {
$counts[$row['tag_name']] = $row['tag_count'];
if (!in_array($row['tag_name'], $system_tags)) {
$tagged += $row['tag_count'];
}
}
return array($total, $tagged, $counts);
}
/** Return a row iterator of either all subscribed feeds for the user, or only
those subscribed feeds which are due for updating.
NOTE: Caller must invoke fof_db_subscription_feed_fix() on rows, to unpack
subscription preferences into feed data.
*/
function fof_db_get_subscriptions($user_id, $dueOnly = false) {
global $FOF_FEED_TABLE, $FOF_SUBSCRIPTION_TABLE;
global $fof_connection;
fof_trace();
$query = "SELECT * FROM $FOF_FEED_TABLE f, $FOF_SUBSCRIPTION_TABLE s WHERE s.user_id = :user_id AND f.feed_id = s.feed_id";
if ($dueOnly) {
$query .= " AND feed_cache_next_attempt < :now";
}
$query .= " ORDER BY feed_title";
$statement = $fof_connection->prepare($query);
$statement->bindValue(':user_id', $user_id);
if ($dueOnly) {
$statement->bindValue(':now', time());
}
$result = $statement->execute();
return $statement;
}
/** Fix subscription preferences of any array containing feed data, in-place,
to convert the user's feed preferences, if present, into usable entries.
Array should contain result of querying
FOF_FEED_TABLE.*,FOF_SUBSCRIPTION_TABLE.subscription_prefs
but will initialize entries to null if not present.
*/
function fof_db_subscription_feed_fix(&$f) {
if (!empty($f['subscription_prefs'])) {
$f['subscription_prefs'] = unserialize($f['subscription_prefs']);
}
if (empty($f['subscription_prefs'])) {
$f['subscription_prefs'] = array();
}
if (empty($f['subscription_prefs']['tags'])) {
$f['subscription_prefs']['tags'] = array();
}
$f['alt_title'] = empty($f['subscription_prefs']['alt_title']) ? null : $f['subscription_prefs']['alt_title'];
$f['alt_image'] = empty($f['subscription_prefs']['alt_image']) ? null : $f['subscription_prefs']['alt_image'];
$f['display_title'] = (!empty($f['alt_title'])) ? $f['alt_title'] : $f['feed_title'];
$f['display_image'] = (!empty($f['alt_image'])) ? $f['alt_image'] : $f['feed_image'];
}
/** Return an array of all the data which defines a feed.
NOTE: This calls fof_db_subscription_feed_fix, so caller doesn't have to.
*/
function fof_db_subscription_feed_get($user_id, $feed_id) {
global $FOF_FEED_TABLE, $FOF_SUBSCRIPTION_TABLE;
global $fof_connection;
fof_trace();
$query = "SELECT f.*, s.subscription_prefs FROM $FOF_FEED_TABLE f, $FOF_SUBSCRIPTION_TABLE s WHERE s.user_id = :user_id AND s.feed_id = :feed_id AND s.feed_id = f.feed_id";
$statement = $fof_connection->prepare($query);
$statement->bindValue(':user_id', $user_id);
$statement->bindValue(':feed_id', $feed_id);
$result = $statement->execute();
$r = fof_db_get_row($statement, NULL, TRUE);
fof_db_subscription_feed_fix($r);
return $r;
}
/** Return a row iterator of feeds due for updating.
*/
function fof_db_get_feeds_needing_attempt() {
global $FOF_FEED_TABLE;
global $fof_connection;
fof_trace();
$query = "SELECT * FROM $FOF_FEED_TABLE WHERE feed_cache_next_attempt < :now";
$statement = $fof_connection->prepare($query);
$statement->bindValue(':now', time());
$result = $statement->execute();
return $statement;
}
/** Return a row iterator which contains a list of feed_ids and the count of
items in each feed which match the specified parameters.
*/
function fof_db_get_item_count($user_id, $what = 'all', $feed_id = NULL, $search = NULL) {
global $FOF_FEED_TABLE, $FOF_ITEM_TABLE, $FOF_TAG_TABLE, $FOF_ITEM_TAG_TABLE, $FOF_SUBSCRIPTION_TABLE;
global $fof_connection;
$what_q = array();
fof_trace();
/* FIXME: confusing */
if ($what == 'starred') {
$what = 'star';
}
/* TODO: global $system_tags or something */
foreach (explode(' ', ($what == 'tagged') ? 'unread star folded' : $what) as $w) {
$what_q[] = $fof_connection->quote($w);
}
/*
First, generate a query which will return rows of feed_id,item_id for
every item matching specifications.
NOTE: would have used more bound parameters in here, but for some
reason, queries were not working with them, so they've been stripped
out and replaced with inline values.
*/
if ($what == 'all') {
$query = "SELECT i.feed_id, i.item_id FROM $FOF_ITEM_TABLE i" .
" JOIN $FOF_SUBSCRIPTION_TABLE s ON s.feed_id = i.feed_id" .
" WHERE s.user_id = " . $user_id;
} else {
$query = "SELECT DISTINCT s.feed_id, i.item_id" .
" FROM $FOF_SUBSCRIPTION_TABLE s" .
" JOIN $FOF_ITEM_TABLE i ON i.feed_id = s.feed_id" .
" JOIN $FOF_ITEM_TAG_TABLE it ON it.item_id = i.item_id AND it.user_id = " . $user_id .
" JOIN $FOF_TAG_TABLE t ON t.tag_id = it.tag_id" .
" WHERE s.user_id = " . $user_id;
}
if (!empty($feed_id)) {
$query .= " AND i.feed_id = :feed_id";
}
if (!empty($search)) {
$query .= " AND (i.item_title LIKE :search OR i.item_content LIKE :search)";
}
switch ($what) {
case 'all':
/* Every item. */
break;
case 'tagged':
/* Item must be tagged, but not by any system tag. */
$query .= " AND t.tag_name NOT IN (" . implode(',', $what_q) . ") GROUP BY it.item_id";
break;
default:
/* Item must have all tags. */
if (!empty($what_q)) {
$query .= " AND t.tag_name IN (" . implode(',', $what_q) . ")";
$query .= " GROUP BY it.item_id HAVING count(it.item_id) = " . count($what_q);
}
}
/* Now tally the item results by feed_id. */
$count_query = 'SELECT feed_id, count(*) AS count FROM (' . $query . ') AS matched_items GROUP BY feed_id';
$statement = $fof_connection->prepare($count_query);
if (!empty($feed_id)) {
$statement->bindValue(':feed_id', $feed_id);
}
if (!empty($search)) {
$statement->bindValue(':search', '%' . $search . '%');
}
$result = $statement->execute();
return $statement;
}
function fof_db_get_subscribed_users($feed_id) {
global $FOF_SUBSCRIPTION_TABLE;
global $fof_connection;
fof_trace();
$query = "SELECT user_id FROM $FOF_SUBSCRIPTION_TABLE WHERE feed_id = :feed_id";
$statement = $fof_connection->prepare($query);
$statement->bindValue(':feed_id', $feed_id);
$result = $statement->execute();
return $statement;
}
function fof_db_get_subscribed_users_count($feed_id) {
fof_trace();
$sub_statement = fof_db_get_subscribed_users($feed_id);
$subscribed_users = $sub_statement->fetchAll();
return count($subscribed_users);
}
function fof_db_is_subscribed($user_id, $feed_url) {
global $FOF_FEED_TABLE, $FOF_SUBSCRIPTION_TABLE;
global $fof_connection;
fof_trace();
$query = "SELECT s.feed_id FROM $FOF_SUBSCRIPTION_TABLE s, $FOF_FEED_TABLE f" .
" WHERE f.feed_url = :feed_url" .
" AND f.feed_id = s.feed_id" .
" AND s.user_id = :user_id;";
$statement = $fof_connection->prepare($query);
$statement->bindValue(':feed_url', $feed_url);
$statement->bindValue(':user_id', $user_id);
$result = $statement->execute();
$row = fof_db_get_row($statement, NULL, TRUE);
if (!empty($row)) {
return true;
}
return false;
}
function fof_db_is_subscribed_id($user_id, $feed_id) {
global $FOF_SUBSCRIPTION_TABLE;
global $fof_connection;
fof_trace();
$query = "SELECT feed_id FROM $FOF_SUBSCRIPTION_TABLE WHERE feed_id = :feed_id AND user_id = :user_id;";
$statement = $fof_connection->prepare($query);
$statement->bindValue(':feed_id', $feed_id);
$statement->bindValue(':user_id', $user_id);
$result = $statement->execute();
$row = fof_db_get_row($statement, NULL, TRUE);
if (!empty($row)) {
return true;
}
return false;
}
function fof_db_get_feed_by_url($feed_url) {
global $FOF_FEED_TABLE;
global $fof_connection;
fof_trace();
$query = "SELECT * FROM $FOF_FEED_TABLE WHERE feed_url = :feed_url";
$statement = $fof_connection->prepare($query);
$statement->bindValue(':feed_url', $feed_url);
$result = $statement->execute();
return fof_db_get_row($statement, NULL, TRUE);
}
function fof_db_get_feed_by_id($feed_id) {
global $FOF_FEED_TABLE;
global $fof_connection;
fof_trace();
$query = "SELECT * FROM $FOF_FEED_TABLE WHERE feed_id = :feed_id";
$statement = $fof_connection->prepare($query);
$statement->bindValue(':feed_id', $feed_id);
$result = $statement->execute();
return fof_db_get_row($statement, NULL, TRUE);
}
function fof_db_add_feed($url, $title, $link, $description) {
global $FOF_FEED_TABLE;
global $fof_connection;
fof_trace();
/* FIXME: just store these as empty here */
if (empty($title)) {
$title = "[no title]";
}
if (empty($link)) {
$link = "[no link]";
}
if (empty($description)) {
$description = "[no description]";
}
$query = "INSERT INTO $FOF_FEED_TABLE (feed_url, feed_title, feed_link, feed_description) VALUES (:url, :title, :link, :description)";
$statement = $fof_connection->prepare($query);
$statement->bindValue(':url', $url);
$statement->bindValue(':title', $title);
$statement->bindValue(':link', $link);
$statement->bindValue(':description', $description);
$result = $statement->execute();
$statement->closeCursor();
$feed_id = $fof_connection->lastInsertId();
return $feed_id;
}
function fof_db_add_subscription($user_id, $feed_id) {
global $FOF_SUBSCRIPTION_TABLE;
global $fof_connection;
fof_trace();
$query = "INSERT INTO $FOF_SUBSCRIPTION_TABLE ( feed_id, user_id, subscription_added ) VALUES ( :feed_id, :user_id, :now )";
$statement = $fof_connection->prepare($query);
$statement->bindValue(':feed_id', $feed_id);
$statement->bindValue(':user_id', $user_id);
$statement->bindValue(':now', time());
$result = $statement->execute();
$statement->closeCursor();
}
function fof_db_delete_subscription($user_id, $feed_id) {
global $FOF_SUBSCRIPTION_TABLE, $FOF_ITEM_TAG_TABLE;
global $fof_connection;
fof_trace();
$all_items = fof_db_get_items($user_id, $feed_id, "all");
$items_q = array();
foreach ($all_items as $i) {
$items_q[] = $fof_connection->quote($i['item_id']);
}
if (count($items_q) > 0) {
$query = "DELETE FROM $FOF_ITEM_TAG_TABLE WHERE user_id = :user_id AND item_id IN ( " . (count($items_q) ? implode(', ', $items_q) : "''") . " )";
$statement = $fof_connection->prepare($query);
$statement->bindValue(':user_id', $user_id);
$result = $statement->execute();
$statement->closeCursor();
}
$query = "DELETE FROM $FOF_SUBSCRIPTION_TABLE WHERE feed_id = :feed_id and user_id = :user_id";
$statement = $fof_connection->prepare($query);
$statement->bindValue(':user_id', $user_id);
$statement->bindValue(':feed_id', $feed_id);
$result = $statement->execute();
$statement->closeCursor();
}
function fof_db_delete_feed($feed_id) {
global $FOF_FEED_TABLE, $FOF_ITEM_TABLE;
global $fof_connection;
fof_trace();
$query = "DELETE FROM $FOF_FEED_TABLE WHERE feed_id = :feed_id";
$statement = $fof_connection->prepare($query);
$statement->bindValue(':feed_id', $feed_id);
$result = $statement->execute();
$statement->closeCursor();
$query = "DELETE FROM $FOF_ITEM_TABLE WHERE feed_id = :feed_id";
$statement = $fof_connection->prepare($query);
$statement->bindValue(':feed_id', $feed_id);
$result = $statement->execute();
$statement->closeCursor();
}
function fof_db_feed_cache_set($feed_id, $next_attempt) {
global $FOF_FEED_TABLE;
global $fof_connection;
fof_trace();
$query = "UPDATE $FOF_FEED_TABLE SET feed_cache_next_attempt = :next_attempt WHERE feed_id = :feed_id";
$statement = $fof_connection->prepare($query);
$statement->bindValue(":feed_id", $feed_id);
$statement->bindValue(":next_attempt", $next_attempt);
$result = $statement->execute();
$statement->closeCursor();
}
/** Return an array representing a feed's activity.
Array contains the number of items added to the feed, indexed by days-ago.
FIXME: there probably ought to be an offset to midnight or something in
here somewhere, but it's just a rough overview
*/
function fof_db_feed_history($feed_id) {
global $FOF_ITEM_TABLE;
global $fof_connection;
$history = array();
$today_day = floor(time() / (60 * 60 * 24));
$query = "SELECT item_updated FROM $FOF_ITEM_TABLE WHERE feed_id = :feed_id";
$statement = $fof_connection->prepare($query);
$statement->bindValue(':feed_id', $feed_id);
$statement->execute();
while (($item_updated = fof_db_get_row($statement, 'item_updated')) !== false) {
$item_day = floor($item_updated / (60 * 60 * 24));
$days_ago = $today_day - $item_day;
if (empty($history[$days_ago])) {
$history[$days_ago] = 1;
} else {
$history[$days_ago]++;
}
}
end($history);
$last = key($history);
reset($history);
if (!empty($last)) {
$history = array_merge(array_fill(0, $last, 0), $history);
}
return $history;
}
////////////////////////////////////////////////////////////////////////////////
// Item level stuff
////////////////////////////////////////////////////////////////////////////////
function fof_db_find_item($feed_id, $item_guid) {
global $FOF_ITEM_TABLE;
global $fof_connection;
fof_trace();
$query = "SELECT item_id FROM $FOF_ITEM_TABLE WHERE feed_id = :feed_id and item_guid = :item_guid";
$statement = $fof_connection->prepare($query);
$statement->bindValue(":feed_id", $feed_id);
$statement->bindValue(":item_guid", $item_guid);
$result = $statement->execute();
return fof_db_get_row($statement, 'item_id', TRUE);
}
function fof_db_add_item($item_id, $feed_id, $guid, $link, $title, $content, $cached, $itemdate, $author) {
global $FOF_ITEM_TABLE;
global $fof_connection;
fof_trace();
/* last-ditch enforce constraints */
if (is_null($link)) {
$link = '';
}
if (is_null($guid)) {
$guid = '';
}
if (is_null($title)) {
$title = '';
}
if (is_null($content)) {
$content = '';
}
if (is_null($cached)) {
$cached = 0;
}
if (is_null($itemdate)) {
$itemdate = 0;
}
if ($item_id == NULL) {
$query = "INSERT INTO $FOF_ITEM_TABLE
(feed_id, item_link, item_guid, item_title, item_content, item_cached, item_published, item_updated, item_author)
VALUES
(:feed_id, :link, :guid, :title, :content, :cached, :itemdate, :itemdate, :author)";
$statement = $fof_connection->prepare($query);
$statement->bindValue(':feed_id', $feed_id);
$statement->bindValue(':link', $link);
$statement->bindValue(':guid', $guid);
$statement->bindValue(':title', $title);
$statement->bindValue(':content', $content);
$statement->bindValue(':cached', $cached);
$statement->bindValue(':itemdate', $itemdate);
$statement->bindValue(':author', $author);
} else {
$query = "UPDATE $FOF_ITEM_TABLE SET
item_link = :link,
item_guid = :guid,
item_title = :title,
item_content = :content,
item_cached = :cached,
item_updated = :itemdate,
item_author = :author
WHERE feed_id = :feed_id and item_id = :item_id";
$statement = $fof_connection->prepare($query);
$statement->bindValue(':link', $link);
$statement->bindValue(':guid', $guid);
$statement->bindValue(':title', $title);
$statement->bindValue(':content', $content);
$statement->bindValue(':cached', $cached);
$statement->bindValue(':itemdate', $itemdate);
$statement->bindValue(':author', $author);
$statement->bindValue(':feed_id', $feed_id);
$statement->bindValue(':item_id', $item_id);
}
$result = $statement->execute();
$statement->closeCursor();
if ($item_id == NULL) {
$item_id = $fof_connection->lastInsertId();
}
return $item_id;
}
// returns whether the item_complete flag has been set
function fof_db_item_get_complete($item_id) {
global $FOF_ITEM_TABLE;
global $fof_connection;
$query = "SELECT item_complete FROM $FOF_ITEM_TABLE WHERE item_id = :item_id";
$statement = $fof_connection->prepare($query);
$statement->bindValue(":item_id", $item_id);
$result = $statement->execute();
return fof_db_get_row($statement, 'item_complete', TRUE);
}
// sets the item_complete flag on an item
function fof_db_item_set_complete($item_id) {
global $FOF_ITEM_TABLE;
global $fof_connection;
$query = "UPDATE $FOF_ITEM_TABLE SET item_complete = 1 WHERE item_id = :item_id";
$statement = $fof_connection->prepare($query);
$statement->bindValue(":item_id", $item_id);
$result = $statement->execute();
}
/* when: Y/m/d or 'today' */
function fof_db_get_items($user_id = 1, $feed = NULL, $what = 'unread', $when = NULL, $start = NULL, $limit = NULL, $order = 'desc', $search = NULL) {
global $FOF_SUBSCRIPTION_TABLE, $FOF_FEED_TABLE, $FOF_ITEM_TABLE, $FOF_ITEM_TAG_TABLE, $FOF_TAG_TABLE;
global $fof_connection;
$all_items = array();
if ($order != 'asc' && $order != 'desc') {
$order = 'desc';
}
fof_trace();
$prefs = fof_prefs();
$select = "SELECT i.*, f.*, s.subscription_prefs";
$from = " FROM $FOF_FEED_TABLE f, $FOF_ITEM_TABLE i, $FOF_SUBSCRIPTION_TABLE s";
if ($what != 'all') {
$from .= ", $FOF_TAG_TABLE t, $FOF_ITEM_TAG_TABLE it";
}
$where = " WHERE s.user_id = " . $fof_connection->quote($user_id) . " AND s.feed_id = f.feed_id AND f.feed_id = i.feed_id";
if (!empty($feed)) {
$where .= " AND f.feed_id = " . $fof_connection->quote($feed);
}
if (!empty($when)) {
$tzoffset = isset($prefs['tzoffset']) ? $prefs['tzoffset'] : 0;
$whendate = explode('/', ($when == 'today') ? fof_todays_date() : $when);
$when_begin = gmmktime(0, 0, 0, $whendate[1], $whendate[2], $whendate[0]) - ($tzoffset * 60 * 60);
$when_end = $when_begin + (24 * 60 * 60);
$where .= " AND i.item_published > " . $fof_connection->quote($when_begin) . " AND i.item_published < " . $fof_connection->quote($when_end);
}
if ($what != 'all') {
$tags_q = array();
foreach (explode(' ', $what) as $tag) {
$tags_q[] = $fof_connection->quote($tag);
}
$where .= " AND it.user_id = s.user_id AND it.tag_id = t.tag_id AND i.item_id = it.item_id AND t.tag_name IN (" . (count($tags_q) ? implode(', ', $tags_q) : "''") . ")";
}
if ($what == 'all') {
$group = "";
} else {
$group = " GROUP BY i.item_id HAVING COUNT( i.item_id ) = " . count($tags_q);
}
if (!empty($search)) {
$search_q = $fof_connection->quote('%' . $search . '%');
$where .= " AND (i.item_title LIKE $search_q OR i.item_content LIKE $search_q )";
}
$order_by = " ORDER BY i.item_published " . strtoupper($order);
if (is_numeric($start)) {
$order_by .= " LIMIT " . $start . ", " . ((is_numeric($limit)) ? $limit : $prefs['howmany']);
}
$query = $select . $from . $where . $group . $order_by;
// fof_log(__FUNCTION__ . " first query: " . $query);
$statement = $fof_connection->prepare($query);
$result = $statement->execute();
$item_ids_q = array();
$lookup = array(); /* remember item_id->all_rows mapping, for populating tags */
$idx = 0;
while (($row = fof_db_get_row($statement)) !== FALSE) {
fof_trace("collecting item_id:" . $row['item_id'] . " idx:$idx");
fof_db_subscription_feed_fix($row); /* feed prefs are included, so decode them */
$item_ids_q[] = $fof_connection->quote($row['item_id']);
$lookup[$row['item_id']] = $idx;
$all_items[$idx] = $row;
$all_items[$idx]['tags'] = array();
$idx += 1;
}
$all_items = fof_multi_sort($all_items, 'item_published', $order != "asc");
$query = "SELECT t.tag_name, it.item_id" .
" FROM $FOF_TAG_TABLE t, $FOF_ITEM_TAG_TABLE it" .
" WHERE t.tag_id = it.tag_id" .
" AND it.item_id IN (" . (count($item_ids_q) ? implode(',', $item_ids_q) : "''") . ")" .
" AND it.user_id = " . $fof_connection->quote($user_id);
// fof_log(__FUNCTION__ . " second query: " . $query);
fof_trace('item_ids_q:' . implode(',', $item_ids_q));
$statement = $fof_connection->prepare($query);
$result = $statement->execute();