summaryrefslogtreecommitdiff
path: root/app/Models/EntryDAO.php
blob: 3f3cc478b22c9453e5cd90a682e4d3c8b511aafe (plain)
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
<?php

class FreshRSS_EntryDAO extends Minz_ModelPdo {
	public function addEntry ($valuesTmp) {
		$sql = 'INSERT INTO `' . $this->prefix . 'entry`(id, guid, title, author, content_bin, link, date, is_read, is_favorite, id_feed, tags) '
		     . 'VALUES(?, ?, ?, ?, COMPRESS(?), ?, ?, ?, ?, ?, ?)';
		$stm = $this->bd->prepare ($sql);

		$values = array (
			$valuesTmp['id'],
			substr($valuesTmp['guid'], 0, 760),
			substr($valuesTmp['title'], 0, 255),
			substr($valuesTmp['author'], 0, 255),
			$valuesTmp['content'],
			substr($valuesTmp['link'], 0, 1023),
			$valuesTmp['date'],
			$valuesTmp['is_read'] ? 1 : 0,
			$valuesTmp['is_favorite'] ? 1 : 0,
			$valuesTmp['id_feed'],
			substr($valuesTmp['tags'], 0, 1023),
		);

		if ($stm && $stm->execute ($values)) {
			return $this->bd->lastInsertId();
		} else {
			$info = $stm->errorInfo();
			if ((int)($info[0] / 1000) !== 23) {	//Filter out "SQLSTATE Class code 23: Constraint Violation" because of expected duplicate entries
				Minz_Log::record ('SQL error ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
				. ' while adding entry in feed ' . $valuesTmp['id_feed'] . ' with title: ' . $valuesTmp['title'], Minz_Log::ERROR);
			} /*else {
				Minz_Log::record ('SQL error ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
				. ' while adding entry in feed ' . $valuesTmp['id_feed'] . ' with title: ' . $valuesTmp['title'], Minz_Log::DEBUG);
			}*/
			return false;
		}
	}

	public function addEntryObject($entry, $conf, $feedHistory) {
		$existingGuids = array_fill_keys(
			$this->listLastGuidsByFeed($entry->feed(), 20), 1
		);

		$nb_month_old = max($conf->old_entries, 1);
		$date_min = time() - (3600 * 24 * 30 * $nb_month_old);

		$eDate = $entry->date(true);

		if ($feedHistory == -2) {
			$feedHistory = $conf->keep_history_default;
		}

		if (!isset($existingGuids[$entry->guid()]) &&
				($feedHistory != 0 || $eDate  >= $date_min)) {
			$values = $entry->toArray();

			$useDeclaredDate = empty($existingGuids);
			$values['id'] = ($useDeclaredDate || $eDate < $date_min) ?
				min(time(), $eDate) . uSecString() :
				uTimeString();

			return $this->addEntry($values);
		}

		// We don't return Entry object to avoid a research in DB
		return -1;
	}

	public function markFavorite($ids, $is_favorite = true) {
		if (!is_array($ids)) {
			$ids = array($ids);
		}
		$sql = 'UPDATE `' . $this->prefix . 'entry` e '
		     . 'SET e.is_favorite = ? '
		     . 'WHERE e.id IN (' . str_repeat('?,', count($ids) - 1). '?)';
		$values = array ($is_favorite ? 1 : 0);
		$values = array_merge($values, $ids);
		$stm = $this->bd->prepare ($sql);
		if ($stm && $stm->execute ($values)) {
			return $stm->rowCount();
		} else {
			$info = $stm->errorInfo();
			Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
			return false;
		}
	}

