UserQuery.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. /**
  3. * Contains the description of a user query
  4. *
  5. * It allows to extract the meaningful bits of the query to be manipulated in an
  6. * easy way.
  7. */
  8. class FreshRSS_UserQuery {
  9. private $deprecated = false;
  10. private $get;
  11. private $get_name;
  12. private $get_type;
  13. private $name;
  14. private $order;
  15. /** @var FreshRSS_BooleanSearch */
  16. private $search;
  17. private $state;
  18. private $url;
  19. /** @var FreshRSS_FeedDAO */
  20. private $feed_dao;
  21. /** @var FreshRSS_CategoryDAO */
  22. private $category_dao;
  23. /** @var FreshRSS_TagDAO */
  24. private $tag_dao;
  25. /**
  26. * @param array<string,string> $query
  27. */
  28. public function __construct($query, FreshRSS_FeedDAO $feed_dao = null, FreshRSS_CategoryDAO $category_dao = null, FreshRSS_TagDAO $tag_dao = null) {
  29. $this->category_dao = $category_dao;
  30. $this->feed_dao = $feed_dao;
  31. $this->tag_dao = $tag_dao;
  32. if (isset($query['get'])) {
  33. $this->parseGet($query['get']);
  34. }
  35. if (isset($query['name'])) {
  36. $this->name = trim($query['name']);
  37. }
  38. if (isset($query['order'])) {
  39. $this->order = $query['order'];
  40. }
  41. if (empty($query['url'])) {
  42. if (!empty($query)) {
  43. unset($query['name']);
  44. $this->url = Minz_Url::display(['params' => $query]);
  45. }
  46. } else {
  47. $this->url = $query['url'];
  48. }
  49. if (!isset($query['search'])) {
  50. $query['search'] = '';
  51. }
  52. // linked too deeply with the search object, need to use dependency injection
  53. $this->search = new FreshRSS_BooleanSearch($query['search']);
  54. if (isset($query['state'])) {
  55. $this->state = $query['state'];
  56. }
  57. }
  58. /**
  59. * Convert the current object to an array.
  60. *
  61. * @return array<string,string>
  62. */
  63. public function toArray() {
  64. return array_filter(array(
  65. 'get' => $this->get,
  66. 'name' => $this->name,
  67. 'order' => $this->order,
  68. 'search' => $this->search->__toString(),
  69. 'state' => $this->state,
  70. 'url' => $this->url,
  71. ));
  72. }
  73. /**
  74. * Parse the get parameter in the query string to extract its name and
  75. * type
  76. *
  77. * @param string $get
  78. */
  79. private function parseGet($get) {
  80. $this->get = $get;
  81. if (preg_match('/(?P<type>[acfst])(_(?P<id>\d+))?/', $get, $matches)) {
  82. $id = intval($matches['id'] ?? '0');
  83. switch ($matches['type']) {
  84. case 'a':
  85. $this->parseAll();
  86. break;
  87. case 'c':
  88. $this->parseCategory($id);
  89. break;
  90. case 'f':
  91. $this->parseFeed($id);
  92. break;
  93. case 's':
  94. $this->parseFavorite();
  95. break;
  96. case 't':
  97. $this->parseTag($id);
  98. break;
  99. }
  100. }
  101. }
  102. /**
  103. * Parse the query string when it is an "all" query
  104. */
  105. private function parseAll() {
  106. $this->get_name = 'all';
  107. $this->get_type = 'all';
  108. }
  109. /**
  110. * Parse the query string when it is a "category" query
  111. *
  112. * @throws FreshRSS_DAO_Exception
  113. */
  114. private function parseCategory(int $id) {
  115. if ($this->category_dao === null) {
  116. throw new FreshRSS_DAO_Exception('Category DAO is not loaded in UserQuery');
  117. }
  118. $category = $this->category_dao->searchById($id);
  119. if ($category) {
  120. $this->get_name = $category->name();
  121. } else {
  122. $this->deprecated = true;
  123. }
  124. $this->get_type = 'category';
  125. }
  126. /**
  127. * Parse the query string when it is a "feed" query
  128. *
  129. * @throws FreshRSS_DAO_Exception
  130. */
  131. private function parseFeed(int $id) {
  132. if ($this->feed_dao === null) {
  133. throw new FreshRSS_DAO_Exception('Feed DAO is not loaded in UserQuery');
  134. }
  135. $feed = $this->feed_dao->searchById($id);
  136. if ($feed) {
  137. $this->get_name = $feed->name();
  138. } else {
  139. $this->deprecated = true;
  140. }
  141. $this->get_type = 'feed';
  142. }
  143. /**
  144. * Parse the query string when it is a "tag" query
  145. *
  146. * @throws FreshRSS_DAO_Exception
  147. */
  148. private function parseTag(int $id) {
  149. if ($this->tag_dao == null) {
  150. throw new FreshRSS_DAO_Exception('Tag DAO is not loaded in UserQuery');
  151. }
  152. $tag = $this->tag_dao->searchById($id);
  153. if ($tag) {
  154. $this->get_name = $tag->name();
  155. } else {
  156. $this->deprecated = true;
  157. }
  158. $this->get_type = 'tag';
  159. }
  160. /**
  161. * Parse the query string when it is a "favorite" query
  162. */
  163. private function parseFavorite() {
  164. $this->get_name = 'favorite';
  165. $this->get_type = 'favorite';
  166. }
  167. /**
  168. * Check if the current user query is deprecated.
  169. * It is deprecated if the category or the feed used in the query are
  170. * not existing.
  171. *
  172. * @return boolean
  173. */
  174. public function isDeprecated() {
  175. return $this->deprecated;
  176. }
  177. /**
  178. * Check if the user query has parameters.
  179. * If the type is 'all', it is considered equal to no parameters
  180. *
  181. * @return boolean
  182. */
  183. public function hasParameters() {
  184. if ($this->get_type === 'all') {
  185. return false;
  186. }
  187. if ($this->hasSearch()) {
  188. return true;
  189. }
  190. if ($this->state) {
  191. return true;
  192. }
  193. if ($this->order) {
  194. return true;
  195. }
  196. if ($this->get) {
  197. return true;
  198. }
  199. return false;
  200. }
  201. /**
  202. * Check if there is a search in the search object
  203. *
  204. * @return boolean
  205. */
  206. public function hasSearch() {
  207. return $this->search->getRawInput() != "";
  208. }
  209. public function getGet() {
  210. return $this->get;
  211. }
  212. public function getGetName() {
  213. return $this->get_name;
  214. }
  215. public function getGetType() {
  216. return $this->get_type;
  217. }
  218. public function getName() {
  219. return $this->name;
  220. }
  221. public function getOrder() {
  222. return $this->order;
  223. }
  224. public function getSearch() {
  225. return $this->search;
  226. }
  227. public function getState() {
  228. return $this->state;
  229. }
  230. public function getUrl() {
  231. return $this->url;
  232. }
  233. }