entryController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. * Controller to handle every entry actions.
  4. */
  5. class FreshRSS_entry_Controller extends Minz_ActionController {
  6. /**
  7. * This action is called before every other action in that class. It is
  8. * the common boiler plate for every action. It is triggered by the
  9. * underlying framework.
  10. */
  11. public function firstAction() {
  12. if (!$this->view->loginOk) {
  13. Minz_Error::error(
  14. 403,
  15. array('error' => array(_t('access_denied')))
  16. );
  17. }
  18. // Keep parameter information (output) to do a correct redirection at
  19. // the end.
  20. $this->params = array();
  21. $output = Minz_Request::param('output', '');
  22. if ($output != '' && $this->view->conf->view_mode !== $output) {
  23. $this->params['output'] = $output;
  24. }
  25. // If ajax request, we do not print layout
  26. $this->ajax = Minz_Request::param('ajax');
  27. if ($this->ajax) {
  28. $this->view->_useLayout(false);
  29. Minz_Request::_param('ajax');
  30. }
  31. }
  32. /**
  33. * Mark one or several entries as read (or not!).
  34. *
  35. * If request concerns several entries, it MUST be a POST request.
  36. * If request concerns several entries, only mark them as read is available.
  37. *
  38. * Parameters are:
  39. * - id (default: false)
  40. * - get (default: false) /(c_\d+|f_\d+|s|a)/
  41. * - nextGet (default: $get)
  42. * - idMax (default: 0)
  43. * - is_read (default: true)
  44. *
  45. * @todo nextGet system should not be present here... or should be?
  46. */
  47. public function readAction() {
  48. $id = Minz_Request::param('id');
  49. $get = Minz_Request::param('get');
  50. $next_get = Minz_Request::param('nextGet', $get);
  51. $id_max = Minz_Request::param('idMax', 0);
  52. $entryDAO = FreshRSS_Factory::createEntryDao();
  53. if ($id === false) {
  54. // id is false? It MUST be a POST request!
  55. if (!Minz_Request::isPost()) {
  56. return;
  57. }
  58. if (!$get) {
  59. // No get? Mark all entries as read (from $id_max)
  60. $entryDAO->markReadEntries($id_max);
  61. } else {
  62. $type_get = $get[0];
  63. $get = substr($get, 2);
  64. switch($type_get) {
  65. case 'c':
  66. $entryDAO->markReadCat($get, $id_max);
  67. break;
  68. case 'f':
  69. $entryDAO->markReadFeed($get, $id_max);
  70. break;
  71. case 's':
  72. $entryDAO->markReadEntries($id_max, true);
  73. break;
  74. case 'a':
  75. $entryDAO->markReadEntries($id_max);
  76. break;
  77. }
  78. if ($next_get !== 'a') {
  79. // Redirect to the correct page (category, feed or starred)
  80. // Not "a" because it is the default value if nothing is
  81. // given.
  82. $this->params['get'] = $next_get;
  83. }
  84. }
  85. } else {
  86. $is_read = (bool)(Minz_Request::param('is_read', true));
  87. $entryDAO->markRead($id, $is_read);
  88. }
  89. if (!$this->ajax) {
  90. Minz_Request::good(_t('feeds_marked_read'), array(
  91. 'c' => 'index',
  92. 'a' => 'index',
  93. 'params' => $this->params,
  94. ), true);
  95. }
  96. }
  97. /**
  98. * This action marks an entry as favourite (bookmark) or not.
  99. *
  100. * Parameter is:
  101. * - id (default: false)
  102. * - is_favorite (default: true)
  103. * If id is false, nothing happened.
  104. */
  105. public function bookmarkAction() {
  106. $id = Minz_Request::param('id');
  107. $is_favourite = (bool)Minz_Request::param('is_favorite', true);
  108. if ($id !== false) {
  109. $entryDAO = FreshRSS_Factory::createEntryDao();
  110. $entryDAO->markFavorite($id, $is_favourite);
  111. }
  112. if (!$this->ajax) {
  113. Minz_Request::forward(array(
  114. 'c' => 'index',
  115. 'a' => 'index',
  116. 'params' => $this->params,
  117. ), true);
  118. }
  119. }
  120. /**
  121. * This action optimizes database to reduce its size.
  122. *
  123. * This action shouldbe reached by a POST request.
  124. *
  125. * @todo move this action in configure controller.
  126. * @todo call this action through web-cron when available
  127. */
  128. public function optimizeAction() {
  129. $url_redirect = array(
  130. 'c' => 'configure',
  131. 'a' => 'archiving',
  132. );
  133. if (!Minz_Request::isPost()) {
  134. Minz_Request::forward($url_redirect, true);
  135. }
  136. @set_time_limit(300);
  137. $entryDAO = FreshRSS_Factory::createEntryDao();
  138. $entryDAO->optimizeTable();
  139. $feedDAO = FreshRSS_Factory::createFeedDao();
  140. $feedDAO->updateCachedValues();
  141. invalidateHttpCache();
  142. Minz_Request::good(_t('optimization_complete'), $url_redirect);
  143. }
  144. /**
  145. * This action purges old entries from feeds.
  146. *
  147. * @todo should be a POST request
  148. * @todo should be in feedController
  149. */
  150. public function purgeAction() {
  151. @set_time_limit(300);
  152. $nb_month_old = max($this->view->conf->old_entries, 1);
  153. $date_min = time() - (3600 * 24 * 30 * $nb_month_old);
  154. $feedDAO = FreshRSS_Factory::createFeedDao();
  155. $feeds = $feedDAO->listFeeds();
  156. $nb_total = 0;
  157. invalidateHttpCache();
  158. foreach ($feeds as $feed) {
  159. $feed_history = $feed->keepHistory();
  160. if ($feed_history == -2) {
  161. // TODO: -2 must be a constant!
  162. // -2 means we take the default value from configuration
  163. $feed_history = $this->view->conf->keep_history_default;
  164. }
  165. if ($feed_history >= 0) {
  166. $nb = $feedDAO->cleanOldEntries($feed->id(), $date_min, $feed_history);
  167. if ($nb > 0) {
  168. $nb_total += $nb;
  169. Minz_Log::debug($nb . ' old entries cleaned in feed [' . $feed->url() . ']');
  170. }
  171. }
  172. }
  173. $feedDAO->updateCachedValues();
  174. invalidateHttpCache();
  175. Minz_Request::good(_t('purge_completed', $nb_total), array(
  176. 'c' => 'configure',
  177. 'a' => 'archiving'
  178. ));
  179. }
  180. }