BooleanSearch.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Contains Boolean search from the search form.
  5. */
  6. class FreshRSS_BooleanSearch implements \Stringable {
  7. private string $raw_input = '';
  8. /** @var list<FreshRSS_BooleanSearch|FreshRSS_Search> */
  9. private array $searches = [];
  10. /**
  11. * @param string $input
  12. * @param int $level
  13. * @param 'AND'|'OR'|'AND NOT'|'OR NOT' $operator
  14. * @param bool $allowUserQueries
  15. */
  16. public function __construct(
  17. string $input,
  18. int $level = 0,
  19. private readonly string $operator = 'AND',
  20. bool $allowUserQueries = true
  21. ) {
  22. $input = trim($input);
  23. if ($input === '') {
  24. return;
  25. }
  26. $this->raw_input = $input;
  27. if ($level === 0) {
  28. $input = self::escapeLiteralParentheses($input);
  29. $input = $this->parseUserQueryNames($input, $allowUserQueries);
  30. $input = $this->parseUserQueryIds($input, $allowUserQueries);
  31. $input = trim($input);
  32. }
  33. $input = self::consistentOrParentheses($input);
  34. // Either parse everything as a series of BooleanSearch’s combined by implicit AND
  35. // or parse everything as a series of Search’s combined by explicit OR
  36. $this->parseParentheses($input, $level) || $this->parseOrSegments($input);
  37. }
  38. public function __clone() {
  39. foreach ($this->searches as $key => $search) {
  40. $this->searches[$key] = clone $search;
  41. }
  42. }
  43. /**
  44. * Parse the user queries (saved searches) by name and expand them in the input string.
  45. */
  46. private function parseUserQueryNames(string $input, bool $allowUserQueries = true): string {
  47. $all_matches = [];
  48. if (preg_match_all('/\bsearch:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matchesFound)) {
  49. $all_matches[] = $matchesFound;
  50. }
  51. if (preg_match_all('/\bsearch:(?P<search>[^\s"]*)/', $input, $matchesFound)) {
  52. $all_matches[] = $matchesFound;
  53. }
  54. if (!empty($all_matches)) {
  55. $queries = [];
  56. foreach (FreshRSS_Context::userConf()->queries as $raw_query) {
  57. if (($raw_query['name'] ?? '') !== '' && ($raw_query['search'] ?? '') !== '') {
  58. $queries[$raw_query['name']] = trim($raw_query['search']);
  59. }
  60. }
  61. $fromS = [];
  62. $toS = [];
  63. foreach ($all_matches as $matches) {
  64. if (empty($matches['search'])) {
  65. continue;
  66. }
  67. for ($i = count($matches['search']) - 1; $i >= 0; $i--) {
  68. $name = trim($matches['search'][$i]);
  69. if (!empty($queries[$name])) {
  70. $fromS[] = $matches[0][$i];
  71. if ($allowUserQueries) {
  72. $toS[] = '(' . self::escapeLiteralParentheses($queries[$name]) . ')';
  73. } else {
  74. $toS[] = '';
  75. }
  76. }
  77. }
  78. }
  79. $input = str_replace($fromS, $toS, $input);
  80. }
  81. return $input;
  82. }
  83. /**
  84. * Parse the user queries (saved searches) by ID and expand them in the input string.
  85. */
  86. private function parseUserQueryIds(string $input, bool $allowUserQueries = true): string {
  87. $all_matches = [];
  88. if (preg_match_all('/\bS:(?P<search>[0-9,]+)/', $input, $matchesFound)) {
  89. $all_matches[] = $matchesFound;
  90. }
  91. if (!empty($all_matches)) {
  92. $queries = [];
  93. foreach (FreshRSS_Context::userConf()->queries as $raw_query) {
  94. $queries[] = trim($raw_query['search'] ?? '');
  95. }
  96. $fromS = [];
  97. $toS = [];
  98. foreach ($all_matches as $matches) {
  99. if (empty($matches['search'])) {
  100. continue;
  101. }
  102. for ($i = count($matches['search']) - 1; $i >= 0; $i--) {
  103. $ids = explode(',', $matches['search'][$i]);
  104. $ids = array_map('intval', $ids);
  105. $matchedQueries = [];
  106. foreach ($ids as $id) {
  107. if (!empty($queries[$id])) {
  108. $matchedQueries[] = $queries[$id];
  109. }
  110. }
  111. if (empty($matchedQueries)) {
  112. continue;
  113. }
  114. $fromS[] = $matches[0][$i];
  115. if ($allowUserQueries) {
  116. $escapedQueries = array_map(fn(string $query): string => self::escapeLiteralParentheses($query), $matchedQueries);
  117. $toS[] = '(' . implode(') OR (', $escapedQueries) . ')';
  118. } else {
  119. $toS[] = '';
  120. }
  121. }
  122. }
  123. $input = str_replace($fromS, $toS, $input);
  124. }
  125. return $input;
  126. }
  127. /**
  128. * Temporarily escape parentheses used in regex expressions or inside quoted strings.
  129. */
  130. public static function escapeLiteralParentheses(string $input): string {
  131. return preg_replace_callback('%(?<=[\\s(:#!-]|^)(?<![\\\\])(?P<delim>[\'"/]).+?(?<!\\\\)(?P=delim)[im]*%',
  132. fn(array $matches): string => str_replace(['(', ')'], ['\\u0028', '\\u0029'], $matches[0]),
  133. $input
  134. ) ?? '';
  135. }
  136. public static function unescapeLiteralParentheses(string $input): string {
  137. return str_replace(['\\u0028', '\\u0029'], ['(', ')'], $input);
  138. }
  139. /**
  140. * Example: 'ab cd OR ef OR "gh ij"' becomes '(ab cd) OR (ef) OR ("gh ij")'
  141. */
  142. public static function addOrParentheses(string $input): string {
  143. $input = trim($input);
  144. if ($input === '') {
  145. return '';
  146. }
  147. $splits = preg_split('/\b(OR)\b/i', $input, -1, PREG_SPLIT_DELIM_CAPTURE) ?: [];
  148. $ns = count($splits);
  149. if ($ns <= 1) {
  150. return $input;
  151. }
  152. $result = '';
  153. $segment = '';
  154. for ($i = 0; $i < $ns; $i++) {
  155. $segment .= $splits[$i];
  156. if (trim($segment) === '') {
  157. $segment = '';
  158. } elseif (strcasecmp($segment, 'OR') === 0) {
  159. $result .= $segment . ' ';
  160. $segment = '';
  161. } else {
  162. $quotes = substr_count($segment, '"') + substr_count($segment, '&quot;');
  163. if ($quotes % 2 === 0) {
  164. $segment = trim($segment);
  165. if (in_array($segment, ['!', '-'], true)) {
  166. $result .= $segment;
  167. } else {
  168. $result .= '(' . $segment . ') ';
  169. }
  170. $segment = '';
  171. }
  172. }
  173. }
  174. $segment = trim($segment);
  175. if (in_array($segment, ['!', '-'], true)) {
  176. $result .= $segment;
  177. } elseif ($segment !== '') {
  178. $result .= '(' . $segment . ')';
  179. }
  180. return trim($result);
  181. }
  182. /**
  183. * If the query contains a mix of `OR` expressions with and without parentheses,
  184. * then add parentheses to make the query consistent.
  185. * Example: '(ab (cd OR ef)) OR gh OR ij OR (kl)' becomes '(ab ((cd) OR (ef))) OR (gh) OR (ij) OR (kl)'
  186. */
  187. public static function consistentOrParentheses(string $input): string {
  188. if (!preg_match('/(?<!\\\\)\\(/', $input)) {
  189. // No unescaped parentheses in the input
  190. return trim($input);
  191. }
  192. $parenthesesCount = 0;
  193. $result = '';
  194. $segment = '';
  195. $length = strlen($input);
  196. for ($i = 0; $i < $length; $i++) {
  197. $c = $input[$i];
  198. $backslashed = $i >= 1 ? $input[$i - 1] === '\\' : false;
  199. if (!$backslashed) {
  200. if ($c === '(') {
  201. if ($parenthesesCount === 0) {
  202. if ($segment !== '') {
  203. $result = rtrim($result) . ' ' . self::addOrParentheses($segment);
  204. $negation = preg_match('/[!-]$/', $result);
  205. if (!$negation) {
  206. $result .= ' ';
  207. }
  208. $segment = '';
  209. }
  210. $c = '';
  211. }
  212. $parenthesesCount++;
  213. } elseif ($c === ')') {
  214. $parenthesesCount--;
  215. if ($parenthesesCount === 0) {
  216. $segment = self::consistentOrParentheses($segment);
  217. if ($segment !== '') {
  218. $result .= '(' . $segment . ')';
  219. $segment = '';
  220. }
  221. $c = '';
  222. }
  223. }
  224. }
  225. $segment .= $c;
  226. }
  227. if (trim($segment) !== '') {
  228. $result = rtrim($result);
  229. $negation = preg_match('/[!-]$/', $segment);
  230. if (!$negation) {
  231. $result .= ' ';
  232. }
  233. $result .= self::addOrParentheses($segment);
  234. }
  235. return trim($result);
  236. }
  237. /** @return bool True if some parenthesis logic took over, false otherwise */
  238. private function parseParentheses(string $input, int $level): bool {
  239. $input = trim($input);
  240. $length = strlen($input);
  241. $i = 0;
  242. $before = '';
  243. $hasParenthesis = false;
  244. $nextOperator = 'AND';
  245. while ($i < $length) {
  246. $c = $input[$i];
  247. $backslashed = $i >= 1 ? $input[$i - 1] === '\\' : false;
  248. if ($c === '(' && !$backslashed) {
  249. $hasParenthesis = true;
  250. $before = trim($before);
  251. if (preg_match('/[!-]$/', $before)) {
  252. // Trim trailing negation
  253. $before = rtrim($before, ' !-');
  254. $isOr = preg_match('/\bOR$/i', $before);
  255. if ($isOr) {
  256. // Trim trailing OR
  257. $before = substr($before, 0, -2);
  258. }
  259. // The text prior to the negation is a BooleanSearch
  260. $searchBefore = new FreshRSS_BooleanSearch($before, $level + 1, $nextOperator);
  261. if (count($searchBefore->searches()) > 0) {
  262. $this->searches[] = $searchBefore;
  263. }
  264. $before = '';
  265. // The next BooleanSearch will have to be combined with AND NOT or OR NOT instead of default AND
  266. $nextOperator = $isOr ? 'OR NOT' : 'AND NOT';
  267. } elseif (preg_match('/\bOR$/i', $before)) {
  268. // Trim trailing OR
  269. $before = substr($before, 0, -2);
  270. // The text prior to the OR is a BooleanSearch
  271. $searchBefore = new FreshRSS_BooleanSearch($before, $level + 1, $nextOperator);
  272. if (count($searchBefore->searches()) > 0) {
  273. $this->searches[] = $searchBefore;
  274. }
  275. $before = '';
  276. // The next BooleanSearch will have to be combined with OR instead of default AND
  277. $nextOperator = 'OR';
  278. } elseif ($before !== '') {
  279. // The text prior to the opening parenthesis is a BooleanSearch
  280. $searchBefore = new FreshRSS_BooleanSearch($before, $level + 1, $nextOperator);
  281. if (count($searchBefore->searches()) > 0) {
  282. $this->searches[] = $searchBefore;
  283. }
  284. $before = '';
  285. }
  286. // Search the matching closing parenthesis
  287. $parentheses = 1;
  288. $sub = '';
  289. $i++;
  290. while ($i < $length) {
  291. $c = $input[$i];
  292. $backslashed = $input[$i - 1] === '\\';
  293. if ($c === '(' && !$backslashed) {
  294. // One nested level deeper
  295. $parentheses++;
  296. $sub .= $c;
  297. } elseif ($c === ')' && !$backslashed) {
  298. $parentheses--;
  299. if ($parentheses === 0) {
  300. // Found the matching closing parenthesis
  301. $searchSub = new FreshRSS_BooleanSearch($sub, $level + 1, $nextOperator);
  302. $nextOperator = 'AND';
  303. if (count($searchSub->searches()) > 0) {
  304. $this->searches[] = $searchSub;
  305. }
  306. $sub = '';
  307. break;
  308. } else {
  309. $sub .= $c;
  310. }
  311. } else {
  312. $sub .= $c;
  313. }
  314. $i++;
  315. }
  316. // $sub = trim($sub);
  317. // if ($sub !== '') {
  318. // // TODO: Consider throwing an error or warning in case of non-matching parenthesis
  319. // }
  320. // } elseif ($c === ')') {
  321. // // TODO: Consider throwing an error or warning in case of non-matching parenthesis
  322. } else {
  323. $before .= $c;
  324. }
  325. $i++;
  326. }
  327. if ($hasParenthesis) {
  328. $before = trim($before);
  329. if (preg_match('/^OR\b/i', $before)) {
  330. // The next BooleanSearch will have to be combined with OR instead of default AND
  331. $nextOperator = 'OR';
  332. // Trim leading OR
  333. $before = substr($before, 2);
  334. }
  335. // The remaining text after the last parenthesis is a BooleanSearch
  336. $searchBefore = new FreshRSS_BooleanSearch($before, $level + 1, $nextOperator);
  337. $nextOperator = 'AND';
  338. if (count($searchBefore->searches()) > 0) {
  339. $this->searches[] = $searchBefore;
  340. }
  341. return true;
  342. }
  343. // There was no parenthesis logic to apply
  344. return false;
  345. }
  346. private function parseOrSegments(string $input): void {
  347. $input = trim($input);
  348. if ($input === '') {
  349. return;
  350. }
  351. $splits = preg_split('/\b(OR)\b/i', $input, -1, PREG_SPLIT_DELIM_CAPTURE) ?: [];
  352. $segment = '';
  353. $ns = count($splits);
  354. for ($i = 0; $i < $ns; $i++) {
  355. $segment = $segment . $splits[$i];
  356. if (trim($segment) === '' || strcasecmp($segment, 'OR') === 0) {
  357. $segment = '';
  358. } else {
  359. $quotes = substr_count($segment, '"') + substr_count($segment, '&quot;');
  360. if ($quotes % 2 === 0) {
  361. $segment = trim($segment);
  362. $this->searches[] = new FreshRSS_Search($segment);
  363. $segment = '';
  364. }
  365. }
  366. }
  367. $segment = trim($segment);
  368. if ($segment !== '') {
  369. $this->searches[] = new FreshRSS_Search($segment);
  370. }
  371. }
  372. /**
  373. * Either a list of FreshRSS_BooleanSearch combined by implicit AND
  374. * or a series of FreshRSS_Search combined by explicit OR
  375. * @return list<FreshRSS_BooleanSearch|FreshRSS_Search>
  376. */
  377. public function searches(): array {
  378. return $this->searches;
  379. }
  380. /** @return 'AND'|'OR'|'AND NOT'|'OR NOT' depending on how this BooleanSearch should be combined */
  381. public function operator(): string {
  382. return $this->operator;
  383. }
  384. /** @param FreshRSS_BooleanSearch|FreshRSS_Search $search */
  385. public function prepend(FreshRSS_BooleanSearch|FreshRSS_Search $search): void {
  386. array_unshift($this->searches, $search);
  387. }
  388. /** @param FreshRSS_BooleanSearch|FreshRSS_Search $search */
  389. public function add(FreshRSS_BooleanSearch|FreshRSS_Search $search): void {
  390. $this->searches[] = $search;
  391. }
  392. /**
  393. * Modify the first compatible search of the Boolean expression, or add it at the beginning.
  394. * Useful to modify some search parameters.
  395. * @return FreshRSS_BooleanSearch a new instance, modified.
  396. */
  397. public function enforce(FreshRSS_Search $search): self {
  398. $result = clone $this;
  399. $result->raw_input = '';
  400. if (count($result->searches) === 1 && $result->searches[0] instanceof FreshRSS_Search) {
  401. $result->searches[0] = $result->searches[0]->enforce($search);
  402. return $result;
  403. }
  404. if (count($result->searches) === 2) {
  405. foreach ($result->searches as $booleanSearch) {
  406. if (!($booleanSearch instanceof FreshRSS_BooleanSearch)) {
  407. break;
  408. }
  409. if ($booleanSearch->operator() === 'AND') {
  410. if (count($booleanSearch->searches) === 1 && $booleanSearch->searches[0] instanceof FreshRSS_Search &&
  411. $booleanSearch->searches[0]->hasSameOperators($search)) {
  412. $booleanSearch->searches[0] = $search;
  413. return $result;
  414. }
  415. }
  416. }
  417. }
  418. if (count($result->searches) > 1 || (count($result->searches) > 0 && $result->searches[0] instanceof FreshRSS_Search)) {
  419. // Wrap the existing searches in a new BooleanSearch if needed
  420. $wrap = new FreshRSS_BooleanSearch('');
  421. foreach ($result->searches as $existingSearch) {
  422. $wrap->add($existingSearch);
  423. }
  424. if (count($wrap->searches) > 0) {
  425. $result->searches = [$wrap];
  426. }
  427. }
  428. array_unshift($result->searches, $search);
  429. return $result;
  430. }
  431. /**
  432. * Remove the first compatible search of the Boolean expression, if any.
  433. * Useful to modify some search parameters.
  434. * @return FreshRSS_BooleanSearch a new instance, modified.
  435. */
  436. public function remove(FreshRSS_Search $search): self {
  437. $result = clone $this;
  438. $result->raw_input = '';
  439. if (count($result->searches) === 1 && $result->searches[0] instanceof FreshRSS_Search) {
  440. $result->searches[0] = $result->searches[0]->remove($search);
  441. return $result;
  442. }
  443. if (count($result->searches) === 2) {
  444. foreach ($result->searches as $booleanSearch) {
  445. if (!($booleanSearch instanceof FreshRSS_BooleanSearch)) {
  446. break;
  447. }
  448. if ($booleanSearch->operator() === 'AND') {
  449. if (count($booleanSearch->searches) === 1 && $booleanSearch->searches[0] instanceof FreshRSS_Search &&
  450. $booleanSearch->searches[0]->hasSameOperators($search)) {
  451. array_shift($booleanSearch->searches);
  452. return $result;
  453. }
  454. }
  455. }
  456. }
  457. return $result;
  458. }
  459. #[\Override]
  460. public function __toString(): string {
  461. $result = '';
  462. foreach ($this->searches as $search) {
  463. $part = $search->__toString();
  464. if ($part === '') {
  465. continue;
  466. }
  467. $operator = $search instanceof FreshRSS_BooleanSearch ? $search->operator : 'OR';
  468. if ((str_contains($part, ' ') || str_starts_with($part, '-')) && (count($this->searches) > 1 || in_array($operator, ['OR NOT', 'AND NOT'], true))) {
  469. $part = '(' . $part . ')';
  470. }
  471. $result .= match ($operator) {
  472. 'OR' => $result === '' ? '' : ' OR ',
  473. 'OR NOT' => $result === '' ? '-' : ' OR -',
  474. 'AND NOT' => $result === '' ? '-' : ' -',
  475. 'AND' => $result === '' ? '' : ' ',
  476. default => throw new InvalidArgumentException('Invalid operator: ' . $operator),
  477. } . $part;
  478. }
  479. return trim($result);
  480. }
  481. /** @return string Plain text search query. Must be XML-encoded or URL-encoded depending on the situation */
  482. public function getRawInput(): string {
  483. return $this->raw_input;
  484. }
  485. }