Feed.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 nbNotRead () {
  59. $feedDAO = new FeedDAO ();
  60. return $feedDAO->countNotRead ($this->id ());
  61. }
  62. public function _id ($value) {
  63. $this->id = $value;
  64. }
  65. public function _url ($value) {
  66. if (!is_null ($value) && !preg_match ('#^https?://#', $value)) {
  67. $value = 'http://' . $value;
  68. }
  69. if (!is_null ($value) && filter_var ($value, FILTER_VALIDATE_URL)) {
  70. $this->url = $value;
  71. } else {
  72. throw new Exception ();
  73. }
  74. }
  75. public function _category ($value) {
  76. $this->category = $value;
  77. }
  78. public function _name ($value) {
  79. if (is_null ($value)) {
  80. $value = '';
  81. }
  82. $this->name = $value;
  83. }
  84. public function _website ($value) {
  85. if (is_null ($value)) {
  86. $value = '';
  87. }
  88. $this->website = $value;
  89. }
  90. public function _description ($value) {
  91. if (is_null ($value)) {
  92. $value = '';
  93. }
  94. $this->description = $value;
  95. }
  96. public function _lastUpdate ($value) {
  97. $this->lastUpdate = $value;
  98. }
  99. public function _pathEntries ($value) {
  100. $this->pathEntries = $value;
  101. }
  102. public function _httpAuth ($value) {
  103. $this->httpAuth = $value;
  104. }
  105. public function load () {
  106. if (!is_null ($this->url)) {
  107. if (CACHE_PATH === false) {
  108. throw new FileNotExistException (
  109. 'CACHE_PATH',
  110. MinzException::ERROR
  111. );
  112. } else {
  113. $feed = new SimplePie ();
  114. $feed->set_feed_url ($this->url);
  115. $feed->set_cache_location (CACHE_PATH);
  116. $feed->init ();
  117. $subscribe_url = $feed->subscribe_url ();
  118. if (!is_null ($subscribe_url) && $subscribe_url != $this->url) {
  119. $this->_url ($subscribe_url);
  120. }
  121. $title = $feed->get_title ();
  122. $this->_name (!is_null ($title) ? $title : $this->url);
  123. $this->_website ($feed->get_link ());
  124. $this->_description ($feed->get_description ());
  125. $this->loadEntries ($feed);
  126. }
  127. }
  128. }
  129. private function loadEntries ($feed) {
  130. $entries = array ();
  131. foreach ($feed->get_items () as $item) {
  132. $title = $item->get_title ();
  133. $author = $item->get_author ();
  134. $link = $item->get_permalink ();
  135. $date = strtotime ($item->get_date ());
  136. // gestion des tags (catégorie == tag)
  137. $tags_tmp = $item->get_categories ();
  138. $tags = array ();
  139. if (!is_null ($tags_tmp)) {
  140. foreach ($tags_tmp as $tag) {
  141. $tags[] = $tag->get_label ();
  142. }
  143. }
  144. // Gestion du contenu
  145. // On cherche à récupérer les articles en entier... même si le flux ne le propose pas
  146. $path = $this->pathEntries ();
  147. if ($path) {
  148. try {
  149. $content = get_content_by_parsing ($item->get_permalink (), $path);
  150. } catch (Exception $e) {
  151. $content = $item->get_content ();
  152. }
  153. } else {
  154. $content = $item->get_content ();
  155. }
  156. $entry = new Entry (
  157. $this->id (),
  158. $item->get_id (),
  159. !is_null ($title) ? $title : '',
  160. !is_null ($author) ? $author->name : '',
  161. !is_null ($content) ? $content : '',
  162. !is_null ($link) ? $link : '',
  163. $date ? $date : time ()
  164. );
  165. $entry->_tags ($tags);
  166. $entries[$entry->id ()] = $entry;
  167. }
  168. $this->entries = $entries;
  169. }
  170. }
  171. class FeedDAO extends Model_pdo {
  172. public function addFeed ($valuesTmp) {
  173. $sql = 'INSERT INTO feed (id, url, category, name, website, description, lastUpdate) VALUES(?, ?, ?, ?, ?, ?, ?)';
  174. $stm = $this->bd->prepare ($sql);
  175. $values = array (
  176. $valuesTmp['id'],
  177. $valuesTmp['url'],
  178. $valuesTmp['category'],
  179. $valuesTmp['name'],
  180. $valuesTmp['website'],
  181. $valuesTmp['description'],
  182. $valuesTmp['lastUpdate'],
  183. );
  184. if ($stm && $stm->execute ($values)) {
  185. return true;
  186. } else {
  187. return false;
  188. }
  189. }
  190. public function updateFeed ($id, $valuesTmp) {
  191. $set = '';
  192. foreach ($valuesTmp as $key => $v) {
  193. $set .= $key . '=?, ';
  194. }
  195. $set = substr ($set, 0, -2);
  196. $sql = 'UPDATE feed SET ' . $set . ' WHERE id=?';
  197. $stm = $this->bd->prepare ($sql);
  198. foreach ($valuesTmp as $v) {
  199. $values[] = $v;
  200. }
  201. $values[] = $id;
  202. if ($stm && $stm->execute ($values)) {
  203. return true;
  204. } else {
  205. return false;
  206. }
  207. }
  208. public function updateLastUpdate ($id) {
  209. $sql = 'UPDATE feed SET lastUpdate=? WHERE id=?';
  210. $stm = $this->bd->prepare ($sql);
  211. $values = array (
  212. time (),
  213. $id
  214. );
  215. if ($stm && $stm->execute ($values)) {
  216. return true;
  217. } else {
  218. return false;
  219. }
  220. }
  221. public function deleteFeed ($id) {
  222. $sql = 'DELETE FROM feed WHERE id=?';
  223. $stm = $this->bd->prepare ($sql);
  224. $values = array ($id);
  225. if ($stm && $stm->execute ($values)) {
  226. return true;
  227. } else {
  228. return false;
  229. }
  230. }
  231. public function searchById ($id) {
  232. $sql = 'SELECT * FROM feed WHERE id=?';
  233. $stm = $this->bd->prepare ($sql);
  234. $values = array ($id);
  235. $stm->execute ($values);
  236. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  237. $feed = HelperFeed::daoToFeed ($res);
  238. if (isset ($feed[$id])) {
  239. return $feed[$id];
  240. } else {
  241. return false;
  242. }
  243. }
  244. public function listFeeds () {
  245. $sql = 'SELECT * FROM feed ORDER BY name';
  246. $stm = $this->bd->prepare ($sql);
  247. $stm->execute ();
  248. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  249. }
  250. public function listFeedsOrderUpdate () {
  251. $sql = 'SELECT * FROM feed ORDER BY lastUpdate';
  252. $stm = $this->bd->prepare ($sql);
  253. $stm->execute ();
  254. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  255. }
  256. public function listByCategory ($cat) {
  257. $sql = 'SELECT * FROM feed WHERE category=? ORDER BY name';
  258. $stm = $this->bd->prepare ($sql);
  259. $values = array ($cat);
  260. $stm->execute ($values);
  261. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  262. }
  263. public function count () {
  264. $sql = 'SELECT COUNT(*) AS count FROM feed';
  265. $stm = $this->bd->prepare ($sql);
  266. $stm->execute ();
  267. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  268. return $res[0]['count'];
  269. }
  270. public function countEntries ($id) {
  271. $sql = 'SELECT COUNT(*) AS count FROM entry WHERE id_feed=?';
  272. $stm = $this->bd->prepare ($sql);
  273. $values = array ($id);
  274. $stm->execute ($values);
  275. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  276. return $res[0]['count'];
  277. }
  278. public function countNotRead ($id) {
  279. $sql = 'SELECT COUNT(*) AS count FROM entry WHERE is_read=0 AND id_feed=?';
  280. $stm = $this->bd->prepare ($sql);
  281. $values = array ($id);
  282. $stm->execute ($values);
  283. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  284. return $res[0]['count'];
  285. }
  286. }
  287. class HelperFeed {
  288. public static function daoToFeed ($listDAO) {
  289. $list = array ();
  290. if (!is_array ($listDAO)) {
  291. $listDAO = array ($listDAO);
  292. }
  293. foreach ($listDAO as $key => $dao) {
  294. if (isset ($dao['id'])) {
  295. $key = $dao['id'];
  296. }
  297. $list[$key] = new Feed ($dao['url']);
  298. $list[$key]->_category ($dao['category']);
  299. $list[$key]->_name ($dao['name']);
  300. $list[$key]->_website ($dao['website']);
  301. $list[$key]->_description ($dao['description']);
  302. $list[$key]->_lastUpdate ($dao['lastUpdate']);
  303. $list[$key]->_pathEntries ($dao['pathEntries']);
  304. $list[$key]->_httpAuth ($dao['httpAuth']);
  305. if (isset ($dao['id'])) {
  306. $list[$key]->_id ($dao['id']);
  307. }
  308. }
  309. return $list;
  310. }
  311. }