entryController.php 6.5 KB

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