entryController.php 5.8 KB

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