Search.php 45 KB

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