4
0

entryController.php 4.8 KB

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