BooleanSearch.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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' or 'AND NOT' */
  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('/[!-]$/i', $before)) {
  107. // Trim trailing negation
  108. $before = substr($before, 0, -1);
  109. // The text prior to the negation 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 AND NOT instead of default AND
  116. $nextOperator = 'AND NOT';
  117. } elseif (preg_match('/\bOR$/i', $before)) {
  118. // Trim trailing OR
  119. $before = substr($before, 0, -2);
  120. // The text prior to the OR is a BooleanSearch
  121. $searchBefore = new FreshRSS_BooleanSearch($before, $level + 1, $nextOperator);
  122. if (count($searchBefore->searches()) > 0) {
  123. $this->searches[] = $searchBefore;
  124. }
  125. $before = '';
  126. // The next BooleanSearch will have to be combined with OR instead of default AND
  127. $nextOperator = 'OR';
  128. } elseif ($before !== '') {
  129. // The text prior to the opening parenthesis is a BooleanSearch
  130. $searchBefore = new FreshRSS_BooleanSearch($before, $level + 1, $nextOperator);
  131. if (count($searchBefore->searches()) > 0) {
  132. $this->searches[] = $searchBefore;
  133. }
  134. $before = '';
  135. }
  136. // Search the matching closing parenthesis
  137. $parentheses = 1;
  138. $sub = '';
  139. $i++;
  140. while ($i < $length) {
  141. $c = $input[$i];
  142. if ($c === '(') {
  143. // One nested level deeper
  144. $parentheses++;
  145. $sub .= $c;
  146. } elseif ($c === ')') {
  147. $parentheses--;
  148. if ($parentheses === 0) {
  149. // Found the matching closing parenthesis
  150. $searchSub = new FreshRSS_BooleanSearch($sub, $level + 1, $nextOperator);
  151. $nextOperator = 'AND';
  152. if (count($searchSub->searches()) > 0) {
  153. $this->searches[] = $searchSub;
  154. }
  155. $sub = '';
  156. break;
  157. } else {
  158. $sub .= $c;
  159. }
  160. } else {
  161. $sub .= $c;
  162. }
  163. $i++;
  164. }
  165. // $sub = trim($sub);
  166. // if ($sub != '') {
  167. // // TODO: Consider throwing an error or warning in case of non-matching parenthesis
  168. // }
  169. // } elseif ($c === ')') {
  170. // // TODO: Consider throwing an error or warning in case of non-matching parenthesis
  171. } else {
  172. $before .= $c;
  173. }
  174. $i++;
  175. }
  176. if ($hasParenthesis) {
  177. $before = trim($before);
  178. if (preg_match('/^OR\b/i', $before)) {
  179. // The next BooleanSearch will have to be combined with OR instead of default AND
  180. $nextOperator = 'OR';
  181. // Trim leading OR
  182. $before = substr($before, 2);
  183. }
  184. // The remaining text after the last parenthesis is a BooleanSearch
  185. $searchBefore = new FreshRSS_BooleanSearch($before, $level + 1, $nextOperator);
  186. $nextOperator = 'AND';
  187. if (count($searchBefore->searches()) > 0) {
  188. $this->searches[] = $searchBefore;
  189. }
  190. return true;
  191. }
  192. // There was no parenthesis logic to apply
  193. return false;
  194. }
  195. private function parseOrSegments(string $input) {
  196. $input = trim($input);
  197. if ($input == '') {
  198. return;
  199. }
  200. $splits = preg_split('/\b(OR)\b/i', $input, -1, PREG_SPLIT_DELIM_CAPTURE);
  201. $segment = '';
  202. $ns = count($splits);
  203. for ($i = 0; $i < $ns; $i++) {
  204. $segment = $segment . $splits[$i];
  205. if (trim($segment) == '' || strcasecmp($segment, 'OR') === 0) {
  206. $segment = '';
  207. } else {
  208. $quotes = substr_count($segment, '"') + substr_count($segment, '&quot;');
  209. if ($quotes % 2 === 0) {
  210. $segment = trim($segment);
  211. $this->searches[] = new FreshRSS_Search($segment);
  212. $segment = '';
  213. }
  214. }
  215. }
  216. $segment = trim($segment);
  217. if ($segment != '') {
  218. $this->searches[] = new FreshRSS_Search($segment);
  219. }
  220. }
  221. /**
  222. * Either a list of FreshRSS_BooleanSearch combined by implicit AND
  223. * or a series of FreshRSS_Search combined by explicit OR
  224. * @return array<FreshRSS_BooleanSearch|FreshRSS_Search>
  225. */
  226. public function searches() {
  227. return $this->searches;
  228. }
  229. /** @return string 'AND' or 'OR' depending on how this BooleanSearch should be combined */
  230. public function operator(): string {
  231. return $this->operator;
  232. }
  233. /** @param FreshRSS_BooleanSearch|FreshRSS_Search $search */
  234. public function add($search) {
  235. $this->searches[] = $search;
  236. }
  237. public function __toString(): string {
  238. return $this->getRawInput();
  239. }
  240. public function getRawInput(): string {
  241. return $this->raw_input;
  242. }
  243. }