Feed.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. $subscribe_url = $feed->subscribe_url ();
  114. if (!is_null ($subscribe_url) && $subscribe_url != $this->url) {
  115. $this->_url ($subscribe_url);
  116. }
  117. $title = $feed->get_title ();
  118. $this->_name (!is_null ($title) ? $title : $this->url);
  119. $this->_website ($feed->get_link ());
  120. $this->_description ($feed->get_description ());
  121. $this->loadEntries ($feed);
  122. }
  123. }
  124. }
  125. private function loadEntries ($feed) {
  126. $entries = array ();
  127. foreach ($feed->get_items () as $item) {
  128. $title = $item->get_title ();
  129. $author = $item->get_author ();
  130. $link = $item->get_permalink ();
  131. $date = strtotime ($item->get_date ());
  132. // gestion des tags (catégorie == tag)
  133. $tags_tmp = $item->get_categories ();
  134. $tags = array ();
  135. if (!is_null ($tags_tmp)) {
  136. foreach ($tags_tmp as $tag) {
  137. $tags[] = $tag->get_label ();
  138. }
  139. }
  140. // Gestion du contenu
  141. // On cherche à récupérer les articles en entier... même si le flux ne le propose pas
  142. $path = $this->pathEntries ();
  143. if ($path) {
  144. try {
  145. $content = get_content_by_parsing ($item->get_permalink (), $path);
  146. } catch (Exception $e) {
  147. $content = $item->get_content ();
  148. }
  149. } else {
  150. $content = $item->get_content ();
  151. }
  152. $entry = new Entry (
  153. $this->id (),
  154. $item->get_id (),
  155. !is_null ($title) ? $title : '',
  156. !is_null ($author) ? $author->name : '',
  157. !is_null ($content) ? $content : '',
  158. !is_null ($link) ? $link : '',
  159. $date ? $date : time ()
  160. );
  161. $entry->_tags ($tags);
  162. $entries[$entry->id ()] = $entry;
  163. }
  164. $this->entries = $entries;
  165. }
  166. }
  167. class FeedDAO extends Model_pdo {
  168. public function addFeed ($valuesTmp) {
  169. $sql = 'INSERT INTO feed (id, url, category, name, website, description, lastUpdate) VALUES(?, ?, ?, ?, ?, ?, ?)';
  170. $stm = $this->bd->prepare ($sql);
  171. $values = array (
  172. $valuesTmp['id'],
  173. $valuesTmp['url'],
  174. $valuesTmp['category'],
  175. $valuesTmp['name'],
  176. $valuesTmp['website'],
  177. $valuesTmp['description'],
  178. $valuesTmp['lastUpdate'],
  179. );
  180. if ($stm && $stm->execute ($values)) {
  181. return true;
  182. } else {
  183. return false;
  184. }
  185. }
  186. public function updateFeed ($id, $valuesTmp) {
  187. $set = '';
  188. foreach ($valuesTmp as $key => $v) {
  189. $set .= $key . '=?, ';
  190. }
  191. $set = substr ($set, 0, -2);
  192. $sql = 'UPDATE feed SET ' . $set . ' WHERE id=?';
  193. $stm = $this->bd->prepare ($sql);
  194. foreach ($valuesTmp as $v) {
  195. $values[] = $v;
  196. }
  197. $values[] = $id;
  198. if ($stm && $stm->execute ($values)) {
  199. return true;
  200. } else {
  201. return false;
  202. }
  203. }
  204. public function updateLastUpdate ($id) {
  205. $sql = 'UPDATE feed SET lastUpdate=? WHERE id=?';
  206. $stm = $this->bd->prepare ($sql);
  207. $values = array (
  208. time (),
  209. $id
  210. );
  211. if ($stm && $stm->execute ($values)) {
  212. return true;
  213. } else {
  214. return false;
  215. }
  216. }
  217. public function deleteFeed ($id) {
  218. $sql = 'DELETE FROM feed WHERE id=?';
  219. $stm = $this->bd->prepare ($sql);
  220. $values = array ($id);
  221. if ($stm && $stm->execute ($values)) {
  222. return true;
  223. } else {
  224. return false;
  225. }
  226. }
  227. public function searchById ($id) {
  228. $sql = 'SELECT * FROM feed WHERE id=?';
  229. $stm = $this->bd->prepare ($sql);
  230. $values = array ($id);
  231. $stm->execute ($values);
  232. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  233. $feed = HelperFeed::daoToFeed ($res);
  234. if (isset ($feed[$id])) {
  235. return $feed[$id];
  236. } else {
  237. return false;
  238. }
  239. }
  240. public function listFeeds () {
  241. $sql = 'SELECT * FROM feed ORDER BY name';
  242. $stm = $this->bd->prepare ($sql);
  243. $stm->execute ();
  244. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  245. }
  246. public function listFeedsOrderUpdate () {
  247. $sql = 'SELECT * FROM feed ORDER BY lastUpdate';
  248. $stm = $this->bd->prepare ($sql);
  249. $stm->execute ();
  250. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  251. }
  252. public function listByCategory ($cat) {
  253. $sql = 'SELECT * FROM feed WHERE category=? ORDER BY name';
  254. $stm = $this->bd->prepare ($sql);
  255. $values = array ($cat);
  256. $stm->execute ($values);
  257. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  258. }
  259. public function count () {
  260. $sql = 'SELECT COUNT(*) AS count FROM feed';
  261. $stm = $this->bd->prepare ($sql);
  262. $stm->execute ();
  263. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  264. return $res[0]['count'];
  265. }
  266. public function countEntries ($id) {
  267. $sql = 'SELECT COUNT(*) AS count FROM entry WHERE id_feed=?';
  268. $stm = $this->bd->prepare ($sql);
  269. $values = array ($id);
  270. $stm->execute ($values);
  271. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  272. return $res[0]['count'];
  273. }
  274. }
  275. class HelperFeed {
  276. public static function daoToFeed ($listDAO) {
  277. $list = array ();
  278. if (!is_array ($listDAO)) {
  279. $listDAO = array ($listDAO);
  280. }
  281. foreach ($listDAO as $key => $dao) {
  282. if (isset ($dao['id'])) {
  283. $key = $dao['id'];
  284. }
  285. $list[$key] = new Feed ($dao['url']);
  286. $list[$key]->_category ($dao['category']);
  287. $list[$key]->_name ($dao['name']);
  288. $list[$key]->_website ($dao['website']);
  289. $list[$key]->_description ($dao['description']);
  290. $list[$key]->_lastUpdate ($dao['lastUpdate']);
  291. $list[$key]->_pathEntries ($dao['pathEntries']);
  292. $list[$key]->_httpAuth ($dao['httpAuth']);
  293. if (isset ($dao['id'])) {
  294. $list[$key]->_id ($dao['id']);
  295. }
  296. }
  297. return $list;
  298. }
  299. }