BooleanSearch.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. $splits = preg_split('/\b(OR)\b/i', $input, -1, PREG_SPLIT_DELIM_CAPTURE);
  16. $segment = '';
  17. $ns = count($splits);
  18. for ($i = 0; $i < $ns; $i++) {
  19. $segment = $segment . $splits[$i];
  20. if (trim($segment) == '' || strcasecmp($segment, 'OR') === 0) {
  21. $segment = '';
  22. } else {
  23. $quotes = substr_count($segment, '"') + substr_count($segment, '&quot;');
  24. if ($quotes % 2 === 0) {
  25. $segment = trim($segment);
  26. if ($segment != '') {
  27. $this->searches[] = new FreshRSS_Search($segment);
  28. }
  29. $segment = '';
  30. }
  31. }
  32. }
  33. $segment = trim($segment);
  34. if ($segment != '') {
  35. $this->searches[] = new FreshRSS_Search($segment);
  36. }
  37. }
  38. public function searches() {
  39. return $this->searches;
  40. }
  41. public function add($search) {
  42. if ($search instanceof FreshRSS_Search) {
  43. $this->searches[] = $search;
  44. return $search;
  45. }
  46. return null;
  47. }
  48. public function __toString() {
  49. return $this->getRawInput();
  50. }
  51. public function getRawInput() {
  52. return $this->raw_input;
  53. }
  54. }