1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
|
<?php
require_once(LIB_PATH . '/lib_date.php');
/**
* Contains a search from the search form.
*
* It allows to extract meaningful bits of the search and store them in a
* convenient object
*/
class FreshRSS_Search {
/**
* This contains the user input string
*/
private string $raw_input = '';
// The following properties are extracted from the raw input
/** @var array<string>|null */
private ?array $entry_ids = null;
/** @var array<int>|null */
private ?array $feed_ids = null;
/** @var array<int>|'*'|null */
private $label_ids = null;
/** @var array<string>|null */
private ?array $label_names = null;
/** @var array<string>|null */
private ?array $intitle = null;
/** @var int|false|null */
private $min_date = null;
/** @var int|false|null */
private $max_date = null;
/** @var int|false|null */
private $min_pubdate = null;
/** @var int|false|null */
private $max_pubdate = null;
/** @var array<string>|null */
private ?array $inurl = null;
/** @var array<string>|null */
private ?array $author = null;
/** @var array<string>|null */
private ?array $tags = null;
/** @var array<string>|null */
private ?array $search = null;
/** @var array<string>|null */
private ?array $not_entry_ids = null;
/** @var array<int>|null */
private ?array $not_feed_ids = null;
/** @var array<int>|'*'|null */
private $not_label_ids = null;
/** @var array<string>|null */
private ?array $not_label_names = null;
/** @var array<string>|null */
private ?array $not_intitle = null;
/** @var int|false|null */
private $not_min_date = null;
/** @var int|false|null */
private $not_max_date = null;
/** @var int|false|null */
private $not_min_pubdate = null;
/** @var int|false|null */
private $not_max_pubdate = null;
/** @var array<string>|null */
private ?array $not_inurl = null;
/** @var array<string>|null */
private ?array $not_author = null;
/** @var array<string>|null */
private ?array $not_tags = null;
/** @var array<string>|null */
private ?array $not_search = null;
public function __construct(string $input) {
$input = self::cleanSearch($input);
$input = self::unescape($input);
$this->raw_input = $input;
$input = $this->parseNotEntryIds($input);
$input = $this->parseNotFeedIds($input);
$input = $this->parseNotLabelIds($input);
$input = $this->parseNotLabelNames($input);
$input = $this->parseNotPubdateSearch($input);
$input = $this->parseNotDateSearch($input);
$input = $this->parseNotIntitleSearch($input);
$input = $this->parseNotAuthorSearch($input);
$input = $this->parseNotInurlSearch($input);
$input = $this->parseNotTagsSearch($input);
$input = $this->parseEntryIds($input);
$input = $this->parseFeedIds($input);
$input = $this->parseLabelIds($input);
$input = $this->parseLabelNames($input);
$input = $this->parsePubdateSearch($input);
$input = $this->parseDateSearch($input);
$input = $this->parseIntitleSearch($input);
$input = $this->parseAuthorSearch($input);
$input = $this->parseInurlSearch($input);
$input = $this->parseTagsSearch($input);
$input = $this->parseQuotedSearch($input);
$input = $this->parseNotSearch($input);
$this->parseSearch($input);
}
public function __toString(): string {
return $this->getRawInput();
}
public function getRawInput(): string {
return $this->raw_input;
}
/** @return array<string>|null */
public function getEntryIds(): ?array {
return $this->entry_ids;
}
/** @return array<string>|null */
public function getNotEntryIds(): ?array {
return $this->not_entry_ids;
}
/** @return array<int>|null */
public function getFeedIds(): ?array {
return $this->feed_ids;
}
/** @return array<int>|null */
public function getNotFeedIds(): ?array {
return $this->not_feed_ids;
}
/** @return array<int>|'*'|null */
public function getLabelIds() {
return $this->label_ids;
}
/** @return array<int>|'*'|null */
public function getNotLabelIds() {
return $this->not_label_ids;
}
/** @return array<string>|null */
public function getLabelNames(): ?array {
return $this->label_names;
}
/** @return array<string>|null */
public function getNotLabelNames(): ?array {
return $this->not_label_names;
}
/** @return array<string>|null */
public function getIntitle(): ?array {
return $this->intitle;
}
/** @return array<string>|null */
public function getNotIntitle(): ?array {
return $this->not_intitle;
}
public function getMinDate(): ?int {
return $this->min_date ?: null;
}
public function getNotMinDate(): ?int {
return $this->not_min_date ?: null;
}
public function setMinDate(int $value): void {
$this->min_date = $value;
}
public function getMaxDate(): ?int {
return $this->max_date ?: null;
}
public function getNotMaxDate(): ?int {
return $this->not_max_date ?: null;
}
public function setMaxDate(int $value): void {
$this->max_date = $value;
}
public function getMinPubdate(): ?int {
return $this->min_pubdate ?: null;
}
public function getNotMinPubdate(): ?int {
return $this->not_min_pubdate ?: null;
}
public function getMaxPubdate(): ?int {
return $this->max_pubdate ?: null;
}
public function getNotMaxPubdate(): ?int {
return $this->not_max_pubdate ?: null;
}
/** @return array<string>|null */
public function getInurl(): ?array {
return $this->inurl;
}
/** @return array<string>|null */
public function getNotInurl(): ?array {
return $this->not_inurl;
}
/** @return array<string>|null */
public function getAuthor(): ?array {
return $this->author;
}
/** @return array<string>|null */
public function getNotAuthor(): ?array {
return $this->not_author;
}
/** @return array<string>|null */
public function getTags(): ?array {
return $this->tags;
}
/** @return array<string>|null */
public function getNotTags(): ?array {
return $this->not_tags;
}
/** @return array<string>|null */
public function getSearch(): ?array {
return $this->search;
}
/** @return array<string>|null */
public function getNotSearch(): ?array {
return $this->not_search;
}
/**
* @param array<string>|null $anArray
* @return array<string>
*/
private static function removeEmptyValues(?array $anArray): array {
return empty($anArray) ? [] : array_filter($anArray, static function(string $value) {
return $value !== '';
});
}
/**
* @param array<string>|string $value
* @return ($value is array ? array<string> : string)
*/
private static function decodeSpaces($value) {
if (is_array($value)) {
for ($i = count($value) - 1; $i >= 0; $i--) {
$value[$i] = self::decodeSpaces($value[$i]);
}
} else {
$value = trim(str_replace('+', ' ', $value));
}
return $value;
}
/**
* Parse the search string to find entry (article) IDs.
*/
private function parseEntryIds(string $input): string {
if (preg_match_all('/\be:(?P<search>[0-9,]*)/', $input, $matches)) {
$input = str_replace($matches[0], '', $input);
$ids_lists = $matches['search'];
$this->entry_ids = [];
foreach ($ids_lists as $ids_list) {
$entry_ids = explode(',', $ids_list);
$entry_ids = self::removeEmptyValues($entry_ids);
if (!empty($entry_ids)) {
$this->entry_ids = array_merge($this->entry_ids, $entry_ids);
}
}
}
return $input;
}
private function parseNotEntryIds(string $input): string {
if (preg_match_all('/(?<=\s|^)[!-]e:(?P<search>[0-9,]*)/', $input, $matches)) {
$input = str_replace($matches[0], '', $input);
$ids_lists = $matches['search'];
$this->not_entry_ids = [];
foreach ($ids_lists as $ids_list) {
$entry_ids = explode(',', $ids_list);
$entry_ids = self::removeEmptyValues($entry_ids);
if (!empty($entry_ids)) {
$this->not_entry_ids = array_merge($this->not_entry_ids, $entry_ids);
}
}
}
return $input;
}
private function parseFeedIds(string $input): string {
if (preg_match_all('/\bf:(?P<search>[0-9,]*)/', $input, $matches)) {
$input = str_replace($matches[0], '', $input);
$ids_lists = $matches['search'];
$this->feed_ids = [];
foreach ($ids_lists as $ids_list) {
$feed_ids = explode(',', $ids_list);
$feed_ids = self::removeEmptyValues($feed_ids);
/** @var array<int> $feed_ids */
$feed_ids = array_map('intval', $feed_ids);
if (!empty($feed_ids)) {
$this->feed_ids = array_merge($this->feed_ids, $feed_ids);
}
}
}
return $input;
}
private function parseNotFeedIds(string $input): string {
if (preg_match_all('/(?<=\s|^)[!-]f:(?P<search>[0-9,]*)/', $input, $matches)) {
$input = str_replace($matches[0], '', $input);
$ids_lists = $matches['search'];
$this->not_feed_ids = [];
foreach ($ids_lists as $ids_list) {
$feed_ids = explode(',', $ids_list);
$feed_ids = self::removeEmptyValues($feed_ids);
/** @var array<int> $feed_ids */
$feed_ids = array_map('intval', $feed_ids);
if (!empty($feed_ids)) {
$this->not_feed_ids = array_merge($this->not_feed_ids, $feed_ids);
}
}
}
return $input;
}
/**
* Parse the search string to find tags (labels) IDs.
*/
private function parseLabelIds(string $input): string {
if (preg_match_all('/\b[lL]:(?P<search>[0-9,]+|[*])/', $input, $matches)) {
$input = str_replace($matches[0], '', $input);
$ids_lists = $matches['search'];
$this->label_ids = [];
foreach ($ids_lists as $ids_list) {
if ($ids_list === '*') {
$this->label_ids = '*';
break;
}
$label_ids = explode(',', $ids_list);
$label_ids = self::removeEmptyValues($label_ids);
/** @var array<int> $label_ids */
$label_ids = array_map('intval', $label_ids);
if (!empty($label_ids)) {
$this->label_ids = array_merge($this->label_ids, $label_ids);
}
}
}
return $input;
}
private function parseNotLabelIds(string $input): string {
if (preg_match_all('/(?<=\s|^)[!-][lL]:(?P<search>[0-9,]+|[*])/', $input, $matches)) {
$input = str_replace($matches[0], '', $input);
$ids_lists = $matches['search'];
$this->not_label_ids = [];
foreach ($ids_lists as $ids_list) {
if ($ids_list === '*') {
$this->not_label_ids = '*';
break;
}
$label_ids = explode(',', $ids_list);
$label_ids = self::removeEmptyValues($label_ids);
/** @var array<int> $label_ids */
$label_ids = array_map('intval', $label_ids);
if (!empty($label_ids)) {
$this->not_label_ids = array_merge($this->not_label_ids, $label_ids);
}
}
}
return $input;
}
/**
* Parse the search string to find tags (labels) names.
*/
private function parseLabelNames(string $input): string {
$names_lists = [];
if (preg_match_all('/\blabels?:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
$names_lists = $matches['search'];
$input = str_replace($matches[0], '', $input);
}
if (preg_match_all('/\blabels?:(?P<search>[^\s"]*)/', $input, $matches)) {
$names_lists = array_merge($names_lists, $matches['search']);
$input = str_replace($matches[0], '', $input);
}
if (!empty($names_lists)) {
$this->label_names = [];
foreach ($names_lists as $names_list) {
$names_array = explode(',', $names_list);
$names_array = self::removeEmptyValues($names_array);
if (!empty($names_array)) {
$this->label_names = array_merge($this->label_names, $names_array);
}
}
}
return $input;
}
/**
* Parse the search string to find tags (labels) names to exclude.
*/
private function parseNotLabelNames(string $input): string {
$names_lists = [];
if (preg_match_all('/(?<=\s|^)[!-]labels?:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
$names_lists = $matches['search'];
$input = str_replace($matches[0], '', $input);
}
if (preg_match_all('/(?<=\s|^)[!-]labels?:(?P<search>[^\s"]*)/', $input, $matches)) {
$names_lists = array_merge($names_lists, $matches['search']);
$input = str_replace($matches[0], '', $input);
}
if (!empty($names_lists)) {
$this->not_label_names = [];
foreach ($names_lists as $names_list) {
$names_array = explode(',', $names_list);
$names_array = self::removeEmptyValues($names_array);
if (!empty($names_array)) {
$this->not_label_names = array_merge($this->not_label_names, $names_array);
}
}
}
return $input;
}
/**
* Parse the search string to find intitle keyword and the search related to it.
* The search is the first word following the keyword.
*/
private function parseIntitleSearch(string $input): string {
if (preg_match_all('/\bintitle:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
$this->intitle = $matches['search'];
$input = str_replace($matches[0], '', $input);
}
if (preg_match_all('/\bintitle:(?P<search>[^\s"]*)/', $input, $matches)) {
$this->intitle = array_merge($this->intitle ?: [], $matches['search']);
$input = str_replace($matches[0], '', $input);
}
$this->intitle = self::removeEmptyValues($this->intitle);
if (empty($this->intitle)) {
$this->intitle = null;
}
return $input;
}
private function parseNotIntitleSearch(string $input): string {
if (preg_match_all('/(?<=\s|^)[!-]intitle:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
$this->not_intitle = $matches['search'];
$input = str_replace($matches[0], '', $input);
}
if (preg_match_all('/(?<=\s|^)[!-]intitle:(?P<search>[^\s"]*)/', $input, $matches)) {
$this->not_intitle = array_merge($this->not_intitle ?: [], $matches['search']);
$input = str_replace($matches[0], '', $input);
}
$this->not_intitle = self::removeEmptyValues($this->not_intitle);
if (empty($this->not_intitle)) {
$this->not_intitle = null;
}
return $input;
}
/**
* Parse the search string to find author keyword and the search related to it.
* The search is the first word following the keyword except when using
* a delimiter. Supported delimiters are single quote (') and double quotes (").
*/
private function parseAuthorSearch(string $input): string {
if (preg_match_all('/\bauthor:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
$this->author = $matches['search'];
$input = str_replace($matches[0], '', $input);
}
if (preg_match_all('/\bauthor:(?P<search>[^\s"]*)/', $input, $matches)) {
$this->author = array_merge($this->author ?: [], $matches['search']);
$input = str_replace($matches[0], '', $input);
}
$this->author = self::removeEmptyValues($this->author);
if (empty($this->author)) {
$this->author = null;
}
return $input;
}
private function parseNotAuthorSearch(string $input): string {
if (preg_match_all('/(?<=\s|^)[!-]author:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
$this->not_author = $matches['search'];
$input = str_replace($matches[0], '', $input);
}
if (preg_match_all('/(?<=\s|^)[!-]author:(?P<search>[^\s"]*)/', $input, $matches)) {
$this->not_author = array_merge($this->not_author ?: [], $matches['search']);
$input = str_replace($matches[0], '', $input);
}
$this->not_author = self::removeEmptyValues($this->not_author);
if (empty($this->not_author)) {
$this->not_author = null;
}
return $input;
}
/**
* Parse the search string to find inurl keyword and the search related to it.
* The search is the first word following the keyword.
*/
private function parseInurlSearch(string $input): string {
if (preg_match_all('/\binurl:(?P<search>[^\s]*)/', $input, $matches)) {
$this->inurl = $matches['search'];
$input = str_replace($matches[0], '', $input);
$this->inurl = self::removeEmptyValues($this->inurl);
}
return $input;
}
private function parseNotInurlSearch(string $input): string {
if (preg_match_all('/(?<=\s|^)[!-]inurl:(?P<search>[^\s]*)/', $input, $matches)) {
$this->not_inurl = $matches['search'];
$input = str_replace($matches[0], '', $input);
$this->not_inurl = self::removeEmptyValues($this->not_inurl);
}
return $input;
}
/**
* Parse the search string to find date keyword and the search related to it.
* The search is the first word following the keyword.
*/
private function parseDateSearch(string $input): string {
if (preg_match_all('/\bdate:(?P<search>[^\s]*)/', $input, $matches)) {
$input = str_replace($matches[0], '', $input);
$dates = self::removeEmptyValues($matches['search']);
if (!empty($dates[0])) {
[$this->min_date, $this->max_date] = parseDateInterval($dates[0]);
}
}
return $input;
}
private function parseNotDateSearch(string $input): string {
if (preg_match_all('/(?<=\s|^)[!-]date:(?P<search>[^\s]*)/', $input, $matches)) {
$input = str_replace($matches[0], '', $input);
$dates = self::removeEmptyValues($matches['search']);
if (!empty($dates[0])) {
[$this->not_min_date, $this->not_max_date] = parseDateInterval($dates[0]);
}
}
return $input;
}
/**
* Parse the search string to find pubdate keyword and the search related to it.
* The search is the first word following the keyword.
*/
private function parsePubdateSearch(string $input): string {
if (preg_match_all('/\bpubdate:(?P<search>[^\s]*)/', $input, $matches)) {
$input = str_replace($matches[0], '', $input);
$dates = self::removeEmptyValues($matches['search']);
if (!empty($dates[0])) {
[$this->min_pubdate, $this->max_pubdate] = parseDateInterval($dates[0]);
}
}
return $input;
}
private function parseNotPubdateSearch(string $input): string {
if (preg_match_all('/(?<=\s|^)[!-]pubdate:(?P<search>[^\s]*)/', $input, $matches)) {
$input = str_replace($matches[0], '', $input);
$dates = self::removeEmptyValues($matches['search']);
if (!empty($dates[0])) {
[$this->not_min_pubdate, $this->not_max_pubdate] = parseDateInterval($dates[0]);
}
}
return $input;
}
/**
* Parse the search string to find tags keyword (# followed by a word)
* and the search related to it.
* The search is the first word following the #.
*/
private function parseTagsSearch(string $input): string {
if (preg_match_all('/#(?P<search>[^\s]+)/', $input, $matches)) {
$this->tags = $matches['search'];
$input = str_replace($matches[0], '', $input);
$this->tags = self::removeEmptyValues($this->tags);
$this->tags = self::decodeSpaces($this->tags);
}
return $input;
}
private function parseNotTagsSearch(string $input): string {
if (preg_match_all('/(?<=\s|^)[!-]#(?P<search>[^\s]+)/', $input, $matches)) {
$this->not_tags = $matches['search'];
$input = str_replace($matches[0], '', $input);
$this->not_tags = self::removeEmptyValues($this->not_tags);
$this->not_tags = self::decodeSpaces($this->not_tags);
}
return $input;
}
/**
* Parse the search string to find search values.
* Every word is a distinct search value using a delimiter.
* Supported delimiters are single quote (') and double quotes (").
*/
private function parseQuotedSearch(string $input): string {
$input = self::cleanSearch($input);
if ($input === '') {
return '';
}
if (preg_match_all('/(?<![!-])(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
$this->search = $matches['search'];
//TODO: Replace all those str_replace with PREG_OFFSET_CAPTURE
$input = str_replace($matches[0], '', $input);
}
return $input;
}
/**
* Parse the search string to find search values.
* Every word is a distinct search value.
*/
private function parseSearch(string $input): string {
$input = self::cleanSearch($input);
if ($input === '') {
return '';
}
if (is_array($this->search)) {
$this->search = array_merge($this->search, explode(' ', $input));
} else {
$this->search = explode(' ', $input);
}
return $input;
}
private function parseNotSearch(string $input): string {
$input = self::cleanSearch($input);
if ($input === '') {
return '';
}
if (preg_match_all('/(?<=\s|^)[!-](?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
$this->not_search = $matches['search'];
$input = str_replace($matches[0], '', $input);
}
$input = self::cleanSearch($input);
if ($input === '') {
return '';
}
if (preg_match_all('/(?<=\s|^)[!-](?P<search>[^\s]+)/', $input, $matches)) {
$this->not_search = array_merge(is_array($this->not_search) ? $this->not_search : [], $matches['search']);
$input = str_replace($matches[0], '', $input);
}
$this->not_search = self::removeEmptyValues($this->not_search);
return $input;
}
/**
* Remove all unnecessary spaces in the search
*/
private static function cleanSearch(string $input): string {
$input = preg_replace('/\s+/', ' ', $input);
if (!is_string($input)) {
return '';
}
return trim($input);
}
/** Remove escaping backslashes for parenthesis logic */
private static function unescape(string $input): string {
return str_replace(['\\(', '\\)'], ['(', ')'], $input);
}
}
|