Entry.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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 toArray () {
  188. return array (
  189. 'id' => $this->id (),
  190. 'guid' => $this->guid (),
  191. 'title' => $this->title (),
  192. 'author' => $this->author (),
  193. 'content' => $this->content (),
  194. 'link' => $this->link (),
  195. 'date' => $this->date (true),
  196. 'is_read' => $this->isRead (),
  197. 'is_favorite' => $this->isFavorite (),
  198. 'is_public' => $this->isPublic (),
  199. 'id_feed' => $this->feed (),
  200. 'lastUpdate' => $this->lastUpdate (true),
  201. 'tags' => $this->tags (true),
  202. 'annotation' => $this->notes ()
  203. );
  204. }
  205. }
  206. class EntryDAO extends Model_pdo {
  207. public function addEntry ($valuesTmp) {
  208. $sql = 'INSERT INTO entry(id, guid, title, author, content, link, date, is_read, is_favorite, is_public, id_feed, lastUpdate, tags) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
  209. $stm = $this->bd->prepare ($sql);
  210. $values = array (
  211. $valuesTmp['id'],
  212. $valuesTmp['guid'],
  213. $valuesTmp['title'],
  214. $valuesTmp['author'],
  215. base64_encode (gzdeflate (serialize ($valuesTmp['content']))),
  216. $valuesTmp['link'],
  217. $valuesTmp['date'],
  218. $valuesTmp['is_read'],
  219. $valuesTmp['is_favorite'],
  220. $valuesTmp['is_public'],
  221. $valuesTmp['id_feed'],
  222. $valuesTmp['lastUpdate'],
  223. $valuesTmp['tags'],
  224. );
  225. if ($stm && $stm->execute ($values)) {
  226. return true;
  227. } else {
  228. $info = $stm->errorInfo();
  229. Log::record ('SQL error : ' . $info[2], Log::ERROR);
  230. return false;
  231. }
  232. }
  233. public function updateEntry ($id, $valuesTmp) {
  234. if (isset ($valuesTmp['content'])) {
  235. $valuesTmp['content'] = base64_encode (gzdeflate (serialize ($valuesTmp['content'])));
  236. }
  237. $set = '';
  238. foreach ($valuesTmp as $key => $v) {
  239. $set .= $key . '=?, ';
  240. }
  241. $set = substr ($set, 0, -2);
  242. $sql = 'UPDATE entry SET ' . $set . ' WHERE id=?';
  243. $stm = $this->bd->prepare ($sql);
  244. foreach ($valuesTmp as $v) {
  245. $values[] = $v;
  246. }
  247. $values[] = $id;
  248. if ($stm && $stm->execute ($values)) {
  249. return true;
  250. } else {
  251. $info = $stm->errorInfo();
  252. Log::record ('SQL error : ' . $info[2], Log::ERROR);
  253. return false;
  254. }
  255. }
  256. public function markReadEntries ($read, $dateMax) {
  257. $sql = 'UPDATE entry e INNER JOIN feed f ON e.id_feed = f.id SET is_read = ? WHERE date < ? AND priority > 0';
  258. $stm = $this->bd->prepare ($sql);
  259. $values = array ($read, $dateMax);
  260. if ($stm && $stm->execute ($values)) {
  261. return true;
  262. } else {
  263. $info = $stm->errorInfo();
  264. Log::record ('SQL error : ' . $info[2], Log::ERROR);
  265. return false;
  266. }
  267. }
  268. public function markReadCat ($id, $read, $dateMax) {
  269. $sql = 'UPDATE entry e INNER JOIN feed f ON e.id_feed = f.id SET is_read = ? WHERE category = ? AND date < ?';
  270. $stm = $this->bd->prepare ($sql);
  271. $values = array ($read, $id, $dateMax);
  272. if ($stm && $stm->execute ($values)) {
  273. return true;
  274. } else {
  275. $info = $stm->errorInfo();
  276. Log::record ('SQL error : ' . $info[2], Log::ERROR);
  277. return false;
  278. }
  279. }
  280. public function markReadFeed ($id, $read, $dateMax) {
  281. $sql = 'UPDATE entry SET is_read = ? WHERE id_feed = ? AND date < ?';
  282. $stm = $this->bd->prepare ($sql);
  283. $values = array ($read, $id, $dateMax);
  284. if ($stm && $stm->execute ($values)) {
  285. return true;
  286. } else {
  287. $info = $stm->errorInfo();
  288. Log::record ('SQL error : ' . $info[2], Log::ERROR);
  289. return false;
  290. }
  291. }
  292. public function updateEntries ($valuesTmp) {
  293. if (isset ($valuesTmp['content'])) {
  294. $valuesTmp['content'] = base64_encode (gzdeflate (serialize ($valuesTmp['content'])));
  295. }
  296. $set = '';
  297. foreach ($valuesTmp as $key => $v) {
  298. $set .= $key . '=?, ';
  299. }
  300. $set = substr ($set, 0, -2);
  301. $sql = 'UPDATE entry SET ' . $set;
  302. $stm = $this->bd->prepare ($sql);
  303. foreach ($valuesTmp as $v) {
  304. $values[] = $v;
  305. }
  306. if ($stm && $stm->execute ($values)) {
  307. return true;
  308. } else {
  309. $info = $stm->errorInfo();
  310. Log::record ('SQL error : ' . $info[2], Log::ERROR);
  311. return false;
  312. }
  313. }
  314. public function cleanOldEntries ($nb_month) {
  315. $date = 60 * 60 * 24 * 30 * $nb_month;
  316. $sql = 'DELETE FROM entry WHERE date <= ? AND is_favorite = 0 AND annotation = ""';
  317. $stm = $this->bd->prepare ($sql);
  318. $values = array (
  319. time () - $date
  320. );
  321. if ($stm && $stm->execute ($values)) {
  322. return true;
  323. } else {
  324. $info = $stm->errorInfo();
  325. Log::record ('SQL error : ' . $info[2], Log::ERROR);
  326. return false;
  327. }
  328. }
  329. public function searchById ($id) {
  330. $sql = 'SELECT * FROM entry WHERE id=?';
  331. $stm = $this->bd->prepare ($sql);
  332. $values = array ($id);
  333. $stm->execute ($values);
  334. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  335. list ($entry, $next) = HelperEntry::daoToEntry ($res);
  336. if (isset ($entry[0])) {
  337. return $entry[0];
  338. } else {
  339. return false;
  340. }
  341. }
  342. public function listWhere ($where, $state, $order, $values = array ()) {
  343. if ($state == 'not_read') {
  344. $where .= ' AND is_read = 0';
  345. } elseif ($state == 'read') {
  346. $where .= ' AND is_read = 1';
  347. }
  348. if ($order == 'low_to_high') {
  349. $order = ' DESC';
  350. } else {
  351. $order = '';
  352. }
  353. $sql = 'SELECT e.* FROM entry e'
  354. . ' INNER JOIN feed f ON e.id_feed = f.id' . $where
  355. . ' ORDER BY date' . $order;
  356. $stm = $this->bd->prepare ($sql);
  357. $stm->execute ($values);
  358. return HelperEntry::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
  359. }
  360. public function listEntries ($state, $order = 'high_to_low') {
  361. return $this->listWhere (' WHERE priority > 0', $state, $order);
  362. }
  363. public function listFavorites ($state, $order = 'high_to_low') {
  364. return $this->listWhere (' WHERE is_favorite = 1', $state, $order);
  365. }
  366. public function listPublic ($state, $order = 'high_to_low') {
  367. return $this->listWhere (' WHERE is_public = 1', $state, $order);
  368. }
  369. public function listByCategory ($cat, $state, $order = 'high_to_low') {
  370. return $this->listWhere (' WHERE category = ?', $state, $order, array ($cat));
  371. }
  372. public function listByFeed ($feed, $state, $order = 'high_to_low') {
  373. return $this->listWhere (' WHERE id_feed = ?', $state, $order, array ($feed));
  374. }
  375. public function count () {
  376. $sql = 'SELECT COUNT(*) AS count FROM entry e INNER JOIN feed f ON e.id_feed = f.id WHERE priority > 0';
  377. $stm = $this->bd->prepare ($sql);
  378. $stm->execute ();
  379. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  380. return $res[0]['count'];
  381. }
  382. public function countNotRead () {
  383. $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';
  384. $stm = $this->bd->prepare ($sql);
  385. $stm->execute ();
  386. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  387. return $res[0]['count'];
  388. }
  389. public function countNotReadByFeed ($id) {
  390. $sql = 'SELECT COUNT(*) AS count FROM entry WHERE is_read = 0 AND id_feed = ?';
  391. $stm = $this->bd->prepare ($sql);
  392. $stm->execute (array ($id));
  393. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  394. return $res[0]['count'];
  395. }
  396. public function countNotReadByCat ($id) {
  397. $sql = 'SELECT COUNT(*) AS count FROM entry e INNER JOIN feed f ON e.id_feed = f.id WHERE is_read=0 AND category = ?';
  398. $stm = $this->bd->prepare ($sql);
  399. $stm->execute (array ($id));
  400. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  401. return $res[0]['count'];
  402. }
  403. public function countNotReadFavorites () {
  404. $sql = 'SELECT COUNT(*) AS count FROM entry WHERE is_read=0 AND is_favorite=1';
  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 countFavorites () {
  411. $sql = 'SELECT COUNT(*) AS count FROM entry WHERE is_favorite=1';
  412. $stm = $this->bd->prepare ($sql);
  413. $stm->execute ();
  414. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  415. return $res[0]['count'];
  416. }
  417. }
  418. class HelperEntry {
  419. public static $nb = 1;
  420. public static $first = '';
  421. public static $filter = array (
  422. 'words' => array (),
  423. 'tags' => array (),
  424. );
  425. public static function daoToEntry ($listDAO) {
  426. $list = array ();
  427. if (!is_array ($listDAO)) {
  428. $listDAO = array ($listDAO);
  429. }
  430. $count = 0;
  431. $first_is_found = false;
  432. $break_after = false;
  433. $next = '';
  434. foreach ($listDAO as $key => $dao) {
  435. $dao['content'] = unserialize (gzinflate (base64_decode ($dao['content'])));
  436. $dao['tags'] = preg_split('/[\s#]/', $dao['tags']);
  437. if (self::tagsMatchEntry ($dao) &&
  438. self::searchMatchEntry ($dao)) {
  439. if ($break_after) {
  440. $next = $dao['id'];
  441. break;
  442. }
  443. if ($first_is_found || $dao['id'] == self::$first || self::$first == '') {
  444. $list[$key] = self::createEntry ($dao);
  445. $count++;
  446. $first_is_found = true;
  447. }
  448. if ($count >= self::$nb) {
  449. $break_after = true;
  450. }
  451. }
  452. }
  453. unset ($listDAO);
  454. return array ($list, $next);
  455. }
  456. private static function createEntry ($dao) {
  457. $entry = new Entry (
  458. $dao['id_feed'],
  459. $dao['guid'],
  460. $dao['title'],
  461. $dao['author'],
  462. $dao['content'],
  463. $dao['link'],
  464. $dao['date'],
  465. $dao['is_read'],
  466. $dao['is_favorite'],
  467. $dao['is_public']
  468. );
  469. $entry->_notes ($dao['annotation']);
  470. $entry->_lastUpdate ($dao['lastUpdate']);
  471. $entry->_tags ($dao['tags']);
  472. if (isset ($dao['id'])) {
  473. $entry->_id ($dao['id']);
  474. }
  475. return $entry;
  476. }
  477. private static function tagsMatchEntry ($dao) {
  478. $tags = self::$filter['tags'];
  479. foreach ($tags as $tag) {
  480. if (!in_array ($tag, $dao['tags'])) {
  481. return false;
  482. }
  483. }
  484. return true;
  485. }
  486. private static function searchMatchEntry ($dao) {
  487. $words = self::$filter['words'];
  488. foreach ($words as $word) {
  489. $word = strtolower ($word);
  490. if (strpos (strtolower ($dao['title']), $word) === false &&
  491. strpos (strtolower ($dao['content']), $word) === false &&
  492. strpos (strtolower ($dao['link']), $word) === false &&
  493. strpos (strtolower ($dao['annotation']), $word) === false) {
  494. return false;
  495. }
  496. }
  497. return true;
  498. }
  499. }