Entry.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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) {
  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 (array ());
  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 isRead () {
  57. return $this->is_read;
  58. }
  59. public function isFavorite () {
  60. return $this->is_favorite;
  61. }
  62. public function feed ($object = false) {
  63. if ($object) {
  64. $feedDAO = new FeedDAO ();
  65. return $feedDAO->searchById ($this->feed);
  66. } else {
  67. return $this->feed;
  68. }
  69. }
  70. public function tags ($inString = false) {
  71. if ($inString) {
  72. if (!empty ($this->tags)) {
  73. return '#' . implode(' #', $this->tags);
  74. } else {
  75. return '';
  76. }
  77. } else {
  78. return $this->tags;
  79. }
  80. }
  81. public function _id ($value) {
  82. $this->id = $value;
  83. }
  84. public function _guid ($value) {
  85. $this->guid = $value;
  86. }
  87. public function _title ($value) {
  88. $this->title = $value;
  89. }
  90. public function _author ($value) {
  91. $this->author = $value;
  92. }
  93. public function _content ($value) {
  94. $this->content = $value;
  95. }
  96. public function _link ($value) {
  97. $this->link = $value;
  98. }
  99. public function _date ($value) {
  100. if (is_int (intval ($value))) {
  101. $this->date = $value;
  102. } else {
  103. $this->date = time ();
  104. }
  105. }
  106. public function _isRead ($value) {
  107. $this->is_read = $value;
  108. }
  109. public function _isFavorite ($value) {
  110. $this->is_favorite = $value;
  111. }
  112. public function _feed ($value) {
  113. $this->feed = $value;
  114. }
  115. public function _tags ($value) {
  116. if (!is_array ($value)) {
  117. $value = array ($value);
  118. }
  119. foreach ($value as $key => $t) {
  120. if (!$t) {
  121. unset ($value[$key]);
  122. }
  123. }
  124. $this->tags = $value;
  125. }
  126. public function isDay ($day) {
  127. $date = getdate ();
  128. $today = mktime (0, 0, 0, $date['mon'], $date['mday'], $date['year']);
  129. $yesterday = $today - 86400;
  130. if ($day == Days::TODAY &&
  131. $this->date >= $today && $this->date < $today + 86400) {
  132. return true;
  133. } elseif ($day == Days::YESTERDAY &&
  134. $this->date >= $yesterday && $this->date < $yesterday + 86400) {
  135. return true;
  136. } elseif ($day == Days::BEFORE_YESTERDAY && $this->date < $yesterday) {
  137. return true;
  138. } else {
  139. return false;
  140. }
  141. }
  142. public function loadCompleteContent($pathEntries) {
  143. // Gestion du contenu
  144. // On cherche à récupérer les articles en entier... même si le flux ne le propose pas
  145. if ($pathEntries) {
  146. $entryDAO = new EntryDAO();
  147. $entry = $entryDAO->searchByGuid($this->feed, $this->guid);
  148. if($entry) {
  149. // l'article existe déjà en BDD, en se contente de recharger ce contenu
  150. $this->content = $entry->content();
  151. } else {
  152. try {
  153. // l'article n'est pas en BDD, on va le chercher sur le site
  154. $this->content = get_content_by_parsing(
  155. $this->link(), $pathEntries
  156. );
  157. } catch (Exception $e) {
  158. // rien à faire, on garde l'ancien contenu (requête a échoué)
  159. }
  160. }
  161. }
  162. }
  163. public function toArray () {
  164. return array (
  165. 'id' => $this->id (),
  166. 'guid' => $this->guid (),
  167. 'title' => $this->title (),
  168. 'author' => $this->author (),
  169. 'content' => $this->content (),
  170. 'link' => $this->link (),
  171. 'date' => $this->date (true),
  172. 'is_read' => $this->isRead (),
  173. 'is_favorite' => $this->isFavorite (),
  174. 'id_feed' => $this->feed (),
  175. 'tags' => $this->tags (true),
  176. );
  177. }
  178. }
  179. class EntryDAO extends Model_pdo {
  180. public function addEntry ($valuesTmp) {
  181. $sql = 'INSERT INTO ' . $this->prefix . 'entry(id, guid, title, author, content, link, date, is_read, is_favorite, id_feed, tags) VALUES(CAST(? * 1000000 AS SIGNED INTEGER), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
  182. $stm = $this->bd->prepare ($sql);
  183. $values = array (
  184. microtime(true),
  185. substr($valuesTmp['guid'], 0, 760),
  186. substr($valuesTmp['title'], 0, 255),
  187. substr($valuesTmp['author'], 0, 255),
  188. base64_encode (gzdeflate (serialize ($valuesTmp['content']))),
  189. substr($valuesTmp['link'], 0, 1023),
  190. $valuesTmp['date'],
  191. $valuesTmp['is_read'],
  192. $valuesTmp['is_favorite'],
  193. $valuesTmp['id_feed'],
  194. substr($valuesTmp['tags'], 0, 1023),
  195. );
  196. if ($stm && $stm->execute ($values)) {
  197. return $this->bd->lastInsertId();
  198. } else {
  199. $info = $stm->errorInfo();
  200. if ((int)($info[0] / 1000) !== 23) { //Filter out "SQLSTATE Class code 23: Constraint Violation" because of expected duplicate entries
  201. Minz_Log::record ('SQL error ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
  202. . ' while adding entry in feed ' . $valuesTmp['id_feed'] . ' with title: ' . $valuesTmp['title'], Minz_Log::ERROR);
  203. } /*else {
  204. Minz_Log::record ('SQL error ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
  205. . ' while adding entry in feed ' . $valuesTmp['id_feed'] . ' with title: ' . $valuesTmp['title'], Minz_Log::DEBUG);
  206. }*/
  207. return false;
  208. }
  209. }
  210. /*public function updateEntry ($id, $valuesTmp) {
  211. if (isset ($valuesTmp['content'])) {
  212. $valuesTmp['content'] = base64_encode (gzdeflate (serialize ($valuesTmp['content'])));
  213. }
  214. $set = '';
  215. foreach ($valuesTmp as $key => $v) {
  216. $set .= $key . '=?, ';
  217. }
  218. $set = substr ($set, 0, -2);
  219. $sql = 'UPDATE ' . $this->prefix . 'entry SET ' . $set . ' WHERE id=?';
  220. $stm = $this->bd->prepare ($sql);
  221. foreach ($valuesTmp as $v) {
  222. $values[] = $v;
  223. }
  224. $values[] = $id;
  225. if ($stm && $stm->execute ($values)) {
  226. return $stm->rowCount();
  227. } else {
  228. $info = $stm->errorInfo();
  229. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  230. return false;
  231. }
  232. }*/
  233. public function markFavorite ($id, $is_favorite = true) {
  234. $sql = 'UPDATE ' . $this->prefix . 'entry e '
  235. . 'SET e.is_favorite = ? '
  236. . 'WHERE e.id=?';
  237. $values = array ($is_favorite ? 1 : 0, $id);
  238. $stm = $this->bd->prepare ($sql);
  239. if ($stm && $stm->execute ($values)) {
  240. return $stm->rowCount();
  241. } else {
  242. $info = $stm->errorInfo();
  243. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  244. return false;
  245. }
  246. }
  247. public function markRead ($id, $is_read = true) {
  248. $sql = 'UPDATE ' . $this->prefix . 'entry e INNER JOIN ' . $this->prefix . 'feed f ON e.id_feed = f.id '
  249. . 'SET e.is_read = ?,'
  250. . 'f.cache_nbUnreads=f.cache_nbUnreads' . ($is_read ? '-' : '+') . '1 '
  251. . 'WHERE e.id=?';
  252. $values = array ($is_read ? 1 : 0, $id);
  253. $stm = $this->bd->prepare ($sql);
  254. if ($stm && $stm->execute ($values)) {
  255. return $stm->rowCount();
  256. } else {
  257. $info = $stm->errorInfo();
  258. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  259. return false;
  260. }
  261. }
  262. public function markReadEntries ($dateMax = 0) {
  263. if ($dateMax === 0) {
  264. $sql = 'UPDATE ' . $this->prefix . 'entry e INNER JOIN ' . $this->prefix . 'feed f ON e.id_feed = f.id '
  265. . 'SET e.is_read = 1, f.cache_nbUnreads=0 '
  266. . 'WHERE e.is_read = 0 AND f.priority > 0';
  267. $stm = $this->bd->prepare ($sql);
  268. if ($stm && $stm->execute ()) {
  269. return $stm->rowCount();
  270. } else {
  271. $info = $stm->errorInfo();
  272. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  273. return false;
  274. }
  275. } else {
  276. $this->bd->beginTransaction ();
  277. $sql = 'UPDATE ' . $this->prefix . 'entry e INNER JOIN ' . $this->prefix . 'feed f ON e.id_feed = f.id '
  278. . 'SET e.is_read = 1 '
  279. . 'WHERE e.is_read = 0 AND e.date < ? AND f.priority > 0';
  280. $values = array ($dateMax);
  281. $stm = $this->bd->prepare ($sql);
  282. if (!($stm && $stm->execute ($values))) {
  283. $info = $stm->errorInfo();
  284. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  285. $this->bd->rollBack ();
  286. return false;
  287. }
  288. $affected = $stm->rowCount();
  289. if ($affected > 0) {
  290. $sql = 'UPDATE ' . $this->prefix . 'feed f '
  291. . 'LEFT OUTER JOIN ('
  292. . 'SELECT e.id_feed, '
  293. . 'COUNT(*) AS nbUnreads '
  294. . 'FROM ' . $this->prefix . 'entry e '
  295. . 'WHERE e.is_read = 0 '
  296. . 'GROUP BY e.id_feed'
  297. . ') x ON x.id_feed=f.id '
  298. . 'SET f.cache_nbUnreads=COALESCE(x.nbUnreads, 0)';
  299. $stm = $this->bd->prepare ($sql);
  300. if (!($stm && $stm->execute ())) {
  301. $info = $stm->errorInfo();
  302. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  303. $this->bd->rollBack ();
  304. return false;
  305. }
  306. }
  307. $this->bd->commit ();
  308. return $affected;
  309. }
  310. }
  311. public function markReadCat ($id, $dateMax = 0) {
  312. if ($dateMax === 0) {
  313. $sql = 'UPDATE ' . $this->prefix . 'entry e INNER JOIN ' . $this->prefix . 'feed f ON e.id_feed = f.id '
  314. . 'SET e.is_read = 1, f.cache_nbUnreads=0 '
  315. . 'WHERE f.category = ? AND e.is_read = 0';
  316. $values = array ($id);
  317. $stm = $this->bd->prepare ($sql);
  318. if ($stm && $stm->execute ($values)) {
  319. return $stm->rowCount();
  320. } else {
  321. $info = $stm->errorInfo();
  322. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  323. return false;
  324. }
  325. } else {
  326. $this->bd->beginTransaction ();
  327. $sql = 'UPDATE ' . $this->prefix . 'entry e INNER JOIN ' . $this->prefix . 'feed f ON e.id_feed = f.id '
  328. . 'SET e.is_read = 1 '
  329. . 'WHERE f.category = ? AND e.is_read = 0 AND e.date < ?';
  330. $values = array ($id, $dateMax);
  331. $stm = $this->bd->prepare ($sql);
  332. if (!($stm && $stm->execute ($values))) {
  333. $info = $stm->errorInfo();
  334. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  335. $this->bd->rollBack ();
  336. return false;
  337. }
  338. $affected = $stm->rowCount();
  339. if ($affected > 0) {
  340. $sql = 'UPDATE ' . $this->prefix . 'feed f '
  341. . 'LEFT OUTER JOIN ('
  342. . 'SELECT e.id_feed, '
  343. . 'COUNT(*) AS nbUnreads '
  344. . 'FROM ' . $this->prefix . 'entry e '
  345. . 'WHERE e.is_read = 0 '
  346. . 'GROUP BY e.id_feed'
  347. . ') x ON x.id_feed=f.id '
  348. . 'SET f.cache_nbUnreads=COALESCE(x.nbUnreads, 0) '
  349. . 'WHERE f.category = ?';
  350. $values = array ($id);
  351. $stm = $this->bd->prepare ($sql);
  352. if (!($stm && $stm->execute ($values))) {
  353. $info = $stm->errorInfo();
  354. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  355. $this->bd->rollBack ();
  356. return false;
  357. }
  358. }
  359. $this->bd->commit ();
  360. return $affected;
  361. }
  362. }
  363. public function markReadFeed ($id, $dateMax = 0) {
  364. if ($dateMax === 0) {
  365. $sql = 'UPDATE ' . $this->prefix . 'entry e INNER JOIN ' . $this->prefix . 'feed f ON e.id_feed = f.id '
  366. . 'SET e.is_read = 1, f.cache_nbUnreads=0 '
  367. . 'WHERE f.id=? AND e.is_read = 0';
  368. $values = array ($id);
  369. $stm = $this->bd->prepare ($sql);
  370. if ($stm && $stm->execute ($values)) {
  371. return $stm->rowCount();
  372. } else {
  373. $info = $stm->errorInfo();
  374. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  375. return false;
  376. }
  377. } else {
  378. $this->bd->beginTransaction ();
  379. $sql = 'UPDATE ' . $this->prefix . 'entry e INNER JOIN ' . $this->prefix . 'feed f ON e.id_feed = f.id '
  380. . 'SET e.is_read = 1 '
  381. . 'WHERE f.id=? AND e.is_read = 0 AND e.date < ?';
  382. $values = array ($id, $dateMax);
  383. $stm = $this->bd->prepare ($sql);
  384. if (!($stm && $stm->execute ($values))) {
  385. $info = $stm->errorInfo();
  386. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  387. $this->bd->rollBack ();
  388. return false;
  389. }
  390. $affected = $stm->rowCount();
  391. if ($affected > 0) {
  392. $sql = 'UPDATE ' . $this->prefix . 'feed f '
  393. . 'SET f.cache_nbUnreads=f.cache_nbUnreads-' . $affected
  394. . ' WHERE f.id=?';
  395. $values = array ($id);
  396. $stm = $this->bd->prepare ($sql);
  397. if (!($stm && $stm->execute ($values))) {
  398. $info = $stm->errorInfo();
  399. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  400. $this->bd->rollBack ();
  401. return false;
  402. }
  403. }
  404. $this->bd->commit ();
  405. return $affected;
  406. }
  407. }
  408. /*public function updateEntries ($valuesTmp) {
  409. if (isset ($valuesTmp['content'])) {
  410. $valuesTmp['content'] = base64_encode (gzdeflate (serialize ($valuesTmp['content'])));
  411. }
  412. $set = '';
  413. foreach ($valuesTmp as $key => $v) {
  414. $set .= $key . '=?, ';
  415. }
  416. $set = substr ($set, 0, -2);
  417. $sql = 'UPDATE ' . $this->prefix . 'entry SET ' . $set;
  418. $stm = $this->bd->prepare ($sql);
  419. foreach ($valuesTmp as $v) {
  420. $values[] = $v;
  421. }
  422. if ($stm && $stm->execute ($values)) {
  423. return $stm->rowCount();
  424. } else {
  425. $info = $stm->errorInfo();
  426. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  427. return false;
  428. }
  429. }*/
  430. public function cleanOldEntries ($date_min) {
  431. $sql = 'DELETE e.* FROM ' . $this->prefix . 'entry e INNER JOIN ' . $this->prefix . 'feed f ON e.id_feed = f.id WHERE e.date <= ? AND e.is_favorite = 0 AND f.keep_history = 0';
  432. $stm = $this->bd->prepare ($sql);
  433. $values = array (
  434. $date_min
  435. );
  436. if ($stm && $stm->execute ($values)) {
  437. return $stm->rowCount();
  438. } else {
  439. $info = $stm->errorInfo();
  440. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  441. return false;
  442. }
  443. }
  444. public function searchByGuid ($feed_id, $id) {
  445. // un guid est unique pour un flux donné
  446. $sql = 'SELECT * FROM ' . $this->prefix . 'entry WHERE id_feed=? AND guid=?';
  447. $stm = $this->bd->prepare ($sql);
  448. $values = array (
  449. $feed_id,
  450. $id
  451. );
  452. $stm->execute ($values);
  453. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  454. list ($entry, $next) = HelperEntry::daoToEntry ($res);
  455. if (isset ($entry[0])) {
  456. return $entry[0];
  457. } else {
  458. return false;
  459. }
  460. }
  461. public function searchById ($id) {
  462. $sql = 'SELECT * FROM ' . $this->prefix . 'entry WHERE id=?';
  463. $stm = $this->bd->prepare ($sql);
  464. $values = array ($id);
  465. $stm->execute ($values);
  466. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  467. list ($entry, $next) = HelperEntry::daoToEntry ($res);
  468. if (isset ($entry[0])) {
  469. return $entry[0];
  470. } else {
  471. return false;
  472. }
  473. }
  474. private function listWhere ($where, $state, $order, $limitFromId = '', $limitCount = '', $values = array ()) {
  475. if ($state === 'not_read') {
  476. $where .= ' AND is_read = 0';
  477. } elseif ($state === 'read') {
  478. $where .= ' AND is_read = 1';
  479. }
  480. if (!empty($limitFromId)) { //TODO: Consider using LPAD(e.date, 11) //CONCAT is for cases when many entries have the same date
  481. $where .= ' AND CONCAT(e.date, e.id) ' . ($order === 'low_to_high' ? '<=' : '>=') . ' (SELECT CONCAT(s.date, s.id) FROM ' . $this->prefix . 'entry s WHERE s.id = "' . $limitFromId . '")';
  482. }
  483. if ($order === 'low_to_high') {
  484. $order = ' DESC';
  485. } else {
  486. $order = '';
  487. }
  488. $sql = 'SELECT e.* FROM ' . $this->prefix . 'entry e'
  489. . ' INNER JOIN ' . $this->prefix . 'feed f ON e.id_feed = f.id' . $where
  490. . ' ORDER BY e.date' . $order . ', e.id' . $order;
  491. if (empty($limitCount)) {
  492. $limitCount = 20000; //TODO: FIXME: Hack temporaire en attendant la recherche côté base-de-données
  493. }
  494. //if (!empty($limitCount)) {
  495. $sql .= ' LIMIT ' . ($limitCount + 2); //TODO: See http://explainextended.com/2009/10/23/mysql-order-by-limit-performance-late-row-lookups/
  496. //}
  497. $stm = $this->bd->prepare ($sql);
  498. $stm->execute ($values);
  499. return HelperEntry::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
  500. }
  501. public function listEntries ($state, $order = 'high_to_low', $limitFromId = '', $limitCount = '') {
  502. return $this->listWhere (' WHERE priority > 0', $state, $order, $limitFromId, $limitCount);
  503. }
  504. public function listFavorites ($state, $order = 'high_to_low', $limitFromId = '', $limitCount = '') {
  505. return $this->listWhere (' WHERE is_favorite = 1', $state, $order, $limitFromId, $limitCount);
  506. }
  507. public function listByCategory ($cat, $state, $order = 'high_to_low', $limitFromId = '', $limitCount = '') {
  508. return $this->listWhere (' WHERE category = ?', $state, $order, $limitFromId, $limitCount, array ($cat));
  509. }
  510. public function listByFeed ($feed, $state, $order = 'high_to_low', $limitFromId = '', $limitCount = '') {
  511. return $this->listWhere (' WHERE id_feed = ?', $state, $order, $limitFromId, $limitCount, array ($feed));
  512. }
  513. public function listLastGuidsByFeed($id, $n) {
  514. $sql = 'SELECT guid FROM ' . $this->prefix . 'entry WHERE id_feed=? ORDER BY date DESC LIMIT ' . intval($n);
  515. $stm = $this->bd->prepare ($sql);
  516. $values = array ($id);
  517. $stm->execute ($values);
  518. return $stm->fetchAll (PDO::FETCH_COLUMN, 0);
  519. }
  520. public function countUnreadRead () {
  521. $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'
  522. . ' 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';
  523. $stm = $this->bd->prepare ($sql);
  524. $stm->execute ();
  525. $res = $stm->fetchAll (PDO::FETCH_COLUMN, 0);
  526. $all = empty($res[0]) ? 0 : $res[0];
  527. $unread = empty($res[1]) ? 0 : $res[1];
  528. return array('all' => $all, 'unread' => $unread, 'read' => $all - $unread);
  529. }
  530. public function count ($minPriority = null) {
  531. $sql = 'SELECT COUNT(e.id) AS count FROM ' . $this->prefix . 'entry e INNER JOIN ' . $this->prefix . 'feed f ON e.id_feed = f.id';
  532. if ($minPriority !== null) {
  533. $sql = ' WHERE priority > ' . intval($minPriority);
  534. }
  535. $stm = $this->bd->prepare ($sql);
  536. $stm->execute ();
  537. $res = $stm->fetchAll (PDO::FETCH_COLUMN, 0);
  538. return $res[0];
  539. }
  540. public function countNotRead ($minPriority = null) {
  541. $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';
  542. if ($minPriority !== null) {
  543. $sql = ' AND priority > ' . intval($minPriority);
  544. }
  545. $stm = $this->bd->prepare ($sql);
  546. $stm->execute ();
  547. $res = $stm->fetchAll (PDO::FETCH_COLUMN, 0);
  548. return $res[0];
  549. }
  550. public function countUnreadReadFavorites () {
  551. $sql = 'SELECT COUNT(id) FROM ' . $this->prefix . 'entry WHERE is_favorite=1'
  552. . ' UNION SELECT COUNT(id) FROM ' . $this->prefix . 'entry WHERE is_favorite=1 AND is_read = 0';
  553. $stm = $this->bd->prepare ($sql);
  554. $stm->execute ();
  555. $res = $stm->fetchAll (PDO::FETCH_COLUMN, 0);
  556. $all = empty($res[0]) ? 0 : $res[0];
  557. $unread = empty($res[1]) ? 0 : $res[1];
  558. return array('all' => $all, 'unread' => $unread, 'read' => $all - $unread);
  559. }
  560. public function optimizeTable() {
  561. $sql = 'OPTIMIZE TABLE ' . $this->prefix . 'entry';
  562. $stm = $this->bd->prepare ($sql);
  563. $stm->execute ();
  564. }
  565. }
  566. class HelperEntry {
  567. public static $nb = 1;
  568. public static $first = '';
  569. public static $filter = array (
  570. 'words' => array (),
  571. 'tags' => array (),
  572. );
  573. public static function daoToEntry ($listDAO) {
  574. $list = array ();
  575. if (!is_array ($listDAO)) {
  576. $listDAO = array ($listDAO);
  577. }
  578. $count = 0;
  579. $first_is_found = false;
  580. $break_after = false;
  581. $next = '';
  582. foreach ($listDAO as $key => $dao) {
  583. $dao['content'] = unserialize (gzinflate (base64_decode ($dao['content'])));
  584. $dao['tags'] = preg_split('/[\s#]/', $dao['tags']);
  585. if (self::tagsMatchEntry ($dao) &&
  586. self::searchMatchEntry ($dao)) {
  587. if ($break_after) {
  588. $next = $dao['id'];
  589. break;
  590. }
  591. if ($first_is_found || $dao['id'] == self::$first || self::$first == '') {
  592. $list[$key] = self::createEntry ($dao);
  593. $count++;
  594. $first_is_found = true; //TODO: Update: Now done in SQL
  595. }
  596. if ($count >= self::$nb) {
  597. $break_after = true;
  598. }
  599. }
  600. }
  601. unset ($listDAO);
  602. return array ($list, $next);
  603. }
  604. private static function createEntry ($dao) {
  605. $entry = new Entry (
  606. $dao['id_feed'],
  607. $dao['guid'],
  608. $dao['title'],
  609. $dao['author'],
  610. $dao['content'],
  611. $dao['link'],
  612. $dao['date'],
  613. $dao['is_read'],
  614. $dao['is_favorite']
  615. );
  616. $entry->_tags ($dao['tags']);
  617. if (isset ($dao['id'])) {
  618. $entry->_id ($dao['id']);
  619. }
  620. return $entry;
  621. }
  622. private static function tagsMatchEntry ($dao) {
  623. $tags = self::$filter['tags'];
  624. foreach ($tags as $tag) {
  625. if (!in_array ($tag, $dao['tags'])) {
  626. return false;
  627. }
  628. }
  629. return true;
  630. }
  631. private static function searchMatchEntry ($dao) {
  632. $words = self::$filter['words'];
  633. foreach ($words as $word) {
  634. $word = strtolower ($word);
  635. if (strpos (strtolower ($dao['title']), $word) === false &&
  636. strpos (strtolower ($dao['content']), $word) === false &&
  637. strpos (strtolower ($dao['link']), $word) === false) {
  638. return false;
  639. }
  640. }
  641. return true;
  642. }
  643. }