Search.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * Contains a search from the search form.
  4. *
  5. * It allows to extract meaningful bits of the search and store them in a
  6. * convenient object
  7. */
  8. class FreshRSS_Search {
  9. // This contains the user input string
  10. private $raw_input;
  11. // The following properties are extracted from the raw input
  12. private $intitle;
  13. private $min_date;
  14. private $max_date;
  15. private $min_pubdate;
  16. private $max_pubdate;
  17. private $inurl;
  18. private $author;
  19. private $tags;
  20. private $search;
  21. public function __construct($input) {
  22. if (strcmp($input, '') == 0) {
  23. return;
  24. }
  25. $this->raw_input = $input;
  26. $input = $this->parseIntitleSearch($input);
  27. $input = $this->parseAuthorSearch($input);
  28. $input = $this->parseInurlSearch($input);
  29. $input = $this->parsePubdateSearch($input);
  30. $input = $this->parseDateSearch($input);
  31. $input = $this->parseTagsSeach($input);
  32. $this->search = $this->cleanSearch($input);
  33. }
  34. public function getRawInput() {
  35. return $this->raw_input;
  36. }
  37. public function getIntitle() {
  38. return $this->intitle;
  39. }
  40. public function getMinDate() {
  41. return $this->min_date;
  42. }
  43. public function getMaxDate() {
  44. return $this->max_date;
  45. }
  46. public function getMinPubdate() {
  47. return $this->min_pubdate;
  48. }
  49. public function getMaxPubdate() {
  50. return $this->max_pubdate;
  51. }
  52. public function getInurl() {
  53. return $this->inurl;
  54. }
  55. public function getAuthor() {
  56. return $this->author;
  57. }
  58. public function getTags() {
  59. return $this->tags;
  60. }
  61. public function getSearch() {
  62. return $this->search;
  63. }
  64. /**
  65. * Parse the search string to find intitle keyword and the search related
  66. * to it.
  67. * The search is the first word following the keyword.
  68. *
  69. * @param string $input
  70. * @return string
  71. */
  72. private function parseIntitleSearch($input) {
  73. if (preg_match('/intitle:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  74. $this->intitle = $matches['search'];
  75. return str_replace($matches[0], '', $input);
  76. }
  77. if (preg_match('/intitle:(?P<search>\w*)/', $input, $matches)) {
  78. $this->intitle = $matches['search'];
  79. return str_replace($matches[0], '', $input);
  80. }
  81. return $input;
  82. }
  83. /**
  84. * Parse the search string to find author keyword and the search related
  85. * to it.
  86. * The search is the first word following the keyword except when using
  87. * a delimiter. Supported delimiters are single quote (') and double
  88. * quotes (").
  89. *
  90. * @param string $input
  91. * @return string
  92. */
  93. private function parseAuthorSearch($input) {
  94. if (preg_match('/author:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  95. $this->author = $matches['search'];
  96. return str_replace($matches[0], '', $input);
  97. }
  98. if (preg_match('/author:(?P<search>\w*)/', $input, $matches)) {
  99. $this->author = $matches['search'];
  100. return str_replace($matches[0], '', $input);
  101. }
  102. return $input;
  103. }
  104. /**
  105. * Parse the search string to find inurl keyword and the search related
  106. * to it.
  107. * The search is the first word following the keyword except.
  108. *
  109. * @param string $input
  110. * @return string
  111. */
  112. private function parseInurlSearch($input) {
  113. if (preg_match('/inurl:(?P<search>[^\s]*)/', $input, $matches)) {
  114. $this->inurl = $matches['search'];
  115. return str_replace($matches[0], '', $input);
  116. }
  117. return $input;
  118. }
  119. /**
  120. * Parse the search string to find date keyword and the search related
  121. * to it.
  122. * The search is the first word following the keyword.
  123. *
  124. * @param string $input
  125. * @return string
  126. */
  127. private function parseDateSearch($input) {
  128. if (preg_match('/date:(?P<search>[^\s]*)/', $input, $matches)) {
  129. list($this->min_date, $this->max_date) = parseDateInterval($matches['search']);
  130. return str_replace($matches[0], '', $input);
  131. }
  132. return $input;
  133. }
  134. /**
  135. * Parse the search string to find pubdate keyword and the search related
  136. * to it.
  137. * The search is the first word following the keyword.
  138. *
  139. * @param string $input
  140. * @return string
  141. */
  142. private function parsePubdateSearch($input) {
  143. if (preg_match('/pubdate:(?P<search>[^\s]*)/', $input, $matches)) {
  144. list($this->min_pubdate, $this->max_pubdate) = parseDateInterval($matches['search']);
  145. return str_replace($matches[0], '', $input);
  146. }
  147. return $input;
  148. }
  149. /**
  150. * Parse the search string to find tags keyword (# followed by a word)
  151. * and the search related to it.
  152. * The search is the first word following the #.
  153. *
  154. * @param string $input
  155. * @return string
  156. */
  157. private function parseTagsSeach($input) {
  158. if (preg_match_all('/#(?P<search>[^\s]+)/', $input, $matches)) {
  159. $this->tags = $matches['search'];
  160. return str_replace($matches[0], '', $input);
  161. }
  162. return $input;
  163. }
  164. /**
  165. * Remove all unnecessary spaces in the search
  166. *
  167. * @param string $input
  168. * @return string
  169. */
  170. private function cleanSearch($input) {
  171. $input = preg_replace('/\s+/', ' ', $input);
  172. return trim($input);
  173. }
  174. }