Feed.php 6.8 KB

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