Entry.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 _hash($value) {
  95. $value = trim($value);
  96. if (ctype_xdigit($value)) {
  97. $this->hash = substr($value, 0, 32);
  98. }
  99. return $this->hash;
  100. }
  101. public function _id($value) {
  102. $this->id = $value;
  103. }
  104. public function _guid($value) {
  105. if ($value == '') {
  106. $value = $this->link;
  107. if ($value == '') {
  108. $value = $this->hash();
  109. }
  110. }
  111. $this->guid = $value;
  112. }
  113. public function _title($value) {
  114. $this->hash = null;
  115. $this->title = $value;
  116. }
  117. public function _author($value) {
  118. $this->hash = null;
  119. $this->author = $value;
  120. }
  121. public function _content($value) {
  122. $this->hash = null;
  123. $this->content = $value;
  124. }
  125. public function _link($value) {
  126. $this->hash = null;
  127. $this->link = $value;
  128. }
  129. public function _date($value) {
  130. $this->hash = null;
  131. $value = intval($value);
  132. $this->date = $value > 1 ? $value : time();
  133. }
  134. public function _isRead($value) {
  135. $this->is_read = $value === null ? null : (bool)$value;
  136. }
  137. public function _isFavorite($value) {
  138. $this->is_favorite = $value;
  139. }
  140. public function _feed($value) {
  141. $this->feed = $value;
  142. }
  143. public function _tags($value) {
  144. $this->hash = null;
  145. if (!is_array($value)) {
  146. $value = array($value);
  147. }
  148. foreach ($value as $key => $t) {
  149. if (!$t) {
  150. unset($value[$key]);
  151. }
  152. }
  153. $this->tags = $value;
  154. }
  155. public function isDay($day, $today) {
  156. $date = $this->dateAdded(true);
  157. switch ($day) {
  158. case FreshRSS_Days::TODAY:
  159. $tomorrow = $today + 86400;
  160. return $date >= $today && $date < $tomorrow;
  161. case FreshRSS_Days::YESTERDAY:
  162. $yesterday = $today - 86400;
  163. return $date >= $yesterday && $date < $today;
  164. case FreshRSS_Days::BEFORE_YESTERDAY:
  165. $yesterday = $today - 86400;
  166. return $date < $yesterday;
  167. default:
  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 = FreshRSS_Factory::createEntryDao();
  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. htmlspecialchars_decode($this->link(), ENT_QUOTES), $pathEntries
  185. );
  186. } catch (Exception $e) {
  187. // rien à faire, on garde l'ancien contenu(requête a échoué)
  188. Minz_Log::warning($e->getMessage());
  189. }
  190. }
  191. }
  192. }
  193. public function toArray() {
  194. return array(
  195. 'id' => $this->id(),
  196. 'guid' => $this->guid(),
  197. 'title' => $this->title(),
  198. 'author' => $this->author(),
  199. 'content' => $this->content(),
  200. 'link' => $this->link(),
  201. 'date' => $this->date(true),
  202. 'hash' => $this->hash(),
  203. 'is_read' => $this->isRead(),
  204. 'is_favorite' => $this->isFavorite(),
  205. 'id_feed' => $this->feed(),
  206. 'tags' => $this->tags(true),
  207. );
  208. }
  209. }