| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271 |
- <?php
- declare(strict_types=1);
- 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 implements \Stringable {
- /**
- * This contains the user input string
- */
- private string $raw_input = '';
- // The following properties are extracted from the raw input
- /** @var list<string>|null */
- private ?array $entry_ids = null;
- /** @var list<int>|null */
- private ?array $feed_ids = null;
- /** @var list<int>|null */
- private ?array $category_ids = null;
- /** @var list<list<int>|'*'>|null */
- private $label_ids = null;
- /** @var list<list<string>>|null */
- private ?array $label_names = null;
- /** @var list<string>|null */
- private ?array $intitle = null;
- /** @var list<string>|null */
- private ?array $intitle_regex = null;
- /** @var list<string>|null */
- private ?array $intext = null;
- /** @var list<string>|null */
- private ?array $intext_regex = 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 int|false|null */
- private $min_userdate = null;
- /** @var int|false|null */
- private $max_userdate = null;
- /** @var list<string>|null */
- private ?array $inurl = null;
- /** @var list<string>|null */
- private ?array $inurl_regex = null;
- /** @var list<string>|null */
- private ?array $author = null;
- /** @var list<string>|null */
- private ?array $author_regex = null;
- /** @var list<string>|null */
- private ?array $tags = null;
- /** @var list<string>|null */
- private ?array $tags_regex = null;
- /** @var list<string>|null */
- private ?array $search = null;
- /** @var list<string>|null */
- private ?array $search_regex = null;
- /** @var list<string>|null */
- private ?array $not_entry_ids = null;
- /** @var list<int>|null */
- private ?array $not_feed_ids = null;
- /** @var list<int>|null */
- private ?array $not_category_ids = null;
- /** @var list<list<int>|'*'>|null */
- private $not_label_ids = null;
- /** @var list<list<string>>|null */
- private ?array $not_label_names = null;
- /** @var list<string>|null */
- private ?array $not_intitle = null;
- /** @var list<string>|null */
- private ?array $not_intitle_regex = null;
- /** @var list<string>|null */
- private ?array $not_intext = null;
- /** @var list<string>|null */
- private ?array $not_intext_regex = 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 int|false|null */
- private $not_min_userdate = null;
- /** @var int|false|null */
- private $not_max_userdate = null;
- /** @var list<string>|null */
- private ?array $not_inurl = null;
- /** @var list<string>|null */
- private ?array $not_inurl_regex = null;
- /** @var list<string>|null */
- private ?array $not_author = null;
- /** @var list<string>|null */
- private ?array $not_author_regex = null;
- /** @var list<string>|null */
- private ?array $not_tags = null;
- /** @var list<string>|null */
- private ?array $not_tags_regex = null;
- /** @var list<string>|null */
- private ?array $not_search = null;
- /** @var list<string>|null */
- private ?array $not_search_regex = null;
- public function __construct(string $input) {
- $input = self::cleanSearch($input);
- $input = self::unescape($input);
- $input = FreshRSS_BooleanSearch::unescapeLiteralParentheses($input);
- $this->raw_input = $input;
- $input = $this->parseNotEntryIds($input);
- $input = $this->parseNotFeedIds($input);
- $input = $this->parseNotCategoryIds($input);
- $input = $this->parseNotLabelIds($input);
- $input = $this->parseNotLabelNames($input);
- $input = $this->parseNotUserdateSearch($input);
- $input = $this->parseNotPubdateSearch($input);
- $input = $this->parseNotDateSearch($input);
- $input = $this->parseNotIntitleSearch($input);
- $input = $this->parseNotIntextSearch($input);
- $input = $this->parseNotAuthorSearch($input);
- $input = $this->parseNotInurlSearch($input);
- $input = $this->parseNotTagsSearch($input);
- $input = $this->parseEntryIds($input);
- $input = $this->parseFeedIds($input);
- $input = $this->parseCategoryIds($input);
- $input = $this->parseLabelIds($input);
- $input = $this->parseLabelNames($input);
- $input = $this->parseUserdateSearch($input);
- $input = $this->parsePubdateSearch($input);
- $input = $this->parseDateSearch($input);
- $input = $this->parseIntitleSearch($input);
- $input = $this->parseIntextSearch($input);
- $input = $this->parseAuthorSearch($input);
- $input = $this->parseInurlSearch($input);
- $input = $this->parseTagsSearch($input);
- $input = $this->parseQuotedSearch($input);
- $input = $this->parseNotSearch($input);
- $this->parseSearch($input);
- }
- private static function quote(string $s): string {
- if (str_contains($s, ' ') || $s === '') {
- return '"' . addcslashes($s, '\\"') . '"';
- }
- return $s;
- }
- private static function dateIntervalToString(?int $min, ?int $max): string {
- if ($min === null && $max === null) {
- return '';
- }
- $s = '';
- if ($min !== null) {
- $s .= date('Y-m-d\\TH:i:s', $min);
- }
- $s .= '/';
- if ($max !== null) {
- $s .= date('Y-m-d\\TH:i:s', $max);
- }
- return $s;
- }
- /**
- * Return true if both searches have the same constraint parameters (even if the values differ), false otherwise.
- */
- public function hasSameOperators(FreshRSS_Search $search): bool {
- $properties = array_keys(get_object_vars($this));
- $properties = array_diff($properties, ['raw_input']); // raw_input is not a constraint parameter
- foreach ($properties as $property) {
- // @phpstan-ignore property.dynamicName, property.dynamicName
- if (gettype($this->$property) !== gettype($search->$property)) {
- if (str_contains($property, 'min_') || str_contains($property, 'max_')) {
- // Process {min_*, max_*} pairs together (for dates)
- $mate = str_contains($property, 'min_') ? str_replace('min_', 'max_', $property) : str_replace('max_', 'min_', $property);
- // @phpstan-ignore property.dynamicName, property.dynamicName, property.dynamicName, property.dynamicName
- if (($this->$property !== null || $this->$mate !== null) !== ($search->$property !== null || $search->$mate !== null)) {
- return false;
- }
- } else {
- return false;
- }
- }
- // @phpstan-ignore property.dynamicName, property.dynamicName
- if (is_array($this->$property) && is_array($search->$property)) {
- // @phpstan-ignore property.dynamicName, property.dynamicName
- if (count($this->$property) !== count($search->$property)) {
- return false;
- }
- }
- }
- return true;
- }
- /**
- * Modifies this search by enforcing the constraint parameters of another search.
- * @return FreshRSS_Search a new instance, modified.
- */
- public function enforce(FreshRSS_Search $search): self {
- $result = clone $this;
- $properties = array_keys(get_object_vars($result));
- $properties = array_diff($properties, ['raw_input']); // raw_input is not a constraint parameter
- $result->raw_input = '';
- foreach ($properties as $property) {
- // @phpstan-ignore property.dynamicName
- if ($search->$property !== null) {
- // @phpstan-ignore property.dynamicName, property.dynamicName
- $result->$property = $search->$property;
- if (str_contains($property, 'min_') || str_contains($property, 'max_')) {
- // Process {min_*, max_*} pairs together (for dates)
- $mate = str_contains($property, 'min_') ? str_replace('min_', 'max_', $property) : str_replace('max_', 'min_', $property);
- // @phpstan-ignore property.dynamicName, property.dynamicName
- $result->$mate = $search->$mate;
- }
- }
- }
- return $result;
- }
- /**
- * Modifies this search by removing the constraints given by another search.
- * @return FreshRSS_Search a new instance, modified.
- */
- public function remove(FreshRSS_Search $search): self {
- $result = clone $this;
- $properties = array_keys(get_object_vars($result));
- $properties = array_diff($properties, ['raw_input']); // raw_input is not a constraint parameter
- $result->raw_input = '';
- foreach ($properties as $property) {
- // @phpstan-ignore property.dynamicName
- if ($search->$property !== null) {
- // @phpstan-ignore property.dynamicName
- $result->$property = null;
- if (str_contains($property, 'min_') || str_contains($property, 'max_')) {
- // Process {min_*, max_*} pairs together (for dates)
- $mate = str_contains($property, 'min_') ? str_replace('min_', 'max_', $property) : str_replace('max_', 'min_', $property);
- // @phpstan-ignore property.dynamicName
- $result->$mate = null;
- }
- }
- }
- return $result;
- }
- #[\Override]
- public function __toString(): string {
- $result = '';
- if ($this->getEntryIds() !== null) {
- $result .= ' e:' . implode(',', $this->getEntryIds());
- }
- if ($this->getFeedIds() !== null) {
- $result .= ' f:' . implode(',', $this->getFeedIds());
- }
- if ($this->getCategoryIds() !== null) {
- $result .= ' c:' . implode(',', $this->getCategoryIds());
- }
- if ($this->getLabelIds() !== null) {
- foreach ($this->getLabelIds() as $ids) {
- $result .= ' L:' . (is_array($ids) ? implode(',', $ids) : $ids);
- }
- }
- if ($this->getLabelNames() !== null) {
- foreach ($this->getLabelNames() as $names) {
- $result .= ' labels:' . self::quote(implode(',', $names));
- }
- }
- if ($this->getMinUserdate() !== null || $this->getMaxUserdate() !== null) {
- $result .= ' userdate:' . self::dateIntervalToString($this->getMinUserdate(), $this->getMaxUserdate());
- }
- if ($this->getMinPubdate() !== null || $this->getMaxPubdate() !== null) {
- $result .= ' pubdate:' . self::dateIntervalToString($this->getMinPubdate(), $this->getMaxPubdate());
- }
- if ($this->getMinDate() !== null || $this->getMaxDate() !== null) {
- $result .= ' date:' . self::dateIntervalToString($this->getMinDate(), $this->getMaxDate());
- }
- if ($this->getIntitleRegex() !== null) {
- foreach ($this->getIntitleRegex() as $s) {
- $result .= ' intitle:' . $s;
- }
- }
- if ($this->getIntitle() !== null) {
- foreach ($this->getIntitle() as $s) {
- $result .= ' intitle:' . self::quote($s);
- }
- }
- if ($this->getIntextRegex() !== null) {
- foreach ($this->getIntextRegex() as $s) {
- $result .= ' intext:' . $s;
- }
- }
- if ($this->getIntext() !== null) {
- foreach ($this->getIntext() as $s) {
- $result .= ' intext:' . self::quote($s);
- }
- }
- if ($this->getAuthorRegex() !== null) {
- foreach ($this->getAuthorRegex() as $s) {
- $result .= ' author:' . $s;
- }
- }
- if ($this->getAuthor() !== null) {
- foreach ($this->getAuthor() as $s) {
- $result .= ' author:' . self::quote($s);
- }
- }
- if ($this->getInurlRegex() !== null) {
- foreach ($this->getInurlRegex() as $s) {
- $result .= ' inurl:' . $s;
- }
- }
- if ($this->getInurl() !== null) {
- foreach ($this->getInurl() as $s) {
- $result .= ' inurl:' . self::quote($s);
- }
- }
- if ($this->getTagsRegex() !== null) {
- foreach ($this->getTagsRegex() as $s) {
- $result .= ' #' . $s;
- }
- }
- if ($this->getTags() !== null) {
- foreach ($this->getTags() as $s) {
- $result .= ' #' . self::quote($s);
- }
- }
- if ($this->getSearchRegex() !== null) {
- foreach ($this->getSearchRegex() as $s) {
- $result .= ' ' . $s;
- }
- }
- if ($this->getSearch() !== null) {
- foreach ($this->getSearch() as $s) {
- $result .= ' ' . self::quote($s);
- }
- }
- if ($this->getNotEntryIds() !== null) {
- $result .= ' -e:' . implode(',', $this->getNotEntryIds());
- }
- if ($this->getNotFeedIds() !== null) {
- $result .= ' -f:' . implode(',', $this->getNotFeedIds());
- }
- if ($this->getNotCategoryIds() !== null) {
- $result .= ' -c:' . implode(',', $this->getNotCategoryIds());
- }
- if ($this->getNotLabelIds() !== null) {
- foreach ($this->getNotLabelIds() as $ids) {
- $result .= ' -L:' . (is_array($ids) ? implode(',', $ids) : $ids);
- }
- }
- if ($this->getNotLabelNames() !== null) {
- foreach ($this->getNotLabelNames() as $names) {
- $result .= ' -labels:' . self::quote(implode(',', $names));
- }
- }
- if ($this->getNotMinUserdate() !== null || $this->getNotMaxUserdate() !== null) {
- $result .= ' -userdate:' . self::dateIntervalToString($this->getNotMinUserdate(), $this->getNotMaxUserdate());
- }
- if ($this->getNotMinPubdate() !== null || $this->getNotMaxPubdate() !== null) {
- $result .= ' -pubdate:' . self::dateIntervalToString($this->getNotMinPubdate(), $this->getNotMaxPubdate());
- }
- if ($this->getNotMinDate() !== null || $this->getNotMaxDate() !== null) {
- $result .= ' -date:' . self::dateIntervalToString($this->getNotMinDate(), $this->getNotMaxDate());
- }
- if ($this->getNotIntitleRegex() !== null) {
- foreach ($this->getNotIntitleRegex() as $s) {
- $result .= ' -intitle:' . $s;
- }
- }
- if ($this->getNotIntitle() !== null) {
- foreach ($this->getNotIntitle() as $s) {
- $result .= ' -intitle:' . self::quote($s);
- }
- }
- if ($this->getNotIntextRegex() !== null) {
- foreach ($this->getNotIntextRegex() as $s) {
- $result .= ' -intext:' . $s;
- }
- }
- if ($this->getNotIntext() !== null) {
- foreach ($this->getNotIntext() as $s) {
- $result .= ' -intext:' . self::quote($s);
- }
- }
- if ($this->getNotAuthorRegex() !== null) {
- foreach ($this->getNotAuthorRegex() as $s) {
- $result .= ' -author:' . $s;
- }
- }
- if ($this->getNotAuthor() !== null) {
- foreach ($this->getNotAuthor() as $s) {
- $result .= ' -author:' . self::quote($s);
- }
- }
- if ($this->getNotInurlRegex() !== null) {
- foreach ($this->getNotInurlRegex() as $s) {
- $result .= ' -inurl:' . $s;
- }
- }
- if ($this->getNotInurl() !== null) {
- foreach ($this->getNotInurl() as $s) {
- $result .= ' -inurl:' . self::quote($s);
- }
- }
- if ($this->getNotTagsRegex() !== null) {
- foreach ($this->getNotTagsRegex() as $s) {
- $result .= ' -#' . $s;
- }
- }
- if ($this->getNotTags() !== null) {
- foreach ($this->getNotTags() as $s) {
- $result .= ' -#' . self::quote($s);
- }
- }
- if ($this->getNotSearchRegex() !== null) {
- foreach ($this->getNotSearchRegex() as $s) {
- $result .= ' -' . $s;
- }
- }
- if ($this->getNotSearch() !== null) {
- foreach ($this->getNotSearch() as $s) {
- $result .= ' -' . self::quote($s);
- }
- }
- return trim($result);
- }
- public function getRawInput(): string {
- return $this->raw_input;
- }
- /** @return list<string>|null */
- public function getEntryIds(): ?array {
- return $this->entry_ids;
- }
- /** @return list<string>|null */
- public function getNotEntryIds(): ?array {
- return $this->not_entry_ids;
- }
- /** @return list<int>|null */
- public function getFeedIds(): ?array {
- return $this->feed_ids;
- }
- /** @return list<int>|null */
- public function getNotFeedIds(): ?array {
- return $this->not_feed_ids;
- }
- /** @return list<int>|null */
- public function getCategoryIds(): ?array {
- return $this->category_ids;
- }
- /** @return list<int>|null */
- public function getNotCategoryIds(): ?array {
- return $this->not_category_ids;
- }
- /** @return list<list<int>|'*'>|null */
- public function getLabelIds(): array|null {
- return $this->label_ids;
- }
- /** @return list<list<int>|'*'>|null */
- public function getNotLabelIds(): array|null {
- return $this->not_label_ids;
- }
- /** @return list<list<string>>|null */
- public function getLabelNames(): ?array {
- return $this->label_names;
- }
- /** @return list<list<string>>|null */
- public function getNotLabelNames(): ?array {
- return $this->not_label_names;
- }
- /** @return list<string>|null */
- public function getIntitle(): ?array {
- return $this->intitle;
- }
- /** @return list<string>|null */
- public function getIntitleRegex(): ?array {
- return $this->intitle_regex;
- }
- /** @return list<string>|null */
- public function getNotIntitle(): ?array {
- return $this->not_intitle;
- }
- /** @return list<string>|null */
- public function getNotIntitleRegex(): ?array {
- return $this->not_intitle_regex;
- }
- /** @return list<string>|null */
- public function getIntext(): ?array {
- return $this->intext;
- }
- /** @return list<string>|null */
- public function getIntextRegex(): ?array {
- return $this->intext_regex;
- }
- /** @return list<string>|null */
- public function getNotIntext(): ?array {
- return $this->not_intext;
- }
- /** @return list<string>|null */
- public function getNotIntextRegex(): ?array {
- return $this->not_intext_regex;
- }
- 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;
- }
- public function setMaxPubdate(int $value): void {
- $this->max_pubdate = $value;
- }
- public function getMinUserdate(): ?int {
- return $this->min_userdate ?: null;
- }
- public function getNotMinUserdate(): ?int {
- return $this->not_min_userdate ?: null;
- }
- public function getMaxUserdate(): ?int {
- return $this->max_userdate ?: null;
- }
- public function getNotMaxUserdate(): ?int {
- return $this->not_max_userdate ?: null;
- }
- /** @return list<string>|null */
- public function getInurl(): ?array {
- return $this->inurl;
- }
- /** @return list<string>|null */
- public function getInurlRegex(): ?array {
- return $this->inurl_regex;
- }
- /** @return list<string>|null */
- public function getNotInurl(): ?array {
- return $this->not_inurl;
- }
- /** @return list<string>|null */
- public function getNotInurlRegex(): ?array {
- return $this->not_inurl_regex;
- }
- /** @return list<string>|null */
- public function getAuthor(): ?array {
- return $this->author;
- }
- /** @return list<string>|null */
- public function getAuthorRegex(): ?array {
- return $this->author_regex;
- }
- /** @return list<string>|null */
- public function getNotAuthor(): ?array {
- return $this->not_author;
- }
- /** @return list<string>|null */
- public function getNotAuthorRegex(): ?array {
- return $this->not_author_regex;
- }
- /** @return list<string>|null */
- public function getTags(): ?array {
- return $this->tags;
- }
- /** @return list<string>|null */
- public function getTagsRegex(): ?array {
- return $this->tags_regex;
- }
- /** @return list<string>|null */
- public function getNotTags(): ?array {
- return $this->not_tags;
- }
- /** @return list<string>|null */
- public function getNotTagsRegex(): ?array {
- return $this->not_tags_regex;
- }
- /** @return list<string>|null */
- public function getSearch(): ?array {
- return $this->search;
- }
- /** @return list<string>|null */
- public function getSearchRegex(): ?array {
- return $this->search_regex;
- }
- /** @return list<string>|null */
- public function getNotSearch(): ?array {
- return $this->not_search;
- }
- /** @return list<string>|null */
- public function getNotSearchRegex(): ?array {
- return $this->not_search_regex;
- }
- /**
- * @param list<string>|null $anArray
- * @return list<string>
- */
- private static function removeEmptyValues(?array $anArray): array {
- return empty($anArray) ? [] : array_values(array_filter($anArray, static fn(string $value) => $value !== ''));
- }
- /**
- * @param list<string>|string $value
- * @return ($value is string ? string : list<string>)
- */
- private static function decodeSpaces(array|string $value): array|string {
- if (is_array($value)) {
- foreach ($value as &$val) {
- $val = self::decodeSpaces($val);
- }
- } else {
- $value = trim(str_replace('+', ' ', $value));
- }
- // @phpstan-ignore return.type
- return $value;
- }
- /**
- * @param list<string> $strings
- * @return list<string>
- */
- private static function htmlspecialchars_decodes(array $strings): array {
- return array_map(static fn(string $s) => htmlspecialchars_decode($s, ENT_QUOTES), $strings);
- }
- /**
- * 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 list<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 list<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;
- }
- private function parseCategoryIds(string $input): string {
- if (preg_match_all('/\\bc:(?P<search>[0-9,]*)/', $input, $matches)) {
- $input = str_replace($matches[0], '', $input);
- $ids_lists = $matches['search'];
- $this->category_ids = [];
- foreach ($ids_lists as $ids_list) {
- $category_ids = explode(',', $ids_list);
- $category_ids = self::removeEmptyValues($category_ids);
- /** @var list<int> $category_ids */
- $category_ids = array_map('intval', $category_ids);
- if (!empty($category_ids)) {
- $this->category_ids = array_merge($this->category_ids, $category_ids);
- }
- }
- }
- return $input;
- }
- private function parseNotCategoryIds(string $input): string {
- if (preg_match_all('/(?<=[\\s(]|^)[!-]c:(?P<search>[0-9,]*)/', $input, $matches)) {
- $input = str_replace($matches[0], '', $input);
- $ids_lists = $matches['search'];
- $this->not_category_ids = [];
- foreach ($ids_lists as $ids_list) {
- $category_ids = explode(',', $ids_list);
- $category_ids = self::removeEmptyValues($category_ids);
- /** @var list<int> $category_ids */
- $category_ids = array_map('intval', $category_ids);
- if (!empty($category_ids)) {
- $this->not_category_ids = array_merge($this->not_category_ids, $category_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 list<int> $label_ids */
- $label_ids = array_map('intval', $label_ids);
- if (!empty($label_ids)) {
- $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 list<int> $label_ids */
- $label_ids = array_map('intval', $label_ids);
- if (!empty($label_ids)) {
- $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[] = $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[] = $names_array;
- }
- }
- }
- return $input;
- }
- /**
- * Parse the search string to find intitle keyword and the search related to it.
- */
- private function parseIntitleSearch(string $input): string {
- if (preg_match_all('#\\bintitle:(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
- $this->intitle_regex = self::htmlspecialchars_decodes($matches['search']);
- $input = str_replace($matches[0], '', $input);
- }
- 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<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
- $this->not_intitle_regex = self::htmlspecialchars_decodes($matches['search']);
- $input = str_replace($matches[0], '', $input);
- }
- 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 intext keyword and the search related to it.
- */
- private function parseIntextSearch(string $input): string {
- if (preg_match_all('#\\bintext:(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
- $this->intext_regex = self::htmlspecialchars_decodes($matches['search']);
- $input = str_replace($matches[0], '', $input);
- }
- if (preg_match_all('/\\bintext:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
- $this->intext = $matches['search'];
- $input = str_replace($matches[0], '', $input);
- }
- if (preg_match_all('/\\bintext:(?P<search>[^\s"]*)/', $input, $matches)) {
- $this->intext = array_merge($this->intext ?? [], $matches['search']);
- $input = str_replace($matches[0], '', $input);
- }
- $this->intext = self::removeEmptyValues($this->intext);
- if (empty($this->intext)) {
- $this->intext = null;
- }
- return $input;
- }
- private function parseNotIntextSearch(string $input): string {
- if (preg_match_all('#(?<=[\\s(]|^)[!-]intext:(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
- $this->not_intext_regex = self::htmlspecialchars_decodes($matches['search']);
- $input = str_replace($matches[0], '', $input);
- }
- if (preg_match_all('/(?<=[\\s(]|^)[!-]intext:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
- $this->not_intext = $matches['search'];
- $input = str_replace($matches[0], '', $input);
- }
- if (preg_match_all('/(?<=[\\s(]|^)[!-]intext:(?P<search>[^\s"]*)/', $input, $matches)) {
- $this->not_intext = array_merge($this->not_intext ?? [], $matches['search']);
- $input = str_replace($matches[0], '', $input);
- }
- $this->not_intext = self::removeEmptyValues($this->not_intext);
- if (empty($this->not_intext)) {
- $this->not_intext = 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<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
- $this->author_regex = self::htmlspecialchars_decodes($matches['search']);
- $input = str_replace($matches[0], '', $input);
- }
- 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<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
- $this->not_author_regex = self::htmlspecialchars_decodes($matches['search']);
- $input = str_replace($matches[0], '', $input);
- }
- 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>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
- $this->inurl_regex = self::htmlspecialchars_decodes($matches['search']);
- $input = str_replace($matches[0], '', $input);
- }
- if (preg_match_all('/\\binurl:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
- $this->inurl = $matches['search'];
- $input = str_replace($matches[0], '', $input);
- }
- 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);
- if (empty($this->inurl)) {
- $this->inurl = null;
- }
- return $input;
- }
- private function parseNotInurlSearch(string $input): string {
- if (preg_match_all('#(?<=[\\s(]|^)[!-]inurl:(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
- $this->not_inurl_regex = self::htmlspecialchars_decodes($matches['search']);
- $input = str_replace($matches[0], '', $input);
- }
- if (preg_match_all('/(?<=[\\s(]|^)[!-]inurl:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
- $this->not_inurl = $matches['search'];
- $input = str_replace($matches[0], '', $input);
- }
- 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);
- if (empty($this->not_inurl)) {
- $this->not_inurl = null;
- }
- 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 userdate keyword and the search related to it.
- * The search is the first word following the keyword.
- */
- private function parseUserdateSearch(string $input): string {
- if (preg_match_all('/\\buserdate:(?P<search>[^\\s]*)/', $input, $matches)) {
- $input = str_replace($matches[0], '', $input);
- $dates = self::removeEmptyValues($matches['search']);
- if (!empty($dates[0])) {
- [$this->min_userdate, $this->max_userdate] = parseDateInterval($dates[0]);
- }
- }
- return $input;
- }
- private function parseNotUserdateSearch(string $input): string {
- if (preg_match_all('/(?<=[\\s(]|^)[!-]userdate:(?P<search>[^\\s]*)/', $input, $matches)) {
- $input = str_replace($matches[0], '', $input);
- $dates = self::removeEmptyValues($matches['search']);
- if (!empty($dates[0])) {
- [$this->not_min_userdate, $this->not_max_userdate] = 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>/.*?(?<!\\\\)/[im]*)%', $input, $matches)) {
- $this->tags_regex = self::htmlspecialchars_decodes($matches['search']);
- $input = str_replace($matches[0], '', $input);
- }
- if (preg_match_all('/#(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
- $this->tags = $matches['search'];
- $input = str_replace($matches[0], '', $input);
- }
- 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);
- if (empty($this->tags)) {
- $this->tags = null;
- } else {
- $this->tags = self::decodeSpaces($this->tags);
- }
- return $input;
- }
- private function parseNotTagsSearch(string $input): string {
- if (preg_match_all('%(?<=[\\s(]|^)[!-]#(?P<search>/.*?(?<!\\\\)/[im]*)%', $input, $matches)) {
- $this->not_tags_regex = self::htmlspecialchars_decodes($matches['search']);
- $input = str_replace($matches[0], '', $input);
- }
- if (preg_match_all('/(?<=[\\s(]|^)[!-]#(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
- $this->not_tags = $matches['search'];
- $input = str_replace($matches[0], '', $input);
- }
- 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);
- if (empty($this->not_tags)) {
- $this->not_tags = null;
- } else {
- $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 (") and regex (/).
- */
- private function parseQuotedSearch(string $input): string {
- $input = self::cleanSearch($input);
- if ($input === '') {
- return '';
- }
- if (preg_match_all('#(?<=[\\s(]|^)(?<![!-\\\\])(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
- $this->search_regex = self::htmlspecialchars_decodes($matches['search']);
- //TODO: Replace all those str_replace with PREG_OFFSET_CAPTURE
- $input = str_replace($matches[0], '', $input);
- }
- if (preg_match_all('/(?<=[\\s(]|^)(?<![!-\\\\])(?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<search>(?<!\\\\)/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
- $this->not_search_regex = self::htmlspecialchars_decodes($matches['search']);
- $input = str_replace($matches[0], '', $input);
- }
- 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);
- }
- }
|