Search.php 20 KB

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