Search.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. <?php
  2. declare(strict_types=1);
  3. require_once(LIB_PATH . '/lib_date.php');
  4. /**
  5. * Contains a search from the search form.
  6. *
  7. * It allows to extract meaningful bits of the search and store them in a
  8. * convenient object
  9. */
  10. class FreshRSS_Search {
  11. /**
  12. * This contains the user input string
  13. */
  14. private string $raw_input = '';
  15. // The following properties are extracted from the raw input
  16. /** @var array<string>|null */
  17. private ?array $entry_ids = null;
  18. /** @var array<int>|null */
  19. private ?array $feed_ids = null;
  20. /** @var array<int>|'*'|null */
  21. private $label_ids = null;
  22. /** @var array<string>|null */
  23. private ?array $label_names = null;
  24. /** @var array<string>|null */
  25. private ?array $intitle = null;
  26. /** @var int|false|null */
  27. private $min_date = null;
  28. /** @var int|false|null */
  29. private $max_date = null;
  30. /** @var int|false|null */
  31. private $min_pubdate = null;
  32. /** @var int|false|null */
  33. private $max_pubdate = null;
  34. /** @var array<string>|null */
  35. private ?array $inurl = null;
  36. /** @var array<string>|null */
  37. private ?array $author = null;
  38. /** @var array<string>|null */
  39. private ?array $tags = null;
  40. /** @var array<string>|null */
  41. private ?array $search = null;
  42. /** @var array<string>|null */
  43. private ?array $not_entry_ids = null;
  44. /** @var array<int>|null */
  45. private ?array $not_feed_ids = null;
  46. /** @var array<int>|'*'|null */
  47. private $not_label_ids = null;
  48. /** @var array<string>|null */
  49. private ?array $not_label_names = null;
  50. /** @var array<string>|null */
  51. private ?array $not_intitle = null;
  52. /** @var int|false|null */
  53. private $not_min_date = null;
  54. /** @var int|false|null */
  55. private $not_max_date = null;
  56. /** @var int|false|null */
  57. private $not_min_pubdate = null;
  58. /** @var int|false|null */
  59. private $not_max_pubdate = null;
  60. /** @var array<string>|null */
  61. private ?array $not_inurl = null;
  62. /** @var array<string>|null */
  63. private ?array $not_author = null;
  64. /** @var array<string>|null */
  65. private ?array $not_tags = null;
  66. /** @var array<string>|null */
  67. private ?array $not_search = null;
  68. public function __construct(string $input) {
  69. $input = self::cleanSearch($input);
  70. $input = self::unescape($input);
  71. $this->raw_input = $input;
  72. $input = $this->parseNotEntryIds($input);
  73. $input = $this->parseNotFeedIds($input);
  74. $input = $this->parseNotLabelIds($input);
  75. $input = $this->parseNotLabelNames($input);
  76. $input = $this->parseNotPubdateSearch($input);
  77. $input = $this->parseNotDateSearch($input);
  78. $input = $this->parseNotIntitleSearch($input);
  79. $input = $this->parseNotAuthorSearch($input);
  80. $input = $this->parseNotInurlSearch($input);
  81. $input = $this->parseNotTagsSearch($input);
  82. $input = $this->parseEntryIds($input);
  83. $input = $this->parseFeedIds($input);
  84. $input = $this->parseLabelIds($input);
  85. $input = $this->parseLabelNames($input);
  86. $input = $this->parsePubdateSearch($input);
  87. $input = $this->parseDateSearch($input);
  88. $input = $this->parseIntitleSearch($input);
  89. $input = $this->parseAuthorSearch($input);
  90. $input = $this->parseInurlSearch($input);
  91. $input = $this->parseTagsSearch($input);
  92. $input = $this->parseQuotedSearch($input);
  93. $input = $this->parseNotSearch($input);
  94. $this->parseSearch($input);
  95. }
  96. #[\Override]
  97. public function __toString(): string {
  98. return $this->getRawInput();
  99. }
  100. public function getRawInput(): string {
  101. return $this->raw_input;
  102. }
  103. /** @return array<string>|null */
  104. public function getEntryIds(): ?array {
  105. return $this->entry_ids;
  106. }
  107. /** @return array<string>|null */
  108. public function getNotEntryIds(): ?array {
  109. return $this->not_entry_ids;
  110. }
  111. /** @return array<int>|null */
  112. public function getFeedIds(): ?array {
  113. return $this->feed_ids;
  114. }
  115. /** @return array<int>|null */
  116. public function getNotFeedIds(): ?array {
  117. return $this->not_feed_ids;
  118. }
  119. /** @return array<int>|'*'|null */
  120. public function getLabelIds() {
  121. return $this->label_ids;
  122. }
  123. /** @return array<int>|'*'|null */
  124. public function getNotLabelIds() {
  125. return $this->not_label_ids;
  126. }
  127. /** @return array<string>|null */
  128. public function getLabelNames(): ?array {
  129. return $this->label_names;
  130. }
  131. /** @return array<string>|null */
  132. public function getNotLabelNames(): ?array {
  133. return $this->not_label_names;
  134. }
  135. /** @return array<string>|null */
  136. public function getIntitle(): ?array {
  137. return $this->intitle;
  138. }
  139. /** @return array<string>|null */
  140. public function getNotIntitle(): ?array {
  141. return $this->not_intitle;
  142. }
  143. public function getMinDate(): ?int {
  144. return $this->min_date ?: null;
  145. }
  146. public function getNotMinDate(): ?int {
  147. return $this->not_min_date ?: null;
  148. }
  149. public function setMinDate(int $value): void {
  150. $this->min_date = $value;
  151. }
  152. public function getMaxDate(): ?int {
  153. return $this->max_date ?: null;
  154. }
  155. public function getNotMaxDate(): ?int {
  156. return $this->not_max_date ?: null;
  157. }
  158. public function setMaxDate(int $value): void {
  159. $this->max_date = $value;
  160. }
  161. public function getMinPubdate(): ?int {
  162. return $this->min_pubdate ?: null;
  163. }
  164. public function getNotMinPubdate(): ?int {
  165. return $this->not_min_pubdate ?: null;
  166. }
  167. public function getMaxPubdate(): ?int {
  168. return $this->max_pubdate ?: null;
  169. }
  170. public function getNotMaxPubdate(): ?int {
  171. return $this->not_max_pubdate ?: null;
  172. }
  173. /** @return array<string>|null */
  174. public function getInurl(): ?array {
  175. return $this->inurl;
  176. }
  177. /** @return array<string>|null */
  178. public function getNotInurl(): ?array {
  179. return $this->not_inurl;
  180. }
  181. /** @return array<string>|null */
  182. public function getAuthor(): ?array {
  183. return $this->author;
  184. }
  185. /** @return array<string>|null */
  186. public function getNotAuthor(): ?array {
  187. return $this->not_author;
  188. }
  189. /** @return array<string>|null */
  190. public function getTags(): ?array {
  191. return $this->tags;
  192. }
  193. /** @return array<string>|null */
  194. public function getNotTags(): ?array {
  195. return $this->not_tags;
  196. }
  197. /** @return array<string>|null */
  198. public function getSearch(): ?array {
  199. return $this->search;
  200. }
  201. /** @return array<string>|null */
  202. public function getNotSearch(): ?array {
  203. return $this->not_search;
  204. }
  205. /**
  206. * @param array<string>|null $anArray
  207. * @return array<string>
  208. */
  209. private static function removeEmptyValues(?array $anArray): array {
  210. return empty($anArray) ? [] : array_filter($anArray, static fn(string $value) => $value !== '');
  211. }
  212. /**
  213. * @param array<string>|string $value
  214. * @return ($value is array ? array<string> : string)
  215. */
  216. private static function decodeSpaces($value) {
  217. if (is_array($value)) {
  218. for ($i = count($value) - 1; $i >= 0; $i--) {
  219. $value[$i] = self::decodeSpaces($value[$i]);
  220. }
  221. } else {
  222. $value = trim(str_replace('+', ' ', $value));
  223. }
  224. return $value;
  225. }
  226. /**
  227. * Parse the search string to find entry (article) IDs.
  228. */
  229. private function parseEntryIds(string $input): string {
  230. if (preg_match_all('/\be:(?P<search>[0-9,]*)/', $input, $matches)) {
  231. $input = str_replace($matches[0], '', $input);
  232. $ids_lists = $matches['search'];
  233. $this->entry_ids = [];
  234. foreach ($ids_lists as $ids_list) {
  235. $entry_ids = explode(',', $ids_list);
  236. $entry_ids = self::removeEmptyValues($entry_ids);
  237. if (!empty($entry_ids)) {
  238. $this->entry_ids = array_merge($this->entry_ids, $entry_ids);
  239. }
  240. }
  241. }
  242. return $input;
  243. }
  244. private function parseNotEntryIds(string $input): string {
  245. if (preg_match_all('/(?<=\s|^)[!-]e:(?P<search>[0-9,]*)/', $input, $matches)) {
  246. $input = str_replace($matches[0], '', $input);
  247. $ids_lists = $matches['search'];
  248. $this->not_entry_ids = [];
  249. foreach ($ids_lists as $ids_list) {
  250. $entry_ids = explode(',', $ids_list);
  251. $entry_ids = self::removeEmptyValues($entry_ids);
  252. if (!empty($entry_ids)) {
  253. $this->not_entry_ids = array_merge($this->not_entry_ids, $entry_ids);
  254. }
  255. }
  256. }
  257. return $input;
  258. }
  259. private function parseFeedIds(string $input): string {
  260. if (preg_match_all('/\bf:(?P<search>[0-9,]*)/', $input, $matches)) {
  261. $input = str_replace($matches[0], '', $input);
  262. $ids_lists = $matches['search'];
  263. $this->feed_ids = [];
  264. foreach ($ids_lists as $ids_list) {
  265. $feed_ids = explode(',', $ids_list);
  266. $feed_ids = self::removeEmptyValues($feed_ids);
  267. /** @var array<int> $feed_ids */
  268. $feed_ids = array_map('intval', $feed_ids);
  269. if (!empty($feed_ids)) {
  270. $this->feed_ids = array_merge($this->feed_ids, $feed_ids);
  271. }
  272. }
  273. }
  274. return $input;
  275. }
  276. private function parseNotFeedIds(string $input): string {
  277. if (preg_match_all('/(?<=\s|^)[!-]f:(?P<search>[0-9,]*)/', $input, $matches)) {
  278. $input = str_replace($matches[0], '', $input);
  279. $ids_lists = $matches['search'];
  280. $this->not_feed_ids = [];
  281. foreach ($ids_lists as $ids_list) {
  282. $feed_ids = explode(',', $ids_list);
  283. $feed_ids = self::removeEmptyValues($feed_ids);
  284. /** @var array<int> $feed_ids */
  285. $feed_ids = array_map('intval', $feed_ids);
  286. if (!empty($feed_ids)) {
  287. $this->not_feed_ids = array_merge($this->not_feed_ids, $feed_ids);
  288. }
  289. }
  290. }
  291. return $input;
  292. }
  293. /**
  294. * Parse the search string to find tags (labels) IDs.
  295. */
  296. private function parseLabelIds(string $input): string {
  297. if (preg_match_all('/\b[lL]:(?P<search>[0-9,]+|[*])/', $input, $matches)) {
  298. $input = str_replace($matches[0], '', $input);
  299. $ids_lists = $matches['search'];
  300. $this->label_ids = [];
  301. foreach ($ids_lists as $ids_list) {
  302. if ($ids_list === '*') {
  303. $this->label_ids = '*';
  304. break;
  305. }
  306. $label_ids = explode(',', $ids_list);
  307. $label_ids = self::removeEmptyValues($label_ids);
  308. /** @var array<int> $label_ids */
  309. $label_ids = array_map('intval', $label_ids);
  310. if (!empty($label_ids)) {
  311. $this->label_ids = array_merge($this->label_ids, $label_ids);
  312. }
  313. }
  314. }
  315. return $input;
  316. }
  317. private function parseNotLabelIds(string $input): string {
  318. if (preg_match_all('/(?<=\s|^)[!-][lL]:(?P<search>[0-9,]+|[*])/', $input, $matches)) {
  319. $input = str_replace($matches[0], '', $input);
  320. $ids_lists = $matches['search'];
  321. $this->not_label_ids = [];
  322. foreach ($ids_lists as $ids_list) {
  323. if ($ids_list === '*') {
  324. $this->not_label_ids = '*';
  325. break;
  326. }
  327. $label_ids = explode(',', $ids_list);
  328. $label_ids = self::removeEmptyValues($label_ids);
  329. /** @var array<int> $label_ids */
  330. $label_ids = array_map('intval', $label_ids);
  331. if (!empty($label_ids)) {
  332. $this->not_label_ids = array_merge($this->not_label_ids, $label_ids);
  333. }
  334. }
  335. }
  336. return $input;
  337. }
  338. /**
  339. * Parse the search string to find tags (labels) names.
  340. */
  341. private function parseLabelNames(string $input): string {
  342. $names_lists = [];
  343. if (preg_match_all('/\blabels?:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  344. $names_lists = $matches['search'];
  345. $input = str_replace($matches[0], '', $input);
  346. }
  347. if (preg_match_all('/\blabels?:(?P<search>[^\s"]*)/', $input, $matches)) {
  348. $names_lists = array_merge($names_lists, $matches['search']);
  349. $input = str_replace($matches[0], '', $input);
  350. }
  351. if (!empty($names_lists)) {
  352. $this->label_names = [];
  353. foreach ($names_lists as $names_list) {
  354. $names_array = explode(',', $names_list);
  355. $names_array = self::removeEmptyValues($names_array);
  356. if (!empty($names_array)) {
  357. $this->label_names = array_merge($this->label_names, $names_array);
  358. }
  359. }
  360. }
  361. return $input;
  362. }
  363. /**
  364. * Parse the search string to find tags (labels) names to exclude.
  365. */
  366. private function parseNotLabelNames(string $input): string {
  367. $names_lists = [];
  368. if (preg_match_all('/(?<=\s|^)[!-]labels?:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  369. $names_lists = $matches['search'];
  370. $input = str_replace($matches[0], '', $input);
  371. }
  372. if (preg_match_all('/(?<=\s|^)[!-]labels?:(?P<search>[^\s"]*)/', $input, $matches)) {
  373. $names_lists = array_merge($names_lists, $matches['search']);
  374. $input = str_replace($matches[0], '', $input);
  375. }
  376. if (!empty($names_lists)) {
  377. $this->not_label_names = [];
  378. foreach ($names_lists as $names_list) {
  379. $names_array = explode(',', $names_list);
  380. $names_array = self::removeEmptyValues($names_array);
  381. if (!empty($names_array)) {
  382. $this->not_label_names = array_merge($this->not_label_names, $names_array);
  383. }
  384. }
  385. }
  386. return $input;
  387. }
  388. /**
  389. * Parse the search string to find intitle keyword and the search related to it.
  390. * The search is the first word following the keyword.
  391. */
  392. private function parseIntitleSearch(string $input): string {
  393. if (preg_match_all('/\bintitle:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  394. $this->intitle = $matches['search'];
  395. $input = str_replace($matches[0], '', $input);
  396. }
  397. if (preg_match_all('/\bintitle:(?P<search>[^\s"]*)/', $input, $matches)) {
  398. $this->intitle = array_merge($this->intitle ?: [], $matches['search']);
  399. $input = str_replace($matches[0], '', $input);
  400. }
  401. $this->intitle = self::removeEmptyValues($this->intitle);
  402. if (empty($this->intitle)) {
  403. $this->intitle = null;
  404. }
  405. return $input;
  406. }
  407. private function parseNotIntitleSearch(string $input): string {
  408. if (preg_match_all('/(?<=\s|^)[!-]intitle:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  409. $this->not_intitle = $matches['search'];
  410. $input = str_replace($matches[0], '', $input);
  411. }
  412. if (preg_match_all('/(?<=\s|^)[!-]intitle:(?P<search>[^\s"]*)/', $input, $matches)) {
  413. $this->not_intitle = array_merge($this->not_intitle ?: [], $matches['search']);
  414. $input = str_replace($matches[0], '', $input);
  415. }
  416. $this->not_intitle = self::removeEmptyValues($this->not_intitle);
  417. if (empty($this->not_intitle)) {
  418. $this->not_intitle = null;
  419. }
  420. return $input;
  421. }
  422. /**
  423. * Parse the search string to find author keyword and the search related to it.
  424. * The search is the first word following the keyword except when using
  425. * a delimiter. Supported delimiters are single quote (') and double quotes (").
  426. */
  427. private function parseAuthorSearch(string $input): string {
  428. if (preg_match_all('/\bauthor:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  429. $this->author = $matches['search'];
  430. $input = str_replace($matches[0], '', $input);
  431. }
  432. if (preg_match_all('/\bauthor:(?P<search>[^\s"]*)/', $input, $matches)) {
  433. $this->author = array_merge($this->author ?: [], $matches['search']);
  434. $input = str_replace($matches[0], '', $input);
  435. }
  436. $this->author = self::removeEmptyValues($this->author);
  437. if (empty($this->author)) {
  438. $this->author = null;
  439. }
  440. return $input;
  441. }
  442. private function parseNotAuthorSearch(string $input): string {
  443. if (preg_match_all('/(?<=\s|^)[!-]author:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  444. $this->not_author = $matches['search'];
  445. $input = str_replace($matches[0], '', $input);
  446. }
  447. if (preg_match_all('/(?<=\s|^)[!-]author:(?P<search>[^\s"]*)/', $input, $matches)) {
  448. $this->not_author = array_merge($this->not_author ?: [], $matches['search']);
  449. $input = str_replace($matches[0], '', $input);
  450. }
  451. $this->not_author = self::removeEmptyValues($this->not_author);
  452. if (empty($this->not_author)) {
  453. $this->not_author = null;
  454. }
  455. return $input;
  456. }
  457. /**
  458. * Parse the search string to find inurl keyword and the search related to it.
  459. * The search is the first word following the keyword.
  460. */
  461. private function parseInurlSearch(string $input): string {
  462. if (preg_match_all('/\binurl:(?P<search>[^\s]*)/', $input, $matches)) {
  463. $this->inurl = $matches['search'];
  464. $input = str_replace($matches[0], '', $input);
  465. $this->inurl = self::removeEmptyValues($this->inurl);
  466. }
  467. return $input;
  468. }
  469. private function parseNotInurlSearch(string $input): string {
  470. if (preg_match_all('/(?<=\s|^)[!-]inurl:(?P<search>[^\s]*)/', $input, $matches)) {
  471. $this->not_inurl = $matches['search'];
  472. $input = str_replace($matches[0], '', $input);
  473. $this->not_inurl = self::removeEmptyValues($this->not_inurl);
  474. }
  475. return $input;
  476. }
  477. /**
  478. * Parse the search string to find date keyword and the search related to it.
  479. * The search is the first word following the keyword.
  480. */
  481. private function parseDateSearch(string $input): string {
  482. if (preg_match_all('/\bdate:(?P<search>[^\s]*)/', $input, $matches)) {
  483. $input = str_replace($matches[0], '', $input);
  484. $dates = self::removeEmptyValues($matches['search']);
  485. if (!empty($dates[0])) {
  486. [$this->min_date, $this->max_date] = parseDateInterval($dates[0]);
  487. }
  488. }
  489. return $input;
  490. }
  491. private function parseNotDateSearch(string $input): string {
  492. if (preg_match_all('/(?<=\s|^)[!-]date:(?P<search>[^\s]*)/', $input, $matches)) {
  493. $input = str_replace($matches[0], '', $input);
  494. $dates = self::removeEmptyValues($matches['search']);
  495. if (!empty($dates[0])) {
  496. [$this->not_min_date, $this->not_max_date] = parseDateInterval($dates[0]);
  497. }
  498. }
  499. return $input;
  500. }
  501. /**
  502. * Parse the search string to find pubdate keyword and the search related to it.
  503. * The search is the first word following the keyword.
  504. */
  505. private function parsePubdateSearch(string $input): string {
  506. if (preg_match_all('/\bpubdate:(?P<search>[^\s]*)/', $input, $matches)) {
  507. $input = str_replace($matches[0], '', $input);
  508. $dates = self::removeEmptyValues($matches['search']);
  509. if (!empty($dates[0])) {
  510. [$this->min_pubdate, $this->max_pubdate] = parseDateInterval($dates[0]);
  511. }
  512. }
  513. return $input;
  514. }
  515. private function parseNotPubdateSearch(string $input): string {
  516. if (preg_match_all('/(?<=\s|^)[!-]pubdate:(?P<search>[^\s]*)/', $input, $matches)) {
  517. $input = str_replace($matches[0], '', $input);
  518. $dates = self::removeEmptyValues($matches['search']);
  519. if (!empty($dates[0])) {
  520. [$this->not_min_pubdate, $this->not_max_pubdate] = parseDateInterval($dates[0]);
  521. }
  522. }
  523. return $input;
  524. }
  525. /**
  526. * Parse the search string to find tags keyword (# followed by a word)
  527. * and the search related to it.
  528. * The search is the first word following the #.
  529. */
  530. private function parseTagsSearch(string $input): string {
  531. if (preg_match_all('/#(?P<search>[^\s]+)/', $input, $matches)) {
  532. $this->tags = $matches['search'];
  533. $input = str_replace($matches[0], '', $input);
  534. $this->tags = self::removeEmptyValues($this->tags);
  535. $this->tags = self::decodeSpaces($this->tags);
  536. }
  537. return $input;
  538. }
  539. private function parseNotTagsSearch(string $input): string {
  540. if (preg_match_all('/(?<=\s|^)[!-]#(?P<search>[^\s]+)/', $input, $matches)) {
  541. $this->not_tags = $matches['search'];
  542. $input = str_replace($matches[0], '', $input);
  543. $this->not_tags = self::removeEmptyValues($this->not_tags);
  544. $this->not_tags = self::decodeSpaces($this->not_tags);
  545. }
  546. return $input;
  547. }
  548. /**
  549. * Parse the search string to find search values.
  550. * Every word is a distinct search value using a delimiter.
  551. * Supported delimiters are single quote (') and double quotes (").
  552. */
  553. private function parseQuotedSearch(string $input): string {
  554. $input = self::cleanSearch($input);
  555. if ($input === '') {
  556. return '';
  557. }
  558. if (preg_match_all('/(?<![!-])(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  559. $this->search = $matches['search'];
  560. //TODO: Replace all those str_replace with PREG_OFFSET_CAPTURE
  561. $input = str_replace($matches[0], '', $input);
  562. }
  563. return $input;
  564. }
  565. /**
  566. * Parse the search string to find search values.
  567. * Every word is a distinct search value.
  568. */
  569. private function parseSearch(string $input): string {
  570. $input = self::cleanSearch($input);
  571. if ($input === '') {
  572. return '';
  573. }
  574. if (is_array($this->search)) {
  575. $this->search = array_merge($this->search, explode(' ', $input));
  576. } else {
  577. $this->search = explode(' ', $input);
  578. }
  579. return $input;
  580. }
  581. private function parseNotSearch(string $input): string {
  582. $input = self::cleanSearch($input);
  583. if ($input === '') {
  584. return '';
  585. }
  586. if (preg_match_all('/(?<=\s|^)[!-](?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  587. $this->not_search = $matches['search'];
  588. $input = str_replace($matches[0], '', $input);
  589. }
  590. $input = self::cleanSearch($input);
  591. if ($input === '') {
  592. return '';
  593. }
  594. if (preg_match_all('/(?<=\s|^)[!-](?P<search>[^\s]+)/', $input, $matches)) {
  595. $this->not_search = array_merge(is_array($this->not_search) ? $this->not_search : [], $matches['search']);
  596. $input = str_replace($matches[0], '', $input);
  597. }
  598. $this->not_search = self::removeEmptyValues($this->not_search);
  599. return $input;
  600. }
  601. /**
  602. * Remove all unnecessary spaces in the search
  603. */
  604. private static function cleanSearch(string $input): string {
  605. $input = preg_replace('/\s+/', ' ', $input);
  606. if (!is_string($input)) {
  607. return '';
  608. }
  609. return trim($input);
  610. }
  611. /** Remove escaping backslashes for parenthesis logic */
  612. private static function unescape(string $input): string {
  613. return str_replace(['\\(', '\\)'], ['(', ')'], $input);
  614. }
  615. }