	public function markRead($ids, $is_read = true) {
		if (is_array($ids)) {
			if (count($ids) < 6) {	//Speed heuristics
				$affected = 0;
				foreach ($ids as $id) {
					$affected += $this->markRead($id, $is_read);
				}
				return $affected;
			}

			$this->bd->beginTransaction();
			$sql = 'UPDATE `' . $this->prefix . 'entry` e '
				 . 'SET e.is_read = ? '
				 . 'WHERE e.id IN (' . str_repeat('?,', count($ids) - 1). '?)';
			$values = array($is_read ? 1 : 0);
			$values = array_merge($values, $ids);
			$stm = $this->bd->prepare($sql);
			if (!($stm && $stm->execute($values))) {
				$info = $stm->errorInfo();
				Minz_Log::record('SQL error : ' . $info[2], Minz_Log::ERROR);
				$this->bd->rollBack();
				return false;
			}
			$affected = $stm->rowCount();

			if ($affected > 0) {
				$sql = 'UPDATE `' . $this->prefix . 'feed` f '
				     . 'INNER JOIN ('
				     .	'SELECT e.id_feed, '
				     .	'COUNT(CASE WHEN e.is_read = 0 THEN 1 END) AS nbUnreads, '
				     .	'COUNT(e.id) AS nbEntries '
				     .	'FROM `' . $this->prefix . 'entry` e '
				     .	'GROUP BY e.id_feed'
				     . ') x ON x.id_feed=f.id '
				     . 'SET f.cache_nbEntries=x.nbEntries, f.cache_nbUnreads=x.nbUnreads';
				$stm = $this->bd->prepare($sql);
				if (!($stm && $stm->execute())) {
					$info = $stm->errorInfo();
					Minz_Log::record('SQL error : ' . $info[2], Minz_Log::ERROR);
					$this->bd->rollBack();
					return false;
				}
			}

			$this->bd->commit();
			return $affected;
		} else {
			$sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
				 . 'SET e.is_read = ?,'
				 . 'f.cache_nbUnreads=f.cache_nbUnreads' . ($is_read ? '-' : '+') . '1 '
				 . 'WHERE e.id=?';
			$values = array($is_read ? 1 : 0, $ids);
			$stm = $this->bd->prepare($sql);
			if ($stm && $stm->execute($values)) {
				return $stm->rowCount();
			} else {
				$info = $stm->errorInfo();
				Minz_Log::record('SQL error : ' . $info[2], Minz_Log::ERROR);
				return false;
			}
		}
	}

	public function markReadEntries ($idMax = 0, $onlyFavorites = false, $priorityMin = 0) {
		if ($idMax == 0) {
			$sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
			     . 'SET e.is_read = 1, f.cache_nbUnreads=0 '
			     . 'WHERE e.is_read = 0';
			if ($onlyFavorites) {
				$sql .= ' AND e.is_favorite = 1';
			} elseif ($priorityMin >= 0) {
				$sql .= ' AND f.priority > ' . intval($priorityMin);
			}
			$stm = $this->bd->prepare ($sql);
			if ($stm && $stm->execute ()) {
				return $stm->rowCount();
			} else {
				$info = $stm->errorInfo();
				Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
				return false;
			}
		} else {
			$this->bd->beginTransaction ();

			$sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
			     . 'SET e.is_read = 1 '
			     . 'WHERE e.is_read = 0 AND e.id <= ?';
			if ($onlyFavorites) {
				$sql .= ' AND e.is_favorite = 1';
			} elseif ($priorityMin >= 0) {
				$sql .= ' AND f.priority > ' . intval($priorityMin);
			}
			$values = array ($idMax);
			$stm = $this->bd->prepare ($sql);
			if (!($stm && $stm->execute ($values))) {
				$info = $stm->errorInfo();
				Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
				$this->bd->rollBack ();
				return false;
			}
			$affected = $stm->rowCount();

			if ($affected > 0) {
				$sql = 'UPDATE `' . $this->prefix . 'feed` f '
			     . 'LEFT OUTER JOIN ('
			     .	'SELECT e.id_feed, '
			     .	'COUNT(*) AS nbUnreads '
			     .	'FROM `' . $this->prefix . 'entry` e '
			     .	'WHERE e.is_read = 0 '
			     .	'GROUP BY e.id_feed'
			     . ') x ON x.id_feed=f.id '
			     . 'SET f.cache_nbUnreads=COALESCE(x.nbUnreads, 0)';
				$stm = $this->bd->prepare ($sql);
				if (!($stm && $stm->execute ())) {
					$info = $stm->errorInfo();
					Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
					$this->bd->rollBack ();
					return false;
				}
			}

			$this->bd->commit ();
			return $affected;
		}
	}

