Entry.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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 $is_public;
  13. private $feed;
  14. private $tags;
  15. private $notes;
  16. private $lastUpdate;
  17. public function __construct ($feed = '', $guid = '', $title = '', $author = '', $content = '',
  18. $link = '', $pubdate = 0, $is_read = false, $is_favorite = false,
  19. $is_public = false) {
  20. $this->_guid ($guid);
  21. $this->_title ($title);
  22. $this->_author ($author);
  23. $this->_content ($content);
  24. $this->_link ($link);
  25. $this->_date ($pubdate);
  26. $this->_isRead ($is_read);
  27. $this->_isFavorite ($is_favorite);
  28. $this->_isPublic ($is_public);
  29. $this->_feed ($feed);
  30. $this->_lastUpdate ($pubdate);
  31. $this->_notes ('');
  32. $this->_tags (array ());
  33. }
  34. public function id () {
  35. if(is_null($this->id)) {
  36. return small_hash ($this->guid . Configuration::selApplication ());
  37. } else {
  38. return $this->id;
  39. }
  40. }
  41. public function guid () {
  42. return $this->guid;
  43. }
  44. public function title () {
  45. return $this->title;
  46. }
  47. public function author () {
  48. if (is_null ($this->author)) {
  49. return '';
  50. } else {
  51. return $this->author;
  52. }
  53. }
  54. public function content () {
  55. return $this->content;
  56. }
  57. public function link () {
  58. return $this->link;
  59. }
  60. public function date ($raw = false) {
  61. if ($raw) {
  62. return $this->date;
  63. } else {
  64. return timestamptodate ($this->date);
  65. }
  66. }
  67. public function isRead () {
  68. return $this->is_read;
  69. }
  70. public function isFavorite () {
  71. return $this->is_favorite;
  72. }
  73. public function isPublic () {
  74. return $this->is_public;
  75. }
  76. public function feed ($object = false) {
  77. if ($object) {
  78. $feedDAO = new FeedDAO ();
  79. return $feedDAO->searchById ($this->feed);
  80. } else {
  81. return $this->feed;
  82. }
  83. }
  84. public function tags ($inString = false) {
  85. if ($inString) {
  86. if (!empty ($this->tags)) {
  87. return '#' . implode(' #', $this->tags);
  88. } else {
  89. return '';
  90. }
  91. } else {
  92. return $this->tags;
  93. }
  94. }
  95. public function notes ($raw = true, $parse_tags = true) {
  96. if ($raw) {
  97. return $this->notes;
  98. } else {
  99. if($parse_tags) {
  100. return parse_tags (bbdecode ($this->notes));
  101. } else {
  102. return bbdecode ($this->notes);
  103. }
  104. }
  105. }
  106. public function lastUpdate ($raw = false) {
  107. if ($raw) {
  108. return $this->lastUpdate;
  109. } else {
  110. return timestamptodate ($this->lastUpdate);
  111. }
  112. }
  113. public function _id ($value) {
  114. $this->id = $value;
  115. }
  116. public function _guid ($value) {
  117. $this->guid = $value;
  118. }
  119. public function _title ($value) {
  120. $this->title = $value;
  121. }
  122. public function _author ($value) {
  123. $this->author = $value;
  124. }
  125. public function _content ($value) {
  126. $this->content = $value;
  127. }
  128. public function _link ($value) {
  129. $this->link = $value;
  130. }
  131. public function _date ($value) {
  132. if (is_int (intval ($value))) {
  133. $this->date = $value;
  134. } else {
  135. $this->date = time ();
  136. }
  137. }
  138. public function _isRead ($value) {
  139. $this->is_read = $value;
  140. }
  141. public function _isFavorite ($value) {
  142. $this->is_favorite = $value;
  143. }
  144. public function _isPublic ($value) {
  145. $this->is_public = $value;
  146. }
  147. public function _feed ($value) {
  148. $this->feed = $value;
  149. }
  150. public function _tags ($value) {
  151. if (!is_array ($value)) {
  152. $value = array ($value);
  153. }
  154. foreach ($value as $key => $t) {
  155. if (!$t) {
  156. unset ($value[$key]);
  157. }
  158. }
  159. $this->tags = $value;
  160. }
  161. public function _notes ($value) {
  162. $this->notes = $value;
  163. }
  164. public function _lastUpdate ($value) {
  165. if (is_int (intval ($value))) {
  166. $this->lastUpdate = $value;
  167. } else {
  168. $this->lastUpdate = $this->date (true);
  169. }
  170. }
  171. public function isDay ($day) {
  172. $date = getdate ();
  173. $today = mktime (0, 0, 0, $date['mon'], $date['mday'], $date['year']);
  174. $yesterday = $today - 86400;
  175. if ($day == Days::TODAY &&
  176. $this->date >= $today && $this->date < $today + 86400) {
  177. return true;
  178. } elseif ($day == Days::YESTERDAY &&
  179. $this->date >= $yesterday && $this->date < $yesterday + 86400) {
  180. return true;
  181. } elseif ($day == Days::BEFORE_YESTERDAY && $this->date < $yesterday) {
  182. return true;
  183. } else {
  184. return false;
  185. }
  186. }
  187. public function loadCompleteContent($pathEntries) {
  188. // Gestion du contenu
  189. // On cherche à récupérer les articles en entier... même si le flux ne le propose pas
  190. if ($pathEntries) {
  191. $entryDAO = new EntryDAO();
  192. $entry = $entryDAO->searchByGuid($this->feed, $this->guid);
  193. if($entry) {
  194. // l'article existe déjà en BDD, en se contente de recharger ce contenu
  195. $this->content = $entry->content();
  196. } else {
  197. try {
  198. // l'article n'est pas en BDD, on va le chercher sur le site
  199. $this->content = get_content_by_parsing(
  200. $this->link(), $pathEntries
  201. );
  202. } catch (Exception $e) {
  203. // rien à faire, on garde l'ancien contenu (requête a échoué)
  204. }
  205. }
  206. }
  207. }
  208. public function toArray () {
  209. return array (
  210. 'id' => $this->id (),
  211. 'guid' => $this->guid (),
  212. 'title' => $this->title (),
  213. 'author' => $this->author (),
  214. 'content' => $this->content (),
  215. 'link' => $this->link (),
  216. 'date' => $this->date (true),
  217. 'is_read' => $this->isRead (),
  218. 'is_favorite' => $this->isFavorite (),
  219. 'is_public' => $this->isPublic (),
  220. 'id_feed' => $this->feed (),
  221. 'lastUpdate' => $this->lastUpdate (true),
  222. 'tags' => $this->tags (true),
  223. 'annotation' => $this->notes ()
  224. );
  225. }
  226. }
  227. class EntryDAO extends Model_pdo {
  228. public function addEntry ($valuesTmp) {
  229. $sql = 'INSERT INTO entry(id, guid, title, author, content, link, date, is_read, is_favorite, is_public, id_feed, lastUpdate, tags) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
  230. $stm = $this->bd->prepare ($sql);
  231. $values = array (
  232. $valuesTmp['id'],
  233. $valuesTmp['guid'],
  234. $valuesTmp['title'],
  235. $valuesTmp['author'],
  236. base64_encode (gzdeflate (serialize ($valuesTmp['content']))),
  237. $valuesTmp['link'],
  238. $valuesTmp['date'],
  239. $valuesTmp['is_read'],
  240. $valuesTmp['is_favorite'],
  241. $valuesTmp['is_public'],
  242. $valuesTmp['id_feed'],
  243. $valuesTmp['lastUpdate'],
  244. $valuesTmp['tags'],
  245. );
  246. if ($stm && $stm->execute ($values)) {
  247. return true;
  248. } else {
  249. $info = $stm->errorInfo();
  250. Log::record ('SQL error : ' . $info[2], Log::NOTICE);
  251. return false;
  252. }
  253. }
  254. public function updateEntry ($id, $valuesTmp) {
  255. if (isset ($valuesTmp['content'])) {
  256. $valuesTmp['content'] = base64_encode (gzdeflate (serialize ($valuesTmp['content'])));
  257. }
  258. $set = '';
  259. foreach ($valuesTmp as $key => $v) {
  260. $set .= $key . '=?, ';
  261. }
  262. $set = substr ($set, 0, -2);
  263. $sql = 'UPDATE entry SET ' . $set . ' WHERE id=?';
  264. $stm = $this->bd->prepare ($sql);
  265. foreach ($valuesTmp as $v) {
  266. $values[] = $v;
  267. }
  268. $values[] = $id;
  269. if ($stm && $stm->execute ($values)) {
  270. return true;
  271. } else {
  272. $info = $stm->errorInfo();
  273. Log::record ('SQL error : ' . $info[2], Log::ERROR);
  274. return false;
  275. }
  276. }
  277. public function markReadEntries ($read, $dateMax) {
  278. $sql = 'UPDATE entry e INNER JOIN feed f ON e.id_feed = f.id SET is_read = ? WHERE date < ? AND priority > 0';
  279. $stm = $this->bd->prepare ($sql);
  280. $values = array ($read, $dateMax);
  281. if ($stm && $stm->execute ($values)) {
  282. return true;
  283. } else {
  284. $info = $stm->errorInfo();
  285. Log::record ('SQL error : ' . $info[2], Log::ERROR);
  286. return false;
  287. }
  288. }
  289. public function markReadCat ($id, $read, $dateMax) {
  290. $sql = 'UPDATE entry e INNER JOIN feed f ON e.id_feed = f.id SET is_read = ? WHERE category = ? AND date < ?';
  291. $stm = $this->bd->prepare ($sql);
  292. $values = array ($read, $id, $dateMax);
  293. if ($stm && $stm->execute ($values)) {
  294. return true;
  295. } else {
  296. $info = $stm->errorInfo();
  297. Log::record ('SQL error : ' . $info[2], Log::ERROR);
  298. return false;
  299. }
  300. }
  301. public function markReadFeed ($id, $read, $dateMax) {
  302. $sql = 'UPDATE entry SET is_read = ? WHERE id_feed = ? AND date < ?';
  303. $stm = $this->bd->prepare ($sql);
  304. $values = array ($read, $id, $dateMax);
  305. if ($stm && $stm->execute ($values)) {
  306. return true;
  307. } else {
  308. $info = $stm->errorInfo();
  309. Log::record ('SQL error : ' . $info[2], Log::ERROR);
  310. return false;
  311. }
  312. }
  313. public function updateEntries ($valuesTmp) {
  314. if (isset ($valuesTmp['content'])) {
  315. $valuesTmp['content'] = base64_encode (gzdeflate (serialize ($valuesTmp['content'])));
  316. }
  317. $set = '';
  318. foreach ($valuesTmp as $key => $v) {
  319. $set .= $key . '=?, ';
  320. }
  321. $set = substr ($set, 0, -2);
  322. $sql = 'UPDATE entry SET ' . $set;
  323. $stm = $this->bd->prepare ($sql);
  324. foreach ($valuesTmp as $v) {
  325. $values[] = $v;
  326. }
  327. if ($stm && $stm->execute ($values)) {
  328. return true;
  329. } else {
  330. $info = $stm->errorInfo();
  331. Log::record ('SQL error : ' . $info[2], Log::ERROR);
  332. return false;
  333. }
  334. }
  335. public function cleanOldEntries ($nb_month) {
  336. $date = 60 * 60 * 24 * 30 * $nb_month;
  337. $sql = 'DELETE FROM entry WHERE date <= ? AND is_favorite = 0 AND annotation = ""';
  338. $stm = $this->bd->prepare ($sql);
  339. $values = array (
  340. time () - $date
  341. );
  342. if ($stm && $stm->execute ($values)) {
  343. return true;
  344. } else {
  345. $info = $stm->errorInfo();
  346. Log::record ('SQL error : ' . $info[2], Log::ERROR);
  347. return false;
  348. }
  349. }
  350. public function searchByGuid ($feed_id, $id) {
  351. // un guid est unique pour un flux donné
  352. $sql = 'SELECT * FROM entry WHERE id_feed=? AND guid=?';
  353. $stm = $this->bd->prepare ($sql);
  354. $values = array (
  355. $feed_id,
  356. $id
  357. );
  358. $stm->execute ($values);
  359. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  360. list ($entry, $next) = HelperEntry::daoToEntry ($res);
  361. if (isset ($entry[0])) {
  362. return $entry[0];
  363. } else {
  364. return false;
  365. }
  366. }
  367. public function searchById ($id) {
  368. $sql = 'SELECT * FROM entry WHERE id=?';
  369. $stm = $this->bd->prepare ($sql);
  370. $values = array ($id);
  371. $stm->execute ($values);
  372. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  373. list ($entry, $next) = HelperEntry::daoToEntry ($res);
  374. if (isset ($entry[0])) {
  375. return $entry[0];
  376. } else {
  377. return false;
  378. }
  379. }
  380. public function listWhere ($where, $state, $order, $values = array ()) {
  381. if ($state == 'not_read') {
  382. $where .= ' AND is_read = 0';
  383. } elseif ($state == 'read') {
  384. $where .= ' AND is_read = 1';
  385. }
  386. if ($order == 'low_to_high') {
  387. $order = ' DESC';
  388. } else {
  389. $order = '';
  390. }
  391. $sql = 'SELECT e.* FROM entry e'
  392. . ' INNER JOIN feed f ON e.id_feed = f.id' . $where
  393. . ' ORDER BY date' . $order;
  394. $stm = $this->bd->prepare ($sql);
  395. $stm->execute ($values);
  396. return HelperEntry::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
  397. }
  398. public function listEntries ($state, $order = 'high_to_low') {
  399. return $this->listWhere (' WHERE priority > 0', $state, $order);
  400. }
  401. public function listFavorites ($state, $order = 'high_to_low') {
  402. return $this->listWhere (' WHERE is_favorite = 1', $state, $order);
  403. }
  404. public function listPublic ($state, $order = 'high_to_low') {
  405. return $this->listWhere (' WHERE is_public = 1', $state, $order);
  406. }
  407. public function listByCategory ($cat, $state, $order = 'high_to_low') {
  408. return $this->listWhere (' WHERE category = ?', $state, $order, array ($cat));
  409. }
  410. public function listByFeed ($feed, $state, $order = 'high_to_low') {
  411. return $this->listWhere (' WHERE id_feed = ?', $state, $order, array ($feed));
  412. }
  413. public function count () {
  414. $sql = 'SELECT COUNT(*) AS count FROM entry e INNER JOIN feed f ON e.id_feed = f.id WHERE priority > 0';
  415. $stm = $this->bd->prepare ($sql);
  416. $stm->execute ();
  417. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  418. return $res[0]['count'];
  419. }
  420. public function countNotRead () {
  421. $sql = 'SELECT COUNT(*) AS count FROM entry e INNER JOIN feed f ON e.id_feed = f.id WHERE is_read=0 AND priority > 0';
  422. $stm = $this->bd->prepare ($sql);
  423. $stm->execute ();
  424. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  425. return $res[0]['count'];
  426. }
  427. public function countNotReadByFeed ($id) {
  428. $sql = 'SELECT COUNT(*) AS count FROM entry WHERE is_read = 0 AND id_feed = ?';
  429. $stm = $this->bd->prepare ($sql);
  430. $stm->execute (array ($id));
  431. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  432. return $res[0]['count'];
  433. }
  434. public function countNotReadByCat ($id) {
  435. $sql = 'SELECT COUNT(*) AS count FROM entry e INNER JOIN feed f ON e.id_feed = f.id WHERE is_read=0 AND category = ?';
  436. $stm = $this->bd->prepare ($sql);
  437. $stm->execute (array ($id));
  438. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  439. return $res[0]['count'];
  440. }
  441. public function countNotReadFavorites () {
  442. $sql = 'SELECT COUNT(*) AS count FROM entry WHERE is_read=0 AND is_favorite=1';
  443. $stm = $this->bd->prepare ($sql);
  444. $stm->execute ();
  445. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  446. return $res[0]['count'];
  447. }
  448. public function countFavorites () {
  449. $sql = 'SELECT COUNT(*) AS count FROM entry WHERE is_favorite=1';
  450. $stm = $this->bd->prepare ($sql);
  451. $stm->execute ();
  452. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  453. return $res[0]['count'];
  454. }
  455. public function optimizeTable() {
  456. $sql = 'OPTIMIZE TABLE entry';
  457. $stm = $this->bd->prepare ($sql);
  458. $stm->execute ();
  459. }
  460. }
  461. class HelperEntry {
  462. public static $nb = 1;
  463. public static $first = '';
  464. public static $filter = array (
  465. 'words' => array (),
  466. 'tags' => array (),
  467. );
  468. public static function daoToEntry ($listDAO) {
  469. $list = array ();
  470. if (!is_array ($listDAO)) {
  471. $listDAO = array ($listDAO);
  472. }
  473. $count = 0;
  474. $first_is_found = false;
  475. $break_after = false;
  476. $next = '';
  477. foreach ($listDAO as $key => $dao) {
  478. $dao['content'] = unserialize (gzinflate (base64_decode ($dao['content'])));
  479. $dao['tags'] = preg_split('/[\s#]/', $dao['tags']);
  480. if (self::tagsMatchEntry ($dao) &&
  481. self::searchMatchEntry ($dao)) {
  482. if ($break_after) {
  483. $next = $dao['id'];
  484. break;
  485. }
  486. if ($first_is_found || $dao['id'] == self::$first || self::$first == '') {
  487. $list[$key] = self::createEntry ($dao);
  488. $count++;
  489. $first_is_found = true;
  490. }
  491. if ($count >= self::$nb) {
  492. $break_after = true;
  493. }
  494. }
  495. }
  496. unset ($listDAO);
  497. return array ($list, $next);
  498. }
  499. private static function createEntry ($dao) {
  500. $entry = new Entry (
  501. $dao['id_feed'],
  502. $dao['guid'],
  503. $dao['title'],
  504. $dao['author'],
  505. $dao['content'],
  506. $dao['link'],
  507. $dao['date'],
  508. $dao['is_read'],
  509. $dao['is_favorite'],
  510. $dao['is_public']
  511. );
  512. $entry->_notes ($dao['annotation']);
  513. $entry->_lastUpdate ($dao['lastUpdate']);
  514. $entry->_tags ($dao['tags']);
  515. if (isset ($dao['id'])) {
  516. $entry->_id ($dao['id']);
  517. }
  518. return $entry;
  519. }
  520. private static function tagsMatchEntry ($dao) {
  521. $tags = self::$filter['tags'];
  522. foreach ($tags as $tag) {
  523. if (!in_array ($tag, $dao['tags'])) {
  524. return false;
  525. }
  526. }
  527. return true;
  528. }
  529. private static function searchMatchEntry ($dao) {
  530. $words = self::$filter['words'];
  531. foreach ($words as $word) {
  532. $word = strtolower ($word);
  533. if (strpos (strtolower ($dao['title']), $word) === false &&
  534. strpos (strtolower ($dao['content']), $word) === false &&
  535. strpos (strtolower ($dao['link']), $word) === false &&
  536. strpos (strtolower ($dao['annotation']), $word) === false) {
  537. return false;
  538. }
  539. }
  540. return true;
  541. }
  542. }