4
0

UserQuery.php 5.3 KB

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