	public function markReadCat ($id, $idMax = 0) {
		if ($idMax == 0) {
			$sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
			     . 'SET e.is_read = 1, f.cache_nbUnreads=0 '
			     . 'WHERE f.category = ? AND e.is_read = 0';
			$values = array ($id);
			$stm = $this->bd->prepare ($sql);
			if ($stm && $stm->execute ($values)) {
				return $stm->rowCount();
			} else {
				$info = $stm->errorInfo();
				Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
				return false;
			}
		} else {
			$this->bd->beginTransaction ();

			$sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
			     . 'SET e.is_read = 1 '
			     . 'WHERE f.category = ? AND e.is_read = 0 AND e.id <= ?';
			$values = array ($id, $idMax);
			$stm = $this->bd->prepare ($sql);
			if (!($stm && $stm->execute ($values))) {
				$info = $stm->errorInfo();
				Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
				$this->bd->rollBack ();
				return false;
			}
			$affected = $stm->rowCount();

			if ($affected > 0) {
				$sql = 'UPDATE `' . $this->prefix . 'feed` f '
			     . 'LEFT OUTER JOIN ('
			     .	'SELECT e.id_feed, '
			     .	'COUNT(*) AS nbUnreads '
			     .	'FROM `' . $this->prefix . 'entry` e '
			     .	'WHERE e.is_read = 0 '
			     .	'GROUP BY e.id_feed'
			     . ') x ON x.id_feed=f.id '
			     . 'SET f.cache_nbUnreads=COALESCE(x.nbUnreads, 0) '
			     . 'WHERE f.category = ?';
				$values = array ($id);
				$stm = $this->bd->prepare ($sql);
				if (!($stm && $stm->execute ($values))) {
					$info = $stm->errorInfo();
					Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
					$this->bd->rollBack ();
					return false;
				}
			}

			$this->bd->commit ();
			return $affected;
		}
	}

	public function markReadCatName($name, $idMax = 0) {
		if ($idMax == 0) {
			$sql = 'UPDATE `' . $this->prefix . 'entry` e '
			     . 'INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
			     . 'INNER JOIN `' . $this->prefix . 'category` c ON c.id = f.category '
			     . 'SET e.is_read = 1, f.cache_nbUnreads=0 '
			     . 'WHERE c.name = ?';
			$values = array($name);
			$stm = $this->bd->prepare($sql);
			if ($stm && $stm->execute($values)) {
				return $stm->rowCount();
			} else {
				$info = $stm->errorInfo();
				Minz_Log::record('SQL error : ' . $info[2], Minz_Log::ERROR);
				return false;
			}
		} else {
			$this->bd->beginTransaction();

			$sql = 'UPDATE `' . $this->prefix . 'entry` e '
			     . 'INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
			     . 'INNER JOIN `' . $this->prefix . 'category` c ON c.id = f.category '
			     . 'SET e.is_read = 1 '
			     . 'WHERE c.name = ? AND e.id <= ?';
			$values = array($name, $idMax);
			$stm = $this->bd->prepare($sql);
			if (!($stm && $stm->execute($values))) {
				$info = $stm->errorInfo();
				Minz_Log::record('SQL error : ' . $info[2], Minz_Log::ERROR);
				$this->bd->rollBack();
				return false;
			}
			$affected = $stm->rowCount();

			if ($affected > 0) {
				$sql = 'UPDATE `' . $this->prefix . 'feed` f '
			     . 'LEFT OUTER JOIN ('
			     .	'SELECT e.id_feed, '
			     .	'COUNT(*) AS nbUnreads '
			     .	'FROM `' . $this->prefix . 'entry` e '
			     .	'WHERE e.is_read = 0 '
			     .	'GROUP BY e.id_feed'
			     . ') x ON x.id_feed=f.id '
			     . 'INNER JOIN `' . $this->prefix . 'category` c ON c.id = f.category '
			     . 'SET f.cache_nbUnreads=COALESCE(x.nbUnreads, 0) '
			     . 'WHERE c.name = ?';
				$values = array($name);
				$stm = $this->bd->prepare($sql);
				if (!($stm && $stm->execute($values))) {
					$info = $stm->errorInfo();
					Minz_Log::record('SQL error : ' . $info[2], Minz_Log::ERROR);
					$this->bd->rollBack();
					return false;
				}
			}

			$this->bd->commit();
			return $affected;
		}
	}

