entryController.php 4.8 KB

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