entryController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. /**
  3. * Controller to handle every entry actions.
  4. */
  5. class FreshRSS_entry_Controller extends FreshRSS_ActionController {
  6. /**
  7. * JavaScript request or not.
  8. * @var bool
  9. */
  10. private $ajax = false;
  11. /**
  12. * This action is called before every other action in that class. It is
  13. * the common boiler plate for every action. It is triggered by the
  14. * underlying framework.
  15. */
  16. public function firstAction() {
  17. if (!FreshRSS_Auth::hasAccess()) {
  18. Minz_Error::error(403);
  19. }
  20. // If ajax request, we do not print layout
  21. $this->ajax = Minz_Request::param('ajax');
  22. if ($this->ajax) {
  23. $this->view->_layout(false);
  24. Minz_Request::_param('ajax');
  25. }
  26. }
  27. /**
  28. * Mark one or several entries as read (or not!).
  29. *
  30. * If request concerns several entries, it MUST be a POST request.
  31. * If request concerns several entries, only mark them as read is available.
  32. *
  33. * Parameters are:
  34. * - id (default: false)
  35. * - get (default: false) /(c_\d+|f_\d+|s|a)/
  36. * - nextGet (default: $get)
  37. * - idMax (default: 0)
  38. * - is_read (default: true)
  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. $is_read = (bool)(Minz_Request::param('is_read', true));
  46. FreshRSS_Context::$search = new FreshRSS_BooleanSearch(Minz_Request::param('search', ''));
  47. FreshRSS_Context::$state = Minz_Request::param('state', 0);
  48. if (FreshRSS_Context::isStateEnabled(FreshRSS_Entry::STATE_FAVORITE)) {
  49. FreshRSS_Context::$state = FreshRSS_Entry::STATE_FAVORITE;
  50. } elseif (FreshRSS_Context::isStateEnabled(FreshRSS_Entry::STATE_NOT_FAVORITE)) {
  51. FreshRSS_Context::$state = FreshRSS_Entry::STATE_NOT_FAVORITE;
  52. } else {
  53. FreshRSS_Context::$state = 0;
  54. }
  55. $params = array();
  56. $this->view->tags = array();
  57. $entryDAO = FreshRSS_Factory::createEntryDao();
  58. if ($id === false) {
  59. // id is false? It MUST be a POST request!
  60. if (!Minz_Request::isPost()) {
  61. Minz_Request::bad(_t('feedback.access.not_found'), array('c' => 'index', 'a' => 'index'));
  62. return;
  63. }
  64. if (!$get) {
  65. // No get? Mark all entries as read (from $id_max)
  66. $entryDAO->markReadEntries($id_max, $is_read);
  67. } else {
  68. $type_get = $get[0];
  69. $get = substr($get, 2);
  70. switch($type_get) {
  71. case 'c':
  72. $entryDAO->markReadCat($get, $id_max, FreshRSS_Context::$search, FreshRSS_Context::$state, $is_read);
  73. break;
  74. case 'f':
  75. $entryDAO->markReadFeed($get, $id_max, FreshRSS_Context::$search, FreshRSS_Context::$state, $is_read);
  76. break;
  77. case 's':
  78. $entryDAO->markReadEntries($id_max, true, 0, FreshRSS_Context::$search, FreshRSS_Context::$state, $is_read);
  79. break;
  80. case 'a':
  81. $entryDAO->markReadEntries($id_max, false, 0, FreshRSS_Context::$search, FreshRSS_Context::$state, $is_read);
  82. break;
  83. case 't':
  84. $entryDAO->markReadTag($get, $id_max, FreshRSS_Context::$search, FreshRSS_Context::$state, $is_read);
  85. break;
  86. case 'T':
  87. $entryDAO->markReadTag('', $id_max, FreshRSS_Context::$search, FreshRSS_Context::$state, $is_read);
  88. break;
  89. }
  90. if ($next_get !== 'a') {
  91. // Redirect to the correct page (category, feed or starred)
  92. // Not "a" because it is the default value if nothing is given.
  93. $params['get'] = $next_get;
  94. }
  95. }
  96. } else {
  97. $ids = is_array($id) ? $id : array($id);
  98. $entryDAO->markRead($ids, $is_read);
  99. $tagDAO = FreshRSS_Factory::createTagDao();
  100. $tagsForEntries = $tagDAO->getTagsForEntries($ids);
  101. $tags = array();
  102. foreach ($tagsForEntries as $line) {
  103. $tags['t_' . $line['id_tag']][] = $line['id_entry'];
  104. }
  105. $this->view->tags = $tags;
  106. }
  107. if (!$this->ajax) {
  108. Minz_Request::good($is_read ? _t('feedback.sub.articles.marked_read') : _t('feedback.sub.articles.marked_unread'),
  109. array(
  110. 'c' => 'index',
  111. 'a' => 'index',
  112. 'params' => $params,
  113. ));
  114. }
  115. }
  116. /**
  117. * This action marks an entry as favourite (bookmark) or not.
  118. *
  119. * Parameter is:
  120. * - id (default: false)
  121. * - is_favorite (default: true)
  122. * If id is false, nothing happened.
  123. */
  124. public function bookmarkAction() {
  125. $id = Minz_Request::param('id');
  126. $is_favourite = (bool)Minz_Request::param('is_favorite', true);
  127. if ($id !== false) {
  128. $entryDAO = FreshRSS_Factory::createEntryDao();
  129. $entryDAO->markFavorite($id, $is_favourite);
  130. }
  131. if (!$this->ajax) {
  132. Minz_Request::forward(array(
  133. 'c' => 'index',
  134. 'a' => 'index',
  135. ), true);
  136. }
  137. }
  138. /**
  139. * This action optimizes database to reduce its size.
  140. *
  141. * This action should be reached by a POST request.
  142. *
  143. * @todo move this action in configure controller.
  144. * @todo call this action through web-cron when available
  145. */
  146. public function optimizeAction() {
  147. $url_redirect = array(
  148. 'c' => 'configure',
  149. 'a' => 'archiving',
  150. );
  151. if (!Minz_Request::isPost()) {
  152. Minz_Request::forward($url_redirect, true);
  153. }
  154. @set_time_limit(300);
  155. $databaseDAO = FreshRSS_Factory::createDatabaseDAO();
  156. $databaseDAO->optimize();
  157. $feedDAO = FreshRSS_Factory::createFeedDao();
  158. $feedDAO->updateCachedValues();
  159. invalidateHttpCache();
  160. Minz_Request::good(_t('feedback.admin.optimization_complete'), $url_redirect);
  161. }
  162. /**
  163. * This action purges old entries from feeds.
  164. *
  165. * @todo should be a POST request
  166. * @todo should be in feedController
  167. */
  168. public function purgeAction() {
  169. @set_time_limit(300);
  170. $feedDAO = FreshRSS_Factory::createFeedDao();
  171. $feeds = $feedDAO->listFeeds();
  172. $nb_total = 0;
  173. invalidateHttpCache();
  174. $feedDAO->beginTransaction();
  175. foreach ($feeds as $feed) {
  176. $nb_total += $feed->cleanOldEntries();
  177. }
  178. $feedDAO->updateCachedValues();
  179. $feedDAO->commit();
  180. $databaseDAO = FreshRSS_Factory::createDatabaseDAO();
  181. $databaseDAO->minorDbMaintenance();
  182. invalidateHttpCache();
  183. Minz_Request::good(_t('feedback.sub.purge_completed', $nb_total), array(
  184. 'c' => 'configure',
  185. 'a' => 'archiving'
  186. ));
  187. }
  188. }