Entry.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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. $valuesTmp['guid'],
  190. $valuesTmp['title'],
  191. $valuesTmp['author'],
  192. base64_encode (gzdeflate (serialize ($valuesTmp['content']))),
  193. $valuesTmp['link'],
  194. $valuesTmp['date'],
  195. $valuesTmp['is_read'],
  196. $valuesTmp['is_favorite'],
  197. $valuesTmp['id_feed'],
  198. $valuesTmp['tags'],
  199. );
  200. if ($stm && $stm->execute ($values)) {
  201. return true;
  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], Minz_Log::NOTICE); //TODO: Consider adding a Minz_Log::DEBUG level
  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 true;
  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 markReadEntries ($read, $dateMax = 0) {
  234. $sql = 'UPDATE ' . $this->prefix . 'entry e INNER JOIN ' . $this->prefix . 'feed f ON e.id_feed = f.id SET is_read = ? WHERE priority > 0';
  235. $values = array ($read);
  236. if ($dateMax > 0) {
  237. $sql .= ' AND date < ?';
  238. $values[] = $dateMax;
  239. }
  240. $stm = $this->bd->prepare ($sql);
  241. if ($stm && $stm->execute ($values)) {
  242. return true;
  243. } else {
  244. $info = $stm->errorInfo();
  245. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  246. return false;
  247. }
  248. }
  249. public function markReadCat ($id, $read, $dateMax = 0) {
  250. $sql = 'UPDATE ' . $this->prefix . 'entry e INNER JOIN ' . $this->prefix . 'feed f ON e.id_feed = f.id SET is_read = ? WHERE category = ?';
  251. $values = array ($read, $id);
  252. if ($dateMax > 0) {
  253. $sql .= ' AND date < ?';
  254. $values[] = $dateMax;
  255. }
  256. $stm = $this->bd->prepare ($sql);
  257. if ($stm && $stm->execute ($values)) {
  258. return true;
  259. } else {
  260. $info = $stm->errorInfo();
  261. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  262. return false;
  263. }
  264. }
  265. public function markReadFeed ($id, $read, $dateMax = 0) {
  266. $sql = 'UPDATE ' . $this->prefix . 'entry SET is_read = ? WHERE id_feed = ?';
  267. $values = array ($read, $id);
  268. if ($dateMax > 0) {
  269. $sql .= ' AND date < ?';
  270. $values[] = $dateMax;
  271. }
  272. $stm = $this->bd->prepare ($sql);
  273. if ($stm && $stm->execute ($values)) {
  274. return true;
  275. } else {
  276. $info = $stm->errorInfo();
  277. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  278. return false;
  279. }
  280. }
  281. public function updateEntries ($valuesTmp) {
  282. if (isset ($valuesTmp['content'])) {
  283. $valuesTmp['content'] = base64_encode (gzdeflate (serialize ($valuesTmp['content'])));
  284. }
  285. $set = '';
  286. foreach ($valuesTmp as $key => $v) {
  287. $set .= $key . '=?, ';
  288. }
  289. $set = substr ($set, 0, -2);
  290. $sql = 'UPDATE ' . $this->prefix . 'entry SET ' . $set;
  291. $stm = $this->bd->prepare ($sql);
  292. foreach ($valuesTmp as $v) {
  293. $values[] = $v;
  294. }
  295. if ($stm && $stm->execute ($values)) {
  296. return true;
  297. } else {
  298. $info = $stm->errorInfo();
  299. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  300. return false;
  301. }
  302. }
  303. public function cleanOldEntries ($nb_month) {
  304. $date = 60 * 60 * 24 * 30 * $nb_month;
  305. $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';
  306. $stm = $this->bd->prepare ($sql);
  307. $values = array (
  308. time () - $date
  309. );
  310. if ($stm && $stm->execute ($values)) {
  311. return true;
  312. } else {
  313. $info = $stm->errorInfo();
  314. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  315. return false;
  316. }
  317. }
  318. public function searchByGuid ($feed_id, $id) {
  319. // un guid est unique pour un flux donné
  320. $sql = 'SELECT * FROM ' . $this->prefix . 'entry WHERE id_feed=? AND guid=?';
  321. $stm = $this->bd->prepare ($sql);
  322. $values = array (
  323. $feed_id,
  324. $id
  325. );
  326. $stm->execute ($values);
  327. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  328. list ($entry, $next) = HelperEntry::daoToEntry ($res);
  329. if (isset ($entry[0])) {
  330. return $entry[0];
  331. } else {
  332. return false;
  333. }
  334. }
  335. public function searchById ($id) {
  336. $sql = 'SELECT * FROM ' . $this->prefix . 'entry WHERE id=?';
  337. $stm = $this->bd->prepare ($sql);
  338. $values = array ($id);
  339. $stm->execute ($values);
  340. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  341. list ($entry, $next) = HelperEntry::daoToEntry ($res);
  342. if (isset ($entry[0])) {
  343. return $entry[0];
  344. } else {
  345. return false;
  346. }
  347. }
  348. private function listWhere ($where, $state, $order, $limitFromId = '', $limitCount = '', $values = array ()) {
  349. if ($state == 'not_read') {
  350. $where .= ' AND is_read = 0';
  351. } elseif ($state == 'read') {
  352. $where .= ' AND is_read = 1';
  353. }
  354. if (!empty($limitFromId)) { //TODO: Consider using LPAD(e.date, 11) //CONCAT is for cases when many entries have the same date
  355. $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 . '")';
  356. }
  357. if ($order == 'low_to_high') {
  358. $order = ' DESC';
  359. } else {
  360. $order = '';
  361. }
  362. $sql = 'SELECT e.* FROM ' . $this->prefix . 'entry e'
  363. . ' INNER JOIN ' . $this->prefix . 'feed f ON e.id_feed = f.id' . $where
  364. . ' ORDER BY e.date' . $order . ', e.id' . $order;
  365. if (empty($limitCount)) {
  366. $limitCount = 20000; //TODO: FIXME: Hack temporaire en attendant la recherche côté base-de-données
  367. }
  368. //if (!empty($limitCount)) {
  369. $sql .= ' LIMIT ' . ($limitCount + 2); //TODO: See http://explainextended.com/2009/10/23/mysql-order-by-limit-performance-late-row-lookups/
  370. //}
  371. $stm = $this->bd->prepare ($sql);
  372. $stm->execute ($values);
  373. return HelperEntry::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
  374. }
  375. public function listEntries ($state, $order = 'high_to_low', $limitFromId = '', $limitCount = '') {
  376. return $this->listWhere (' WHERE priority > 0', $state, $order, $limitFromId, $limitCount);
  377. }
  378. public function listFavorites ($state, $order = 'high_to_low', $limitFromId = '', $limitCount = '') {
  379. return $this->listWhere (' WHERE is_favorite = 1', $state, $order, $limitFromId, $limitCount);
  380. }
  381. public function listPublic ($state, $order = 'high_to_low', $limitFromId = '', $limitCount = '') {
  382. return $this->listWhere (' WHERE is_public = 1', $state, $order, $limitFromId, $limitCount);
  383. }
  384. public function listByCategory ($cat, $state, $order = 'high_to_low', $limitFromId = '', $limitCount = '') {
  385. return $this->listWhere (' WHERE category = ?', $state, $order, $limitFromId, $limitCount, array ($cat));
  386. }
  387. public function listByFeed ($feed, $state, $order = 'high_to_low', $limitFromId = '', $limitCount = '') {
  388. return $this->listWhere (' WHERE id_feed = ?', $state, $order, $limitFromId, $limitCount, array ($feed));
  389. }
  390. public function listLastIdsByFeed($id, $n) {
  391. $sql = 'SELECT id FROM ' . $this->prefix . 'entry WHERE id_feed=? ORDER BY date DESC LIMIT ' . intval($n);
  392. $stm = $this->bd->prepare ($sql);
  393. $values = array ($id);
  394. $stm->execute ($values);
  395. return $stm->fetchAll (PDO::FETCH_COLUMN, 0);
  396. }
  397. public function countUnreadRead () {
  398. $sql = 'SELECT is_read, COUNT(*) AS count FROM ' . $this->prefix . 'entry e INNER JOIN ' . $this->prefix . 'feed f ON e.id_feed = f.id WHERE priority > 0 GROUP BY is_read';
  399. $stm = $this->bd->prepare ($sql);
  400. $stm->execute ();
  401. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  402. $readUnread = array('unread' => 0, 'read' => 0);
  403. foreach ($res as $line) {
  404. switch (intval($line['is_read'])) {
  405. case 0: $readUnread['unread'] = intval($line['count']); break;
  406. case 1: $readUnread['read'] = intval($line['count']); break;
  407. }
  408. }
  409. return $readUnread;
  410. }
  411. public function count () { //Deprecated: use countUnreadRead() instead
  412. $unreadRead = $this->countUnreadRead (); //This makes better use of caching
  413. return $unreadRead['unread'] + $unreadRead['read'];
  414. }
  415. public function countNotRead () { //Deprecated: use countUnreadRead() instead
  416. $unreadRead = $this->countUnreadRead (); //This makes better use of caching
  417. return $unreadRead['unread'];
  418. }
  419. public function countUnreadReadFavorites () {
  420. $sql = 'SELECT is_read, COUNT(*) AS count FROM ' . $this->prefix . 'entry WHERE is_favorite=1 GROUP BY is_read';
  421. $stm = $this->bd->prepare ($sql);
  422. $stm->execute ();
  423. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  424. $readUnread = array('unread' => 0, 'read' => 0);
  425. foreach ($res as $line) {
  426. switch (intval($line['is_read'])) {
  427. case 0: $readUnread['unread'] = intval($line['count']); break;
  428. case 1: $readUnread['read'] = intval($line['count']); break;
  429. }
  430. }
  431. return $readUnread;
  432. }
  433. public function countFavorites () { //Deprecated: use countUnreadReadFavorites() instead
  434. $unreadRead = $this->countUnreadReadFavorites (); //This makes better use of caching
  435. return $unreadRead['unread'] + $unreadRead['read'];
  436. }
  437. public function optimizeTable() {
  438. $sql = 'OPTIMIZE TABLE ' . $this->prefix . 'entry';
  439. $stm = $this->bd->prepare ($sql);
  440. $stm->execute ();
  441. }
  442. }
  443. class HelperEntry {
  444. public static $nb = 1;
  445. public static $first = '';
  446. public static $filter = array (
  447. 'words' => array (),
  448. 'tags' => array (),
  449. );
  450. public static function daoToEntry ($listDAO) {
  451. $list = array ();
  452. if (!is_array ($listDAO)) {
  453. $listDAO = array ($listDAO);
  454. }
  455. $count = 0;
  456. $first_is_found = false;
  457. $break_after = false;
  458. $next = '';
  459. foreach ($listDAO as $key => $dao) {
  460. $dao['content'] = unserialize (gzinflate (base64_decode ($dao['content'])));
  461. $dao['tags'] = preg_split('/[\s#]/', $dao['tags']);
  462. if (self::tagsMatchEntry ($dao) &&
  463. self::searchMatchEntry ($dao)) {
  464. if ($break_after) {
  465. $next = $dao['id'];
  466. break;
  467. }
  468. if ($first_is_found || $dao['id'] == self::$first || self::$first == '') {
  469. $list[$key] = self::createEntry ($dao);
  470. $count++;
  471. $first_is_found = true; //TODO: Update: Now done in SQL
  472. }
  473. if ($count >= self::$nb) {
  474. $break_after = true;
  475. }
  476. }
  477. }
  478. unset ($listDAO);
  479. return array ($list, $next);
  480. }
  481. private static function createEntry ($dao) {
  482. $entry = new Entry (
  483. $dao['id_feed'],
  484. $dao['guid'],
  485. $dao['title'],
  486. $dao['author'],
  487. $dao['content'],
  488. $dao['link'],
  489. $dao['date'],
  490. $dao['is_read'],
  491. $dao['is_favorite']
  492. );
  493. $entry->_tags ($dao['tags']);
  494. if (isset ($dao['id'])) {
  495. $entry->_id ($dao['id']);
  496. }
  497. return $entry;
  498. }
  499. private static function tagsMatchEntry ($dao) {
  500. $tags = self::$filter['tags'];
  501. foreach ($tags as $tag) {
  502. if (!in_array ($tag, $dao['tags'])) {
  503. return false;
  504. }
  505. }
  506. return true;
  507. }
  508. private static function searchMatchEntry ($dao) {
  509. $words = self::$filter['words'];
  510. foreach ($words as $word) {
  511. $word = strtolower ($word);
  512. if (strpos (strtolower ($dao['title']), $word) === false &&
  513. strpos (strtolower ($dao['content']), $word) === false &&
  514. strpos (strtolower ($dao['link']), $word) === false) {
  515. return false;
  516. }
  517. }
  518. return true;
  519. }
  520. }