Feed.php 6.6 KB

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