Entry.php 14 KB

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