Feed.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. $i = 0;
  85. foreach ($feed->get_items () as $item) {
  86. $title = $item->get_title ();
  87. $author = $item->get_author ();
  88. $link = $item->get_permalink ();
  89. $date = strtotime ($item->get_date ());
  90. // Gestion du contenu
  91. // On cherche à récupérer les articles en entier... même si le flux ne le propose pas
  92. $path = get_path ($this->website ());
  93. if ($path) {
  94. try {
  95. $content = get_content_by_parsing ($item->get_permalink (), $path);
  96. $i++;
  97. } catch (Exception $e) {
  98. $content = $item->get_content ();
  99. }
  100. } else {
  101. $content = $item->get_content ();
  102. }
  103. $entry = new Entry (
  104. $this->id (),
  105. $item->get_id (),
  106. !is_null ($title) ? $title : '',
  107. !is_null ($author) ? $author->name : '',
  108. !is_null ($content) ? $content : '',
  109. !is_null ($link) ? $link : '',
  110. $date ? $date : time ()
  111. );
  112. $entries[$entry->id ()] = $entry;
  113. if ($i > 10) {
  114. break;
  115. }
  116. }
  117. $this->entries = $entries;
  118. }
  119. }
  120. class FeedDAO extends Model_pdo {
  121. public function addFeed ($valuesTmp) {
  122. $sql = 'INSERT INTO feed (id, url, category, name, website, description) VALUES(?, ?, ?, ?, ?, ?)';
  123. $stm = $this->bd->prepare ($sql);
  124. $values = array (
  125. $valuesTmp['id'],
  126. $valuesTmp['url'],
  127. $valuesTmp['category'],
  128. $valuesTmp['name'],
  129. $valuesTmp['website'],
  130. $valuesTmp['description'],
  131. );
  132. if ($stm && $stm->execute ($values)) {
  133. return true;
  134. } else {
  135. return false;
  136. }
  137. }
  138. public function updateFeed ($id, $valuesTmp) {
  139. $set = '';
  140. foreach ($valuesTmp as $key => $v) {
  141. $set .= $key . '=?, ';
  142. }
  143. $set = substr ($set, 0, -2);
  144. $sql = 'UPDATE feed SET ' . $set . ' WHERE id=?';
  145. $stm = $this->bd->prepare ($sql);
  146. foreach ($valuesTmp as $v) {
  147. $values[] = $v;
  148. }
  149. $values[] = $id;
  150. if ($stm && $stm->execute ($values)) {
  151. return true;
  152. } else {
  153. return false;
  154. }
  155. }
  156. public function deleteFeed ($id) {
  157. $sql = 'DELETE FROM feed WHERE id=?';
  158. $stm = $this->bd->prepare ($sql);
  159. $values = array ($id);
  160. if ($stm && $stm->execute ($values)) {
  161. return true;
  162. } else {
  163. return false;
  164. }
  165. }
  166. public function searchById ($id) {
  167. $sql = 'SELECT * FROM feed WHERE id=?';
  168. $stm = $this->bd->prepare ($sql);
  169. $values = array ($id);
  170. $stm->execute ($values);
  171. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  172. $feed = HelperFeed::daoToFeed ($res);
  173. if (isset ($feed[0])) {
  174. return $feed[0];
  175. } else {
  176. return false;
  177. }
  178. }
  179. public function listFeeds () {
  180. $sql = 'SELECT * FROM feed ORDER BY name';
  181. $stm = $this->bd->prepare ($sql);
  182. $stm->execute ();
  183. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  184. }
  185. public function listByCategory ($cat) {
  186. $sql = 'SELECT * FROM feed WHERE category=? ORDER BY name';
  187. $stm = $this->bd->prepare ($sql);
  188. $values = array ($cat);
  189. $stm->execute ($values);
  190. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  191. }
  192. public function count () {
  193. $sql = 'SELECT COUNT(*) AS count FROM feed';
  194. $stm = $this->bd->prepare ($sql);
  195. $stm->execute ();
  196. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  197. return $res[0]['count'];
  198. }
  199. public function countEntries ($id) {
  200. $sql = 'SELECT COUNT(*) AS count FROM entry WHERE id_feed=?';
  201. $stm = $this->bd->prepare ($sql);
  202. $values = array ($id);
  203. $stm->execute ($values);
  204. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  205. return $res[0]['count'];
  206. }
  207. }
  208. class HelperFeed {
  209. public static function daoToFeed ($listDAO) {
  210. $list = array ();
  211. if (!is_array ($listDAO)) {
  212. $listDAO = array ($listDAO);
  213. }
  214. foreach ($listDAO as $key => $dao) {
  215. $list[$key] = new Feed ($dao['url']);
  216. $list[$key]->_category ($dao['category']);
  217. $list[$key]->_name ($dao['name']);
  218. $list[$key]->_website ($dao['website']);
  219. $list[$key]->_description ($dao['description']);
  220. }
  221. return $list;
  222. }
  223. }