feedController.php 11 KB

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