UserQuery.php 5.1 KB

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