Search.php 42 KB

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