	public function markReadFeed ($id, $idMax = 0) {
		if ($idMax == 0) {
			$sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
			     . 'SET e.is_read = 1, f.cache_nbUnreads=0 '
			     . 'WHERE f.id=? AND e.is_read = 0';
			$values = array ($id);
			$stm = $this->bd->prepare ($sql);
			if ($stm && $stm->execute ($values)) {
				return $stm->rowCount();
			} else {
				$info = $stm->errorInfo();
				Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
				return false;
			}
		} else {
			$this->bd->beginTransaction ();

			$sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
			     . 'SET e.is_read = 1 '
			     . 'WHERE f.id=? AND e.is_read = 0 AND e.id <= ?';
			$values = array ($id, $idMax);
			$stm = $this->bd->prepare ($sql);
			if (!($stm && $stm->execute ($values))) {
				$info = $stm->errorInfo();
				Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
				$this->bd->rollBack ();
				return false;
			}
			$affected = $stm->rowCount();

			if ($affected > 0) {
				$sql = 'UPDATE `' . $this->prefix . 'feed` f '
				     . 'SET f.cache_nbUnreads=f.cache_nbUnreads-' . $affected
				     . ' WHERE f.id=?';
				$values = array ($id);
				$stm = $this->bd->prepare ($sql);
				if (!($stm && $stm->execute ($values))) {
					$info = $stm->errorInfo();
					Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
					$this->bd->rollBack ();
					return false;
				}
			}

			$this->bd->commit ();
			return $affected;
		}
	}

	public function searchByGuid ($feed_id, $id) {
		// un guid est unique pour un flux donné
		$sql = 'SELECT id, guid, title, author, UNCOMPRESS(content_bin) AS content, link, date, is_read, is_favorite, id_feed, tags '
		     . 'FROM `' . $this->prefix . 'entry` WHERE id_feed=? AND guid=?';
		$stm = $this->bd->prepare ($sql);

		$values = array (
			$feed_id,
			$id
		);

		$stm->execute ($values);
		$res = $stm->fetchAll (PDO::FETCH_ASSOC);
		$entries = self::daoToEntry ($res);
		return isset ($entries[0]) ? $entries[0] : null;
	}

	public function searchById ($id) {
		$sql = 'SELECT id, guid, title, author, UNCOMPRESS(content_bin) AS content, link, date, is_read, is_favorite, id_feed, tags '
		     . 'FROM `' . $this->prefix . 'entry` WHERE id=?';
		$stm = $this->bd->prepare ($sql);

		$values = array ($id);

		$stm->execute ($values);
		$res = $stm->fetchAll (PDO::FETCH_ASSOC);
		$entries = self::daoToEntry ($res);
		return isset ($entries[0]) ? $entries[0] : null;
	}

