BooleanSearch.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * Contains Boolean search from the search form.
  4. */
  5. class FreshRSS_BooleanSearch {
  6. private $raw_input = '';
  7. private $searches = array();
  8. public function __construct($input) {
  9. $input = trim($input);
  10. if ($input == '') {
  11. return;
  12. }
  13. $this->raw_input = $input;
  14. $input = preg_replace('/:&quot;(.*?)&quot;/', ':"\1"', $input);
  15. $input = preg_replace('/(?<=[\s!-]|^)&quot;(.*?)&quot;/', '"\1"', $input);
  16. $splits = preg_split('/\b(OR)\b/i', $input, -1, PREG_SPLIT_DELIM_CAPTURE);
  17. $segment = '';
  18. $ns = count($splits);
  19. for ($i = 0; $i < $ns; $i++) {
  20. $segment = $segment . $splits[$i];
  21. if (trim($segment) == '' || strcasecmp($segment, 'OR') === 0) {
  22. $segment = '';
  23. } else {
  24. $quotes = substr_count($segment, '"') + substr_count($segment, '&quot;');
  25. if ($quotes % 2 === 0) {
  26. $segment = trim($segment);
  27. if ($segment != '') {
  28. $this->searches[] = new FreshRSS_Search($segment);
  29. }
  30. $segment = '';
  31. }
  32. }
  33. }
  34. $segment = trim($segment);
  35. if ($segment != '') {
  36. $this->searches[] = new FreshRSS_Search($segment);
  37. }
  38. }
  39. public function searches() {
  40. return $this->searches;
  41. }
  42. public function add($search) {
  43. if ($search instanceof FreshRSS_Search) {
  44. $this->searches[] = $search;
  45. return $search;
  46. }
  47. return null;
  48. }
  49. public function __toString() {
  50. return $this->getRawInput();
  51. }
  52. public function getRawInput() {
  53. return $this->raw_input;
  54. }
  55. }