4
0

Entry.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. class FreshRSS_Entry extends Minz_Model {
  3. const STATE_READ = 1;
  4. const STATE_NOT_READ = 2;
  5. const STATE_ALL = 3;
  6. const STATE_FAVORITE = 4;
  7. const STATE_NOT_FAVORITE = 8;
  8. private $id = 0;
  9. private $guid;
  10. private $title;
  11. private $author;
  12. private $content;
  13. private $link;
  14. private $date;
  15. private $hash = null;
  16. private $is_read; //Nullable boolean
  17. private $is_favorite;
  18. private $feed;
  19. private $tags;
  20. public function __construct($feed = '', $guid = '', $title = '', $author = '', $content = '',
  21. $link = '', $pubdate = 0, $is_read = false, $is_favorite = false, $tags = '') {
  22. $this->_guid($guid);
  23. $this->_title($title);
  24. $this->_author($author);
  25. $this->_content($content);
  26. $this->_link($link);
  27. $this->_date($pubdate);
  28. $this->_isRead($is_read);
  29. $this->_isFavorite($is_favorite);
  30. $this->_feed($feed);
  31. $this->_tags(preg_split('/[\s#]/', $tags));
  32. }
  33. public function id() {
  34. return $this->id;
  35. }
  36. public function guid() {
  37. return $this->guid;
  38. }
  39. public function title() {
  40. return $this->title;
  41. }
  42. public function author() {
  43. return $this->author === null ? '' : $this->author;
  44. }
  45. public function content() {
  46. return $this->content;
  47. }
  48. public function link() {
  49. return $this->link;
  50. }
  51. public function date($raw = false) {
  52. if ($raw) {
  53. return $this->date;
  54. } else {
  55. return timestamptodate($this->date);
  56. }
  57. }
  58. public function dateAdded($raw = false) {
  59. $date = intval(substr($this->id, 0, -6));
  60. if ($raw) {
  61. return $date;
  62. } else {
  63. return timestamptodate($date);
  64. }
  65. }
  66. public function isRead() {
  67. return $this->is_read;
  68. }
  69. public function isFavorite() {
  70. return $this->is_favorite;
  71. }
  72. public function feed($object = false) {
  73. if ($object) {
  74. $feedDAO = FreshRSS_Factory::createFeedDao();
  75. return $feedDAO->searchById($this->feed);
  76. } else {
  77. return $this->feed;
  78. }
  79. }
  80. public function tags($inString = false) {
  81. if ($inString) {
  82. return empty($this->tags) ? '' : '#' . implode(' #', $this->tags);
  83. } else {
  84. return $this->tags;
  85. }
  86. }
  87. public function hash() {
  88. if ($this->hash === null) {
  89. //Do not include $this->date because it may be automatically generated when lacking
  90. $this->hash = md5($this->link . $this->title . $this->author . $this->content . $this->tags(true));
  91. }
  92. return $this->hash;
  93. }
  94. public function _id($value) {
  95. $this->id = $value;
  96. }
  97. public function _guid($value) {
  98. $this->guid = $value;
  99. }
  100. public function _title($value) {
  101. $this->hash = null;
  102. $this->title = $value;
  103. }
  104. public function _author($value) {
  105. $this->hash = null;
  106. $this->author = $value;
  107. }
  108. public function _content($value) {
  109. $this->hash = null;
  110. $this->content = $value;
  111. }
  112. public function _link($value) {
  113. $this->hash = null;
  114. $this->link = $value;
  115. }
  116. public function _date($value) {
  117. $this->hash = null;
  118. $value = intval($value);
  119. $this->date = $value > 1 ? $value : time();
  120. }
  121. public function _isRead($value) {
  122. $this->is_read = $value === null ? null : (bool)$value;
  123. }
  124. public function _isFavorite($value) {
  125. $this->is_favorite = $value;
  126. }
  127. public function _feed($value) {
  128. $this->feed = $value;
  129. }
  130. public function _tags($value) {
  131. $this->hash = null;
  132. if (!is_array($value)) {
  133. $value = array($value);
  134. }
  135. foreach ($value as $key => $t) {
  136. if (!$t) {
  137. unset($value[$key]);
  138. }
  139. }
  140. $this->tags = $value;
  141. }
  142. public function isDay($day, $today) {
  143. $date = $this->dateAdded(true);
  144. switch ($day) {
  145. case FreshRSS_Days::TODAY:
  146. $tomorrow = $today + 86400;
  147. return $date >= $today && $date < $tomorrow;
  148. case FreshRSS_Days::YESTERDAY:
  149. $yesterday = $today - 86400;
  150. return $date >= $yesterday && $date < $today;
  151. case FreshRSS_Days::BEFORE_YESTERDAY:
  152. $yesterday = $today - 86400;
  153. return $date < $yesterday;
  154. default:
  155. return false;
  156. }
  157. }
  158. public function loadCompleteContent($pathEntries) {
  159. // Gestion du contenu
  160. // On cherche à récupérer les articles en entier... même si le flux ne le propose pas
  161. if ($pathEntries) {
  162. $entryDAO = FreshRSS_Factory::createEntryDao();
  163. $entry = $entryDAO->searchByGuid($this->feed, $this->guid);
  164. if ($entry) {
  165. // l'article existe déjà en BDD, en se contente de recharger ce contenu
  166. $this->content = $entry->content();
  167. } else {
  168. try {
  169. // l'article n'est pas en BDD, on va le chercher sur le site
  170. $this->content = get_content_by_parsing(
  171. htmlspecialchars_decode($this->link(), ENT_QUOTES), $pathEntries
  172. );
  173. } catch (Exception $e) {
  174. // rien à faire, on garde l'ancien contenu(requête a échoué)
  175. }
  176. }
  177. }
  178. }
  179. public function toArray() {
  180. return array(
  181. 'id' => $this->id(),
  182. 'guid' => $this->guid(),
  183. 'title' => $this->title(),
  184. 'author' => $this->author(),
  185. 'content' => $this->content(),
  186. 'link' => $this->link(),
  187. 'date' => $this->date(true),
  188. 'hash' => $this->hash(),
  189. 'is_read' => $this->isRead(),
  190. 'is_favorite' => $this->isFavorite(),
  191. 'id_feed' => $this->feed(),
  192. 'tags' => $this->tags(true),
  193. );
  194. }
  195. }