From 01087dff33c8b475284d913d5786c5c74ce7e14d Mon Sep 17 00:00:00 2001 From: Butschster Date: Tue, 2 Dec 2014 01:16:24 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B5=D0=BC=D0=BD=D0=BE=D0=B3=D0=BE=20?= =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B5=D0=B4=D0=B5=D0=BB=D0=B0=D0=BD=20=D0=BE?= =?UTF-8?q?=D0=BC=D0=B4=D1=83=D0=BB=D1=8C=20=D0=BF=D0=BE=D0=B8=D1=81=D0=BA?= =?UTF-8?q?=D0=B0,=20=D1=82=D0=B5=D0=BF=D0=B5=D1=80=D1=8C=20=D0=B4=D0=BB?= =?UTF-8?q?=D1=8F=20=D0=BF=D0=BE=D0=B8=D1=81=D0=BA=D0=B0=20=D0=B4=D0=B0?= =?UTF-8?q?=D0=BD=D0=BD=D1=8B=D1=85=20=D0=B8=D1=81=D0=BF=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D0=B7=D1=83=D0=B5=D1=82=D1=81=D1=8F=20=D0=BE=D1=82=D0=B4=D0=B5?= =?UTF-8?q?=D0=BB=D1=8C=D0=BD=D1=8B=D0=B9=20=D0=B7=D0=B0=D0=BF=D1=80=D0=BE?= =?UTF-8?q?=D1=81=20=D0=B8=20=D0=B2=D0=BE=D0=B7=D0=B2=D1=80=D0=B0=D1=89?= =?UTF-8?q?=D0=B0=D1=8E=D1=82=D1=81=D1=8F=20ID=20=D0=B7=D0=B0=D0=BF=D0=B8?= =?UTF-8?q?=D1=81=D0=B5=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../classes/datasource/section/headline.php | 24 +++-- cms/modules/search/classes/kodicms/search.php | 39 ++++++-- .../search/classes/kodicms/search/mysql.php | 92 ++++++++++--------- .../classes/model/widget/page/search.php | 7 +- .../classes/model/widget/hybrid/headline.php | 22 ++++- 5 files changed, 121 insertions(+), 63 deletions(-) diff --git a/cms/modules/datasource/classes/datasource/section/headline.php b/cms/modules/datasource/classes/datasource/section/headline.php index 3826c989d..4277d3c82 100644 --- a/cms/modules/datasource/classes/datasource/section/headline.php +++ b/cms/modules/datasource/classes/datasource/section/headline.php @@ -231,28 +231,38 @@ abstract public function count_total( array $ids = NULL ); * @param Database_Query $query * @return Database_Query */ - public function search_by_keyword( Database_Query $query ) + public function search_by_keyword(Database_Query $query) { $keyword = Request::initial()->query('keyword'); - if(empty($keyword)) + if (empty($keyword)) { return $query; } - if($this->_section->is_indexable()) + if ($this->_section->is_indexable()) { - $query = Search::instance()->get_module_query($query, $keyword, 'ds_' . $this->_section->id()); + $ids = Search::instance()->find_by_keyword($keyword, FALSE, 'ds_' . $this->_section->id(), NULL); + $ids = Arr::get($ids, 'ds_' . $this->_section->id()); + + if(!empty($ids)) + { + $query->where('d.id', 'in', array_keys($ids)); + } + else + { + $query->where('d.id', '=', 0); + } } else { $query ->where_open() - ->where('d.id', 'like', '%'.$keyword.'%') - ->or_where('d.header', 'like', '%'.$keyword.'%') + ->where('d.id', 'like', '%' . $keyword . '%') + ->or_where('d.header', 'like', '%' . $keyword . '%') ->where_close(); } - + return $query; } diff --git a/cms/modules/search/classes/kodicms/search.php b/cms/modules/search/classes/kodicms/search.php index 7d79b09da..1855533df 100644 --- a/cms/modules/search/classes/kodicms/search.php +++ b/cms/modules/search/classes/kodicms/search.php @@ -42,18 +42,17 @@ public static function instance($group = NULL) $config = Kohana::$config->load('search'); - if ( ! $config->offsetExists($group)) + if (!$config->offsetExists($group)) { throw new Kohana_Exception( - 'Failed to load Kohana Search group: :group', - array(':group' => $group) - ); + 'Failed to load Kohana Search group: :group', array( + ':group' => $group)); } $config = $config->get($group); // Create a new search type instance - $search_class = 'Search_'.ucfirst($config['driver']); + $search_class = 'Search_' . ucfirst($config['driver']); Search::$instances[$group] = new $search_class($config); // Return the instance @@ -87,7 +86,9 @@ protected function __construct(array $config) public function config($key = NULL, $value = NULL) { if ($key === NULL) + { return $this->_config; + } if (is_array($key)) { @@ -96,7 +97,9 @@ public function config($key = NULL, $value = NULL) else { if ($value === NULL) + { return Arr::get($this->_config, $key); + } $this->_config[$key] = $value; } @@ -124,13 +127,33 @@ final public function __clone() * @param array $params * @return bool */ - abstract public function add_to_index( $module, $id, $title, $content = '', $annotation, $params = array() ); - + abstract public function add_to_index($module, $id, $title, $content = '', $annotation, $params = array()); + /** * * @param string $module * @param integer $id * @return bool */ - abstract public function remove_from_index( $module, $id = NULL ); + abstract public function remove_from_index($module, $id = NULL); + + /** + * + * @param string $keyword + * @param boolean $only_title + * @param string|array $modules + * @param integer $limit + * @param integer $offset + * @return array + */ + abstract public function find_by_keyword($keyword, $only_title = FALSE, $modules = NULL, $limit = 50, $offset = 0); + + /** + * + * @param string $keyword + * @param boolean $only_title + * @param string $modules + * @return integer + */ + abstract public function count_by_keyword($keyword, $only_title = FALSE, $modules = NULL); } \ No newline at end of file diff --git a/cms/modules/search/classes/kodicms/search/mysql.php b/cms/modules/search/classes/kodicms/search/mysql.php index f7a52c12d..1b929ca82 100644 --- a/cms/modules/search/classes/kodicms/search/mysql.php +++ b/cms/modules/search/classes/kodicms/search/mysql.php @@ -20,26 +20,26 @@ public function stem_query($keyword) $result = ''; $text = UTF8::strtolower($keyword); - $text = trim( $text ); - $text = strip_tags( $text ); + $text = trim($text); + $text = strip_tags($text); $stop_words = Model_Search_Stopwords::get(); // Parse original text and stem all words that are not tags $tkn = new Model_Search_Tokenizer(); - $tkn->set_text( $text ); + $tkn->set_text($text); $tkn->stopwords = $stop_words; - + $stemmer = new Model_Search_Stemmer($tkn); - while ( $cur = $tkn->next() ) + while ($cur = $tkn->next()) { $result .= $stemmer->stem($cur); } - + return $result; } - + /** * * @param string $keyword @@ -49,7 +49,7 @@ public function stem_query($keyword) * @param integer $offset * @return array */ - public function find_by_keyword( $keyword, $only_title = FALSE, $modules = NULL, $limit = 50, $offset = 0) + public function find_by_keyword($keyword, $only_title = FALSE, $modules = NULL, $limit = 50, $offset = 0) { if (Kohana::$profiling === TRUE) { @@ -68,7 +68,7 @@ public function find_by_keyword( $keyword, $only_title = FALSE, $modules = NULL, foreach($result as $row) { $row['params'] = Kohana::unserialize($row['params']); - $ids[$row['module']][] = $row; + $ids[$row['module']][$row['id']] = $row; } if (isset($benchmark)) @@ -86,12 +86,12 @@ public function find_by_keyword( $keyword, $only_title = FALSE, $modules = NULL, * @param string $modules * @return integer */ - public function count_by_keyword( $keyword, $only_title = FALSE, $modules = NULL ) + public function count_by_keyword($keyword, $only_title = FALSE, $modules = NULL) { $query = DB::select(array(DB::expr('COUNT(*)'), 'total')) ->from('search_index'); - - return $this->_get_query( $query, $keyword, $only_title, $modules, NULL ) + + return $this->_get_query($query, $keyword, $only_title, $modules, NULL) ->execute() ->get('total'); } @@ -105,23 +105,23 @@ public function match_against_query($keyword) { $keyword = trim(strtolower($keyword)); $result = '>"' . $keyword . '"<'; - + $words = explode(' ', $keyword); - if(count($words) > 1 ) + if (count($words) > 1) { $result .= '('; - foreach ($words as $word ) + foreach ($words as $word) { $result .= '+' . $word; } - + $result .= ') '; } - + return $result; } - + /** * * @param Database_Query $query @@ -129,11 +129,11 @@ public function match_against_query($keyword) * @param string $module * @param bool $only_title */ - public function get_module_query( Database_Query $query, $keyword, $module, $only_title = FALSE ) + public function get_module_query(Database_Query $query, $keyword, $module, $only_title = FALSE) { return $this->_get_query($query, $keyword, $only_title, $module, NULL) ->join('search_index', 'left') - ->on('search_index.id', '=', 'd.id'); + ->on('search_index.id', '=', 'd.id'); } /** @@ -150,51 +150,58 @@ protected function _get_query(Database_Query $query, $keyword, $only_title = FAL { $keyword = $this->stem_query($keyword); - if($limit !== NULL) + if ($limit !== NULL) { $query ->limit((int) $limit) ->offset($offset); } - - if(is_array($modules)) + + if (is_array($modules)) { $query->where('search_index.module', 'in', $modules); } - elseif ($modules !== NULL) + elseif ($modules !== NULL) { $query->where('search_index.module', '=', $modules); } - - if($this->config('full_text_search') === TRUE) + + if ($this->config('full_text_search') === TRUE) { - $query->where(DB::expr('MATCH(`search_index`.`header`, `search_index`.`content`)'), 'AGAINST', DB::expr("('".self::match_against_query($keyword)."' IN BOOLEAN MODE)")); + $query->where(DB::expr('MATCH(`search_index`.`header`, `search_index`.`content`)'), 'AGAINST', DB::expr("('" . self::match_against_query($keyword) . "' IN BOOLEAN MODE)")); } else { $words = explode(' ', $keyword); $query->where_open(); - $query->where_open(); + $query->where_open(); - foreach ($words as $word ) + foreach ($words as $word) + { + if (!empty($word)) { - if(!empty($word)) - $query->where('search_index.header', 'like', '%'.$word.'%'); + $query->where('search_index.header', 'like', '%' . $word . '%'); } + } - $query->where_close(); + $query->where_close(); - if($only_title === FALSE) + if ($only_title === FALSE) + { + $query->or_where_open(); + + foreach ($words as $word) { - $query->or_where_open(); - foreach ($words as $word ) + if (!empty($word)) { - if(!empty($word)) - $query->where('search_index.content', 'like', '%'.$word.'%'); + $query->where('search_index.content', 'like', '%' . $word . '%'); } - $query->where_close(); } + + $query->where_close(); + } + $query->where_close(); } @@ -210,21 +217,22 @@ protected function _get_query(Database_Query $query, $keyword, $only_title = FAL * @param array $params * @return bool */ - public function add_to_index( $module, $id, $title, $content = '', $annotation, $params = array() ) + public function add_to_index($module, $id, $title, $content = '', $annotation, $params = array()) { $indexer = new Model_Search_Indexer; return $indexer->add($module, $id, $title, $content, $annotation, $params); } - + /** * * @param string $module * @param integer $id * @return bool */ - public function remove_from_index( $module, $id = NULL ) + public function remove_from_index($module, $id = NULL) { $indexer = new Model_Search_Indexer; - return $indexer->remove( $module, $id ); + return $indexer->remove($module, $id); } + } \ No newline at end of file diff --git a/cms/modules/search/classes/model/widget/page/search.php b/cms/modules/search/classes/model/widget/page/search.php index debede72f..50a5d7057 100644 --- a/cms/modules/search/classes/model/widget/page/search.php +++ b/cms/modules/search/classes/model/widget/page/search.php @@ -44,8 +44,11 @@ public function fetch_data() $ids = Search::instance()->find_by_keyword($keyword, FALSE, 'pages', $this->list_size, $this->list_offset); - if(empty($ids['pages'])) return $return; - + if(empty($ids['pages'])) + { + return $return; + } + $pages = array(); foreach ($ids['pages'] as $item) { diff --git a/cms/plugins/hybrid/classes/model/widget/hybrid/headline.php b/cms/plugins/hybrid/classes/model/widget/hybrid/headline.php index 4bca2fd63..01820eada 100644 --- a/cms/plugins/hybrid/classes/model/widget/hybrid/headline.php +++ b/cms/plugins/hybrid/classes/model/widget/hybrid/headline.php @@ -276,15 +276,29 @@ protected function _parse_doc_id() * * @param Database_Query $query */ - protected function _search_by_keyword( Database_Query $query ) + protected function _search_by_keyword(Database_Query $query, $only_title = FALSE) { - if($this->search_key === NULL OR trim($this->search_key) == '') return $query; + if ($this->search_key === NULL OR trim($this->search_key) == '') + { + return $query; + } $keyword = $this->_ctx->get($this->search_key); + + if (empty($keyword)) + { + return $query; + } - if( empty($keyword) )return $query; + $ids = Search::instance()->find_by_keyword($keyword, $only_title, 'ds_' . $this->ds_id, NULL); + + $ids = Arr::get($ids, 'ds_' . $this->ds_id); + if (!empty($ids)) + { + return $query->where('id', 'in', array_keys($ids)); + } - return Search::instance()->get_module_query($query, $keyword, 'ds_' . $this->ds_id); + return $query; } /**