Search.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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->parseSearch($input);
  34. }
  35. public function __toString() {
  36. return $this->getRawInput();
  37. }
  38. public function getRawInput() {
  39. return $this->raw_input;
  40. }
  41. public function getIntitle() {
  42. return $this->intitle;
  43. }
  44. public function getMinDate() {
  45. return $this->min_date;
  46. }
  47. public function getMaxDate() {
  48. return $this->max_date;
  49. }
  50. public function getMinPubdate() {
  51. return $this->min_pubdate;
  52. }
  53. public function getMaxPubdate() {
  54. return $this->max_pubdate;
  55. }
  56. public function getInurl() {
  57. return $this->inurl;
  58. }
  59. public function getAuthor() {
  60. return $this->author;
  61. }
  62. public function getTags() {
  63. return $this->tags;
  64. }
  65. public function getSearch() {
  66. return $this->search;
  67. }
  68. /**
  69. * Parse the search string to find intitle keyword and the search related
  70. * to it.
  71. * The search is the first word following the keyword.
  72. *
  73. * @param string $input
  74. * @return string
  75. */
  76. private function parseIntitleSearch($input) {
  77. if (preg_match('/intitle:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  78. $this->intitle = $matches['search'];
  79. return str_replace($matches[0], '', $input);
  80. }
  81. if (preg_match('/intitle:(?P<search>\w*)/', $input, $matches)) {
  82. $this->intitle = $matches['search'];
  83. return str_replace($matches[0], '', $input);
  84. }
  85. return $input;
  86. }
  87. /**
  88. * Parse the search string to find author keyword and the search related
  89. * to it.
  90. * The search is the first word following the keyword except when using
  91. * a delimiter. Supported delimiters are single quote (') and double
  92. * quotes (").
  93. *
  94. * @param string $input
  95. * @return string
  96. */
  97. private function parseAuthorSearch($input) {
  98. if (preg_match('/author:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  99. $this->author = $matches['search'];
  100. return str_replace($matches[0], '', $input);
  101. }
  102. if (preg_match('/author:(?P<search>\w*)/', $input, $matches)) {
  103. $this->author = $matches['search'];
  104. return str_replace($matches[0], '', $input);
  105. }
  106. return $input;
  107. }
  108. /**
  109. * Parse the search string to find inurl keyword and the search related
  110. * to it.
  111. * The search is the first word following the keyword except.
  112. *
  113. * @param string $input
  114. * @return string
  115. */
  116. private function parseInurlSearch($input) {
  117. if (preg_match('/inurl:(?P<search>[^\s]*)/', $input, $matches)) {
  118. $this->inurl = $matches['search'];
  119. return str_replace($matches[0], '', $input);
  120. }
  121. return $input;
  122. }
  123. /**
  124. * Parse the search string to find date keyword and the search related
  125. * to it.
  126. * The search is the first word following the keyword.
  127. *
  128. * @param string $input
  129. * @return string
  130. */
  131. private function parseDateSearch($input) {
  132. if (preg_match('/date:(?P<search>[^\s]*)/', $input, $matches)) {
  133. list($this->min_date, $this->max_date) = parseDateInterval($matches['search']);
  134. return str_replace($matches[0], '', $input);
  135. }
  136. return $input;
  137. }
  138. /**
  139. * Parse the search string to find pubdate keyword and the search related
  140. * to it.
  141. * The search is the first word following the keyword.
  142. *
  143. * @param string $input
  144. * @return string
  145. */
  146. private function parsePubdateSearch($input) {
  147. if (preg_match('/pubdate:(?P<search>[^\s]*)/', $input, $matches)) {
  148. list($this->min_pubdate, $this->max_pubdate) = parseDateInterval($matches['search']);
  149. return str_replace($matches[0], '', $input);
  150. }
  151. return $input;
  152. }
  153. /**
  154. * Parse the search string to find tags keyword (# followed by a word)
  155. * and the search related to it.
  156. * The search is the first word following the #.
  157. *
  158. * @param string $input
  159. * @return string
  160. */
  161. private function parseTagsSeach($input) {
  162. if (preg_match_all('/#(?P<search>[^\s]+)/', $input, $matches)) {
  163. $this->tags = $matches['search'];
  164. return str_replace($matches[0], '', $input);
  165. }
  166. return $input;
  167. }
  168. /**
  169. * Parse the search string to find search values.
  170. * Every word is a distinct search value, except when using a delimiter.
  171. * Supported delimiters are single quote (') and double quotes (").
  172. *
  173. * @param string $input
  174. * @return string
  175. */
  176. private function parseSearch($input) {
  177. $input = $this->cleanSearch($input);
  178. if (strcmp($input, '') == 0) {
  179. return;
  180. }
  181. if (preg_match_all('/(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  182. $this->search = $matches['search'];
  183. $input = str_replace($matches[0], '', $input);
  184. }
  185. $input = $this->cleanSearch($input);
  186. if (strcmp($input, '') == 0) {
  187. return;
  188. }
  189. if (is_array($this->search)) {
  190. $this->search = array_merge($this->search, explode(' ', $input));
  191. } else {
  192. $this->search = explode(' ', $input);
  193. }
  194. }
  195. /**
  196. * Remove all unnecessary spaces in the search
  197. *
  198. * @param string $input
  199. * @return string
  200. */
  201. private function cleanSearch($input) {
  202. $input = preg_replace('/\s+/', ' ', $input);
  203. return trim($input);
  204. }
  205. }