Search.php 50 KB

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