Search.php 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  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 (strpbrk($s, ' "\'\\') !== false || $s === '') {
  145. return '"' . addcslashes($s, '\\"') . '"';
  146. }
  147. return $s;
  148. }
  149. private static function dateIntervalToString(int|false|null $min, int|false|null $max): string {
  150. if ($min === false) {
  151. $min = null;
  152. }
  153. if ($max === false) {
  154. $max = null;
  155. }
  156. if ($min === null && $max === null) {
  157. return '';
  158. }
  159. $s = '';
  160. if ($min !== null) {
  161. $s .= date('Y-m-d\\TH:i:s', $min);
  162. }
  163. $s .= '/';
  164. if ($max !== null) {
  165. $s .= date('Y-m-d\\TH:i:s', $max);
  166. }
  167. return $s;
  168. }
  169. /**
  170. * Return true if both searches have the same constraint parameters (even if the values differ), false otherwise.
  171. */
  172. public function hasSameOperators(FreshRSS_Search $search): bool {
  173. $properties = array_keys(get_object_vars($this));
  174. $properties = array_diff($properties, ['raw_input']); // raw_input is not a constraint parameter
  175. foreach ($properties as $property) {
  176. // @phpstan-ignore property.dynamicName, property.dynamicName
  177. if (gettype($this->$property) !== gettype($search->$property)) {
  178. if (str_contains($property, 'min_') || str_contains($property, 'max_')) {
  179. // Process {min_*, max_*} pairs together (for dates)
  180. $mate = str_contains($property, 'min_') ? str_replace('min_', 'max_', $property) : str_replace('max_', 'min_', $property);
  181. // @phpstan-ignore property.dynamicName, property.dynamicName, property.dynamicName, property.dynamicName
  182. if (($this->$property !== null || $this->$mate !== null) !== ($search->$property !== null || $search->$mate !== null)) {
  183. return false;
  184. }
  185. } else {
  186. return false;
  187. }
  188. }
  189. // @phpstan-ignore property.dynamicName, property.dynamicName
  190. if (is_array($this->$property) && is_array($search->$property)) {
  191. // @phpstan-ignore property.dynamicName, property.dynamicName
  192. if (count($this->$property) !== count($search->$property)) {
  193. return false;
  194. }
  195. }
  196. }
  197. return true;
  198. }
  199. /**
  200. * Modifies this search by enforcing the constraint parameters of another search.
  201. * @return FreshRSS_Search a new instance, modified.
  202. */
  203. public function enforce(FreshRSS_Search $search): self {
  204. $result = clone $this;
  205. $properties = array_keys(get_object_vars($result));
  206. $properties = array_diff($properties, ['raw_input']); // raw_input is not a constraint parameter
  207. $result->raw_input = '';
  208. foreach ($properties as $property) {
  209. // @phpstan-ignore property.dynamicName
  210. if ($search->$property !== null) {
  211. // @phpstan-ignore property.dynamicName, property.dynamicName
  212. $result->$property = $search->$property;
  213. if (str_contains($property, 'min_') || str_contains($property, 'max_')) {
  214. // Process {min_*, max_*} pairs together (for dates)
  215. $mate = str_contains($property, 'min_') ? str_replace('min_', 'max_', $property) : str_replace('max_', 'min_', $property);
  216. // @phpstan-ignore property.dynamicName, property.dynamicName
  217. $result->$mate = $search->$mate;
  218. }
  219. }
  220. }
  221. return $result;
  222. }
  223. /**
  224. * Modifies this search by removing the constraints given by another search.
  225. * @return FreshRSS_Search a new instance, modified.
  226. */
  227. public function remove(FreshRSS_Search $search): self {
  228. $result = clone $this;
  229. $properties = array_keys(get_object_vars($result));
  230. $properties = array_diff($properties, ['raw_input']); // raw_input is not a constraint parameter
  231. $result->raw_input = '';
  232. foreach ($properties as $property) {
  233. // @phpstan-ignore property.dynamicName
  234. if ($search->$property !== null) {
  235. // @phpstan-ignore property.dynamicName
  236. $result->$property = null;
  237. if (str_contains($property, 'min_') || str_contains($property, 'max_')) {
  238. // Process {min_*, max_*} pairs together (for dates)
  239. $mate = str_contains($property, 'min_') ? str_replace('min_', 'max_', $property) : str_replace('max_', 'min_', $property);
  240. // @phpstan-ignore property.dynamicName
  241. $result->$mate = null;
  242. }
  243. }
  244. }
  245. return $result;
  246. }
  247. #[\Override]
  248. public function __toString(): string {
  249. $result = '';
  250. if ($this->entry_ids !== null) {
  251. $result .= ' e:' . implode(',', $this->entry_ids);
  252. }
  253. if ($this->feed_ids !== null) {
  254. $result .= ' f:' . implode(',', $this->feed_ids);
  255. }
  256. if ($this->category_ids !== null) {
  257. $result .= ' c:' . implode(',', $this->category_ids);
  258. }
  259. if ($this->label_ids !== null) {
  260. foreach ($this->label_ids as $ids) {
  261. $result .= ' L:' . (is_array($ids) ? implode(',', $ids) : $ids);
  262. }
  263. }
  264. if ($this->label_names !== null) {
  265. foreach ($this->label_names as $names) {
  266. $result .= ' labels:' . self::quote(implode(',', $names));
  267. }
  268. }
  269. if ($this->min_userdate !== null || $this->max_userdate !== null) {
  270. $result .= ' userdate:' . self::dateIntervalToString($this->min_userdate, $this->max_userdate);
  271. }
  272. if ($this->min_pubdate !== null || $this->max_pubdate !== null) {
  273. $result .= ' pubdate:' . self::dateIntervalToString($this->min_pubdate, $this->max_pubdate);
  274. }
  275. if ($this->min_date !== null || $this->max_date !== null) {
  276. $result .= ' date:' . self::dateIntervalToString($this->min_date, $this->max_date);
  277. }
  278. if ($this->intitle_regex !== null) {
  279. foreach ($this->intitle_regex as $s) {
  280. $result .= ' intitle:' . $s;
  281. }
  282. }
  283. if ($this->intitle !== null) {
  284. foreach ($this->intitle as $s) {
  285. $result .= ' intitle:' . self::quote($s);
  286. }
  287. }
  288. if ($this->intext_regex !== null) {
  289. foreach ($this->intext_regex as $s) {
  290. $result .= ' intext:' . $s;
  291. }
  292. }
  293. if ($this->intext !== null) {
  294. foreach ($this->intext as $s) {
  295. $result .= ' intext:' . self::quote($s);
  296. }
  297. }
  298. if ($this->author_regex !== null) {
  299. foreach ($this->author_regex as $s) {
  300. $result .= ' author:' . $s;
  301. }
  302. }
  303. if ($this->author !== null) {
  304. foreach ($this->author as $s) {
  305. $result .= ' author:' . self::quote($s);
  306. }
  307. }
  308. if ($this->inurl_regex !== null) {
  309. foreach ($this->inurl_regex as $s) {
  310. $result .= ' inurl:' . $s;
  311. }
  312. }
  313. if ($this->inurl !== null) {
  314. foreach ($this->inurl as $s) {
  315. $result .= ' inurl:' . self::quote($s);
  316. }
  317. }
  318. if ($this->tags_regex !== null) {
  319. foreach ($this->tags_regex as $s) {
  320. $result .= ' #' . $s;
  321. }
  322. }
  323. if ($this->tags !== null) {
  324. foreach ($this->tags as $s) {
  325. $result .= ' #' . self::quote($s);
  326. }
  327. }
  328. if ($this->search_regex !== null) {
  329. foreach ($this->search_regex as $s) {
  330. $result .= ' ' . $s;
  331. }
  332. }
  333. if ($this->search !== null) {
  334. foreach ($this->search as $s) {
  335. $result .= ' ' . self::quote($s);
  336. }
  337. }
  338. if ($this->not_entry_ids !== null) {
  339. $result .= ' -e:' . implode(',', $this->not_entry_ids);
  340. }
  341. if ($this->not_feed_ids !== null) {
  342. $result .= ' -f:' . implode(',', $this->not_feed_ids);
  343. }
  344. if ($this->not_category_ids !== null) {
  345. $result .= ' -c:' . implode(',', $this->not_category_ids);
  346. }
  347. if ($this->not_label_ids !== null) {
  348. foreach ($this->not_label_ids as $ids) {
  349. $result .= ' -L:' . (is_array($ids) ? implode(',', $ids) : $ids);
  350. }
  351. }
  352. if ($this->not_label_names !== null) {
  353. foreach ($this->not_label_names as $names) {
  354. $result .= ' -labels:' . self::quote(implode(',', $names));
  355. }
  356. }
  357. if ($this->not_min_userdate !== null || $this->not_max_userdate !== null) {
  358. $result .= ' -userdate:' . self::dateIntervalToString($this->not_min_userdate, $this->not_max_userdate);
  359. }
  360. if ($this->not_min_pubdate !== null || $this->not_max_pubdate !== null) {
  361. $result .= ' -pubdate:' . self::dateIntervalToString($this->not_min_pubdate, $this->not_max_pubdate);
  362. }
  363. if ($this->not_min_date !== null || $this->not_max_date !== null) {
  364. $result .= ' -date:' . self::dateIntervalToString($this->not_min_date, $this->not_max_date);
  365. }
  366. if ($this->not_intitle_regex !== null) {
  367. foreach ($this->not_intitle_regex as $s) {
  368. $result .= ' -intitle:' . $s;
  369. }
  370. }
  371. if ($this->not_intitle !== null) {
  372. foreach ($this->not_intitle as $s) {
  373. $result .= ' -intitle:' . self::quote($s);
  374. }
  375. }
  376. if ($this->not_intext_regex !== null) {
  377. foreach ($this->not_intext_regex as $s) {
  378. $result .= ' -intext:' . $s;
  379. }
  380. }
  381. if ($this->not_intext !== null) {
  382. foreach ($this->not_intext as $s) {
  383. $result .= ' -intext:' . self::quote($s);
  384. }
  385. }
  386. if ($this->not_author_regex !== null) {
  387. foreach ($this->not_author_regex as $s) {
  388. $result .= ' -author:' . $s;
  389. }
  390. }
  391. if ($this->not_author !== null) {
  392. foreach ($this->not_author as $s) {
  393. $result .= ' -author:' . self::quote($s);
  394. }
  395. }
  396. if ($this->not_inurl_regex !== null) {
  397. foreach ($this->not_inurl_regex as $s) {
  398. $result .= ' -inurl:' . $s;
  399. }
  400. }
  401. if ($this->not_inurl !== null) {
  402. foreach ($this->not_inurl as $s) {
  403. $result .= ' -inurl:' . self::quote($s);
  404. }
  405. }
  406. if ($this->not_tags_regex !== null) {
  407. foreach ($this->not_tags_regex as $s) {
  408. $result .= ' -#' . $s;
  409. }
  410. }
  411. if ($this->not_tags !== null) {
  412. foreach ($this->not_tags as $s) {
  413. $result .= ' -#' . self::quote($s);
  414. }
  415. }
  416. if ($this->not_search_regex !== null) {
  417. foreach ($this->not_search_regex as $s) {
  418. $result .= ' -' . $s;
  419. }
  420. }
  421. if ($this->not_search !== null) {
  422. foreach ($this->not_search as $s) {
  423. $result .= ' -' . self::quote($s);
  424. }
  425. }
  426. return trim($result);
  427. }
  428. #[Deprecated('Use __tostring() instead')]
  429. public function getRawInput(): string {
  430. return $this->raw_input;
  431. }
  432. /** @return list<string>|null */
  433. public function getEntryIds(): ?array {
  434. return $this->entry_ids;
  435. }
  436. /** @return list<string>|null */
  437. public function getNotEntryIds(): ?array {
  438. return $this->not_entry_ids;
  439. }
  440. /** @return list<int>|null */
  441. public function getFeedIds(): ?array {
  442. return $this->feed_ids;
  443. }
  444. /** @return list<int>|null */
  445. public function getNotFeedIds(): ?array {
  446. return $this->not_feed_ids;
  447. }
  448. /** @return list<int>|null */
  449. public function getCategoryIds(): ?array {
  450. return $this->category_ids;
  451. }
  452. /** @return list<int>|null */
  453. public function getNotCategoryIds(): ?array {
  454. return $this->not_category_ids;
  455. }
  456. /** @return list<list<int>|'*'>|null */
  457. public function getLabelIds(): array|null {
  458. return $this->label_ids;
  459. }
  460. /** @return list<list<int>|'*'>|null */
  461. public function getNotLabelIds(): array|null {
  462. return $this->not_label_ids;
  463. }
  464. /** @return list<list<string>>|null */
  465. public function getLabelNames(bool $plaintext = false): ?array {
  466. return $plaintext ? $this->label_names : Minz_Helper::htmlspecialchars_utf8($this->label_names, ENT_NOQUOTES);
  467. }
  468. /** @return list<list<string>>|null */
  469. public function getNotLabelNames(bool $plaintext = false): ?array {
  470. return $plaintext ? $this->not_label_names : Minz_Helper::htmlspecialchars_utf8($this->not_label_names, ENT_NOQUOTES);
  471. }
  472. /** @return list<string>|null */
  473. public function getIntitle(bool $plaintext = false): ?array {
  474. return $plaintext ? $this->intitle : Minz_Helper::htmlspecialchars_utf8($this->intitle, ENT_NOQUOTES);
  475. }
  476. /** @return list<string>|null */
  477. public function getIntitleRegex(): ?array {
  478. return $this->intitle_regex;
  479. }
  480. /** @return list<string>|null */
  481. public function getNotIntitle(bool $plaintext = false): ?array {
  482. return $plaintext ? $this->not_intitle : Minz_Helper::htmlspecialchars_utf8($this->not_intitle, ENT_NOQUOTES);
  483. }
  484. /** @return list<string>|null */
  485. public function getNotIntitleRegex(): ?array {
  486. return $this->not_intitle_regex;
  487. }
  488. /** @return list<string>|null */
  489. public function getIntext(bool $plaintext = false): ?array {
  490. return $plaintext ? $this->intext : Minz_Helper::htmlspecialchars_utf8($this->intext, ENT_NOQUOTES);
  491. }
  492. /** @return list<string>|null */
  493. public function getIntextRegex(): ?array {
  494. return $this->intext_regex;
  495. }
  496. /** @return list<string>|null */
  497. public function getNotIntext(bool $plaintext = false): ?array {
  498. return $plaintext ? $this->not_intext : Minz_Helper::htmlspecialchars_utf8($this->not_intext, ENT_NOQUOTES);
  499. }
  500. /** @return list<string>|null */
  501. public function getNotIntextRegex(): ?array {
  502. return $this->not_intext_regex;
  503. }
  504. public function getMinDate(): ?int {
  505. return $this->min_date ?: null;
  506. }
  507. public function getNotMinDate(): ?int {
  508. return $this->not_min_date ?: null;
  509. }
  510. public function setMinDate(int $value): void {
  511. $this->min_date = $value;
  512. }
  513. public function getMaxDate(): ?int {
  514. return $this->max_date ?: null;
  515. }
  516. public function getNotMaxDate(): ?int {
  517. return $this->not_max_date ?: null;
  518. }
  519. public function setMaxDate(int $value): void {
  520. $this->max_date = $value;
  521. }
  522. public function getMinPubdate(): ?int {
  523. return $this->min_pubdate ?: null;
  524. }
  525. public function getNotMinPubdate(): ?int {
  526. return $this->not_min_pubdate ?: null;
  527. }
  528. public function getMaxPubdate(): ?int {
  529. return $this->max_pubdate ?: null;
  530. }
  531. public function getNotMaxPubdate(): ?int {
  532. return $this->not_max_pubdate ?: null;
  533. }
  534. public function setMaxPubdate(int $value): void {
  535. $this->max_pubdate = $value;
  536. }
  537. public function getMinUserdate(): ?int {
  538. return $this->min_userdate ?: null;
  539. }
  540. public function getNotMinUserdate(): ?int {
  541. return $this->not_min_userdate ?: null;
  542. }
  543. public function getMaxUserdate(): ?int {
  544. return $this->max_userdate ?: null;
  545. }
  546. public function getNotMaxUserdate(): ?int {
  547. return $this->not_max_userdate ?: null;
  548. }
  549. /** @return list<string>|null */
  550. public function getInurl(bool $plaintext = false): ?array {
  551. return $plaintext ? $this->inurl : Minz_Helper::htmlspecialchars_utf8($this->inurl, ENT_NOQUOTES);
  552. }
  553. /** @return list<string>|null */
  554. public function getInurlRegex(): ?array {
  555. return $this->inurl_regex;
  556. }
  557. /** @return list<string>|null */
  558. public function getNotInurl(bool $plaintext = false): ?array {
  559. return $plaintext ? $this->not_inurl : Minz_Helper::htmlspecialchars_utf8($this->not_inurl, ENT_NOQUOTES);
  560. }
  561. /** @return list<string>|null */
  562. public function getNotInurlRegex(): ?array {
  563. return $this->not_inurl_regex;
  564. }
  565. /** @return list<string>|null */
  566. public function getAuthor(bool $plaintext = false): ?array {
  567. return $plaintext ? $this->author : Minz_Helper::htmlspecialchars_utf8($this->author, ENT_NOQUOTES);
  568. }
  569. /** @return list<string>|null */
  570. public function getAuthorRegex(): ?array {
  571. return $this->author_regex;
  572. }
  573. /** @return list<string>|null */
  574. public function getNotAuthor(bool $plaintext = false): ?array {
  575. return $plaintext ? $this->not_author : Minz_Helper::htmlspecialchars_utf8($this->not_author, ENT_NOQUOTES);
  576. }
  577. /** @return list<string>|null */
  578. public function getNotAuthorRegex(): ?array {
  579. return $this->not_author_regex;
  580. }
  581. /** @return list<string>|null */
  582. public function getTags(bool $plaintext = false): ?array {
  583. return $plaintext ? $this->tags : Minz_Helper::htmlspecialchars_utf8($this->tags, ENT_NOQUOTES);
  584. }
  585. /** @return list<string>|null */
  586. public function getTagsRegex(): ?array {
  587. return $this->tags_regex;
  588. }
  589. /** @return list<string>|null */
  590. public function getNotTags(bool $plaintext = false): ?array {
  591. return $plaintext ? $this->not_tags : Minz_Helper::htmlspecialchars_utf8($this->not_tags, ENT_NOQUOTES);
  592. }
  593. /** @return list<string>|null */
  594. public function getNotTagsRegex(): ?array {
  595. return $this->not_tags_regex;
  596. }
  597. /** @return list<string>|null */
  598. public function getSearch(bool $plaintext = false): ?array {
  599. return $plaintext ? $this->search : Minz_Helper::htmlspecialchars_utf8($this->search, ENT_NOQUOTES);
  600. }
  601. /** @return list<string>|null */
  602. public function getSearchRegex(): ?array {
  603. return $this->search_regex;
  604. }
  605. /** @return list<string>|null */
  606. public function getNotSearch(bool $plaintext = false): ?array {
  607. return $plaintext ? $this->not_search : Minz_Helper::htmlspecialchars_utf8($this->not_search, ENT_NOQUOTES);
  608. }
  609. /** @return list<string>|null */
  610. public function getNotSearchRegex(): ?array {
  611. return $this->not_search_regex;
  612. }
  613. /**
  614. * @param list<string>|null $anArray
  615. * @return list<string>
  616. */
  617. private static function removeEmptyValues(?array $anArray): array {
  618. return empty($anArray) ? [] : array_values(array_filter($anArray, static fn(string $value) => $value !== ''));
  619. }
  620. /**
  621. * @param list<string>|string $value
  622. * @return ($value is string ? string : list<string>)
  623. */
  624. private static function decodeSpaces(array|string $value): array|string {
  625. if (is_array($value)) {
  626. foreach ($value as &$val) {
  627. $val = self::decodeSpaces($val);
  628. }
  629. } else {
  630. $value = trim(str_replace('+', ' ', $value));
  631. }
  632. // @phpstan-ignore return.type
  633. return $value;
  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 = $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 = $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 = $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 = $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 = $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 = $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 = $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 = $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 = $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 = $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 = $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 = $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. }