Feed.php 5.7 KB

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