Entry.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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->_title($title);
  23. $this->_author($author);
  24. $this->_content($content);
  25. $this->_link($link);
  26. $this->_date($pubdate);
  27. $this->_isRead($is_read);
  28. $this->_isFavorite($is_favorite);
  29. $this->_feed($feed);
  30. $this->_tags(preg_split('/[\s#]/', $tags));
  31. $this->_guid($guid);
  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. if ($value == '') {
  99. $value = $this->link;
  100. if ($value == '') {
  101. $value = $this->hash();
  102. }
  103. }
  104. $this->guid = $value;
  105. }
  106. public function _title($value) {
  107. $this->hash = null;
  108. $this->title = $value;
  109. }
  110. public function _author($value) {
  111. $this->hash = null;
  112. $this->author = $value;
  113. }
  114. public function _content($value) {
  115. $this->hash = null;
  116. $this->content = $value;
  117. }
  118. public function _link($value) {
  119. $this->hash = null;
  120. $this->link = $value;
  121. }
  122. public function _date($value) {
  123. $this->hash = null;
  124. $value = intval($value);
  125. $this->date = $value > 1 ? $value : time();
  126. }
  127. public function _isRead($value) {
  128. $this->is_read = $value === null ? null : (bool)$value;
  129. }
  130. public function _isFavorite($value) {
  131. $this->is_favorite = $value;
  132. }
  133. public function _feed($value) {
  134. $this->feed = $value;
  135. }
  136. public function _tags($value) {
  137. $this->hash = null;
  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 isDay($day, $today) {
  149. $date = $this->dateAdded(true);
  150. switch ($day) {
  151. case FreshRSS_Days::TODAY:
  152. $tomorrow = $today + 86400;
  153. return $date >= $today && $date < $tomorrow;
  154. case FreshRSS_Days::YESTERDAY:
  155. $yesterday = $today - 86400;
  156. return $date >= $yesterday && $date < $today;
  157. case FreshRSS_Days::BEFORE_YESTERDAY:
  158. $yesterday = $today - 86400;
  159. return $date < $yesterday;
  160. default:
  161. return false;
  162. }
  163. }
  164. public function loadCompleteContent($pathEntries) {
  165. // Gestion du contenu
  166. // On cherche à récupérer les articles en entier... même si le flux ne le propose pas
  167. if ($pathEntries) {
  168. $entryDAO = FreshRSS_Factory::createEntryDao();
  169. $entry = $entryDAO->searchByGuid($this->feed, $this->guid);
  170. if ($entry) {
  171. // l'article existe déjà en BDD, en se contente de recharger ce contenu
  172. $this->content = $entry->content();
  173. } else {
  174. try {
  175. // l'article n'est pas en BDD, on va le chercher sur le site
  176. $this->content = get_content_by_parsing(
  177. htmlspecialchars_decode($this->link(), ENT_QUOTES), $pathEntries
  178. );
  179. } catch (Exception $e) {
  180. // rien à faire, on garde l'ancien contenu(requête a échoué)
  181. Minz_Log::warning($e->getMessage());
  182. }
  183. }
  184. }
  185. }
  186. public function toArray() {
  187. return array(
  188. 'id' => $this->id(),
  189. 'guid' => $this->guid(),
  190. 'title' => $this->title(),
  191. 'author' => $this->author(),
  192. 'content' => $this->content(),
  193. 'link' => $this->link(),
  194. 'date' => $this->date(true),
  195. 'hash' => $this->hash(),
  196. 'is_read' => $this->isRead(),
  197. 'is_favorite' => $this->isFavorite(),
  198. 'id_feed' => $this->feed(),
  199. 'tags' => $this->tags(true),
  200. );
  201. }
  202. }