	private function sqlListWhere($type = 'a', $id = '', $state = null , $order = 'DESC', $limit = 1, $firstId = '', $filter = '', $date_min = 0, $showOlderUnreadsorFavorites = false, $keepHistoryDefault = 0) {
		if (!$state) {
			$state = FreshRSS_Configuration::STATE_ALL;
		}
		$where = '';
		$joinFeed = false;
		$values = array();
		switch ($type) {
			case 'a':
				$where .= 'f.priority > 0 ';
				$joinFeed = true;
				break;
			case 's':
				// This is deprecated. The favorite button does not exist anymore
				$where .= 'e1.is_favorite = 1 ';
				break;
			case 'c':
				$where .= 'f.category = ? ';
				$values[] = intval($id);
				$joinFeed = true;
				break;
			case 'f':
				$where .= 'e1.id_feed = ? ';
				$values[] = intval($id);
				break;
			case 'A':
				$where .= '1 ';
				break;
			default:
				throw new FreshRSS_EntriesGetter_Exception ('Bad type in Entry->listByType: [' . $type . ']!');
		}

		if ($state & FreshRSS_Configuration::STATE_NOT_READ) {
			if (!($state & FreshRSS_Configuration::STATE_READ)) {
				$where .= 'AND e1.is_read = 0 ';
			}
		}
		if ($state & FreshRSS_Configuration::STATE_READ) {
			if (!($state & FreshRSS_Configuration::STATE_NOT_READ)) {
				$where .= 'AND e1.is_read = 1 ';
			}
		}
		if ($state & FreshRSS_Configuration::STATE_NOT_FAVORITE) {
			if (!($state & FreshRSS_Configuration::STATE_FAVORITE)) {
				$where .= 'AND e1.is_favorite = 0 ';
			}
		}
		if ($state & FreshRSS_Configuration::STATE_FAVORITE) {
			if (!($state & FreshRSS_Configuration::STATE_NOT_FAVORITE)) {
				$where .= 'AND e1.is_favorite = 1 ';
			}
		}

		switch ($order) {
			case 'DESC':
			case 'ASC':
				break;
			default:
				throw new FreshRSS_EntriesGetter_Exception ('Bad order in Entry->listByType: [' . $order . ']!');
		}
		if ($firstId !== '') {
			$where .= 'AND e1.id ' . ($order === 'DESC' ? '<=' : '>=') . $firstId . ' ';
		}
		if (($date_min > 0) && ($type !== 's')) {
			$where .= 'AND (e1.id >= ' . $date_min . '000000';
			if ($showOlderUnreadsorFavorites) {	//Lax date constraint
				$where .= ' OR e1.is_read = 0 OR e1.is_favorite = 1 OR (f.keep_history <> 0';
				if (intval($keepHistoryDefault) === 0) {
					$where .= ' AND f.keep_history <> -2';	//default
				}
				$where .= ')';
			}
			$where .= ') ';
			$joinFeed = true;
		}
		$search = '';
		if ($filter !== '') {
			$filter = trim($filter);
			$filter = addcslashes($filter, '\\%_');
			if (stripos($filter, 'intitle:') === 0) {
				$filter = substr($filter, strlen('intitle:'));
				$intitle = true;
			} else {
				$intitle = false;
			}
			if (stripos($filter, 'inurl:') === 0) {
				$filter = substr($filter, strlen('inurl:'));
				$inurl = true;
			} else {
				$inurl = false;
			}
			if (stripos($filter, 'author:') === 0) {
				$filter = substr($filter, strlen('author:'));
				$author = true;
			} else {
				$author = false;
			}
			$terms = array_unique(explode(' ', $filter));
			sort($terms);	//Put #tags first
			foreach ($terms as $word) {
				$word = trim($word);
				if (strlen($word) > 0) {
					if ($intitle) {
						$search .= 'AND e1.title LIKE ? ';
						$values[] = '%' . $word .'%';
					} elseif ($inurl) {
						$search .= 'AND CONCAT(e1.link, e1.guid) LIKE ? ';
						$values[] = '%' . $word .'%';
					} elseif ($author) {
						$search .= 'AND e1.author LIKE ? ';
						$values[] = '%' . $word .'%';
					} else {
						if ($word[0] === '#' && isset($word[1])) {
							$search .= 'AND e1.tags LIKE ? ';
							$values[] = '%' . $word .'%';
						} else {
							$search .= 'AND CONCAT(e1.title, UNCOMPRESS(e1.content_bin)) LIKE ? ';
							$values[] = '%' . $word .'%';
						}
					}
				}
			}
		}

		return array($values,
			'SELECT e1.id FROM `' . $this->prefix . 'entry` e1 '
			. ($joinFeed ? 'INNER JOIN `' . $this->prefix . 'feed` f ON e1.id_feed = f.id ' : '')
			. 'WHERE ' . $where
			. $search
			. 'ORDER BY e1.id ' . $order
			. ($limit > 0 ? ' LIMIT ' . $limit : ''));	//TODO: See http://explainextended.com/2009/10/23/mysql-order-by-limit-performance-late-row-lookups/
	}

	public function listWhere($type = 'a', $id = '', $state = null, $order = 'DESC', $limit = 1, $firstId = '', $filter = '', $date_min = 0, $showOlderUnreadsorFavorites = false, $keepHistoryDefault = 0) {
		list($values, $sql) = $this->sqlListWhere($type, $id, $state, $order, $limit, $firstId, $filter, $date_min, $showOlderUnreadsorFavorites, $keepHistoryDefault);

		$sql = 'SELECT e.id, e.guid, e.title, e.author, UNCOMPRESS(e.content_bin) AS content, e.link, e.date, e.is_read, e.is_favorite, e.id_feed, e.tags '
		     . 'FROM `' . $this->prefix . 'entry` e '
		     . 'INNER JOIN ('
		     . $sql
		     . ') e2 ON e2.id = e.id '
		     . 'ORDER BY e.id ' . $order;

		$stm = $this->bd->prepare ($sql);
		$stm->execute ($values);

		return self::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
	}

