Entry.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. class FreshRSS_Entry extends Minz_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, $tags = '') {
  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 (preg_split('/[\s#]/', $tags));
  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 FreshRSS_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. return empty ($this->tags) ? '' : '#' . implode(' #', $this->tags);
  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 (ctype_digit ($value)) {
  105. $this->date = intval ($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 = $this->dateAdded(true);
  132. $today = @strtotime('today');
  133. $yesterday = $today - 86400;
  134. if ($day === FreshRSS_Days::TODAY &&
  135. $date >= $today && $date < $today + 86400) {
  136. return true;
  137. } elseif ($day === FreshRSS_Days::YESTERDAY &&
  138. $date >= $yesterday && $date < $yesterday + 86400) {
  139. return true;
  140. } elseif ($day === FreshRSS_Days::BEFORE_YESTERDAY && $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 FreshRSS_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. }