feedController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?php
  2. class feedController extends ActionController {
  3. public function firstAction () {
  4. $token = $this->view->conf->token();
  5. $token_param = Request::param ('token', '');
  6. $token_is_ok = ($token != '' && $token == $token_param);
  7. $action = Request::actionName ();
  8. if (login_is_conf ($this->view->conf) &&
  9. !is_logged () &&
  10. !($token_is_ok && $action == 'actualize')) {
  11. Error::error (
  12. 403,
  13. array ('error' => array (Translate::t ('access_denied')))
  14. );
  15. }
  16. $this->catDAO = new CategoryDAO ();
  17. $this->catDAO->checkDefault ();
  18. }
  19. public function addAction () {
  20. if (Request::isPost ()) {
  21. $url = Request::param ('url_rss');
  22. $cat = Request::param ('category', false);
  23. if ($cat === false) {
  24. $def_cat = $this->catDAO->getDefault ();
  25. $cat = $def_cat->id ();
  26. }
  27. $user = Request::param ('username');
  28. $pass = Request::param ('password');
  29. $params = array ();
  30. $transactionStarted = false;
  31. try {
  32. $feed = new Feed ($url);
  33. $feed->_category ($cat);
  34. $httpAuth = '';
  35. if ($user != '' || $pass != '') {
  36. $httpAuth = $user . ':' . $pass;
  37. }
  38. $feed->_httpAuth ($httpAuth);
  39. $feed->load ();
  40. $feedDAO = new FeedDAO ();
  41. $values = array (
  42. 'id' => $feed->id (),
  43. 'url' => $feed->url (),
  44. 'category' => $feed->category (),
  45. 'name' => $feed->name (),
  46. 'website' => $feed->website (),
  47. 'description' => $feed->description (),
  48. 'lastUpdate' => time (),
  49. 'httpAuth' => $feed->httpAuth (),
  50. );
  51. if ($feedDAO->searchByUrl ($values['url'])) {
  52. // on est déjà abonné à ce flux
  53. $notif = array (
  54. 'type' => 'bad',
  55. 'content' => Translate::t ('already_subscribed', $feed->name ())
  56. );
  57. Session::_param ('notification', $notif);
  58. } elseif (!$feedDAO->addFeed ($values)) {
  59. // problème au niveau de la base de données
  60. $notif = array (
  61. 'type' => 'bad',
  62. 'content' => Translate::t ('feed_not_added', $feed->name ())
  63. );
  64. Session::_param ('notification', $notif);
  65. } else {
  66. $entryDAO = new EntryDAO ();
  67. $entries = $feed->entries ();
  68. // on calcule la date des articles les plus anciens qu'on accepte
  69. $nb_month_old = $this->view->conf->oldEntries ();
  70. $date_min = time () - (60 * 60 * 24 * 30 * $nb_month_old);
  71. $transactionStarted = true;
  72. $feedDAO->beginTransaction ();
  73. // on ajoute les articles en masse sans vérification
  74. foreach ($entries as $entry) {
  75. if ($entry->date (true) >= $date_min ||
  76. $feed->keepHistory ()) {
  77. $values = $entry->toArray ();
  78. $entryDAO->addEntry ($values);
  79. }
  80. }
  81. $feedDAO->updateLastUpdate ($feed->id ());
  82. $feedDAO->commit ();
  83. $transactionStarted = false;
  84. // ok, ajout terminé
  85. $notif = array (
  86. 'type' => 'good',
  87. 'content' => Translate::t ('feed_added', $feed->name ())
  88. );
  89. Session::_param ('notification', $notif);
  90. // permet de rediriger vers la page de conf du flux
  91. $params['id'] = $feed->id ();
  92. }
  93. } catch (BadUrlException $e) {
  94. Minz_Log::record ($e->getMessage (), Minz_Log::WARNING);
  95. $notif = array (
  96. 'type' => 'bad',
  97. 'content' => Translate::t ('invalid_url', $url)
  98. );
  99. Session::_param ('notification', $notif);
  100. } catch (FeedException $e) {
  101. Minz_Log::record ($e->getMessage (), Minz_Log::WARNING);
  102. $notif = array (
  103. 'type' => 'bad',
  104. 'content' => Translate::t ('internal_problem_feed')
  105. );
  106. Session::_param ('notification', $notif);
  107. } catch (FileNotExistException $e) {
  108. // Répertoire de cache n'existe pas
  109. Minz_Log::record ($e->getMessage (), Minz_Log::ERROR);
  110. $notif = array (
  111. 'type' => 'bad',
  112. 'content' => Translate::t ('internal_problem_feed')
  113. );
  114. Session::_param ('notification', $notif);
  115. }
  116. if ($transactionStarted) {
  117. $feedDAO->rollBack ();
  118. }
  119. Request::forward (array ('c' => 'configure', 'a' => 'feed', 'params' => $params), true);
  120. }
  121. }
  122. public function actualizeAction () {
  123. $feedDAO = new FeedDAO ();
  124. $entryDAO = new EntryDAO ();
  125. $id = Request::param ('id');
  126. $force = Request::param ('force', false);
  127. // on créé la liste des flux à mettre à actualiser
  128. // si on veut mettre un flux à jour spécifiquement, on le met
  129. // dans la liste, mais seul (permet d'automatiser le traitement)
  130. $feeds = array ();
  131. if ($id) {
  132. $feed = $feedDAO->searchById ($id);
  133. if ($feed) {
  134. $feeds = array ($feed);
  135. }
  136. } else {
  137. $feeds = $feedDAO->listFeedsOrderUpdate ();
  138. }
  139. // on calcule la date des articles les plus anciens qu'on accepte
  140. $nb_month_old = $this->view->conf->oldEntries ();
  141. $date_min = time () - (60 * 60 * 24 * 30 * $nb_month_old);
  142. if (rand(0, 30) === 1) {
  143. $nb = $entryDAO->cleanOldEntries ($date_min);
  144. Minz_Log::record ($nb . ' old entries cleaned.', Minz_Log::DEBUG);
  145. if ($nb > 0) {
  146. $nb = $feedDAO->updateCachedValues ();
  147. Minz_Log::record ($nb . ' cached values updated.', Minz_Log::DEBUG);
  148. }
  149. }
  150. $i = 0;
  151. $flux_update = 0;
  152. foreach ($feeds as $feed) {
  153. try {
  154. $feed->load ();
  155. $entries = $feed->entries ();
  156. //For this feed, check last n entry GUIDs already in database
  157. $existingGuids = array_fill_keys ($entryDAO->listLastGuidsByFeed ($feed->id (), count($entries) + 10), 1);
  158. // On ne vérifie pas strictement que l'article n'est pas déjà en BDD
  159. // La BDD refusera l'ajout car (id_feed, guid) doit être unique
  160. $feedDAO->beginTransaction ();
  161. foreach ($entries as $entry) {
  162. if ((!isset ($existingGuids[$entry->guid ()])) &&
  163. ($entry->date (true) >= $date_min ||
  164. $feed->keepHistory ())) {
  165. $values = $entry->toArray ();
  166. $entryDAO->addEntry ($values);
  167. }
  168. }
  169. // on indique que le flux vient d'être mis à jour en BDD
  170. $feedDAO->updateLastUpdate ($feed->id ());
  171. $feedDAO->commit ();
  172. $flux_update++;
  173. } catch (FeedException $e) {
  174. Minz_Log::record ($e->getMessage (), Minz_Log::NOTICE);
  175. $feedDAO->updateLastUpdate ($feed->id (), 1);
  176. }
  177. // On arrête à 10 flux pour ne pas surcharger le serveur
  178. // sauf si le paramètre $force est à vrai
  179. $i++;
  180. if ($i >= 10 && !$force) {
  181. break;
  182. }
  183. }
  184. $url = array ();
  185. if ($flux_update === 1) {
  186. // on a mis un seul flux à jour
  187. $notif = array (
  188. 'type' => 'good',
  189. 'content' => Translate::t ('feed_actualized', $feed->name ())
  190. );
  191. } elseif ($flux_update > 1) {
  192. // plusieurs flux on été mis à jour
  193. $notif = array (
  194. 'type' => 'good',
  195. 'content' => Translate::t ('n_feeds_actualized', $flux_update)
  196. );
  197. } else {
  198. // aucun flux n'a été mis à jour, oups
  199. $notif = array (
  200. 'type' => 'bad',
  201. 'content' => Translate::t ('no_feed_actualized')
  202. );
  203. }
  204. if ($i === 1) {
  205. // Si on a voulu mettre à jour qu'un flux
  206. // on filtre l'affichage par ce flux
  207. $feed = reset ($feeds);
  208. $url['params'] = array ('get' => 'f_' . $feed->id ());
  209. }
  210. if (Request::param ('ajax', 0) === 0) {
  211. Session::_param ('notification', $notif);
  212. Request::forward ($url, true);
  213. } else {
  214. // Une requête Ajax met un seul flux à jour.
  215. // Comme en principe plusieurs requêtes ont lieu,
  216. // on indique que "plusieurs flux ont été mis à jour".
  217. // Cela permet d'avoir une notification plus proche du
  218. // ressenti utilisateur
  219. $notif = array (
  220. 'type' => 'good',
  221. 'content' => Translate::t ('feeds_actualized')
  222. );
  223. Session::_param ('notification', $notif);
  224. // et on désactive le layout car ne sert à rien
  225. $this->view->_useLayout (false);
  226. }
  227. }
  228. public function massiveImportAction () {
  229. $entryDAO = new EntryDAO ();
  230. $feedDAO = new FeedDAO ();
  231. $categories = Request::param ('categories', array (), true);
  232. $feeds = Request::param ('feeds', array (), true);
  233. // on ajoute les catégories en masse dans une fonction à part
  234. $this->addCategories ($categories);
  235. // on calcule la date des articles les plus anciens qu'on accepte
  236. $nb_month_old = $this->view->conf->oldEntries ();
  237. $date_min = time () - (60 * 60 * 24 * 30 * $nb_month_old);
  238. // la variable $error permet de savoir si une erreur est survenue
  239. // Le but est de ne pas arrêter l'import même en cas d'erreur
  240. // L'utilisateur sera mis au courant s'il y a eu des erreurs, mais
  241. // ne connaîtra pas les détails. Ceux-ci seront toutefois logguées
  242. $error = false;
  243. $i = 0;
  244. foreach ($feeds as $feed) {
  245. try {
  246. $feed->load ();
  247. $values = array (
  248. 'id' => $feed->id (),
  249. 'url' => $feed->url (),
  250. 'category' => $feed->category (),
  251. 'name' => $feed->name (),
  252. 'website' => $feed->website (),
  253. 'description' => $feed->description (),
  254. 'lastUpdate' => 0,
  255. 'httpAuth' => $feed->httpAuth ()
  256. );
  257. // ajout du flux que s'il n'est pas déjà en BDD
  258. if (!$feedDAO->searchByUrl ($values['url'])) {
  259. if (!$feedDAO->addFeed ($values)) {
  260. $error = true;
  261. }
  262. }
  263. } catch (FeedException $e) {
  264. $error = true;
  265. Minz_Log::record ($e->getMessage (), Minz_Log::WARNING);
  266. }
  267. }
  268. if ($error) {
  269. $res = Translate::t ('feeds_imported_with_errors');
  270. } else {
  271. $res = Translate::t ('feeds_imported');
  272. }
  273. $notif = array (
  274. 'type' => 'good',
  275. 'content' => $res
  276. );
  277. Session::_param ('notification', $notif);
  278. Session::_param ('actualize_feeds', true);
  279. // et on redirige vers la page import/export
  280. Request::forward (array (
  281. 'c' => 'configure',
  282. 'a' => 'importExport'
  283. ), true);
  284. }
  285. public function deleteAction () {
  286. $type = Request::param ('type', 'feed');
  287. $id = Request::param ('id');
  288. $feedDAO = new FeedDAO ();
  289. if ($type == 'category') {
  290. if ($feedDAO->deleteFeedByCategory ($id)) {
  291. $notif = array (
  292. 'type' => 'good',
  293. 'content' => Translate::t ('category_emptied')
  294. );
  295. } else {
  296. $notif = array (
  297. 'type' => 'bad',
  298. 'content' => Translate::t ('error_occured')
  299. );
  300. }
  301. } else {
  302. if ($feedDAO->deleteFeed ($id)) {
  303. $notif = array (
  304. 'type' => 'good',
  305. 'content' => Translate::t ('feed_deleted')
  306. );
  307. } else {
  308. $notif = array (
  309. 'type' => 'bad',
  310. 'content' => Translate::t ('error_occured')
  311. );
  312. }
  313. }
  314. Session::_param ('notification', $notif);
  315. if ($type == 'category') {
  316. Request::forward (array ('c' => 'configure', 'a' => 'categorize'), true);
  317. } else {
  318. Request::forward (array ('c' => 'configure', 'a' => 'feed'), true);
  319. }
  320. }
  321. private function addCategories ($categories) {
  322. $catDAO = new CategoryDAO ();
  323. foreach ($categories as $cat) {
  324. if (!$catDAO->searchByName ($cat->name ())) {
  325. $values = array (
  326. 'id' => $cat->id (),
  327. 'name' => $cat->name (),
  328. 'color' => $cat->color ()
  329. );
  330. $catDAO->addCategory ($values);
  331. }
  332. }
  333. }
  334. }