Feed.php 6.4 KB

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