Search.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 = $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. $input = str_replace($matches[0], '', $input);
  76. } else if (preg_match('/intitle:(?P<search>\w*)/', $input, $matches)) {
  77. $this->intitle = $matches['search'];
  78. $input = str_replace($matches[0], '', $input);
  79. }
  80. return $this->cleanSearch($input);
  81. }
  82. /**
  83. * Parse the search string to find author keyword and the search related
  84. * to it.
  85. * The search is the first word following the keyword except when using
  86. * a delimiter. Supported delimiters are single quote (') and double
  87. * quotes (").
  88. *
  89. * @param string $input
  90. * @return string
  91. */
  92. private function parseAuthorSearch($input) {
  93. if (preg_match('/author:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  94. $this->author = $matches['search'];
  95. $input = str_replace($matches[0], '', $input);
  96. } else if (preg_match('/author:(?P<search>\w*)/', $input, $matches)) {
  97. $this->author = $matches['search'];
  98. $input = str_replace($matches[0], '', $input);
  99. }
  100. return $this->cleanSearch($input);
  101. }
  102. /**
  103. * Parse the search string to find inurl keyword and the search related
  104. * to it.
  105. * The search is the first word following the keyword except.
  106. *
  107. * @param string $input
  108. * @return string
  109. */
  110. private function parseInurlSearch($input) {
  111. if (preg_match('/inurl:(?P<search>[^\s]*)/', $input, $matches)) {
  112. $this->inurl = $matches['search'];
  113. $input = str_replace($matches[0], '', $input);
  114. }
  115. return $this->cleanSearch($input);
  116. }
  117. /**
  118. * Parse the search string to find date keyword and the search related
  119. * to it.
  120. * The search is the first word following the keyword.
  121. *
  122. * @param string $input
  123. * @return string
  124. */
  125. private function parseDateSearch($input) {
  126. if (preg_match('/date:(?P<search>[^\s]*)/', $input, $matches)) {
  127. list($this->min_date, $this->max_date) = parseDateInterval($matches['search']);
  128. $input = str_replace($matches[0], '', $input);
  129. }
  130. return $this->cleanSearch($input);
  131. }
  132. /**
  133. * Parse the search string to find pubdate keyword and the search related
  134. * to it.
  135. * The search is the first word following the keyword.
  136. *
  137. * @param string $input
  138. * @return string
  139. */
  140. private function parsePubdateSearch($input) {
  141. if (preg_match('/pubdate:(?P<search>[^\s]*)/', $input, $matches)) {
  142. list($this->min_pubdate, $this->max_pubdate) = parseDateInterval($matches['search']);
  143. $input = str_replace($matches[0], '', $input);
  144. }
  145. return $this->cleanSearch($input);
  146. }
  147. /**
  148. * Parse the search string to find tags keyword (# followed by a word)
  149. * and the search related to it.
  150. * The search is the first word following the #.
  151. *
  152. * @param string $input
  153. * @return string
  154. */
  155. private function parseTagsSeach($input) {
  156. if (preg_match_all('/#(?P<search>[^\s]+)/', $input, $matches)) {
  157. $this->tags = $matches['search'];
  158. $input = str_replace($matches[0], '', $input);
  159. }
  160. return $this->cleanSearch($input);
  161. }
  162. private function cleanSearch($input) {
  163. $input = preg_replace('/\s+/', ' ', $input);
  164. return trim($input);
  165. }
  166. }