Entry.php 21 KB

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