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. private $search;
  16. private $state;
  17. private $url;
  18. private $feed_dao;
  19. private $category_dao;
  20. private $tag_dao;
  21. /**
  22. * @param array<string,string> $query
  23. * @param FreshRSS_Searchable $feed_dao
  24. * @param FreshRSS_Searchable $category_dao
  25. */
  26. public function __construct($query, FreshRSS_Searchable $feed_dao = null, FreshRSS_Searchable $category_dao = null, FreshRSS_Searchable $tag_dao = null) {
  27. $this->category_dao = $category_dao;
  28. $this->feed_dao = $feed_dao;
  29. $this->tag_dao = $tag_dao;
  30. if (isset($query['get'])) {
  31. $this->parseGet($query['get']);
  32. }
  33. if (isset($query['name'])) {
  34. $this->name = $query['name'];
  35. }
  36. if (isset($query['order'])) {
  37. $this->order = $query['order'];
  38. }
  39. if (empty($query['url'])) {
  40. if (!empty($query)) {
  41. unset($query['name']);
  42. $this->url = Minz_Url::display(array('params' => $query));
  43. }
  44. } else {
  45. $this->url = $query['url'];
  46. }
  47. if (!isset($query['search'])) {
  48. $query['search'] = '';
  49. }
  50. // linked too deeply with the search object, need to use dependency injection
  51. $this->search = new FreshRSS_BooleanSearch($query['search']);
  52. if (isset($query['state'])) {
  53. $this->state = $query['state'];
  54. }
  55. }
  56. /**
  57. * Convert the current object to an array.
  58. *
  59. * @return array<string,string>
  60. */
  61. public function toArray() {
  62. return array_filter(array(
  63. 'get' => $this->get,
  64. 'name' => $this->name,
  65. 'order' => $this->order,
  66. 'search' => $this->search->__toString(),
  67. 'state' => $this->state,
  68. 'url' => $this->url,
  69. ));
  70. }
  71. /**
  72. * Parse the get parameter in the query string to extract its name and
  73. * type
  74. *
  75. * @param string $get
  76. */
  77. private function parseGet($get) {
  78. $this->get = $get;
  79. if (preg_match('/(?P<type>[acfst])(_(?P<id>\d+))?/', $get, $matches)) {
  80. switch ($matches['type']) {
  81. case 'a':
  82. $this->parseAll();
  83. break;
  84. case 'c':
  85. $this->parseCategory($matches['id']);
  86. break;
  87. case 'f':
  88. $this->parseFeed($matches['id']);
  89. break;
  90. case 's':
  91. $this->parseFavorite();
  92. break;
  93. case 't':
  94. $this->parseTag($matches['id']);
  95. break;
  96. }
  97. }
  98. }
  99. /**
  100. * Parse the query string when it is an "all" query
  101. */
  102. private function parseAll() {
  103. $this->get_name = 'all';
  104. $this->get_type = 'all';
  105. }
  106. /**
  107. * Parse the query string when it is a "category" query
  108. *
  109. * @param integer $id
  110. * @throws FreshRSS_DAO_Exception
  111. */
  112. private function parseCategory($id) {
  113. if (is_null($this->category_dao)) {
  114. throw new FreshRSS_DAO_Exception('Category DAO is not loaded in UserQuery');
  115. }
  116. $category = $this->category_dao->searchById($id);
  117. if ($category) {
  118. $this->get_name = $category->name();
  119. } else {
  120. $this->deprecated = true;
  121. }
  122. $this->get_type = 'category';
  123. }
  124. /**
  125. * Parse the query string when it is a "feed" query
  126. *
  127. * @param integer $id
  128. * @throws FreshRSS_DAO_Exception
  129. */
  130. private function parseFeed($id) {
  131. if (is_null($this->feed_dao)) {
  132. throw new FreshRSS_DAO_Exception('Feed DAO is not loaded in UserQuery');
  133. }
  134. $feed = $this->feed_dao->searchById($id);
  135. if ($feed) {
  136. $this->get_name = $feed->name();
  137. } else {
  138. $this->deprecated = true;
  139. }
  140. $this->get_type = 'feed';
  141. }
  142. /**
  143. * Parse the query string when it is a "tag" query
  144. *
  145. * @param integer $id
  146. * @throws FreshRSS_DAO_Exception
  147. */
  148. private function parseTag($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. }