	public function listIdsWhere($type = 'a', $id = '', $state = null, $order = 'DESC', $limit = 1, $firstId = '', $filter = '', $date_min = 0, $showOlderUnreadsorFavorites = false, $keepHistoryDefault = 0) {	//For API
		list($values, $sql) = $this->sqlListWhere($type, $id, $state, $order, $limit, $firstId, $filter, $date_min, $showOlderUnreadsorFavorites, $keepHistoryDefault);

		$stm = $this->bd->prepare($sql);
		$stm->execute($values);

		return $stm->fetchAll(PDO::FETCH_COLUMN, 0);
	}

	public function listLastGuidsByFeed($id, $n) {
		$sql = 'SELECT guid FROM `' . $this->prefix . 'entry` WHERE id_feed=? ORDER BY id DESC LIMIT ' . intval($n);
		$stm = $this->bd->prepare ($sql);
		$values = array ($id);
		$stm->execute ($values);
		return $stm->fetchAll (PDO::FETCH_COLUMN, 0);
	}

	public function countUnreadRead () {
		$sql = 'SELECT COUNT(e.id) AS count FROM `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id WHERE priority > 0'
		     . ' UNION SELECT COUNT(e.id) AS count FROM `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id WHERE priority > 0 AND is_read = 0';
		$stm = $this->bd->prepare ($sql);
		$stm->execute ();
		$res = $stm->fetchAll (PDO::FETCH_COLUMN, 0);
		$all = empty($res[0]) ? 0 : $res[0];
		$unread = empty($res[1]) ? 0 : $res[1];
		return array('all' => $all, 'unread' => $unread, 'read' => $all - $unread);
	}
	public function count ($minPriority = null) {
		$sql = 'SELECT COUNT(e.id) AS count FROM `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id';
		if ($minPriority !== null) {
			$sql = ' WHERE priority > ' . intval($minPriority);
		}
		$stm = $this->bd->prepare ($sql);
		$stm->execute ();
		$res = $stm->fetchAll (PDO::FETCH_COLUMN, 0);
		return $res[0];
	}
	public function countNotRead ($minPriority = null) {
		$sql = 'SELECT COUNT(e.id) AS count FROM `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id WHERE is_read = 0';
		if ($minPriority !== null) {
			$sql = ' AND priority > ' . intval($minPriority);
		}
		$stm = $this->bd->prepare ($sql);
		$stm->execute ();
		$res = $stm->fetchAll (PDO::FETCH_COLUMN, 0);
		return $res[0];
	}

	public function countUnreadReadFavorites () {
		$sql = 'SELECT COUNT(id) FROM `' . $this->prefix . 'entry` WHERE is_favorite=1'
		     . ' UNION SELECT COUNT(id) FROM `' . $this->prefix . 'entry` WHERE is_favorite=1 AND is_read = 0';
		$stm = $this->bd->prepare ($sql);
		$stm->execute ();
		$res = $stm->fetchAll (PDO::FETCH_COLUMN, 0);
		$all = empty($res[0]) ? 0 : $res[0];
		$unread = empty($res[1]) ? 0 : $res[1];
		return array('all' => $all, 'unread' => $unread, 'read' => $all - $unread);
	}

	public function optimizeTable() {
		$sql = 'OPTIMIZE TABLE `' . $this->prefix . 'entry`';
		$stm = $this->bd->prepare ($sql);
		$stm->execute ();
	}

	public static function daoToEntry ($listDAO) {
		$list = array ();

		if (!is_array ($listDAO)) {
			$listDAO = array ($listDAO);
		}

		foreach ($listDAO as $key => $dao) {
			$entry = new FreshRSS_Entry (
				$dao['id_feed'],
				$dao['guid'],
				$dao['title'],
				$dao['author'],
				$dao['content'],
				$dao['link'],
				$dao['date'],
				$dao['is_read'],
				$dao['is_favorite'],
				$dao['tags']
			);
			if (isset ($dao['id'])) {
				$entry->_id ($dao['id']);
			}
			$list[] = $entry;
		}

		unset ($listDAO);

		return $list;
	}
}