Feed.php 7.2 KB

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