entryController.php 4.7 KB

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