feedController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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::ERROR);
  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::ERROR);
  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. Minz_Log::record ('CleanOldEntries', Minz_Log::NOTICE); //TODO: Remove
  144. if ($entryDAO->cleanOldEntries ($date_min) > 0) {
  145. Minz_Log::record ('UpdateCachedValues', Minz_Log::NOTICE); //TODO: Remove
  146. $feedDAO->updateCachedValues ();
  147. }
  148. }
  149. $i = 0;
  150. $flux_update = 0;
  151. foreach ($feeds as $feed) {
  152. try {
  153. $feed->load ();
  154. $entries = $feed->entries ();
  155. //For this feed, check last n entry IDs already in database
  156. $existingIds = array_fill_keys ($entryDAO->listLastIdsByFeed ($feed->id (), count($entries) + 2), 1);
  157. // ajout des articles en masse sans se soucier des erreurs
  158. // On ne vérifie pas que l'article n'est pas déjà en BDD
  159. // car demanderait plus de ressources
  160. // La BDD refusera l'ajout de son côté car l'id doit être
  161. // unique
  162. $feedDAO->beginTransaction ();
  163. foreach ($entries as $entry) {
  164. if ((!isset ($existingIds[$entry->id ()])) &&
  165. ($entry->date (true) >= $date_min ||
  166. $feed->keepHistory ())) {
  167. $values = $entry->toArray ();
  168. $entryDAO->addEntry ($values);
  169. }
  170. }
  171. // on indique que le flux vient d'être mis à jour en BDD
  172. $feedDAO->updateLastUpdate ($feed->id ());
  173. $feedDAO->commit ();
  174. $flux_update++;
  175. } catch (FeedException $e) {
  176. Minz_Log::record ($e->getMessage (), Minz_Log::ERROR);
  177. $feedDAO->updateLastUpdate ($feed->id (), 1);
  178. }
  179. // On arrête à 10 flux pour ne pas surcharger le serveur
  180. // sauf si le paramètre $force est à vrai
  181. $i++;
  182. if ($i >= 10 && !$force) {
  183. break;
  184. }
  185. }
  186. $url = array ();
  187. if ($flux_update === 1) {
  188. // on a mis un seul flux à jour
  189. $notif = array (
  190. 'type' => 'good',
  191. 'content' => Translate::t ('feed_actualized', $feed->name ())
  192. );
  193. } elseif ($flux_update > 1) {
  194. // plusieurs flux on été mis à jour
  195. $notif = array (
  196. 'type' => 'good',
  197. 'content' => Translate::t ('n_feeds_actualized', $flux_update)
  198. );
  199. } else {
  200. // aucun flux n'a été mis à jour, oups
  201. $notif = array (
  202. 'type' => 'bad',
  203. 'content' => Translate::t ('no_feed_actualized')
  204. );
  205. }
  206. if ($i === 1) {
  207. // Si on a voulu mettre à jour qu'un flux
  208. // on filtre l'affichage par ce flux
  209. $feed = reset ($feeds);
  210. $url['params'] = array ('get' => 'f_' . $feed->id ());
  211. }
  212. if (Request::param ('ajax', 0) === 0) {
  213. Session::_param ('notification', $notif);
  214. Request::forward ($url, true);
  215. } else {
  216. // Une requête Ajax met un seul flux à jour.
  217. // Comme en principe plusieurs requêtes ont lieu,
  218. // on indique que "plusieurs flux ont été mis à jour".
  219. // Cela permet d'avoir une notification plus proche du
  220. // ressenti utilisateur
  221. $notif = array (
  222. 'type' => 'good',
  223. 'content' => Translate::t ('feeds_actualized')
  224. );
  225. Session::_param ('notification', $notif);
  226. // et on désactive le layout car ne sert à rien
  227. $this->view->_useLayout (false);
  228. }
  229. }
  230. public function massiveImportAction () {
  231. $entryDAO = new EntryDAO ();
  232. $feedDAO = new FeedDAO ();
  233. $categories = Request::param ('categories', array (), true);
  234. $feeds = Request::param ('feeds', array (), true);
  235. // on ajoute les catégories en masse dans une fonction à part
  236. $this->addCategories ($categories);
  237. // on calcule la date des articles les plus anciens qu'on accepte
  238. $nb_month_old = $this->view->conf->oldEntries ();
  239. $date_min = time () - (60 * 60 * 24 * 30 * $nb_month_old);
  240. // la variable $error permet de savoir si une erreur est survenue
  241. // Le but est de ne pas arrêter l'import même en cas d'erreur
  242. // L'utilisateur sera mis au courant s'il y a eu des erreurs, mais
  243. // ne connaîtra pas les détails. Ceux-ci seront toutefois logguées
  244. $error = false;
  245. $i = 0;
  246. foreach ($feeds as $feed) {
  247. try {
  248. $feed->load ();
  249. $values = array (
  250. 'id' => $feed->id (),
  251. 'url' => $feed->url (),
  252. 'category' => $feed->category (),
  253. 'name' => $feed->name (),
  254. 'website' => $feed->website (),
  255. 'description' => $feed->description (),
  256. 'lastUpdate' => 0,
  257. 'httpAuth' => $feed->httpAuth ()
  258. );
  259. // ajout du flux que s'il n'est pas déjà en BDD
  260. if (!$feedDAO->searchByUrl ($values['url'])) {
  261. if (!$feedDAO->addFeed ($values)) {
  262. $error = true;
  263. }
  264. }
  265. } catch (FeedException $e) {
  266. $error = true;
  267. Minz_Log::record ($e->getMessage (), Minz_Log::ERROR);
  268. }
  269. }
  270. if ($error) {
  271. $res = Translate::t ('feeds_imported_with_errors');
  272. } else {
  273. $res = Translate::t ('feeds_imported');
  274. }
  275. $notif = array (
  276. 'type' => 'good',
  277. 'content' => $res
  278. );
  279. Session::_param ('notification', $notif);
  280. Session::_param ('actualize_feeds', true);
  281. // et on redirige vers la page import/export
  282. Request::forward (array (
  283. 'c' => 'configure',
  284. 'a' => 'importExport'
  285. ), true);
  286. }
  287. public function deleteAction () {
  288. $type = Request::param ('type', 'feed');
  289. $id = Request::param ('id');
  290. $feedDAO = new FeedDAO ();
  291. if ($type == 'category') {
  292. if ($feedDAO->deleteFeedByCategory ($id)) {
  293. $notif = array (
  294. 'type' => 'good',
  295. 'content' => Translate::t ('category_emptied')
  296. );
  297. } else {
  298. $notif = array (
  299. 'type' => 'bad',
  300. 'content' => Translate::t ('error_occured')
  301. );
  302. }
  303. } else {
  304. if ($feedDAO->deleteFeed ($id)) {
  305. $notif = array (
  306. 'type' => 'good',
  307. 'content' => Translate::t ('feed_deleted')
  308. );
  309. } else {
  310. $notif = array (
  311. 'type' => 'bad',
  312. 'content' => Translate::t ('error_occured')
  313. );
  314. }
  315. }
  316. Session::_param ('notification', $notif);
  317. if ($type == 'category') {
  318. Request::forward (array ('c' => 'configure', 'a' => 'categorize'), true);
  319. } else {
  320. Request::forward (array ('c' => 'configure', 'a' => 'feed'), true);
  321. }
  322. }
  323. private function addCategories ($categories) {
  324. $catDAO = new CategoryDAO ();
  325. foreach ($categories as $cat) {
  326. if (!$catDAO->searchByName ($cat->name ())) {
  327. $values = array (
  328. 'id' => $cat->id (),
  329. 'name' => $cat->name (),
  330. 'color' => $cat->color ()
  331. );
  332. $catDAO->addCategory ($values);
  333. }
  334. }
  335. }
  336. }