Search.php 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271
  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 implements \Stringable {
  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 list<string>|null */
  17. private ?array $entry_ids = null;
  18. /** @var list<int>|null */
  19. private ?array $feed_ids = null;
  20. /** @var list<int>|null */
  21. private ?array $category_ids = null;
  22. /** @var list<list<int>|'*'>|null */
  23. private $label_ids = null;
  24. /** @var list<list<string>>|null */
  25. private ?array $label_names = null;
  26. /** @var list<string>|null */
  27. private ?array $intitle = null;
  28. /** @var list<string>|null */
  29. private ?array $intitle_regex = null;
  30. /** @var list<string>|null */
  31. private ?array $intext = null;
  32. /** @var list<string>|null */
  33. private ?array $intext_regex = null;
  34. /** @var int|false|null */
  35. private $min_date = null;
  36. /** @var int|false|null */
  37. private $max_date = null;
  38. /** @var int|false|null */
  39. private $min_pubdate = null;
  40. /** @var int|false|null */
  41. private $max_pubdate = null;
  42. /** @var int|false|null */
  43. private $min_userdate = null;
  44. /** @var int|false|null */
  45. private $max_userdate = null;
  46. /** @var list<string>|null */
  47. private ?array $inurl = null;
  48. /** @var list<string>|null */
  49. private ?array $inurl_regex = null;
  50. /** @var list<string>|null */
  51. private ?array $author = null;
  52. /** @var list<string>|null */
  53. private ?array $author_regex = null;
  54. /** @var list<string>|null */
  55. private ?array $tags = null;
  56. /** @var list<string>|null */
  57. private ?array $tags_regex = null;
  58. /** @var list<string>|null */
  59. private ?array $search = null;
  60. /** @var list<string>|null */
  61. private ?array $search_regex = null;
  62. /** @var list<string>|null */
  63. private ?array $not_entry_ids = null;
  64. /** @var list<int>|null */
  65. private ?array $not_feed_ids = null;
  66. /** @var list<int>|null */
  67. private ?array $not_category_ids = null;
  68. /** @var list<list<int>|'*'>|null */
  69. private $not_label_ids = null;
  70. /** @var list<list<string>>|null */
  71. private ?array $not_label_names = null;
  72. /** @var list<string>|null */
  73. private ?array $not_intitle = null;
  74. /** @var list<string>|null */
  75. private ?array $not_intitle_regex = null;
  76. /** @var list<string>|null */
  77. private ?array $not_intext = null;
  78. /** @var list<string>|null */
  79. private ?array $not_intext_regex = null;
  80. /** @var int|false|null */
  81. private $not_min_date = null;
  82. /** @var int|false|null */
  83. private $not_max_date = null;
  84. /** @var int|false|null */
  85. private $not_min_pubdate = null;
  86. /** @var int|false|null */
  87. private $not_max_pubdate = null;
  88. /** @var int|false|null */
  89. private $not_min_userdate = null;
  90. /** @var int|false|null */
  91. private $not_max_userdate = null;
  92. /** @var list<string>|null */
  93. private ?array $not_inurl = null;
  94. /** @var list<string>|null */
  95. private ?array $not_inurl_regex = null;
  96. /** @var list<string>|null */
  97. private ?array $not_author = null;
  98. /** @var list<string>|null */
  99. private ?array $not_author_regex = null;
  100. /** @var list<string>|null */
  101. private ?array $not_tags = null;
  102. /** @var list<string>|null */
  103. private ?array $not_tags_regex = null;
  104. /** @var list<string>|null */
  105. private ?array $not_search = null;
  106. /** @var list<string>|null */
  107. private ?array $not_search_regex = null;
  108. public function __construct(string $input) {
  109. $input = self::cleanSearch($input);
  110. $input = self::unescape($input);
  111. $input = FreshRSS_BooleanSearch::unescapeLiteralParentheses($input);
  112. $this->raw_input = $input;
  113. $input = $this->parseNotEntryIds($input);
  114. $input = $this->parseNotFeedIds($input);
  115. $input = $this->parseNotCategoryIds($input);
  116. $input = $this->parseNotLabelIds($input);
  117. $input = $this->parseNotLabelNames($input);
  118. $input = $this->parseNotUserdateSearch($input);
  119. $input = $this->parseNotPubdateSearch($input);
  120. $input = $this->parseNotDateSearch($input);
  121. $input = $this->parseNotIntitleSearch($input);
  122. $input = $this->parseNotIntextSearch($input);
  123. $input = $this->parseNotAuthorSearch($input);
  124. $input = $this->parseNotInurlSearch($input);
  125. $input = $this->parseNotTagsSearch($input);
  126. $input = $this->parseEntryIds($input);
  127. $input = $this->parseFeedIds($input);
  128. $input = $this->parseCategoryIds($input);
  129. $input = $this->parseLabelIds($input);
  130. $input = $this->parseLabelNames($input);
  131. $input = $this->parseUserdateSearch($input);
  132. $input = $this->parsePubdateSearch($input);
  133. $input = $this->parseDateSearch($input);
  134. $input = $this->parseIntitleSearch($input);
  135. $input = $this->parseIntextSearch($input);
  136. $input = $this->parseAuthorSearch($input);
  137. $input = $this->parseInurlSearch($input);
  138. $input = $this->parseTagsSearch($input);
  139. $input = $this->parseQuotedSearch($input);
  140. $input = $this->parseNotSearch($input);
  141. $this->parseSearch($input);
  142. }
  143. private static function quote(string $s): string {
  144. if (str_contains($s, ' ') || $s === '') {
  145. return '"' . addcslashes($s, '\\"') . '"';
  146. }
  147. return $s;
  148. }
  149. private static function dateIntervalToString(?int $min, ?int $max): string {
  150. if ($min === null && $max === null) {
  151. return '';
  152. }
  153. $s = '';
  154. if ($min !== null) {
  155. $s .= date('Y-m-d\\TH:i:s', $min);
  156. }
  157. $s .= '/';
  158. if ($max !== null) {
  159. $s .= date('Y-m-d\\TH:i:s', $max);
  160. }
  161. return $s;
  162. }
  163. /**
  164. * Return true if both searches have the same constraint parameters (even if the values differ), false otherwise.
  165. */
  166. public function hasSameOperators(FreshRSS_Search $search): bool {
  167. $properties = array_keys(get_object_vars($this));
  168. $properties = array_diff($properties, ['raw_input']); // raw_input is not a constraint parameter
  169. foreach ($properties as $property) {
  170. // @phpstan-ignore property.dynamicName, property.dynamicName
  171. if (gettype($this->$property) !== gettype($search->$property)) {
  172. if (str_contains($property, 'min_') || str_contains($property, 'max_')) {
  173. // Process {min_*, max_*} pairs together (for dates)
  174. $mate = str_contains($property, 'min_') ? str_replace('min_', 'max_', $property) : str_replace('max_', 'min_', $property);
  175. // @phpstan-ignore property.dynamicName, property.dynamicName, property.dynamicName, property.dynamicName
  176. if (($this->$property !== null || $this->$mate !== null) !== ($search->$property !== null || $search->$mate !== null)) {
  177. return false;
  178. }
  179. } else {
  180. return false;
  181. }
  182. }
  183. // @phpstan-ignore property.dynamicName, property.dynamicName
  184. if (is_array($this->$property) && is_array($search->$property)) {
  185. // @phpstan-ignore property.dynamicName, property.dynamicName
  186. if (count($this->$property) !== count($search->$property)) {
  187. return false;
  188. }
  189. }
  190. }
  191. return true;
  192. }
  193. /**
  194. * Modifies this search by enforcing the constraint parameters of another search.
  195. * @return FreshRSS_Search a new instance, modified.
  196. */
  197. public function enforce(FreshRSS_Search $search): self {
  198. $result = clone $this;
  199. $properties = array_keys(get_object_vars($result));
  200. $properties = array_diff($properties, ['raw_input']); // raw_input is not a constraint parameter
  201. $result->raw_input = '';
  202. foreach ($properties as $property) {
  203. // @phpstan-ignore property.dynamicName
  204. if ($search->$property !== null) {
  205. // @phpstan-ignore property.dynamicName, property.dynamicName
  206. $result->$property = $search->$property;
  207. if (str_contains($property, 'min_') || str_contains($property, 'max_')) {
  208. // Process {min_*, max_*} pairs together (for dates)
  209. $mate = str_contains($property, 'min_') ? str_replace('min_', 'max_', $property) : str_replace('max_', 'min_', $property);
  210. // @phpstan-ignore property.dynamicName, property.dynamicName
  211. $result->$mate = $search->$mate;
  212. }
  213. }
  214. }
  215. return $result;
  216. }
  217. /**
  218. * Modifies this search by removing the constraints given by another search.
  219. * @return FreshRSS_Search a new instance, modified.
  220. */
  221. public function remove(FreshRSS_Search $search): self {
  222. $result = clone $this;
  223. $properties = array_keys(get_object_vars($result));
  224. $properties = array_diff($properties, ['raw_input']); // raw_input is not a constraint parameter
  225. $result->raw_input = '';
  226. foreach ($properties as $property) {
  227. // @phpstan-ignore property.dynamicName
  228. if ($search->$property !== null) {
  229. // @phpstan-ignore property.dynamicName
  230. $result->$property = null;
  231. if (str_contains($property, 'min_') || str_contains($property, 'max_')) {
  232. // Process {min_*, max_*} pairs together (for dates)
  233. $mate = str_contains($property, 'min_') ? str_replace('min_', 'max_', $property) : str_replace('max_', 'min_', $property);
  234. // @phpstan-ignore property.dynamicName
  235. $result->$mate = null;
  236. }
  237. }
  238. }
  239. return $result;
  240. }
  241. #[\Override]
  242. public function __toString(): string {
  243. $result = '';
  244. if ($this->getEntryIds() !== null) {
  245. $result .= ' e:' . implode(',', $this->getEntryIds());
  246. }
  247. if ($this->getFeedIds() !== null) {
  248. $result .= ' f:' . implode(',', $this->getFeedIds());
  249. }
  250. if ($this->getCategoryIds() !== null) {
  251. $result .= ' c:' . implode(',', $this->getCategoryIds());
  252. }
  253. if ($this->getLabelIds() !== null) {
  254. foreach ($this->getLabelIds() as $ids) {
  255. $result .= ' L:' . (is_array($ids) ? implode(',', $ids) : $ids);
  256. }
  257. }
  258. if ($this->getLabelNames() !== null) {
  259. foreach ($this->getLabelNames() as $names) {
  260. $result .= ' labels:' . self::quote(implode(',', $names));
  261. }
  262. }
  263. if ($this->getMinUserdate() !== null || $this->getMaxUserdate() !== null) {
  264. $result .= ' userdate:' . self::dateIntervalToString($this->getMinUserdate(), $this->getMaxUserdate());
  265. }
  266. if ($this->getMinPubdate() !== null || $this->getMaxPubdate() !== null) {
  267. $result .= ' pubdate:' . self::dateIntervalToString($this->getMinPubdate(), $this->getMaxPubdate());
  268. }
  269. if ($this->getMinDate() !== null || $this->getMaxDate() !== null) {
  270. $result .= ' date:' . self::dateIntervalToString($this->getMinDate(), $this->getMaxDate());
  271. }
  272. if ($this->getIntitleRegex() !== null) {
  273. foreach ($this->getIntitleRegex() as $s) {
  274. $result .= ' intitle:' . $s;
  275. }
  276. }
  277. if ($this->getIntitle() !== null) {
  278. foreach ($this->getIntitle() as $s) {
  279. $result .= ' intitle:' . self::quote($s);
  280. }
  281. }
  282. if ($this->getIntextRegex() !== null) {
  283. foreach ($this->getIntextRegex() as $s) {
  284. $result .= ' intext:' . $s;
  285. }
  286. }
  287. if ($this->getIntext() !== null) {
  288. foreach ($this->getIntext() as $s) {
  289. $result .= ' intext:' . self::quote($s);
  290. }
  291. }
  292. if ($this->getAuthorRegex() !== null) {
  293. foreach ($this->getAuthorRegex() as $s) {
  294. $result .= ' author:' . $s;
  295. }
  296. }
  297. if ($this->getAuthor() !== null) {
  298. foreach ($this->getAuthor() as $s) {
  299. $result .= ' author:' . self::quote($s);
  300. }
  301. }
  302. if ($this->getInurlRegex() !== null) {
  303. foreach ($this->getInurlRegex() as $s) {
  304. $result .= ' inurl:' . $s;
  305. }
  306. }
  307. if ($this->getInurl() !== null) {
  308. foreach ($this->getInurl() as $s) {
  309. $result .= ' inurl:' . self::quote($s);
  310. }
  311. }
  312. if ($this->getTagsRegex() !== null) {
  313. foreach ($this->getTagsRegex() as $s) {
  314. $result .= ' #' . $s;
  315. }
  316. }
  317. if ($this->getTags() !== null) {
  318. foreach ($this->getTags() as $s) {
  319. $result .= ' #' . self::quote($s);
  320. }
  321. }
  322. if ($this->getSearchRegex() !== null) {
  323. foreach ($this->getSearchRegex() as $s) {
  324. $result .= ' ' . $s;
  325. }
  326. }
  327. if ($this->getSearch() !== null) {
  328. foreach ($this->getSearch() as $s) {
  329. $result .= ' ' . self::quote($s);
  330. }
  331. }
  332. if ($this->getNotEntryIds() !== null) {
  333. $result .= ' -e:' . implode(',', $this->getNotEntryIds());
  334. }
  335. if ($this->getNotFeedIds() !== null) {
  336. $result .= ' -f:' . implode(',', $this->getNotFeedIds());
  337. }
  338. if ($this->getNotCategoryIds() !== null) {
  339. $result .= ' -c:' . implode(',', $this->getNotCategoryIds());
  340. }
  341. if ($this->getNotLabelIds() !== null) {
  342. foreach ($this->getNotLabelIds() as $ids) {
  343. $result .= ' -L:' . (is_array($ids) ? implode(',', $ids) : $ids);
  344. }
  345. }
  346. if ($this->getNotLabelNames() !== null) {
  347. foreach ($this->getNotLabelNames() as $names) {
  348. $result .= ' -labels:' . self::quote(implode(',', $names));
  349. }
  350. }
  351. if ($this->getNotMinUserdate() !== null || $this->getNotMaxUserdate() !== null) {
  352. $result .= ' -userdate:' . self::dateIntervalToString($this->getNotMinUserdate(), $this->getNotMaxUserdate());
  353. }
  354. if ($this->getNotMinPubdate() !== null || $this->getNotMaxPubdate() !== null) {
  355. $result .= ' -pubdate:' . self::dateIntervalToString($this->getNotMinPubdate(), $this->getNotMaxPubdate());
  356. }
  357. if ($this->getNotMinDate() !== null || $this->getNotMaxDate() !== null) {
  358. $result .= ' -date:' . self::dateIntervalToString($this->getNotMinDate(), $this->getNotMaxDate());
  359. }
  360. if ($this->getNotIntitleRegex() !== null) {
  361. foreach ($this->getNotIntitleRegex() as $s) {
  362. $result .= ' -intitle:' . $s;
  363. }
  364. }
  365. if ($this->getNotIntitle() !== null) {
  366. foreach ($this->getNotIntitle() as $s) {
  367. $result .= ' -intitle:' . self::quote($s);
  368. }
  369. }
  370. if ($this->getNotIntextRegex() !== null) {
  371. foreach ($this->getNotIntextRegex() as $s) {
  372. $result .= ' -intext:' . $s;
  373. }
  374. }
  375. if ($this->getNotIntext() !== null) {
  376. foreach ($this->getNotIntext() as $s) {
  377. $result .= ' -intext:' . self::quote($s);
  378. }
  379. }
  380. if ($this->getNotAuthorRegex() !== null) {
  381. foreach ($this->getNotAuthorRegex() as $s) {
  382. $result .= ' -author:' . $s;
  383. }
  384. }
  385. if ($this->getNotAuthor() !== null) {
  386. foreach ($this->getNotAuthor() as $s) {
  387. $result .= ' -author:' . self::quote($s);
  388. }
  389. }
  390. if ($this->getNotInurlRegex() !== null) {
  391. foreach ($this->getNotInurlRegex() as $s) {
  392. $result .= ' -inurl:' . $s;
  393. }
  394. }
  395. if ($this->getNotInurl() !== null) {
  396. foreach ($this->getNotInurl() as $s) {
  397. $result .= ' -inurl:' . self::quote($s);
  398. }
  399. }
  400. if ($this->getNotTagsRegex() !== null) {
  401. foreach ($this->getNotTagsRegex() as $s) {
  402. $result .= ' -#' . $s;
  403. }
  404. }
  405. if ($this->getNotTags() !== null) {
  406. foreach ($this->getNotTags() as $s) {
  407. $result .= ' -#' . self::quote($s);
  408. }
  409. }
  410. if ($this->getNotSearchRegex() !== null) {
  411. foreach ($this->getNotSearchRegex() as $s) {
  412. $result .= ' -' . $s;
  413. }
  414. }
  415. if ($this->getNotSearch() !== null) {
  416. foreach ($this->getNotSearch() as $s) {
  417. $result .= ' -' . self::quote($s);
  418. }
  419. }
  420. return trim($result);
  421. }
  422. public function getRawInput(): string {
  423. return $this->raw_input;
  424. }
  425. /** @return list<string>|null */
  426. public function getEntryIds(): ?array {
  427. return $this->entry_ids;
  428. }
  429. /** @return list<string>|null */
  430. public function getNotEntryIds(): ?array {
  431. return $this->not_entry_ids;
  432. }
  433. /** @return list<int>|null */
  434. public function getFeedIds(): ?array {
  435. return $this->feed_ids;
  436. }
  437. /** @return list<int>|null */
  438. public function getNotFeedIds(): ?array {
  439. return $this->not_feed_ids;
  440. }
  441. /** @return list<int>|null */
  442. public function getCategoryIds(): ?array {
  443. return $this->category_ids;
  444. }
  445. /** @return list<int>|null */
  446. public function getNotCategoryIds(): ?array {
  447. return $this->not_category_ids;
  448. }
  449. /** @return list<list<int>|'*'>|null */
  450. public function getLabelIds(): array|null {
  451. return $this->label_ids;
  452. }
  453. /** @return list<list<int>|'*'>|null */
  454. public function getNotLabelIds(): array|null {
  455. return $this->not_label_ids;
  456. }
  457. /** @return list<list<string>>|null */
  458. public function getLabelNames(): ?array {
  459. return $this->label_names;
  460. }
  461. /** @return list<list<string>>|null */
  462. public function getNotLabelNames(): ?array {
  463. return $this->not_label_names;
  464. }
  465. /** @return list<string>|null */
  466. public function getIntitle(): ?array {
  467. return $this->intitle;
  468. }
  469. /** @return list<string>|null */
  470. public function getIntitleRegex(): ?array {
  471. return $this->intitle_regex;
  472. }
  473. /** @return list<string>|null */
  474. public function getNotIntitle(): ?array {
  475. return $this->not_intitle;
  476. }
  477. /** @return list<string>|null */
  478. public function getNotIntitleRegex(): ?array {
  479. return $this->not_intitle_regex;
  480. }
  481. /** @return list<string>|null */
  482. public function getIntext(): ?array {
  483. return $this->intext;
  484. }
  485. /** @return list<string>|null */
  486. public function getIntextRegex(): ?array {
  487. return $this->intext_regex;
  488. }
  489. /** @return list<string>|null */
  490. public function getNotIntext(): ?array {
  491. return $this->not_intext;
  492. }
  493. /** @return list<string>|null */
  494. public function getNotIntextRegex(): ?array {
  495. return $this->not_intext_regex;
  496. }
  497. public function getMinDate(): ?int {
  498. return $this->min_date ?: null;
  499. }
  500. public function getNotMinDate(): ?int {
  501. return $this->not_min_date ?: null;
  502. }
  503. public function setMinDate(int $value): void {
  504. $this->min_date = $value;
  505. }
  506. public function getMaxDate(): ?int {
  507. return $this->max_date ?: null;
  508. }
  509. public function getNotMaxDate(): ?int {
  510. return $this->not_max_date ?: null;
  511. }
  512. public function setMaxDate(int $value): void {
  513. $this->max_date = $value;
  514. }
  515. public function getMinPubdate(): ?int {
  516. return $this->min_pubdate ?: null;
  517. }
  518. public function getNotMinPubdate(): ?int {
  519. return $this->not_min_pubdate ?: null;
  520. }
  521. public function getMaxPubdate(): ?int {
  522. return $this->max_pubdate ?: null;
  523. }
  524. public function getNotMaxPubdate(): ?int {
  525. return $this->not_max_pubdate ?: null;
  526. }
  527. public function setMaxPubdate(int $value): void {
  528. $this->max_pubdate = $value;
  529. }
  530. public function getMinUserdate(): ?int {
  531. return $this->min_userdate ?: null;
  532. }
  533. public function getNotMinUserdate(): ?int {
  534. return $this->not_min_userdate ?: null;
  535. }
  536. public function getMaxUserdate(): ?int {
  537. return $this->max_userdate ?: null;
  538. }
  539. public function getNotMaxUserdate(): ?int {
  540. return $this->not_max_userdate ?: null;
  541. }
  542. /** @return list<string>|null */
  543. public function getInurl(): ?array {
  544. return $this->inurl;
  545. }
  546. /** @return list<string>|null */
  547. public function getInurlRegex(): ?array {
  548. return $this->inurl_regex;
  549. }
  550. /** @return list<string>|null */
  551. public function getNotInurl(): ?array {
  552. return $this->not_inurl;
  553. }
  554. /** @return list<string>|null */
  555. public function getNotInurlRegex(): ?array {
  556. return $this->not_inurl_regex;
  557. }
  558. /** @return list<string>|null */
  559. public function getAuthor(): ?array {
  560. return $this->author;
  561. }
  562. /** @return list<string>|null */
  563. public function getAuthorRegex(): ?array {
  564. return $this->author_regex;
  565. }
  566. /** @return list<string>|null */
  567. public function getNotAuthor(): ?array {
  568. return $this->not_author;
  569. }
  570. /** @return list<string>|null */
  571. public function getNotAuthorRegex(): ?array {
  572. return $this->not_author_regex;
  573. }
  574. /** @return list<string>|null */
  575. public function getTags(): ?array {
  576. return $this->tags;
  577. }
  578. /** @return list<string>|null */
  579. public function getTagsRegex(): ?array {
  580. return $this->tags_regex;
  581. }
  582. /** @return list<string>|null */
  583. public function getNotTags(): ?array {
  584. return $this->not_tags;
  585. }
  586. /** @return list<string>|null */
  587. public function getNotTagsRegex(): ?array {
  588. return $this->not_tags_regex;
  589. }
  590. /** @return list<string>|null */
  591. public function getSearch(): ?array {
  592. return $this->search;
  593. }
  594. /** @return list<string>|null */
  595. public function getSearchRegex(): ?array {
  596. return $this->search_regex;
  597. }
  598. /** @return list<string>|null */
  599. public function getNotSearch(): ?array {
  600. return $this->not_search;
  601. }
  602. /** @return list<string>|null */
  603. public function getNotSearchRegex(): ?array {
  604. return $this->not_search_regex;
  605. }
  606. /**
  607. * @param list<string>|null $anArray
  608. * @return list<string>
  609. */
  610. private static function removeEmptyValues(?array $anArray): array {
  611. return empty($anArray) ? [] : array_values(array_filter($anArray, static fn(string $value) => $value !== ''));
  612. }
  613. /**
  614. * @param list<string>|string $value
  615. * @return ($value is string ? string : list<string>)
  616. */
  617. private static function decodeSpaces(array|string $value): array|string {
  618. if (is_array($value)) {
  619. foreach ($value as &$val) {
  620. $val = self::decodeSpaces($val);
  621. }
  622. } else {
  623. $value = trim(str_replace('+', ' ', $value));
  624. }
  625. // @phpstan-ignore return.type
  626. return $value;
  627. }
  628. /**
  629. * @param list<string> $strings
  630. * @return list<string>
  631. */
  632. private static function htmlspecialchars_decodes(array $strings): array {
  633. return array_map(static fn(string $s) => htmlspecialchars_decode($s, ENT_QUOTES), $strings);
  634. }
  635. /**
  636. * Parse the search string to find entry (article) IDs.
  637. */
  638. private function parseEntryIds(string $input): string {
  639. if (preg_match_all('/\\be:(?P<search>[0-9,]*)/', $input, $matches)) {
  640. $input = str_replace($matches[0], '', $input);
  641. $ids_lists = $matches['search'];
  642. $this->entry_ids = [];
  643. foreach ($ids_lists as $ids_list) {
  644. $entry_ids = explode(',', $ids_list);
  645. $entry_ids = self::removeEmptyValues($entry_ids);
  646. if (!empty($entry_ids)) {
  647. $this->entry_ids = array_merge($this->entry_ids, $entry_ids);
  648. }
  649. }
  650. }
  651. return $input;
  652. }
  653. private function parseNotEntryIds(string $input): string {
  654. if (preg_match_all('/(?<=[\\s(]|^)[!-]e:(?P<search>[0-9,]*)/', $input, $matches)) {
  655. $input = str_replace($matches[0], '', $input);
  656. $ids_lists = $matches['search'];
  657. $this->not_entry_ids = [];
  658. foreach ($ids_lists as $ids_list) {
  659. $entry_ids = explode(',', $ids_list);
  660. $entry_ids = self::removeEmptyValues($entry_ids);
  661. if (!empty($entry_ids)) {
  662. $this->not_entry_ids = array_merge($this->not_entry_ids, $entry_ids);
  663. }
  664. }
  665. }
  666. return $input;
  667. }
  668. private function parseFeedIds(string $input): string {
  669. if (preg_match_all('/\\bf:(?P<search>[0-9,]*)/', $input, $matches)) {
  670. $input = str_replace($matches[0], '', $input);
  671. $ids_lists = $matches['search'];
  672. $this->feed_ids = [];
  673. foreach ($ids_lists as $ids_list) {
  674. $feed_ids = explode(',', $ids_list);
  675. $feed_ids = self::removeEmptyValues($feed_ids);
  676. /** @var list<int> $feed_ids */
  677. $feed_ids = array_map('intval', $feed_ids);
  678. if (!empty($feed_ids)) {
  679. $this->feed_ids = array_merge($this->feed_ids, $feed_ids);
  680. }
  681. }
  682. }
  683. return $input;
  684. }
  685. private function parseNotFeedIds(string $input): string {
  686. if (preg_match_all('/(?<=[\\s(]|^)[!-]f:(?P<search>[0-9,]*)/', $input, $matches)) {
  687. $input = str_replace($matches[0], '', $input);
  688. $ids_lists = $matches['search'];
  689. $this->not_feed_ids = [];
  690. foreach ($ids_lists as $ids_list) {
  691. $feed_ids = explode(',', $ids_list);
  692. $feed_ids = self::removeEmptyValues($feed_ids);
  693. /** @var list<int> $feed_ids */
  694. $feed_ids = array_map('intval', $feed_ids);
  695. if (!empty($feed_ids)) {
  696. $this->not_feed_ids = array_merge($this->not_feed_ids, $feed_ids);
  697. }
  698. }
  699. }
  700. return $input;
  701. }
  702. private function parseCategoryIds(string $input): string {
  703. if (preg_match_all('/\\bc:(?P<search>[0-9,]*)/', $input, $matches)) {
  704. $input = str_replace($matches[0], '', $input);
  705. $ids_lists = $matches['search'];
  706. $this->category_ids = [];
  707. foreach ($ids_lists as $ids_list) {
  708. $category_ids = explode(',', $ids_list);
  709. $category_ids = self::removeEmptyValues($category_ids);
  710. /** @var list<int> $category_ids */
  711. $category_ids = array_map('intval', $category_ids);
  712. if (!empty($category_ids)) {
  713. $this->category_ids = array_merge($this->category_ids, $category_ids);
  714. }
  715. }
  716. }
  717. return $input;
  718. }
  719. private function parseNotCategoryIds(string $input): string {
  720. if (preg_match_all('/(?<=[\\s(]|^)[!-]c:(?P<search>[0-9,]*)/', $input, $matches)) {
  721. $input = str_replace($matches[0], '', $input);
  722. $ids_lists = $matches['search'];
  723. $this->not_category_ids = [];
  724. foreach ($ids_lists as $ids_list) {
  725. $category_ids = explode(',', $ids_list);
  726. $category_ids = self::removeEmptyValues($category_ids);
  727. /** @var list<int> $category_ids */
  728. $category_ids = array_map('intval', $category_ids);
  729. if (!empty($category_ids)) {
  730. $this->not_category_ids = array_merge($this->not_category_ids, $category_ids);
  731. }
  732. }
  733. }
  734. return $input;
  735. }
  736. /**
  737. * Parse the search string to find tags (labels) IDs.
  738. */
  739. private function parseLabelIds(string $input): string {
  740. if (preg_match_all('/\\b[lL]:(?P<search>[0-9,]+|[*])/', $input, $matches)) {
  741. $input = str_replace($matches[0], '', $input);
  742. $ids_lists = $matches['search'];
  743. $this->label_ids = [];
  744. foreach ($ids_lists as $ids_list) {
  745. if ($ids_list === '*') {
  746. $this->label_ids[] = '*';
  747. break;
  748. }
  749. $label_ids = explode(',', $ids_list);
  750. $label_ids = self::removeEmptyValues($label_ids);
  751. /** @var list<int> $label_ids */
  752. $label_ids = array_map('intval', $label_ids);
  753. if (!empty($label_ids)) {
  754. $this->label_ids[] = $label_ids;
  755. }
  756. }
  757. }
  758. return $input;
  759. }
  760. private function parseNotLabelIds(string $input): string {
  761. if (preg_match_all('/(?<=[\\s(]|^)[!-][lL]:(?P<search>[0-9,]+|[*])/', $input, $matches)) {
  762. $input = str_replace($matches[0], '', $input);
  763. $ids_lists = $matches['search'];
  764. $this->not_label_ids = [];
  765. foreach ($ids_lists as $ids_list) {
  766. if ($ids_list === '*') {
  767. $this->not_label_ids[] = '*';
  768. break;
  769. }
  770. $label_ids = explode(',', $ids_list);
  771. $label_ids = self::removeEmptyValues($label_ids);
  772. /** @var list<int> $label_ids */
  773. $label_ids = array_map('intval', $label_ids);
  774. if (!empty($label_ids)) {
  775. $this->not_label_ids[] = $label_ids;
  776. }
  777. }
  778. }
  779. return $input;
  780. }
  781. /**
  782. * Parse the search string to find tags (labels) names.
  783. */
  784. private function parseLabelNames(string $input): string {
  785. $names_lists = [];
  786. if (preg_match_all('/\\blabels?:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  787. $names_lists = $matches['search'];
  788. $input = str_replace($matches[0], '', $input);
  789. }
  790. if (preg_match_all('/\\blabels?:(?P<search>[^\s"]*)/', $input, $matches)) {
  791. $names_lists = array_merge($names_lists, $matches['search']);
  792. $input = str_replace($matches[0], '', $input);
  793. }
  794. if (!empty($names_lists)) {
  795. $this->label_names = [];
  796. foreach ($names_lists as $names_list) {
  797. $names_array = explode(',', $names_list);
  798. $names_array = self::removeEmptyValues($names_array);
  799. if (!empty($names_array)) {
  800. $this->label_names[] = $names_array;
  801. }
  802. }
  803. }
  804. return $input;
  805. }
  806. /**
  807. * Parse the search string to find tags (labels) names to exclude.
  808. */
  809. private function parseNotLabelNames(string $input): string {
  810. $names_lists = [];
  811. if (preg_match_all('/(?<=[\\s(]|^)[!-]labels?:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  812. $names_lists = $matches['search'];
  813. $input = str_replace($matches[0], '', $input);
  814. }
  815. if (preg_match_all('/(?<=[\\s(]|^)[!-]labels?:(?P<search>[^\\s"]*)/', $input, $matches)) {
  816. $names_lists = array_merge($names_lists, $matches['search']);
  817. $input = str_replace($matches[0], '', $input);
  818. }
  819. if (!empty($names_lists)) {
  820. $this->not_label_names = [];
  821. foreach ($names_lists as $names_list) {
  822. $names_array = explode(',', $names_list);
  823. $names_array = self::removeEmptyValues($names_array);
  824. if (!empty($names_array)) {
  825. $this->not_label_names[] = $names_array;
  826. }
  827. }
  828. }
  829. return $input;
  830. }
  831. /**
  832. * Parse the search string to find intitle keyword and the search related to it.
  833. */
  834. private function parseIntitleSearch(string $input): string {
  835. if (preg_match_all('#\\bintitle:(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
  836. $this->intitle_regex = self::htmlspecialchars_decodes($matches['search']);
  837. $input = str_replace($matches[0], '', $input);
  838. }
  839. if (preg_match_all('/\\bintitle:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  840. $this->intitle = $matches['search'];
  841. $input = str_replace($matches[0], '', $input);
  842. }
  843. if (preg_match_all('/\\bintitle:(?P<search>[^\s"]*)/', $input, $matches)) {
  844. $this->intitle = array_merge($this->intitle ?? [], $matches['search']);
  845. $input = str_replace($matches[0], '', $input);
  846. }
  847. $this->intitle = self::removeEmptyValues($this->intitle);
  848. if (empty($this->intitle)) {
  849. $this->intitle = null;
  850. }
  851. return $input;
  852. }
  853. private function parseNotIntitleSearch(string $input): string {
  854. if (preg_match_all('#(?<=[\\s(]|^)[!-]intitle:(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
  855. $this->not_intitle_regex = self::htmlspecialchars_decodes($matches['search']);
  856. $input = str_replace($matches[0], '', $input);
  857. }
  858. if (preg_match_all('/(?<=[\\s(]|^)[!-]intitle:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  859. $this->not_intitle = $matches['search'];
  860. $input = str_replace($matches[0], '', $input);
  861. }
  862. if (preg_match_all('/(?<=[\\s(]|^)[!-]intitle:(?P<search>[^\s"]*)/', $input, $matches)) {
  863. $this->not_intitle = array_merge($this->not_intitle ?? [], $matches['search']);
  864. $input = str_replace($matches[0], '', $input);
  865. }
  866. $this->not_intitle = self::removeEmptyValues($this->not_intitle);
  867. if (empty($this->not_intitle)) {
  868. $this->not_intitle = null;
  869. }
  870. return $input;
  871. }
  872. /**
  873. * Parse the search string to find intext keyword and the search related to it.
  874. */
  875. private function parseIntextSearch(string $input): string {
  876. if (preg_match_all('#\\bintext:(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
  877. $this->intext_regex = self::htmlspecialchars_decodes($matches['search']);
  878. $input = str_replace($matches[0], '', $input);
  879. }
  880. if (preg_match_all('/\\bintext:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  881. $this->intext = $matches['search'];
  882. $input = str_replace($matches[0], '', $input);
  883. }
  884. if (preg_match_all('/\\bintext:(?P<search>[^\s"]*)/', $input, $matches)) {
  885. $this->intext = array_merge($this->intext ?? [], $matches['search']);
  886. $input = str_replace($matches[0], '', $input);
  887. }
  888. $this->intext = self::removeEmptyValues($this->intext);
  889. if (empty($this->intext)) {
  890. $this->intext = null;
  891. }
  892. return $input;
  893. }
  894. private function parseNotIntextSearch(string $input): string {
  895. if (preg_match_all('#(?<=[\\s(]|^)[!-]intext:(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
  896. $this->not_intext_regex = self::htmlspecialchars_decodes($matches['search']);
  897. $input = str_replace($matches[0], '', $input);
  898. }
  899. if (preg_match_all('/(?<=[\\s(]|^)[!-]intext:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  900. $this->not_intext = $matches['search'];
  901. $input = str_replace($matches[0], '', $input);
  902. }
  903. if (preg_match_all('/(?<=[\\s(]|^)[!-]intext:(?P<search>[^\s"]*)/', $input, $matches)) {
  904. $this->not_intext = array_merge($this->not_intext ?? [], $matches['search']);
  905. $input = str_replace($matches[0], '', $input);
  906. }
  907. $this->not_intext = self::removeEmptyValues($this->not_intext);
  908. if (empty($this->not_intext)) {
  909. $this->not_intext = null;
  910. }
  911. return $input;
  912. }
  913. /**
  914. * Parse the search string to find author keyword and the search related to it.
  915. * The search is the first word following the keyword except when using
  916. * a delimiter. Supported delimiters are single quote (') and double quotes (").
  917. */
  918. private function parseAuthorSearch(string $input): string {
  919. if (preg_match_all('#\\bauthor:(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
  920. $this->author_regex = self::htmlspecialchars_decodes($matches['search']);
  921. $input = str_replace($matches[0], '', $input);
  922. }
  923. if (preg_match_all('/\\bauthor:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  924. $this->author = $matches['search'];
  925. $input = str_replace($matches[0], '', $input);
  926. }
  927. if (preg_match_all('/\\bauthor:(?P<search>[^\s"]*)/', $input, $matches)) {
  928. $this->author = array_merge($this->author ?? [], $matches['search']);
  929. $input = str_replace($matches[0], '', $input);
  930. }
  931. $this->author = self::removeEmptyValues($this->author);
  932. if (empty($this->author)) {
  933. $this->author = null;
  934. }
  935. return $input;
  936. }
  937. private function parseNotAuthorSearch(string $input): string {
  938. if (preg_match_all('#(?<=[\\s(]|^)[!-]author:(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
  939. $this->not_author_regex = self::htmlspecialchars_decodes($matches['search']);
  940. $input = str_replace($matches[0], '', $input);
  941. }
  942. if (preg_match_all('/(?<=[\\s(]|^)[!-]author:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  943. $this->not_author = $matches['search'];
  944. $input = str_replace($matches[0], '', $input);
  945. }
  946. if (preg_match_all('/(?<=[\\s(]|^)[!-]author:(?P<search>[^\s"]*)/', $input, $matches)) {
  947. $this->not_author = array_merge($this->not_author ?? [], $matches['search']);
  948. $input = str_replace($matches[0], '', $input);
  949. }
  950. $this->not_author = self::removeEmptyValues($this->not_author);
  951. if (empty($this->not_author)) {
  952. $this->not_author = null;
  953. }
  954. return $input;
  955. }
  956. /**
  957. * Parse the search string to find inurl keyword and the search related to it.
  958. * The search is the first word following the keyword.
  959. */
  960. private function parseInurlSearch(string $input): string {
  961. if (preg_match_all('#\\binurl:(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
  962. $this->inurl_regex = self::htmlspecialchars_decodes($matches['search']);
  963. $input = str_replace($matches[0], '', $input);
  964. }
  965. if (preg_match_all('/\\binurl:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  966. $this->inurl = $matches['search'];
  967. $input = str_replace($matches[0], '', $input);
  968. }
  969. if (preg_match_all('/\\binurl:(?P<search>[^\\s]*)/', $input, $matches)) {
  970. $this->inurl = $matches['search'];
  971. $input = str_replace($matches[0], '', $input);
  972. }
  973. $this->inurl = self::removeEmptyValues($this->inurl);
  974. if (empty($this->inurl)) {
  975. $this->inurl = null;
  976. }
  977. return $input;
  978. }
  979. private function parseNotInurlSearch(string $input): string {
  980. if (preg_match_all('#(?<=[\\s(]|^)[!-]inurl:(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
  981. $this->not_inurl_regex = self::htmlspecialchars_decodes($matches['search']);
  982. $input = str_replace($matches[0], '', $input);
  983. }
  984. if (preg_match_all('/(?<=[\\s(]|^)[!-]inurl:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  985. $this->not_inurl = $matches['search'];
  986. $input = str_replace($matches[0], '', $input);
  987. }
  988. if (preg_match_all('/(?<=[\\s(]|^)[!-]inurl:(?P<search>[^\\s]*)/', $input, $matches)) {
  989. $this->not_inurl = $matches['search'];
  990. $input = str_replace($matches[0], '', $input);
  991. }
  992. $this->not_inurl = self::removeEmptyValues($this->not_inurl);
  993. if (empty($this->not_inurl)) {
  994. $this->not_inurl = null;
  995. }
  996. return $input;
  997. }
  998. /**
  999. * Parse the search string to find date keyword and the search related to it.
  1000. * The search is the first word following the keyword.
  1001. */
  1002. private function parseDateSearch(string $input): string {
  1003. if (preg_match_all('/\\bdate:(?P<search>[^\\s]*)/', $input, $matches)) {
  1004. $input = str_replace($matches[0], '', $input);
  1005. $dates = self::removeEmptyValues($matches['search']);
  1006. if (!empty($dates[0])) {
  1007. [$this->min_date, $this->max_date] = parseDateInterval($dates[0]);
  1008. }
  1009. }
  1010. return $input;
  1011. }
  1012. private function parseNotDateSearch(string $input): string {
  1013. if (preg_match_all('/(?<=[\\s(]|^)[!-]date:(?P<search>[^\\s]*)/', $input, $matches)) {
  1014. $input = str_replace($matches[0], '', $input);
  1015. $dates = self::removeEmptyValues($matches['search']);
  1016. if (!empty($dates[0])) {
  1017. [$this->not_min_date, $this->not_max_date] = parseDateInterval($dates[0]);
  1018. }
  1019. }
  1020. return $input;
  1021. }
  1022. /**
  1023. * Parse the search string to find pubdate keyword and the search related to it.
  1024. * The search is the first word following the keyword.
  1025. */
  1026. private function parsePubdateSearch(string $input): string {
  1027. if (preg_match_all('/\\bpubdate:(?P<search>[^\\s]*)/', $input, $matches)) {
  1028. $input = str_replace($matches[0], '', $input);
  1029. $dates = self::removeEmptyValues($matches['search']);
  1030. if (!empty($dates[0])) {
  1031. [$this->min_pubdate, $this->max_pubdate] = parseDateInterval($dates[0]);
  1032. }
  1033. }
  1034. return $input;
  1035. }
  1036. private function parseNotPubdateSearch(string $input): string {
  1037. if (preg_match_all('/(?<=[\\s(]|^)[!-]pubdate:(?P<search>[^\\s]*)/', $input, $matches)) {
  1038. $input = str_replace($matches[0], '', $input);
  1039. $dates = self::removeEmptyValues($matches['search']);
  1040. if (!empty($dates[0])) {
  1041. [$this->not_min_pubdate, $this->not_max_pubdate] = parseDateInterval($dates[0]);
  1042. }
  1043. }
  1044. return $input;
  1045. }
  1046. /**
  1047. * Parse the search string to find userdate keyword and the search related to it.
  1048. * The search is the first word following the keyword.
  1049. */
  1050. private function parseUserdateSearch(string $input): string {
  1051. if (preg_match_all('/\\buserdate:(?P<search>[^\\s]*)/', $input, $matches)) {
  1052. $input = str_replace($matches[0], '', $input);
  1053. $dates = self::removeEmptyValues($matches['search']);
  1054. if (!empty($dates[0])) {
  1055. [$this->min_userdate, $this->max_userdate] = parseDateInterval($dates[0]);
  1056. }
  1057. }
  1058. return $input;
  1059. }
  1060. private function parseNotUserdateSearch(string $input): string {
  1061. if (preg_match_all('/(?<=[\\s(]|^)[!-]userdate:(?P<search>[^\\s]*)/', $input, $matches)) {
  1062. $input = str_replace($matches[0], '', $input);
  1063. $dates = self::removeEmptyValues($matches['search']);
  1064. if (!empty($dates[0])) {
  1065. [$this->not_min_userdate, $this->not_max_userdate] = parseDateInterval($dates[0]);
  1066. }
  1067. }
  1068. return $input;
  1069. }
  1070. /**
  1071. * Parse the search string to find tags keyword (# followed by a word)
  1072. * and the search related to it.
  1073. * The search is the first word following the #.
  1074. */
  1075. private function parseTagsSearch(string $input): string {
  1076. if (preg_match_all('%#(?P<search>/.*?(?<!\\\\)/[im]*)%', $input, $matches)) {
  1077. $this->tags_regex = self::htmlspecialchars_decodes($matches['search']);
  1078. $input = str_replace($matches[0], '', $input);
  1079. }
  1080. if (preg_match_all('/#(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  1081. $this->tags = $matches['search'];
  1082. $input = str_replace($matches[0], '', $input);
  1083. }
  1084. if (preg_match_all('/#(?P<search>[^\\s]+)/', $input, $matches)) {
  1085. $this->tags = $matches['search'];
  1086. $input = str_replace($matches[0], '', $input);
  1087. }
  1088. $this->tags = self::removeEmptyValues($this->tags);
  1089. if (empty($this->tags)) {
  1090. $this->tags = null;
  1091. } else {
  1092. $this->tags = self::decodeSpaces($this->tags);
  1093. }
  1094. return $input;
  1095. }
  1096. private function parseNotTagsSearch(string $input): string {
  1097. if (preg_match_all('%(?<=[\\s(]|^)[!-]#(?P<search>/.*?(?<!\\\\)/[im]*)%', $input, $matches)) {
  1098. $this->not_tags_regex = self::htmlspecialchars_decodes($matches['search']);
  1099. $input = str_replace($matches[0], '', $input);
  1100. }
  1101. if (preg_match_all('/(?<=[\\s(]|^)[!-]#(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  1102. $this->not_tags = $matches['search'];
  1103. $input = str_replace($matches[0], '', $input);
  1104. }
  1105. if (preg_match_all('/(?<=[\\s(]|^)[!-]#(?P<search>[^\\s]+)/', $input, $matches)) {
  1106. $this->not_tags = $matches['search'];
  1107. $input = str_replace($matches[0], '', $input);
  1108. }
  1109. $this->not_tags = self::removeEmptyValues($this->not_tags);
  1110. if (empty($this->not_tags)) {
  1111. $this->not_tags = null;
  1112. } else {
  1113. $this->not_tags = self::decodeSpaces($this->not_tags);
  1114. }
  1115. return $input;
  1116. }
  1117. /**
  1118. * Parse the search string to find search values.
  1119. * Every word is a distinct search value using a delimiter.
  1120. * Supported delimiters are single quote (') and double quotes (") and regex (/).
  1121. */
  1122. private function parseQuotedSearch(string $input): string {
  1123. $input = self::cleanSearch($input);
  1124. if ($input === '') {
  1125. return '';
  1126. }
  1127. if (preg_match_all('#(?<=[\\s(]|^)(?<![!-\\\\])(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
  1128. $this->search_regex = self::htmlspecialchars_decodes($matches['search']);
  1129. //TODO: Replace all those str_replace with PREG_OFFSET_CAPTURE
  1130. $input = str_replace($matches[0], '', $input);
  1131. }
  1132. if (preg_match_all('/(?<=[\\s(]|^)(?<![!-\\\\])(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  1133. $this->search = $matches['search'];
  1134. //TODO: Replace all those str_replace with PREG_OFFSET_CAPTURE
  1135. $input = str_replace($matches[0], '', $input);
  1136. }
  1137. return $input;
  1138. }
  1139. /**
  1140. * Parse the search string to find search values.
  1141. * Every word is a distinct search value.
  1142. */
  1143. private function parseSearch(string $input): string {
  1144. $input = self::cleanSearch($input);
  1145. if ($input === '') {
  1146. return '';
  1147. }
  1148. if (is_array($this->search)) {
  1149. $this->search = array_merge($this->search, explode(' ', $input));
  1150. } else {
  1151. $this->search = explode(' ', $input);
  1152. }
  1153. return $input;
  1154. }
  1155. private function parseNotSearch(string $input): string {
  1156. $input = self::cleanSearch($input);
  1157. if ($input === '') {
  1158. return '';
  1159. }
  1160. if (preg_match_all('#(?<=[\\s(]|^)[!-](?P<search>(?<!\\\\)/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
  1161. $this->not_search_regex = self::htmlspecialchars_decodes($matches['search']);
  1162. $input = str_replace($matches[0], '', $input);
  1163. }
  1164. if (preg_match_all('/(?<=[\\s(]|^)[!-](?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  1165. $this->not_search = $matches['search'];
  1166. $input = str_replace($matches[0], '', $input);
  1167. }
  1168. $input = self::cleanSearch($input);
  1169. if ($input === '') {
  1170. return '';
  1171. }
  1172. if (preg_match_all('/(?<=[\\s(]|^)[!-](?P<search>[^\\s]+)/', $input, $matches)) {
  1173. $this->not_search = array_merge(is_array($this->not_search) ? $this->not_search : [], $matches['search']);
  1174. $input = str_replace($matches[0], '', $input);
  1175. }
  1176. $this->not_search = self::removeEmptyValues($this->not_search);
  1177. return $input;
  1178. }
  1179. /**
  1180. * Remove all unnecessary spaces in the search
  1181. */
  1182. private static function cleanSearch(string $input): string {
  1183. $input = preg_replace('/\\s+/', ' ', $input);
  1184. if (!is_string($input)) {
  1185. return '';
  1186. }
  1187. return trim($input);
  1188. }
  1189. /** Remove escaping backslashes for parenthesis logic */
  1190. private static function unescape(string $input): string {
  1191. return str_replace(['\\(', '\\)'], ['(', ')'], $input);
  1192. }
  1193. }