entryController.php 5.5 KB

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