Feed.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. class Feed extends Model {
  3. private $url;
  4. private $category = '';
  5. private $entries = null;
  6. private $name = '';
  7. private $website = '';
  8. private $description = '';
  9. public function __construct ($url) {
  10. $this->_url ($url);
  11. }
  12. public function id () {
  13. return small_hash ($this->url . Configuration::selApplication ());
  14. }
  15. public function url () {
  16. return $this->url;
  17. }
  18. public function category () {
  19. return $this->category;
  20. }
  21. public function entries () {
  22. if (!is_null ($this->entries)) {
  23. return $this->entries;
  24. } else {
  25. return array ();
  26. }
  27. }
  28. public function name () {
  29. return $this->name;
  30. }
  31. public function website () {
  32. return $this->website;
  33. }
  34. public function description () {
  35. return $this->description;
  36. }
  37. public function nbEntries () {
  38. $feedDAO = new FeedDAO ();
  39. return $feedDAO->countEntries ($this->id ());
  40. }
  41. public function _url ($value) {
  42. if (!is_null ($value) && filter_var ($value, FILTER_VALIDATE_URL)) {
  43. $this->url = $value;
  44. } else {
  45. throw new Exception ();
  46. }
  47. }
  48. public function _category ($value) {
  49. $this->category = $value;
  50. }
  51. public function _name ($value) {
  52. if (is_null ($value)) {
  53. $value = '';
  54. }
  55. $this->name = $value;
  56. }
  57. public function _website ($value) {
  58. if (is_null ($value)) {
  59. $value = '';
  60. }
  61. $this->website = $value;
  62. }
  63. public function _description ($value) {
  64. if (is_null ($value)) {
  65. $value = '';
  66. }
  67. $this->description = $value;
  68. }
  69. public function load () {
  70. if (!is_null ($this->url)) {
  71. $feed = new SimplePie ();
  72. $feed->set_feed_url ($this->url);
  73. $feed->set_cache_location (CACHE_PATH);
  74. $feed->init ();
  75. $title = $feed->get_title ();
  76. $this->_name (!is_null ($title) ? $title : $this->url);
  77. $this->_website ($feed->get_link ());
  78. $this->_description ($feed->get_description ());
  79. $this->loadEntries ($feed);
  80. }
  81. }
  82. private function loadEntries ($feed) {
  83. $entries = array ();
  84. foreach ($feed->get_items () as $item) {
  85. $title = $item->get_title ();
  86. $author = $item->get_author ();
  87. $link = $item->get_permalink ();
  88. $date = strtotime ($item->get_date ());
  89. // Gestion du contenu
  90. // On cherche à récupérer les articles en entier... même si le flux ne le propose pas
  91. $path = get_path ($this->website ());
  92. if ($path) {
  93. try {
  94. $content = get_content_by_parsing ($item->get_permalink (), $path);
  95. } catch (Exception $e) {
  96. $content = $item->get_content ();
  97. }
  98. } else {
  99. $content = $item->get_content ();
  100. }
  101. $entry = new Entry (
  102. $this->id (),
  103. $item->get_id (),
  104. !is_null ($title) ? $title : '',
  105. !is_null ($author) ? $author->name : '',
  106. !is_null ($content) ? $content : '',
  107. !is_null ($link) ? $link : '',
  108. $date ? $date : time ()
  109. );
  110. $entries[$entry->id ()] = $entry;
  111. }
  112. $this->entries = $entries;
  113. }
  114. }
  115. class FeedDAO extends Model_pdo {
  116. public function addFeed ($valuesTmp) {
  117. $sql = 'INSERT INTO feed (id, url, category, name, website, description) VALUES(?, ?, ?, ?, ?, ?)';
  118. $stm = $this->bd->prepare ($sql);
  119. $values = array (
  120. $valuesTmp['id'],
  121. $valuesTmp['url'],
  122. $valuesTmp['category'],
  123. $valuesTmp['name'],
  124. $valuesTmp['website'],
  125. $valuesTmp['description'],
  126. );
  127. if ($stm && $stm->execute ($values)) {
  128. return true;
  129. } else {
  130. return false;
  131. }
  132. }
  133. public function updateFeed ($id, $valuesTmp) {
  134. $set = '';
  135. foreach ($valuesTmp as $key => $v) {
  136. $set .= $key . '=?, ';
  137. }
  138. $set = substr ($set, 0, -2);
  139. $sql = 'UPDATE feed SET ' . $set . ' WHERE id=?';
  140. $stm = $this->bd->prepare ($sql);
  141. foreach ($valuesTmp as $v) {
  142. $values[] = $v;
  143. }
  144. $values[] = $id;
  145. if ($stm && $stm->execute ($values)) {
  146. return true;
  147. } else {
  148. return false;
  149. }
  150. }
  151. public function deleteFeed ($id) {
  152. $sql = 'DELETE FROM feed WHERE id=?';
  153. $stm = $this->bd->prepare ($sql);
  154. $values = array ($id);
  155. if ($stm && $stm->execute ($values)) {
  156. return true;
  157. } else {
  158. return false;
  159. }
  160. }
  161. public function searchById ($id) {
  162. $sql = 'SELECT * FROM feed WHERE id=?';
  163. $stm = $this->bd->prepare ($sql);
  164. $values = array ($id);
  165. $stm->execute ($values);
  166. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  167. $feed = HelperFeed::daoToFeed ($res);
  168. if (isset ($feed[0])) {
  169. return $feed[0];
  170. } else {
  171. return false;
  172. }
  173. }
  174. public function listFeeds () {
  175. $sql = 'SELECT * FROM feed ORDER BY name';
  176. $stm = $this->bd->prepare ($sql);
  177. $stm->execute ();
  178. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  179. }
  180. public function listByCategory ($cat) {
  181. $sql = 'SELECT * FROM feed WHERE category=? ORDER BY name';
  182. $stm = $this->bd->prepare ($sql);
  183. $values = array ($cat);
  184. $stm->execute ($values);
  185. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  186. }
  187. public function count () {
  188. $sql = 'SELECT COUNT(*) AS count FROM feed';
  189. $stm = $this->bd->prepare ($sql);
  190. $stm->execute ();
  191. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  192. return $res[0]['count'];
  193. }
  194. public function countEntries ($id) {
  195. $sql = 'SELECT COUNT(*) AS count FROM entry WHERE id_feed=?';
  196. $stm = $this->bd->prepare ($sql);
  197. $values = array ($id);
  198. $stm->execute ($values);
  199. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  200. return $res[0]['count'];
  201. }
  202. }
  203. class HelperFeed {
  204. public static function daoToFeed ($listDAO) {
  205. $list = array ();
  206. if (!is_array ($listDAO)) {
  207. $listDAO = array ($listDAO);
  208. }
  209. foreach ($listDAO as $key => $dao) {
  210. $list[$key] = new Feed ($dao['url']);
  211. $list[$key]->_category ($dao['category']);
  212. $list[$key]->_name ($dao['name']);
  213. $list[$key]->_website ($dao['website']);
  214. $list[$key]->_description ($dao['description']);
  215. }
  216. return $list;
  217. }
  218. }