Entry.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. <?php
  2. class Entry extends Model {
  3. private $id = null;
  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. if(is_null($this->id)) {
  29. return small_hash ($this->guid . Configuration::selApplication ());
  30. } else {
  31. return $this->id;
  32. }
  33. }
  34. public function guid () {
  35. return $this->guid;
  36. }
  37. public function title () {
  38. return $this->title;
  39. }
  40. public function author () {
  41. if (is_null ($this->author)) {
  42. return '';
  43. } else {
  44. return $this->author;
  45. }
  46. }
  47. public function content () {
  48. return $this->content;
  49. }
  50. public function link () {
  51. return $this->link;
  52. }
  53. public function date ($raw = false) {
  54. if ($raw) {
  55. return $this->date;
  56. } else {
  57. return timestamptodate ($this->date);
  58. }
  59. }
  60. public function isRead () {
  61. return $this->is_read;
  62. }
  63. public function isFavorite () {
  64. return $this->is_favorite;
  65. }
  66. public function feed ($object = false) {
  67. if ($object) {
  68. $feedDAO = new FeedDAO ();
  69. return $feedDAO->searchById ($this->feed);
  70. } else {
  71. return $this->feed;
  72. }
  73. }
  74. public function tags ($inString = false) {
  75. if ($inString) {
  76. if (!empty ($this->tags)) {
  77. return '#' . implode(' #', $this->tags);
  78. } else {
  79. return '';
  80. }
  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 (is_int (intval ($value))) {
  105. $this->date = $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 = getdate ();
  132. $today = mktime (0, 0, 0, $date['mon'], $date['mday'], $date['year']);
  133. $yesterday = $today - 86400;
  134. if ($day == Days::TODAY &&
  135. $this->date >= $today && $this->date < $today + 86400) {
  136. return true;
  137. } elseif ($day == Days::YESTERDAY &&
  138. $this->date >= $yesterday && $this->date < $yesterday + 86400) {
  139. return true;
  140. } elseif ($day == Days::BEFORE_YESTERDAY && $this->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, link, date, is_read, is_favorite, id_feed, tags) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
  186. $stm = $this->bd->prepare ($sql);
  187. $values = array (
  188. $valuesTmp['id'],
  189. substr($valuesTmp['guid'], 0, 760),
  190. substr($valuesTmp['title'], 0, 255),
  191. substr($valuesTmp['author'], 0, 255),
  192. base64_encode (gzdeflate (serialize ($valuesTmp['content']))),
  193. substr($valuesTmp['link'], 0, 1023),
  194. $valuesTmp['date'],
  195. $valuesTmp['is_read'],
  196. $valuesTmp['is_favorite'],
  197. $valuesTmp['id_feed'],
  198. substr($valuesTmp['tags'], 0, 1023),
  199. );
  200. if ($stm && $stm->execute ($values)) {
  201. return $this->bd->lastInsertId();
  202. } else {
  203. $info = $stm->errorInfo();
  204. if ((int)($info[0] / 1000) !== 23) { //Filter out "SQLSTATE Class code 23: Constraint Violation" because of expected duplicate entries
  205. Minz_Log::record ('SQL error ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
  206. . ' while adding entry in feed ' . $valuesTmp['id_feed'] . ' with title: ' . $valuesTmp['title'], Minz_Log::ERROR);
  207. } /*else {
  208. Minz_Log::record ('SQL error ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
  209. . ' while adding entry in feed ' . $valuesTmp['id_feed'] . ' with title: ' . $valuesTmp['title'], Minz_Log::DEBUG);
  210. }*/
  211. return false;
  212. }
  213. }
  214. /*public function updateEntry ($id, $valuesTmp) {
  215. if (isset ($valuesTmp['content'])) {
  216. $valuesTmp['content'] = base64_encode (gzdeflate (serialize ($valuesTmp['content'])));
  217. }
  218. $set = '';
  219. foreach ($valuesTmp as $key => $v) {
  220. $set .= $key . '=?, ';
  221. }
  222. $set = substr ($set, 0, -2);
  223. $sql = 'UPDATE ' . $this->prefix . 'entry SET ' . $set . ' WHERE id=?';
  224. $stm = $this->bd->prepare ($sql);
  225. foreach ($valuesTmp as $v) {
  226. $values[] = $v;
  227. }
  228. $values[] = $id;
  229. if ($stm && $stm->execute ($values)) {
  230. return $stm->rowCount();
  231. } else {
  232. $info = $stm->errorInfo();
  233. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  234. return false;
  235. }
  236. }*/
  237. public function markFavorite ($id, $is_favorite = true) {
  238. $sql = 'UPDATE ' . $this->prefix . 'entry e '
  239. . 'SET e.is_favorite = ? '
  240. . 'WHERE e.id=?';
  241. $values = array ($is_favorite ? 1 : 0, $id);
  242. $stm = $this->bd->prepare ($sql);
  243. if ($stm && $stm->execute ($values)) {
  244. return $stm->rowCount();
  245. } else {
  246. $info = $stm->errorInfo();
  247. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  248. return false;
  249. }
  250. }
  251. public function markRead ($id, $is_read = true) {
  252. $sql = 'UPDATE ' . $this->prefix . 'entry e INNER JOIN ' . $this->prefix . 'feed f ON e.id_feed = f.id '
  253. . 'SET e.is_read = ?,'
  254. . 'f.cache_nbUnreads=f.cache_nbUnreads' . ($is_read ? '-' : '+') . '1 '
  255. . 'WHERE e.id=?';
  256. $values = array ($is_read ? 1 : 0, $id);
  257. $stm = $this->bd->prepare ($sql);
  258. if ($stm && $stm->execute ($values)) {
  259. return $stm->rowCount();
  260. } else {
  261. $info = $stm->errorInfo();
  262. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  263. return false;
  264. }
  265. }
  266. public function markReadEntries ($dateMax = 0) {
  267. if ($dateMax === 0) {
  268. $sql = 'UPDATE ' . $this->prefix . 'entry e INNER JOIN ' . $this->prefix . 'feed f ON e.id_feed = f.id '
  269. . 'SET e.is_read = 1, f.cache_nbUnreads=0 '
  270. . 'WHERE e.is_read = 0 AND f.priority > 0';
  271. $stm = $this->bd->prepare ($sql);
  272. if ($stm && $stm->execute ()) {
  273. return $stm->rowCount();
  274. } else {
  275. $info = $stm->errorInfo();
  276. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  277. return false;
  278. }
  279. } else {
  280. $this->bd->beginTransaction ();
  281. $sql = 'UPDATE ' . $this->prefix . 'entry e INNER JOIN ' . $this->prefix . 'feed f ON e.id_feed = f.id '
  282. . 'SET e.is_read = 1 '
  283. . 'WHERE e.is_read = 0 AND e.date < ? AND f.priority > 0';
  284. $values = array ($dateMax);
  285. $stm = $this->bd->prepare ($sql);
  286. if (!($stm && $stm->execute ($values))) {
  287. $info = $stm->errorInfo();
  288. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  289. $this->bd->rollBack ();
  290. return false;
  291. }
  292. $affected = $stm->rowCount();
  293. if ($affected > 0) {
  294. $sql = 'UPDATE ' . $this->prefix . 'feed f '
  295. . 'LEFT OUTER JOIN ('
  296. . 'SELECT e.id_feed, '
  297. . 'COUNT(*) AS nbUnreads '
  298. . 'FROM ' . $this->prefix . 'entry e '
  299. . 'WHERE e.is_read = 0 '
  300. . 'GROUP BY e.id_feed'
  301. . ') x ON x.id_feed=f.id '
  302. . 'SET f.cache_nbUnreads=COALESCE(x.nbUnreads, 0)';
  303. $stm = $this->bd->prepare ($sql);
  304. if (!($stm && $stm->execute ())) {
  305. $info = $stm->errorInfo();
  306. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  307. $this->bd->rollBack ();
  308. return false;
  309. }
  310. }
  311. $this->bd->commit ();
  312. return $affected;
  313. }
  314. }
  315. public function markReadCat ($id, $dateMax = 0) {
  316. if ($dateMax === 0) {
  317. $sql = 'UPDATE ' . $this->prefix . 'entry e INNER JOIN ' . $this->prefix . 'feed f ON e.id_feed = f.id '
  318. . 'SET e.is_read = 1, f.cache_nbUnreads=0 '
  319. . 'WHERE f.category = ? AND e.is_read = 0';
  320. $values = array ($id);
  321. $stm = $this->bd->prepare ($sql);
  322. if ($stm && $stm->execute ($values)) {
  323. return $stm->rowCount();
  324. } else {
  325. $info = $stm->errorInfo();
  326. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  327. return false;
  328. }
  329. } else {
  330. $this->bd->beginTransaction ();
  331. $sql = 'UPDATE ' . $this->prefix . 'entry e INNER JOIN ' . $this->prefix . 'feed f ON e.id_feed = f.id '
  332. . 'SET e.is_read = 1 '
  333. . 'WHERE f.category = ? AND e.is_read = 0 AND e.date < ?';
  334. $values = array ($id, $dateMax);
  335. $stm = $this->bd->prepare ($sql);
  336. if (!($stm && $stm->execute ($values))) {
  337. $info = $stm->errorInfo();
  338. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  339. $this->bd->rollBack ();
  340. return false;
  341. }
  342. $affected = $stm->rowCount();
  343. if ($affected > 0) {
  344. $sql = 'UPDATE ' . $this->prefix . 'feed f '
  345. . 'LEFT OUTER JOIN ('
  346. . 'SELECT e.id_feed, '
  347. . 'COUNT(*) AS nbUnreads '
  348. . 'FROM ' . $this->prefix . 'entry e '
  349. . 'WHERE e.is_read = 0 '
  350. . 'GROUP BY e.id_feed'
  351. . ') x ON x.id_feed=f.id '
  352. . 'SET f.cache_nbUnreads=COALESCE(x.nbUnreads, 0) '
  353. . 'WHERE f.category = ?';
  354. $values = array ($id);
  355. $stm = $this->bd->prepare ($sql);
  356. if (!($stm && $stm->execute ($values))) {
  357. $info = $stm->errorInfo();
  358. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  359. $this->bd->rollBack ();
  360. return false;
  361. }
  362. }
  363. $this->bd->commit ();
  364. return $affected;
  365. }
  366. }
  367. public function markReadFeed ($id, $dateMax = 0) {
  368. if ($dateMax === 0) {
  369. $sql = 'UPDATE ' . $this->prefix . 'entry e INNER JOIN ' . $this->prefix . 'feed f ON e.id_feed = f.id '
  370. . 'SET e.is_read = 1, f.cache_nbUnreads=0 '
  371. . 'WHERE f.id=? AND e.is_read = 0';
  372. $values = array ($id);
  373. $stm = $this->bd->prepare ($sql);
  374. if ($stm && $stm->execute ($values)) {
  375. return $stm->rowCount();
  376. } else {
  377. $info = $stm->errorInfo();
  378. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  379. return false;
  380. }
  381. } else {
  382. $this->bd->beginTransaction ();
  383. $sql = 'UPDATE ' . $this->prefix . 'entry e INNER JOIN ' . $this->prefix . 'feed f ON e.id_feed = f.id '
  384. . 'SET e.is_read = 1 '
  385. . 'WHERE f.id=? AND e.is_read = 0 AND e.date < ?';
  386. $values = array ($id, $dateMax);
  387. $stm = $this->bd->prepare ($sql);
  388. if (!($stm && $stm->execute ($values))) {
  389. $info = $stm->errorInfo();
  390. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  391. $this->bd->rollBack ();
  392. return false;
  393. }
  394. $affected = $stm->rowCount();
  395. if ($affected > 0) {
  396. $sql = 'UPDATE ' . $this->prefix . 'feed f '
  397. . 'SET f.cache_nbUnreads=f.cache_nbUnreads-' . $affected
  398. . ' WHERE f.id=?';
  399. $values = array ($id);
  400. $stm = $this->bd->prepare ($sql);
  401. if (!($stm && $stm->execute ($values))) {
  402. $info = $stm->errorInfo();
  403. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  404. $this->bd->rollBack ();
  405. return false;
  406. }
  407. }
  408. $this->bd->commit ();
  409. return $affected;
  410. }
  411. }
  412. /*public function updateEntries ($valuesTmp) {
  413. if (isset ($valuesTmp['content'])) {
  414. $valuesTmp['content'] = base64_encode (gzdeflate (serialize ($valuesTmp['content'])));
  415. }
  416. $set = '';
  417. foreach ($valuesTmp as $key => $v) {
  418. $set .= $key . '=?, ';
  419. }
  420. $set = substr ($set, 0, -2);
  421. $sql = 'UPDATE ' . $this->prefix . 'entry SET ' . $set;
  422. $stm = $this->bd->prepare ($sql);
  423. foreach ($valuesTmp as $v) {
  424. $values[] = $v;
  425. }
  426. if ($stm && $stm->execute ($values)) {
  427. return $stm->rowCount();
  428. } else {
  429. $info = $stm->errorInfo();
  430. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  431. return false;
  432. }
  433. }*/
  434. public function cleanOldEntries ($date_min) {
  435. $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';
  436. $stm = $this->bd->prepare ($sql);
  437. $values = array (
  438. $date_min
  439. );
  440. if ($stm && $stm->execute ($values)) {
  441. return $stm->rowCount();
  442. } else {
  443. $info = $stm->errorInfo();
  444. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  445. return false;
  446. }
  447. }
  448. public function searchByGuid ($feed_id, $id) {
  449. // un guid est unique pour un flux donné
  450. $sql = 'SELECT * FROM ' . $this->prefix . 'entry WHERE id_feed=? AND guid=?';
  451. $stm = $this->bd->prepare ($sql);
  452. $values = array (
  453. $feed_id,
  454. $id
  455. );
  456. $stm->execute ($values);
  457. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  458. list ($entry, $next) = HelperEntry::daoToEntry ($res);
  459. if (isset ($entry[0])) {
  460. return $entry[0];
  461. } else {
  462. return false;
  463. }
  464. }
  465. public function searchById ($id) {
  466. $sql = 'SELECT * FROM ' . $this->prefix . 'entry WHERE id=?';
  467. $stm = $this->bd->prepare ($sql);
  468. $values = array ($id);
  469. $stm->execute ($values);
  470. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  471. list ($entry, $next) = HelperEntry::daoToEntry ($res);
  472. if (isset ($entry[0])) {
  473. return $entry[0];
  474. } else {
  475. return false;
  476. }
  477. }
  478. private function listWhere ($where, $state, $order, $limitFromId = '', $limitCount = '', $values = array ()) {
  479. if ($state === 'not_read') {
  480. $where .= ' AND is_read = 0';
  481. } elseif ($state === 'read') {
  482. $where .= ' AND is_read = 1';
  483. }
  484. if (!empty($limitFromId)) { //TODO: Consider using LPAD(e.date, 11) //CONCAT is for cases when many entries have the same date
  485. $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 . '")';
  486. }
  487. if ($order === 'low_to_high') {
  488. $order = ' DESC';
  489. } else {
  490. $order = '';
  491. }
  492. $sql = 'SELECT e.* FROM ' . $this->prefix . 'entry e'
  493. . ' INNER JOIN ' . $this->prefix . 'feed f ON e.id_feed = f.id' . $where
  494. . ' ORDER BY e.date' . $order . ', e.id' . $order;
  495. if (empty($limitCount)) {
  496. $limitCount = 20000; //TODO: FIXME: Hack temporaire en attendant la recherche côté base-de-données
  497. }
  498. //if (!empty($limitCount)) {
  499. $sql .= ' LIMIT ' . ($limitCount + 2); //TODO: See http://explainextended.com/2009/10/23/mysql-order-by-limit-performance-late-row-lookups/
  500. //}
  501. $stm = $this->bd->prepare ($sql);
  502. $stm->execute ($values);
  503. return HelperEntry::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
  504. }
  505. public function listEntries ($state, $order = 'high_to_low', $limitFromId = '', $limitCount = '') {
  506. return $this->listWhere (' WHERE priority > 0', $state, $order, $limitFromId, $limitCount);
  507. }
  508. public function listFavorites ($state, $order = 'high_to_low', $limitFromId = '', $limitCount = '') {
  509. return $this->listWhere (' WHERE is_favorite = 1', $state, $order, $limitFromId, $limitCount);
  510. }
  511. public function listByCategory ($cat, $state, $order = 'high_to_low', $limitFromId = '', $limitCount = '') {
  512. return $this->listWhere (' WHERE category = ?', $state, $order, $limitFromId, $limitCount, array ($cat));
  513. }
  514. public function listByFeed ($feed, $state, $order = 'high_to_low', $limitFromId = '', $limitCount = '') {
  515. return $this->listWhere (' WHERE id_feed = ?', $state, $order, $limitFromId, $limitCount, array ($feed));
  516. }
  517. public function listLastIdsByFeed($id, $n) {
  518. $sql = 'SELECT id FROM ' . $this->prefix . 'entry WHERE id_feed=? ORDER BY date DESC LIMIT ' . intval($n);
  519. $stm = $this->bd->prepare ($sql);
  520. $values = array ($id);
  521. $stm->execute ($values);
  522. return $stm->fetchAll (PDO::FETCH_COLUMN, 0);
  523. }
  524. public function countUnreadRead () {
  525. $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'
  526. . ' 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';
  527. $stm = $this->bd->prepare ($sql);
  528. $stm->execute ();
  529. $res = $stm->fetchAll (PDO::FETCH_COLUMN, 0);
  530. $all = empty($res[0]) ? 0 : $res[0];
  531. $unread = empty($res[1]) ? 0 : $res[1];
  532. return array('all' => $all, 'unread' => $unread, 'read' => $all - $unread);
  533. }
  534. public function count ($minPriority = null) {
  535. $sql = 'SELECT COUNT(e.id) AS count FROM ' . $this->prefix . 'entry e INNER JOIN ' . $this->prefix . 'feed f ON e.id_feed = f.id';
  536. if ($minPriority !== null) {
  537. $sql = ' WHERE priority > ' . intval($minPriority);
  538. }
  539. $stm = $this->bd->prepare ($sql);
  540. $stm->execute ();
  541. $res = $stm->fetchAll (PDO::FETCH_COLUMN, 0);
  542. return $res[0];
  543. }
  544. public function countNotRead ($minPriority = null) {
  545. $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';
  546. if ($minPriority !== null) {
  547. $sql = ' AND priority > ' . intval($minPriority);
  548. }
  549. $stm = $this->bd->prepare ($sql);
  550. $stm->execute ();
  551. $res = $stm->fetchAll (PDO::FETCH_COLUMN, 0);
  552. return $res[0];
  553. }
  554. public function countUnreadReadFavorites () {
  555. $sql = 'SELECT COUNT(id) FROM ' . $this->prefix . 'entry WHERE is_favorite=1'
  556. . ' UNION SELECT COUNT(id) FROM ' . $this->prefix . 'entry WHERE is_favorite=1 AND is_read = 0';
  557. $stm = $this->bd->prepare ($sql);
  558. $stm->execute ();
  559. $res = $stm->fetchAll (PDO::FETCH_COLUMN, 0);
  560. $all = empty($res[0]) ? 0 : $res[0];
  561. $unread = empty($res[1]) ? 0 : $res[1];
  562. return array('all' => $all, 'unread' => $unread, 'read' => $all - $unread);
  563. }
  564. public function optimizeTable() {
  565. $sql = 'OPTIMIZE TABLE ' . $this->prefix . 'entry';
  566. $stm = $this->bd->prepare ($sql);
  567. $stm->execute ();
  568. }
  569. }
  570. class HelperEntry {
  571. public static $nb = 1;
  572. public static $first = '';
  573. public static $filter = array (
  574. 'words' => array (),
  575. 'tags' => array (),
  576. );
  577. public static function daoToEntry ($listDAO) {
  578. $list = array ();
  579. if (!is_array ($listDAO)) {
  580. $listDAO = array ($listDAO);
  581. }
  582. $count = 0;
  583. $first_is_found = false;
  584. $break_after = false;
  585. $next = '';
  586. foreach ($listDAO as $key => $dao) {
  587. $dao['content'] = unserialize (gzinflate (base64_decode ($dao['content'])));
  588. $dao['tags'] = preg_split('/[\s#]/', $dao['tags']);
  589. if (self::tagsMatchEntry ($dao) &&
  590. self::searchMatchEntry ($dao)) {
  591. if ($break_after) {
  592. $next = $dao['id'];
  593. break;
  594. }
  595. if ($first_is_found || $dao['id'] == self::$first || self::$first == '') {
  596. $list[$key] = self::createEntry ($dao);
  597. $count++;
  598. $first_is_found = true; //TODO: Update: Now done in SQL
  599. }
  600. if ($count >= self::$nb) {
  601. $break_after = true;
  602. }
  603. }
  604. }
  605. unset ($listDAO);
  606. return array ($list, $next);
  607. }
  608. private static function createEntry ($dao) {
  609. $entry = new Entry (
  610. $dao['id_feed'],
  611. $dao['guid'],
  612. $dao['title'],
  613. $dao['author'],
  614. $dao['content'],
  615. $dao['link'],
  616. $dao['date'],
  617. $dao['is_read'],
  618. $dao['is_favorite']
  619. );
  620. $entry->_tags ($dao['tags']);
  621. if (isset ($dao['id'])) {
  622. $entry->_id ($dao['id']);
  623. }
  624. return $entry;
  625. }
  626. private static function tagsMatchEntry ($dao) {
  627. $tags = self::$filter['tags'];
  628. foreach ($tags as $tag) {
  629. if (!in_array ($tag, $dao['tags'])) {
  630. return false;
  631. }
  632. }
  633. return true;
  634. }
  635. private static function searchMatchEntry ($dao) {
  636. $words = self::$filter['words'];
  637. foreach ($words as $word) {
  638. $word = strtolower ($word);
  639. if (strpos (strtolower ($dao['title']), $word) === false &&
  640. strpos (strtolower ($dao['content']), $word) === false &&
  641. strpos (strtolower ($dao['link']), $word) === false) {
  642. return false;
  643. }
  644. }
  645. return true;
  646. }
  647. }