BooleanSearch.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. /**
  3. * Contains Boolean search from the search form.
  4. */
  5. class FreshRSS_BooleanSearch {
  6. /** @var string */
  7. private $raw_input = '';
  8. /** @var array<FreshRSS_BooleanSearch|FreshRSS_Search> */
  9. private $searches = array();
  10. /** @var string 'AND' or 'OR' */
  11. private $operator;
  12. public function __construct(string $input, int $level = 0, $operator = 'AND') {
  13. $this->operator = $operator;
  14. $input = trim($input);
  15. if ($input == '') {
  16. return;
  17. }
  18. $this->raw_input = $input;
  19. if ($level === 0) {
  20. $input = preg_replace('/:&quot;(.*?)&quot;/', ':"\1"', $input);
  21. $input = preg_replace('/(?<=[\s!-]|^)&quot;(.*?)&quot;/', '"\1"', $input);
  22. $input = $this->parseUserQueryNames($input);
  23. $input = $this->parseUserQueryIds($input);
  24. }
  25. // Either parse everything as a series of BooleanSearch's combined by implicit AND
  26. // or parse everything as a series of Search's combined by explicit OR
  27. $this->parseParentheses($input, $level) || $this->parseOrSegments($input);
  28. }
  29. /**
  30. * Parse the user queries (saved searches) by name and expand them in the input string.
  31. */
  32. private function parseUserQueryNames(string $input): string {
  33. $all_matches = [];
  34. if (preg_match_all('/\bsearch:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  35. $all_matches[] = $matches;
  36. }
  37. if (preg_match_all('/\bsearch:(?P<search>[^\s"]*)/', $input, $matches)) {
  38. $all_matches[] = $matches;
  39. }
  40. if (!empty($all_matches)) {
  41. /** @var array<string,FreshRSS_UserQuery> */
  42. $queries = [];
  43. foreach (FreshRSS_Context::$user_conf->queries as $raw_query) {
  44. $query = new FreshRSS_UserQuery($raw_query);
  45. $queries[$query->getName()] = $query;
  46. }
  47. $fromS = [];
  48. $toS = [];
  49. foreach ($all_matches as $matches) {
  50. for ($i = count($matches['search']) - 1; $i >= 0; $i--) {
  51. $name = trim($matches['search'][$i]);
  52. if (!empty($queries[$name])) {
  53. $fromS[] = $matches[0][$i];
  54. $toS[] = '(' . trim($queries[$name]->getSearch()) . ')';
  55. }
  56. }
  57. }
  58. $input = str_replace($fromS, $toS, $input);
  59. }
  60. return $input;
  61. }
  62. /**
  63. * Parse the user queries (saved searches) by ID and expand them in the input string.
  64. */
  65. private function parseUserQueryIds(string $input): string {
  66. $all_matches = [];
  67. if (preg_match_all('/\bS:(?P<search>\d+)/', $input, $matches)) {
  68. $all_matches[] = $matches;
  69. }
  70. if (!empty($all_matches)) {
  71. /** @var array<string,FreshRSS_UserQuery> */
  72. $queries = [];
  73. foreach (FreshRSS_Context::$user_conf->queries as $raw_query) {
  74. $query = new FreshRSS_UserQuery($raw_query);
  75. $queries[] = $query;
  76. }
  77. $fromS = [];
  78. $toS = [];
  79. foreach ($all_matches as $matches) {
  80. for ($i = count($matches['search']) - 1; $i >= 0; $i--) {
  81. // Index starting from 1
  82. $id = intval(trim($matches['search'][$i])) - 1;
  83. if (!empty($queries[$id])) {
  84. $fromS[] = $matches[0][$i];
  85. $toS[] = '(' . trim($queries[$id]->getSearch()) . ')';
  86. }
  87. }
  88. }
  89. $input = str_replace($fromS, $toS, $input);
  90. }
  91. return $input;
  92. }
  93. /** @return bool True if some parenthesis logic took over, false otherwise */
  94. private function parseParentheses(string $input, int $level): bool {
  95. $input = trim($input);
  96. $length = strlen($input);
  97. $i = 0;
  98. $before = '';
  99. $hasParenthesis = false;
  100. $nextOperator = 'AND';
  101. while ($i < $length) {
  102. $c = $input[$i];
  103. if ($c === '(') {
  104. $hasParenthesis = true;
  105. $before = trim($before);
  106. if (preg_match('/\bOR$/i', $before)) {
  107. // Trim trailing OR
  108. $before = substr($before, 0, -2);
  109. // The text prior to the OR is a BooleanSearch
  110. $searchBefore = new FreshRSS_BooleanSearch($before, $level + 1, $nextOperator);
  111. if (count($searchBefore->searches()) > 0) {
  112. $this->searches[] = $searchBefore;
  113. }
  114. $before = '';
  115. // The next BooleanSearch will have to be combined with OR instead of default AND
  116. $nextOperator = 'OR';
  117. } elseif ($before !== '') {
  118. // The text prior to the opening parenthesis is a BooleanSearch
  119. $searchBefore = new FreshRSS_BooleanSearch($before, $level + 1, $nextOperator);
  120. if (count($searchBefore->searches()) > 0) {
  121. $this->searches[] = $searchBefore;
  122. }
  123. $before = '';
  124. }
  125. // Search the matching closing parenthesis
  126. $parentheses = 1;
  127. $sub = '';
  128. $i++;
  129. while ($i < $length) {
  130. $c = $input[$i];
  131. if ($c === '(') {
  132. // One nested level deeper
  133. $parentheses++;
  134. $sub .= $c;
  135. } elseif ($c === ')') {
  136. $parentheses--;
  137. if ($parentheses === 0) {
  138. // Found the matching closing parenthesis
  139. $searchSub = new FreshRSS_BooleanSearch($sub, $level + 1, $nextOperator);
  140. $nextOperator = 'AND';
  141. if (count($searchSub->searches()) > 0) {
  142. $this->searches[] = $searchSub;
  143. }
  144. $sub = '';
  145. break;
  146. } else {
  147. $sub .= $c;
  148. }
  149. } else {
  150. $sub .= $c;
  151. }
  152. $i++;
  153. }
  154. // $sub = trim($sub);
  155. // if ($sub != '') {
  156. // // TODO: Consider throwing an error or warning in case of non-matching parenthesis
  157. // }
  158. // } elseif ($c === ')') {
  159. // // TODO: Consider throwing an error or warning in case of non-matching parenthesis
  160. } else {
  161. $before .= $c;
  162. }
  163. $i++;
  164. }
  165. if ($hasParenthesis) {
  166. $before = trim($before);
  167. if (preg_match('/^OR\b/i', $before)) {
  168. // The next BooleanSearch will have to be combined with OR instead of default AND
  169. $nextOperator = 'OR';
  170. // Trim leading OR
  171. $before = substr($before, 2);
  172. }
  173. // The remaining text after the last parenthesis is a BooleanSearch
  174. $searchBefore = new FreshRSS_BooleanSearch($before, $level + 1, $nextOperator);
  175. $nextOperator = 'AND';
  176. if (count($searchBefore->searches()) > 0) {
  177. $this->searches[] = $searchBefore;
  178. }
  179. return true;
  180. }
  181. // There was no parenthesis logic to apply
  182. return false;
  183. }
  184. private function parseOrSegments(string $input) {
  185. $input = trim($input);
  186. if ($input == '') {
  187. return;
  188. }
  189. $splits = preg_split('/\b(OR)\b/i', $input, -1, PREG_SPLIT_DELIM_CAPTURE);
  190. $segment = '';
  191. $ns = count($splits);
  192. for ($i = 0; $i < $ns; $i++) {
  193. $segment = $segment . $splits[$i];
  194. if (trim($segment) == '' || strcasecmp($segment, 'OR') === 0) {
  195. $segment = '';
  196. } else {
  197. $quotes = substr_count($segment, '"') + substr_count($segment, '&quot;');
  198. if ($quotes % 2 === 0) {
  199. $segment = trim($segment);
  200. $this->searches[] = new FreshRSS_Search($segment);
  201. $segment = '';
  202. }
  203. }
  204. }
  205. $segment = trim($segment);
  206. if ($segment != '') {
  207. $this->searches[] = new FreshRSS_Search($segment);
  208. }
  209. }
  210. /**
  211. * Either a list of FreshRSS_BooleanSearch combined by implicit AND
  212. * or a series of FreshRSS_Search combined by explicit OR
  213. * @return array<FreshRSS_BooleanSearch|FreshRSS_Search>
  214. */
  215. public function searches() {
  216. return $this->searches;
  217. }
  218. /** @return string 'AND' or 'OR' depending on how this BooleanSearch should be combined */
  219. public function operator(): string {
  220. return $this->operator;
  221. }
  222. /** @param FreshRSS_BooleanSearch|FreshRSS_Search $search */
  223. public function add($search) {
  224. $this->searches[] = $search;
  225. }
  226. public function __toString(): string {
  227. return $this->getRawInput();
  228. }
  229. public function getRawInput(): string {
  230. return $this->raw_input;
  231. }
  232. }