entryController.php 6.0 KB

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