feedController.php 12 KB

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