entryController.php 4.8 KB

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