Search.php 4.8 KB

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