From 230d8c96791b12cb4430adcabe28b16e665c006e Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Wed, 30 May 2018 16:08:44 -0100 Subject: get options from search query string Signed-off-by: Maxence Lange --- lib/Model/IndexDocument.php | 37 +++++++++++++++++++++++++++++++++ lib/Model/SearchRequest.php | 50 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 86 insertions(+), 1 deletion(-) (limited to 'lib/Model') diff --git a/lib/Model/IndexDocument.php b/lib/Model/IndexDocument.php index 0d2bb4c..c3d4bb9 100644 --- a/lib/Model/IndexDocument.php +++ b/lib/Model/IndexDocument.php @@ -59,6 +59,9 @@ class IndexDocument implements \JsonSerializable { /** @var string */ public $content = null; + /** @var string */ + public $hash = ''; + /** @var array */ public $parts = []; @@ -275,6 +278,38 @@ class IndexDocument implements \JsonSerializable { } + /** + * @return $this + */ + public function initHash() { + if ($this->getContent() === '' || is_null($this->getContent())) { + return $this; + } + + $this->hash = hash("md5", $this->getContent()); + + return $this; + } + + /** + * @param $hash + * + * @return $this + */ + public function setHash($hash) { + $this->hash = $hash; + + return $this; + } + + /** + * @return string + */ + public function getHash() { + return $this->hash; + } + + /** * @param string $part * @param string $content @@ -468,6 +503,7 @@ class IndexDocument implements \JsonSerializable { unset($this->modifiedTime); unset($this->title); unset($this->content); + unset($this->hash); unset($this->link); unset($this->source); unset($this->tags); @@ -491,6 +527,7 @@ class IndexDocument implements \JsonSerializable { 'link' => $this->getLink(), 'source' => $this->getSource(), 'info' => $this->getInfoAll(), + 'hash' => $this->getHash(), 'tags' => $this->getTags(), 'more' => $this->getMore(), 'excerpts' => $this->getExcerpts(), diff --git a/lib/Model/SearchRequest.php b/lib/Model/SearchRequest.php index 35d1bbb..2a9013a 100644 --- a/lib/Model/SearchRequest.php +++ b/lib/Model/SearchRequest.php @@ -122,8 +122,44 @@ class SearchRequest implements \JsonSerializable { $this->search = $search; } + public function cleanSearch() { - $this->search = trim(str_replace(' ', ' ', $this->search)); + $search = trim(str_replace(' ', ' ', $this->getSearch())); + + preg_match_all('/[^?]"(?:\\\\.|[^\\\\"])*"|\S+/', " $search ", $words); + $searchItems = []; + foreach ($words[0] as $word) { + if ($this->searchQueryOptions($word)) { + continue; + } + + $searchItems[] = $word; + } + + $this->setSearch(implode(" ", $searchItems)); + } + + + /** + * @param $word + * + * @return bool + */ + private function searchQueryOptions($word) { + if (($pos = strpos($word, ':')) === false) { + return false; + } + + list($kw, $value) = explode(':', $word, 2); + $options = ['is', 'show']; + + if (in_array($kw, $options)) { + $this->addOption($kw . '_' . $value, '1'); + + return true; + } + + return false; } @@ -175,6 +211,18 @@ class SearchRequest implements \JsonSerializable { $this->options = $options; } + /** + * @param $key + * @param $value + * + * @return $this + */ + public function addOption($key, $value) { + $this->options[$key] = $value; + + return $this; + } + /** * @param string $option * -- cgit v1.2.3