Entry.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. <?php
  2. class Entry extends Model {
  3. private $id = 0;
  4. private $guid;
  5. private $title;
  6. private $author;
  7. private $content;
  8. private $link;
  9. private $date;
  10. private $is_read;
  11. private $is_favorite;
  12. private $feed;
  13. private $tags;
  14. public function __construct ($feed = '', $guid = '', $title = '', $author = '', $content = '',
  15. $link = '', $pubdate = 0, $is_read = false, $is_favorite = false, $tags = '') {
  16. $this->_guid ($guid);
  17. $this->_title ($title);
  18. $this->_author ($author);
  19. $this->_content ($content);
  20. $this->_link ($link);
  21. $this->_date ($pubdate);
  22. $this->_isRead ($is_read);
  23. $this->_isFavorite ($is_favorite);
  24. $this->_feed ($feed);
  25. $this->_tags (preg_split('/[\s#]/', $tags));
  26. }
  27. public function id () {
  28. return $this->id;
  29. }
  30. public function guid () {
  31. return $this->guid;
  32. }
  33. public function title () {
  34. return $this->title;
  35. }
  36. public function author () {
  37. if (is_null ($this->author)) {
  38. return '';
  39. } else {
  40. return $this->author;
  41. }
  42. }
  43. public function content () {
  44. return $this->content;
  45. }
  46. public function link () {
  47. return $this->link;
  48. }
  49. public function date ($raw = false) {
  50. if ($raw) {
  51. return $this->date;
  52. } else {
  53. return timestamptodate ($this->date);
  54. }
  55. }
  56. public function dateAdded ($raw = false) {
  57. $date = intval(substr($this->id, 0, -6));
  58. if ($raw) {
  59. return $date;
  60. } else {
  61. return timestamptodate ($date);
  62. }
  63. }
  64. public function isRead () {
  65. return $this->is_read;
  66. }
  67. public function isFavorite () {
  68. return $this->is_favorite;
  69. }
  70. public function feed ($object = false) {
  71. if ($object) {
  72. $feedDAO = new FeedDAO ();
  73. return $feedDAO->searchById ($this->feed);
  74. } else {
  75. return $this->feed;
  76. }
  77. }
  78. public function tags ($inString = false) {
  79. if ($inString) {
  80. return empty ($this->tags) ? '' : '#' . implode(' #', $this->tags);
  81. } else {
  82. return $this->tags;
  83. }
  84. }
  85. public function _id ($value) {
  86. $this->id = $value;
  87. }
  88. public function _guid ($value) {
  89. $this->guid = $value;
  90. }
  91. public function _title ($value) {
  92. $this->title = $value;
  93. }
  94. public function _author ($value) {
  95. $this->author = $value;
  96. }
  97. public function _content ($value) {
  98. $this->content = $value;
  99. }
  100. public function _link ($value) {
  101. $this->link = $value;
  102. }
  103. public function _date ($value) {
  104. if (ctype_digit ($value)) {
  105. $this->date = intval ($value);
  106. } else {
  107. $this->date = time ();
  108. }
  109. }
  110. public function _isRead ($value) {
  111. $this->is_read = $value;
  112. }
  113. public function _isFavorite ($value) {
  114. $this->is_favorite = $value;
  115. }
  116. public function _feed ($value) {
  117. $this->feed = $value;
  118. }
  119. public function _tags ($value) {
  120. if (!is_array ($value)) {
  121. $value = array ($value);
  122. }
  123. foreach ($value as $key => $t) {
  124. if (!$t) {
  125. unset ($value[$key]);
  126. }
  127. }
  128. $this->tags = $value;
  129. }
  130. public function isDay ($day) {
  131. $date = $this->dateAdded(true);
  132. $today = @strtotime('today');
  133. $yesterday = $today - 86400;
  134. if ($day === Days::TODAY &&
  135. $date >= $today && $date < $today + 86400) {
  136. return true;
  137. } elseif ($day === Days::YESTERDAY &&
  138. $date >= $yesterday && $date < $yesterday + 86400) {
  139. return true;
  140. } elseif ($day === Days::BEFORE_YESTERDAY && $date < $yesterday) {
  141. return true;
  142. } else {
  143. return false;
  144. }
  145. }
  146. public function loadCompleteContent($pathEntries) {
  147. // Gestion du contenu
  148. // On cherche à récupérer les articles en entier... même si le flux ne le propose pas
  149. if ($pathEntries) {
  150. $entryDAO = new EntryDAO();
  151. $entry = $entryDAO->searchByGuid($this->feed, $this->guid);
  152. if($entry) {
  153. // l'article existe déjà en BDD, en se contente de recharger ce contenu
  154. $this->content = $entry->content();
  155. } else {
  156. try {
  157. // l'article n'est pas en BDD, on va le chercher sur le site
  158. $this->content = get_content_by_parsing(
  159. $this->link(), $pathEntries
  160. );
  161. } catch (Exception $e) {
  162. // rien à faire, on garde l'ancien contenu (requête a échoué)
  163. }
  164. }
  165. }
  166. }
  167. public function toArray () {
  168. return array (
  169. 'id' => $this->id (),
  170. 'guid' => $this->guid (),
  171. 'title' => $this->title (),
  172. 'author' => $this->author (),
  173. 'content' => $this->content (),
  174. 'link' => $this->link (),
  175. 'date' => $this->date (true),
  176. 'is_read' => $this->isRead (),
  177. 'is_favorite' => $this->isFavorite (),
  178. 'id_feed' => $this->feed (),
  179. 'tags' => $this->tags (true),
  180. );
  181. }
  182. }
  183. class EntryDAO extends Model_pdo {
  184. public function addEntry ($valuesTmp) {
  185. $sql = 'INSERT INTO `' . $this->prefix . 'entry`(id, guid, title, author, content_bin, link, date, is_read, is_favorite, id_feed, tags) '
  186. . 'VALUES(CAST(? * 1000000 AS SIGNED INTEGER), ?, ?, ?, COMPRESS(?), ?, ?, ?, ?, ?, ?)';
  187. $stm = $this->bd->prepare ($sql);
  188. $values = array (
  189. $valuesTmp['id'],
  190. substr($valuesTmp['guid'], 0, 760),
  191. substr($valuesTmp['title'], 0, 255),
  192. substr($valuesTmp['author'], 0, 255),
  193. $valuesTmp['content'],
  194. substr($valuesTmp['link'], 0, 1023),
  195. $valuesTmp['date'],
  196. $valuesTmp['is_read'],
  197. $valuesTmp['is_favorite'],
  198. $valuesTmp['id_feed'],
  199. substr($valuesTmp['tags'], 0, 1023),
  200. );
  201. if ($stm && $stm->execute ($values)) {
  202. return $this->bd->lastInsertId();
  203. } else {
  204. $info = $stm->errorInfo();
  205. if ((int)($info[0] / 1000) !== 23) { //Filter out "SQLSTATE Class code 23: Constraint Violation" because of expected duplicate entries
  206. Minz_Log::record ('SQL error ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
  207. . ' while adding entry in feed ' . $valuesTmp['id_feed'] . ' with title: ' . $valuesTmp['title'], Minz_Log::ERROR);
  208. } /*else {
  209. Minz_Log::record ('SQL error ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
  210. . ' while adding entry in feed ' . $valuesTmp['id_feed'] . ' with title: ' . $valuesTmp['title'], Minz_Log::DEBUG);
  211. }*/
  212. return false;
  213. }
  214. }
  215. public function markFavorite ($id, $is_favorite = true) {
  216. $sql = 'UPDATE `' . $this->prefix . 'entry` e '
  217. . 'SET e.is_favorite = ? '
  218. . 'WHERE e.id=?';
  219. $values = array ($is_favorite ? 1 : 0, $id);
  220. $stm = $this->bd->prepare ($sql);
  221. if ($stm && $stm->execute ($values)) {
  222. return $stm->rowCount();
  223. } else {
  224. $info = $stm->errorInfo();
  225. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  226. return false;
  227. }
  228. }
  229. public function markRead ($id, $is_read = true) {
  230. $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
  231. . 'SET e.is_read = ?,'
  232. . 'f.cache_nbUnreads=f.cache_nbUnreads' . ($is_read ? '-' : '+') . '1 '
  233. . 'WHERE e.id=?';
  234. $values = array ($is_read ? 1 : 0, $id);
  235. $stm = $this->bd->prepare ($sql);
  236. if ($stm && $stm->execute ($values)) {
  237. return $stm->rowCount();
  238. } else {
  239. $info = $stm->errorInfo();
  240. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  241. return false;
  242. }
  243. }
  244. public function markReadEntries ($idMax = 0) {
  245. if ($idMax === 0) {
  246. $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
  247. . 'SET e.is_read = 1, f.cache_nbUnreads=0 '
  248. . 'WHERE e.is_read = 0 AND f.priority > 0';
  249. $stm = $this->bd->prepare ($sql);
  250. if ($stm && $stm->execute ()) {
  251. return $stm->rowCount();
  252. } else {
  253. $info = $stm->errorInfo();
  254. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  255. return false;
  256. }
  257. } else {
  258. $this->bd->beginTransaction ();
  259. $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
  260. . 'SET e.is_read = 1 '
  261. . 'WHERE e.is_read = 0 AND e.id <= ? AND f.priority > 0';
  262. $values = array ($idMax);
  263. $stm = $this->bd->prepare ($sql);
  264. if (!($stm && $stm->execute ($values))) {
  265. $info = $stm->errorInfo();
  266. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  267. $this->bd->rollBack ();
  268. return false;
  269. }
  270. $affected = $stm->rowCount();
  271. if ($affected > 0) {
  272. $sql = 'UPDATE `' . $this->prefix . 'feed` f '
  273. . 'LEFT OUTER JOIN ('
  274. . 'SELECT e.id_feed, '
  275. . 'COUNT(*) AS nbUnreads '
  276. . 'FROM `' . $this->prefix . 'entry` e '
  277. . 'WHERE e.is_read = 0 '
  278. . 'GROUP BY e.id_feed'
  279. . ') x ON x.id_feed=f.id '
  280. . 'SET f.cache_nbUnreads=COALESCE(x.nbUnreads, 0)';
  281. $stm = $this->bd->prepare ($sql);
  282. if (!($stm && $stm->execute ())) {
  283. $info = $stm->errorInfo();
  284. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  285. $this->bd->rollBack ();
  286. return false;
  287. }
  288. }
  289. $this->bd->commit ();
  290. return $affected;
  291. }
  292. }
  293. public function markReadCat ($id, $idMax = 0) {
  294. if ($idMax === 0) {
  295. $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
  296. . 'SET e.is_read = 1, f.cache_nbUnreads=0 '
  297. . 'WHERE f.category = ? AND e.is_read = 0';
  298. $values = array ($id);
  299. $stm = $this->bd->prepare ($sql);
  300. if ($stm && $stm->execute ($values)) {
  301. return $stm->rowCount();
  302. } else {
  303. $info = $stm->errorInfo();
  304. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  305. return false;
  306. }
  307. } else {
  308. $this->bd->beginTransaction ();
  309. $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
  310. . 'SET e.is_read = 1 '
  311. . 'WHERE f.category = ? AND e.is_read = 0 AND e.id <= ?';
  312. $values = array ($id, $idMax);
  313. $stm = $this->bd->prepare ($sql);
  314. if (!($stm && $stm->execute ($values))) {
  315. $info = $stm->errorInfo();
  316. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  317. $this->bd->rollBack ();
  318. return false;
  319. }
  320. $affected = $stm->rowCount();
  321. if ($affected > 0) {
  322. $sql = 'UPDATE `' . $this->prefix . 'feed` f '
  323. . 'LEFT OUTER JOIN ('
  324. . 'SELECT e.id_feed, '
  325. . 'COUNT(*) AS nbUnreads '
  326. . 'FROM `' . $this->prefix . 'entry` e '
  327. . 'WHERE e.is_read = 0 '
  328. . 'GROUP BY e.id_feed'
  329. . ') x ON x.id_feed=f.id '
  330. . 'SET f.cache_nbUnreads=COALESCE(x.nbUnreads, 0) '
  331. . 'WHERE f.category = ?';
  332. $values = array ($id);
  333. $stm = $this->bd->prepare ($sql);
  334. if (!($stm && $stm->execute ($values))) {
  335. $info = $stm->errorInfo();
  336. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  337. $this->bd->rollBack ();
  338. return false;
  339. }
  340. }
  341. $this->bd->commit ();
  342. return $affected;
  343. }
  344. }
  345. public function markReadFeed ($id, $idMax = 0) {
  346. if ($idMax === 0) {
  347. $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
  348. . 'SET e.is_read = 1, f.cache_nbUnreads=0 '
  349. . 'WHERE f.id=? AND e.is_read = 0';
  350. $values = array ($id);
  351. $stm = $this->bd->prepare ($sql);
  352. if ($stm && $stm->execute ($values)) {
  353. return $stm->rowCount();
  354. } else {
  355. $info = $stm->errorInfo();
  356. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  357. return false;
  358. }
  359. } else {
  360. $this->bd->beginTransaction ();
  361. $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
  362. . 'SET e.is_read = 1 '
  363. . 'WHERE f.id=? AND e.is_read = 0 AND e.id <= ?';
  364. $values = array ($id, $idMax);
  365. $stm = $this->bd->prepare ($sql);
  366. if (!($stm && $stm->execute ($values))) {
  367. $info = $stm->errorInfo();
  368. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  369. $this->bd->rollBack ();
  370. return false;
  371. }
  372. $affected = $stm->rowCount();
  373. if ($affected > 0) {
  374. $sql = 'UPDATE `' . $this->prefix . 'feed` f '
  375. . 'SET f.cache_nbUnreads=f.cache_nbUnreads-' . $affected
  376. . ' WHERE f.id=?';
  377. $values = array ($id);
  378. $stm = $this->bd->prepare ($sql);
  379. if (!($stm && $stm->execute ($values))) {
  380. $info = $stm->errorInfo();
  381. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  382. $this->bd->rollBack ();
  383. return false;
  384. }
  385. }
  386. $this->bd->commit ();
  387. return $affected;
  388. }
  389. }
  390. public function cleanOldEntries ($date_min) {
  391. $sql = 'DELETE e.* FROM `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
  392. . 'WHERE e.id <= ? AND e.is_favorite = 0 AND f.keep_history = 0';
  393. $stm = $this->bd->prepare ($sql);
  394. $values = array (
  395. $date_min . '000000'
  396. );
  397. if ($stm && $stm->execute ($values)) {
  398. return $stm->rowCount();
  399. } else {
  400. $info = $stm->errorInfo();
  401. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  402. return false;
  403. }
  404. }
  405. public function searchByGuid ($feed_id, $id) {
  406. // un guid est unique pour un flux donné
  407. $sql = 'SELECT id, guid, title, author, UNCOMPRESS(content_bin) AS content, link, date, is_read, is_favorite, id_feed, tags '
  408. . 'FROM `' . $this->prefix . 'entry` WHERE id_feed=? AND guid=?';
  409. $stm = $this->bd->prepare ($sql);
  410. $values = array (
  411. $feed_id,
  412. $id
  413. );
  414. $stm->execute ($values);
  415. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  416. $entries = HelperEntry::daoToEntry ($res);
  417. return isset ($entries[0]) ? $entries[0] : false;
  418. }
  419. public function searchById ($id) {
  420. $sql = 'SELECT id, guid, title, author, UNCOMPRESS(content_bin) AS content, link, date, is_read, is_favorite, id_feed, tags '
  421. . 'FROM `' . $this->prefix . 'entry` WHERE id=?';
  422. $stm = $this->bd->prepare ($sql);
  423. $values = array ($id);
  424. $stm->execute ($values);
  425. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  426. $entries = HelperEntry::daoToEntry ($res);
  427. return isset ($entries[0]) ? $entries[0] : false;
  428. }
  429. public function listWhere($type = 'a', $id = '', $state = 'all', $order = 'DESC', $limit = 1, $firstId = -1, $filter = '') {
  430. $where = '';
  431. $values = array();
  432. switch ($type) {
  433. case 'a':
  434. $where .= 'priority > 0 ';
  435. break;
  436. case 's':
  437. $where .= 'is_favorite = 1 ';
  438. break;
  439. case 'c':
  440. $where .= 'category = ? ';
  441. $values[] = intval($id);
  442. break;
  443. case 'f':
  444. $where .= 'id_feed = ? ';
  445. $values[] = intval($id);
  446. break;
  447. default:
  448. throw new EntriesGetterException ('Bad type in Entry->listByType: [' . $type . ']!');
  449. }
  450. switch ($state) {
  451. case 'all':
  452. break;
  453. case 'not_read':
  454. $where .= 'AND is_read = 0 ';
  455. break;
  456. case 'read':
  457. $where .= 'AND is_read = 1 ';
  458. break;
  459. default:
  460. throw new EntriesGetterException ('Bad state in Entry->listByType: [' . $state . ']!');
  461. }
  462. switch ($order) {
  463. case 'DESC':
  464. case 'ASC':
  465. break;
  466. default:
  467. throw new EntriesGetterException ('Bad order in Entry->listByType: [' . $order . ']!');
  468. }
  469. if ($firstId > 0) {
  470. $where .= 'AND e.id ' . ($order === 'DESC' ? '<=' : '>=') . $firstId . ' ';
  471. }
  472. $terms = array_unique(explode(' ', trim($filter)));
  473. sort($terms); //Put #tags first
  474. $having = '';
  475. foreach ($terms as $word) {
  476. if (!empty($word)) {
  477. if ($word[0] === '#' && isset($word[1])) {
  478. $having .= 'AND tags LIKE ? ';
  479. $values[] = '%' . $word .'%';
  480. } elseif (!empty($word)) {
  481. $having .= 'AND (e.title LIKE ? OR content LIKE ?) ';
  482. $values[] = '%' . $word .'%';
  483. $values[] = '%' . $word .'%';
  484. }
  485. }
  486. }
  487. $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 '
  488. . 'FROM `' . $this->prefix . 'entry` e '
  489. . 'INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id WHERE ' . $where
  490. . (empty($having) ? '' : 'HAVING' . substr($having, 3))
  491. . 'ORDER BY e.id ' . $order;
  492. if ($limit > 0) {
  493. $sql .= ' LIMIT ' . $limit; //TODO: See http://explainextended.com/2009/10/23/mysql-order-by-limit-performance-late-row-lookups/
  494. }
  495. $stm = $this->bd->prepare ($sql);
  496. $stm->execute ($values);
  497. return HelperEntry::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
  498. }
  499. public function listLastGuidsByFeed($id, $n) {
  500. $sql = 'SELECT guid FROM `' . $this->prefix . 'entry` WHERE id_feed=? ORDER BY id DESC LIMIT ' . intval($n);
  501. $stm = $this->bd->prepare ($sql);
  502. $values = array ($id);
  503. $stm->execute ($values);
  504. return $stm->fetchAll (PDO::FETCH_COLUMN, 0);
  505. }
  506. public function countUnreadRead () {
  507. $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'
  508. . ' 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';
  509. $stm = $this->bd->prepare ($sql);
  510. $stm->execute ();
  511. $res = $stm->fetchAll (PDO::FETCH_COLUMN, 0);
  512. $all = empty($res[0]) ? 0 : $res[0];
  513. $unread = empty($res[1]) ? 0 : $res[1];
  514. return array('all' => $all, 'unread' => $unread, 'read' => $all - $unread);
  515. }
  516. public function count ($minPriority = null) {
  517. $sql = 'SELECT COUNT(e.id) AS count FROM `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id';
  518. if ($minPriority !== null) {
  519. $sql = ' WHERE priority > ' . intval($minPriority);
  520. }
  521. $stm = $this->bd->prepare ($sql);
  522. $stm->execute ();
  523. $res = $stm->fetchAll (PDO::FETCH_COLUMN, 0);
  524. return $res[0];
  525. }
  526. public function countNotRead ($minPriority = null) {
  527. $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';
  528. if ($minPriority !== null) {
  529. $sql = ' AND priority > ' . intval($minPriority);
  530. }
  531. $stm = $this->bd->prepare ($sql);
  532. $stm->execute ();
  533. $res = $stm->fetchAll (PDO::FETCH_COLUMN, 0);
  534. return $res[0];
  535. }
  536. public function countUnreadReadFavorites () {
  537. $sql = 'SELECT COUNT(id) FROM `' . $this->prefix . 'entry` WHERE is_favorite=1'
  538. . ' UNION SELECT COUNT(id) FROM `' . $this->prefix . 'entry` WHERE is_favorite=1 AND is_read = 0';
  539. $stm = $this->bd->prepare ($sql);
  540. $stm->execute ();
  541. $res = $stm->fetchAll (PDO::FETCH_COLUMN, 0);
  542. $all = empty($res[0]) ? 0 : $res[0];
  543. $unread = empty($res[1]) ? 0 : $res[1];
  544. return array('all' => $all, 'unread' => $unread, 'read' => $all - $unread);
  545. }
  546. public function optimizeTable() {
  547. $sql = 'OPTIMIZE TABLE `' . $this->prefix . 'entry`';
  548. $stm = $this->bd->prepare ($sql);
  549. $stm->execute ();
  550. }
  551. }
  552. class HelperEntry {
  553. public static function daoToEntry ($listDAO) {
  554. $list = array ();
  555. if (!is_array ($listDAO)) {
  556. $listDAO = array ($listDAO);
  557. }
  558. foreach ($listDAO as $key => $dao) {
  559. $entry = new Entry (
  560. $dao['id_feed'],
  561. $dao['guid'],
  562. $dao['title'],
  563. $dao['author'],
  564. $dao['content'],
  565. $dao['link'],
  566. $dao['date'],
  567. $dao['is_read'],
  568. $dao['is_favorite'],
  569. $dao['tags']
  570. );
  571. if (isset ($dao['id'])) {
  572. $entry->_id ($dao['id']);
  573. }
  574. $list[] = $entry;
  575. }
  576. unset ($listDAO);
  577. return $list;
  578. }
  579. }