Search.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. * @author alexis
  9. */
  10. class FreshRSS_Search {
  11. // This contains the user input string
  12. private $raw_input;
  13. // The following properties are extracted from the raw input
  14. private $intitle;
  15. private $min_date;
  16. private $max_date;
  17. private $min_pubdate;
  18. private $max_pubdate;
  19. private $inurl;
  20. private $author;
  21. private $tags;
  22. private $search;
  23. public function __construct($input) {
  24. if (strcmp($input, '') == 0) {
  25. return;
  26. }
  27. $this->raw_input = $input;
  28. $input = $this->parseIntitleSearch($input);
  29. $input = $this->parseAuthorSearch($input);
  30. $input = $this->parseInurlSearch($input);
  31. $input = $this->parsePubdateSearch($input);
  32. $input = $this->parseDateSearch($input);
  33. $input = $this->parseTagsSeach($input);
  34. $this->search = $input;
  35. }
  36. public function getRawInput() {
  37. return $this->raw_input;
  38. }
  39. public function getIntitle() {
  40. return $this->intitle;
  41. }
  42. public function getMinDate() {
  43. return $this->min_date;
  44. }
  45. public function getMaxDate() {
  46. return $this->max_date;
  47. }
  48. public function getMinPubdate() {
  49. return $this->min_pubdate;
  50. }
  51. public function getMaxPubdate() {
  52. return $this->max_pubdate;
  53. }
  54. public function getInurl() {
  55. return $this->inurl;
  56. }
  57. public function getAuthor() {
  58. return $this->author;
  59. }
  60. public function getTags() {
  61. return $this->tags;
  62. }
  63. public function getSearch() {
  64. return $this->search;
  65. }
  66. /**
  67. * Parse the search string to find intitle keyword and the search related
  68. * to it.
  69. * The search is the first word following the keyword.
  70. *
  71. * @param string $input
  72. * @return string
  73. */
  74. private function parseIntitleSearch($input) {
  75. if (preg_match('/intitle:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  76. $this->intitle = $matches['search'];
  77. $input = str_replace($matches[0], '', $input);
  78. } else if (preg_match('/intitle:(?P<search>\w*)/', $input, $matches)) {
  79. $this->intitle = $matches['search'];
  80. $input = str_replace($matches[0], '', $input);
  81. }
  82. return $this->cleanSearch($input);
  83. }
  84. /**
  85. * Parse the search string to find author keyword and the search related
  86. * to it.
  87. * The search is the first word following the keyword except when using
  88. * a delimiter. Supported delimiters are single quote (') and double
  89. * quotes (").
  90. *
  91. * @param string $input
  92. * @return string
  93. */
  94. private function parseAuthorSearch($input) {
  95. if (preg_match('/author:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  96. $this->author = $matches['search'];
  97. $input = str_replace($matches[0], '', $input);
  98. } else if (preg_match('/author:(?P<search>\w*)/', $input, $matches)) {
  99. $this->author = $matches['search'];
  100. $input = str_replace($matches[0], '', $input);
  101. }
  102. return $this->cleanSearch($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. $input = str_replace($matches[0], '', $input);
  116. }
  117. return $this->cleanSearch($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. $input = str_replace($matches[0], '', $input);
  131. }
  132. return $this->cleanSearch($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. $input = str_replace($matches[0], '', $input);
  146. }
  147. return $this->cleanSearch($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. $input = str_replace($matches[0], '', $input);
  161. }
  162. return $this->cleanSearch($input);
  163. }
  164. private function cleanSearch($input) {
  165. $input = preg_replace('/\s+/', ' ', $input);
  166. return trim($input);
  167. }
  168. }