Entry.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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_bin, link, date, is_read, is_favorite, id_feed, tags) '
  190. . 'VALUES(CAST(? * 1000000 AS SIGNED INTEGER), ?, ?, ?, COMPRESS(?), ?, ?, ?, ?, ?, ?)';
  191. $stm = $this->bd->prepare ($sql);
  192. $values = array (
  193. $valuesTmp['id'],
  194. substr($valuesTmp['guid'], 0, 760),
  195. substr($valuesTmp['title'], 0, 255),
  196. substr($valuesTmp['author'], 0, 255),
  197. $valuesTmp['content'],
  198. substr($valuesTmp['link'], 0, 1023),
  199. $valuesTmp['date'],
  200. $valuesTmp['is_read'],
  201. $valuesTmp['is_favorite'],
  202. $valuesTmp['id_feed'],
  203. substr($valuesTmp['tags'], 0, 1023),
  204. );
  205. if ($stm && $stm->execute ($values)) {
  206. return $this->bd->lastInsertId();
  207. } else {
  208. $info = $stm->errorInfo();
  209. if ((int)($info[0] / 1000) !== 23) { //Filter out "SQLSTATE Class code 23: Constraint Violation" because of expected duplicate entries
  210. Minz_Log::record ('SQL error ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
  211. . ' while adding entry in feed ' . $valuesTmp['id_feed'] . ' with title: ' . $valuesTmp['title'], Minz_Log::ERROR);
  212. } /*else {
  213. Minz_Log::record ('SQL error ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
  214. . ' while adding entry in feed ' . $valuesTmp['id_feed'] . ' with title: ' . $valuesTmp['title'], Minz_Log::DEBUG);
  215. }*/
  216. return false;
  217. }
  218. }
  219. public function markFavorite ($id, $is_favorite = true) {
  220. $sql = 'UPDATE `' . $this->prefix . 'entry` e '
  221. . 'SET e.is_favorite = ? '
  222. . 'WHERE e.id=?';
  223. $values = array ($is_favorite ? 1 : 0, $id);
  224. $stm = $this->bd->prepare ($sql);
  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 markRead ($id, $is_read = true) {
  234. $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
  235. . 'SET e.is_read = ?,'
  236. . 'f.cache_nbUnreads=f.cache_nbUnreads' . ($is_read ? '-' : '+') . '1 '
  237. . 'WHERE e.id=?';
  238. $values = array ($is_read ? 1 : 0, $id);
  239. $stm = $this->bd->prepare ($sql);
  240. if ($stm && $stm->execute ($values)) {
  241. return $stm->rowCount();
  242. } else {
  243. $info = $stm->errorInfo();
  244. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  245. return false;
  246. }
  247. }
  248. public function markReadEntries ($idMax = 0) {
  249. if ($idMax === 0) {
  250. $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
  251. . 'SET e.is_read = 1, f.cache_nbUnreads=0 '
  252. . 'WHERE e.is_read = 0 AND f.priority > 0';
  253. $stm = $this->bd->prepare ($sql);
  254. if ($stm && $stm->execute ()) {
  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. } else {
  262. $this->bd->beginTransaction ();
  263. $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
  264. . 'SET e.is_read = 1 '
  265. . 'WHERE e.is_read = 0 AND e.id <= ? AND f.priority > 0';
  266. $values = array ($idMax);
  267. $stm = $this->bd->prepare ($sql);
  268. if (!($stm && $stm->execute ($values))) {
  269. $info = $stm->errorInfo();
  270. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  271. $this->bd->rollBack ();
  272. return false;
  273. }
  274. $affected = $stm->rowCount();
  275. if ($affected > 0) {
  276. $sql = 'UPDATE `' . $this->prefix . 'feed` f '
  277. . 'LEFT OUTER JOIN ('
  278. . 'SELECT e.id_feed, '
  279. . 'COUNT(*) AS nbUnreads '
  280. . 'FROM `' . $this->prefix . 'entry` e '
  281. . 'WHERE e.is_read = 0 '
  282. . 'GROUP BY e.id_feed'
  283. . ') x ON x.id_feed=f.id '
  284. . 'SET f.cache_nbUnreads=COALESCE(x.nbUnreads, 0)';
  285. $stm = $this->bd->prepare ($sql);
  286. if (!($stm && $stm->execute ())) {
  287. $info = $stm->errorInfo();
  288. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  289. $this->bd->rollBack ();
  290. return false;
  291. }
  292. }
  293. $this->bd->commit ();
  294. return $affected;
  295. }
  296. }
  297. public function markReadCat ($id, $idMax = 0) {
  298. if ($idMax === 0) {
  299. $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
  300. . 'SET e.is_read = 1, f.cache_nbUnreads=0 '
  301. . 'WHERE f.category = ? AND e.is_read = 0';
  302. $values = array ($id);
  303. $stm = $this->bd->prepare ($sql);
  304. if ($stm && $stm->execute ($values)) {
  305. return $stm->rowCount();
  306. } else {
  307. $info = $stm->errorInfo();
  308. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  309. return false;
  310. }
  311. } else {
  312. $this->bd->beginTransaction ();
  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 '
  315. . 'WHERE f.category = ? AND e.is_read = 0 AND e.id <= ?';
  316. $values = array ($id, $idMax);
  317. $stm = $this->bd->prepare ($sql);
  318. if (!($stm && $stm->execute ($values))) {
  319. $info = $stm->errorInfo();
  320. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  321. $this->bd->rollBack ();
  322. return false;
  323. }
  324. $affected = $stm->rowCount();
  325. if ($affected > 0) {
  326. $sql = 'UPDATE `' . $this->prefix . 'feed` f '
  327. . 'LEFT OUTER JOIN ('
  328. . 'SELECT e.id_feed, '
  329. . 'COUNT(*) AS nbUnreads '
  330. . 'FROM `' . $this->prefix . 'entry` e '
  331. . 'WHERE e.is_read = 0 '
  332. . 'GROUP BY e.id_feed'
  333. . ') x ON x.id_feed=f.id '
  334. . 'SET f.cache_nbUnreads=COALESCE(x.nbUnreads, 0) '
  335. . 'WHERE f.category = ?';
  336. $values = array ($id);
  337. $stm = $this->bd->prepare ($sql);
  338. if (!($stm && $stm->execute ($values))) {
  339. $info = $stm->errorInfo();
  340. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  341. $this->bd->rollBack ();
  342. return false;
  343. }
  344. }
  345. $this->bd->commit ();
  346. return $affected;
  347. }
  348. }
  349. public function markReadFeed ($id, $idMax = 0) {
  350. if ($idMax === 0) {
  351. $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
  352. . 'SET e.is_read = 1, f.cache_nbUnreads=0 '
  353. . 'WHERE f.id=? AND e.is_read = 0';
  354. $values = array ($id);
  355. $stm = $this->bd->prepare ($sql);
  356. if ($stm && $stm->execute ($values)) {
  357. return $stm->rowCount();
  358. } else {
  359. $info = $stm->errorInfo();
  360. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  361. return false;
  362. }
  363. } else {
  364. $this->bd->beginTransaction ();
  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 '
  367. . 'WHERE f.id=? AND e.is_read = 0 AND e.id <= ?';
  368. $values = array ($id, $idMax);
  369. $stm = $this->bd->prepare ($sql);
  370. if (!($stm && $stm->execute ($values))) {
  371. $info = $stm->errorInfo();
  372. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  373. $this->bd->rollBack ();
  374. return false;
  375. }
  376. $affected = $stm->rowCount();
  377. if ($affected > 0) {
  378. $sql = 'UPDATE `' . $this->prefix . 'feed` f '
  379. . 'SET f.cache_nbUnreads=f.cache_nbUnreads-' . $affected
  380. . ' WHERE f.id=?';
  381. $values = array ($id);
  382. $stm = $this->bd->prepare ($sql);
  383. if (!($stm && $stm->execute ($values))) {
  384. $info = $stm->errorInfo();
  385. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  386. $this->bd->rollBack ();
  387. return false;
  388. }
  389. }
  390. $this->bd->commit ();
  391. return $affected;
  392. }
  393. }
  394. public function cleanOldEntries ($date_min) {
  395. $sql = 'DELETE e.* FROM `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
  396. . 'WHERE e.id <= ? AND e.is_favorite = 0 AND f.keep_history = 0';
  397. $stm = $this->bd->prepare ($sql);
  398. $values = array (
  399. $date_min . '000000'
  400. );
  401. if ($stm && $stm->execute ($values)) {
  402. return $stm->rowCount();
  403. } else {
  404. $info = $stm->errorInfo();
  405. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  406. return false;
  407. }
  408. }
  409. public function searchByGuid ($feed_id, $id) {
  410. // un guid est unique pour un flux donné
  411. $sql = 'SELECT id, guid, title, author, UNCOMPRESS(content_bin) AS content, link, date, is_read, is_favorite, id_feed, tags '
  412. . 'FROM `' . $this->prefix . 'entry` WHERE id_feed=? AND guid=?';
  413. $stm = $this->bd->prepare ($sql);
  414. $values = array (
  415. $feed_id,
  416. $id
  417. );
  418. $stm->execute ($values);
  419. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  420. list ($entry, $next) = HelperEntry::daoToEntry ($res);
  421. if (isset ($entry[0])) {
  422. return $entry[0];
  423. } else {
  424. return false;
  425. }
  426. }
  427. public function searchById ($id) {
  428. $sql = 'SELECT id, guid, title, author, UNCOMPRESS(content_bin) AS content, link, date, is_read, is_favorite, id_feed, tags '
  429. . 'FROM `' . $this->prefix . 'entry` WHERE id=?';
  430. $stm = $this->bd->prepare ($sql);
  431. $values = array ($id);
  432. $stm->execute ($values);
  433. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  434. list ($entry, $next) = HelperEntry::daoToEntry ($res);
  435. if (isset ($entry[0])) {
  436. return $entry[0];
  437. } else {
  438. return false;
  439. }
  440. }
  441. private function listWhere ($where, $state, $order, $limitFromId = '', $limitCount = '', $values = array ()) {
  442. if ($state === 'not_read') {
  443. $where .= ' AND is_read = 0';
  444. } elseif ($state === 'read') {
  445. $where .= ' AND is_read = 1';
  446. }
  447. if (!empty($limitFromId)) {
  448. $where .= ' AND e.id ' . ($order === 'low_to_high' ? '<=' : '>=') . $limitFromId;
  449. }
  450. if ($order === 'low_to_high') {
  451. $order = ' DESC';
  452. } else {
  453. $order = '';
  454. }
  455. $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 '
  456. . 'FROM `' . $this->prefix . 'entry` e '
  457. . 'INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id ' . $where
  458. . ' ORDER BY e.id' . $order;
  459. if (empty($limitCount)) {
  460. $limitCount = 20000; //TODO: FIXME: Hack temporaire en attendant la recherche côté base-de-données
  461. }
  462. //if (!empty($limitCount)) {
  463. $sql .= ' LIMIT ' . ($limitCount + 2); //TODO: See http://explainextended.com/2009/10/23/mysql-order-by-limit-performance-late-row-lookups/
  464. //}
  465. $stm = $this->bd->prepare ($sql);
  466. $stm->execute ($values);
  467. return HelperEntry::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
  468. }
  469. public function listEntries ($state, $order = 'high_to_low', $limitFromId = '', $limitCount = '') {
  470. return $this->listWhere ('WHERE priority > 0', $state, $order, $limitFromId, $limitCount);
  471. }
  472. public function listFavorites ($state, $order = 'high_to_low', $limitFromId = '', $limitCount = '') {
  473. return $this->listWhere ('WHERE is_favorite = 1', $state, $order, $limitFromId, $limitCount);
  474. }
  475. public function listByCategory ($cat, $state, $order = 'high_to_low', $limitFromId = '', $limitCount = '') {
  476. return $this->listWhere ('WHERE category = ?', $state, $order, $limitFromId, $limitCount, array ($cat));
  477. }
  478. public function listByFeed ($feed, $state, $order = 'high_to_low', $limitFromId = '', $limitCount = '') {
  479. return $this->listWhere ('WHERE id_feed = ?', $state, $order, $limitFromId, $limitCount, array ($feed));
  480. }
  481. public function listLastGuidsByFeed($id, $n) {
  482. $sql = 'SELECT guid FROM `' . $this->prefix . 'entry` WHERE id_feed=? ORDER BY id DESC LIMIT ' . intval($n);
  483. $stm = $this->bd->prepare ($sql);
  484. $values = array ($id);
  485. $stm->execute ($values);
  486. return $stm->fetchAll (PDO::FETCH_COLUMN, 0);
  487. }
  488. public function countUnreadRead () {
  489. $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'
  490. . ' 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';
  491. $stm = $this->bd->prepare ($sql);
  492. $stm->execute ();
  493. $res = $stm->fetchAll (PDO::FETCH_COLUMN, 0);
  494. $all = empty($res[0]) ? 0 : $res[0];
  495. $unread = empty($res[1]) ? 0 : $res[1];
  496. return array('all' => $all, 'unread' => $unread, 'read' => $all - $unread);
  497. }
  498. public function count ($minPriority = null) {
  499. $sql = 'SELECT COUNT(e.id) AS count FROM `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id';
  500. if ($minPriority !== null) {
  501. $sql = ' WHERE priority > ' . intval($minPriority);
  502. }
  503. $stm = $this->bd->prepare ($sql);
  504. $stm->execute ();
  505. $res = $stm->fetchAll (PDO::FETCH_COLUMN, 0);
  506. return $res[0];
  507. }
  508. public function countNotRead ($minPriority = null) {
  509. $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';
  510. if ($minPriority !== null) {
  511. $sql = ' AND priority > ' . intval($minPriority);
  512. }
  513. $stm = $this->bd->prepare ($sql);
  514. $stm->execute ();
  515. $res = $stm->fetchAll (PDO::FETCH_COLUMN, 0);
  516. return $res[0];
  517. }
  518. public function countUnreadReadFavorites () {
  519. $sql = 'SELECT COUNT(id) FROM `' . $this->prefix . 'entry` WHERE is_favorite=1'
  520. . ' UNION SELECT COUNT(id) FROM `' . $this->prefix . 'entry` WHERE is_favorite=1 AND is_read = 0';
  521. $stm = $this->bd->prepare ($sql);
  522. $stm->execute ();
  523. $res = $stm->fetchAll (PDO::FETCH_COLUMN, 0);
  524. $all = empty($res[0]) ? 0 : $res[0];
  525. $unread = empty($res[1]) ? 0 : $res[1];
  526. return array('all' => $all, 'unread' => $unread, 'read' => $all - $unread);
  527. }
  528. public function optimizeTable() {
  529. $sql = 'OPTIMIZE TABLE `' . $this->prefix . 'entry`';
  530. $stm = $this->bd->prepare ($sql);
  531. $stm->execute ();
  532. }
  533. }
  534. class HelperEntry {
  535. public static $nb = 1;
  536. public static $first = '';
  537. public static $filter = array (
  538. 'words' => array (),
  539. 'tags' => array (),
  540. );
  541. public static function daoToEntry ($listDAO) {
  542. $list = array ();
  543. if (!is_array ($listDAO)) {
  544. $listDAO = array ($listDAO);
  545. }
  546. $count = 0;
  547. $first_is_found = false;
  548. $break_after = false;
  549. $next = '';
  550. foreach ($listDAO as $key => $dao) {
  551. $dao['tags'] = preg_split('/[\s#]/', $dao['tags']);
  552. if (self::tagsMatchEntry ($dao) &&
  553. self::searchMatchEntry ($dao)) {
  554. if ($break_after) {
  555. $next = $dao['id'];
  556. break;
  557. }
  558. if ($first_is_found || $dao['id'] == self::$first || self::$first == '') {
  559. $list[$key] = self::createEntry ($dao);
  560. $count++;
  561. $first_is_found = true; //TODO: Update: Now done in SQL
  562. }
  563. if ($count >= self::$nb) {
  564. $break_after = true;
  565. }
  566. }
  567. }
  568. unset ($listDAO);
  569. return array ($list, $next);
  570. }
  571. private static function createEntry ($dao) {
  572. $entry = new Entry (
  573. $dao['id_feed'],
  574. $dao['guid'],
  575. $dao['title'],
  576. $dao['author'],
  577. $dao['content'],
  578. $dao['link'],
  579. $dao['date'],
  580. $dao['is_read'],
  581. $dao['is_favorite']
  582. );
  583. $entry->_tags ($dao['tags']);
  584. if (isset ($dao['id'])) {
  585. $entry->_id ($dao['id']);
  586. }
  587. return $entry;
  588. }
  589. private static function tagsMatchEntry ($dao) {
  590. $tags = self::$filter['tags'];
  591. foreach ($tags as $tag) {
  592. if (!in_array ($tag, $dao['tags'])) {
  593. return false;
  594. }
  595. }
  596. return true;
  597. }
  598. private static function searchMatchEntry ($dao) {
  599. $words = self::$filter['words'];
  600. foreach ($words as $word) {
  601. $word = strtolower ($word);
  602. if (strpos (strtolower ($dao['title']), $word) === false &&
  603. strpos (strtolower ($dao['content']), $word) === false &&
  604. strpos (strtolower ($dao['link']), $word) === false) {
  605. return false;
  606. }
  607. }
  608. return true;
  609. }
  610. }