27 private $_repositoryApi;
37 if(!$this->_repositoryApi) {
38 if(!empty($this->searchUrl) || !empty($this->autosuggestUrl)) {
39 $repositoryApi = clone Yii::app()->repositoryApi;
40 if(!empty($this->searchUrl)){
43 if(!empty($this->autosuggestUrl)){
46 $this->_repositoryApi = $repositoryApi;
48 $this->_repositoryApi = Yii::app()->repositoryApi;
51 return $this->_repositoryApi;
74 public function search($phrase, $params = array(), $start = null, $count = null)
76 if (!$this->_checkPhrase($phrase)) {
79 $whereToSearch = isset($params[
'whereToSearch']) ? $params[
'whereToSearch'] : Yii::app()->getModule(
'search')->whereToSearch[
'default'];
80 $mimetype = isset($params[
'mimetype']) ? $params[
'mimetype'] : Yii::app()->getModule(
'search')->mimetype[
'default'];
81 $searchType = isset($params[
'searchType']) ? $params[
'searchType'] : Yii::app()->getModule(
'search')->searchType[
'default'];
82 $urlLimiter = isset($params[
'urlLimiter']) ? $params[
'urlLimiter'] : Yii::app()->getModule(
'search')->urlLimiter[
'default'];
83 $enableSearchOperators = isset($params[
'enableSearchOperators']) ? $params[
'enableSearchOperators'] : Yii::app()->getModule(
'search')->searchType[
'enableSearchOperators'];
88 'metaresolvable' => array()
90 $data = $this->_preformSearch($phrase, $whereToSearch, $mimetype, $searchType, $urlLimiter, $start, $count, $enableSearchOperators);
91 if($data[
'status'] !=
'ok'){
94 unset($data[
'status']);
95 $meta = $data[
'10001'][
'attributes'];
96 unset($data[
'10001']);
117 private function _preformSearch($phrase, $whereToSearch, $mimetype, $searchType, $urlLimiter, $start = null, $count = null, $enableSearchOperators =
false)
120 if ($searchType ==
'EXACT' || count( explode(
' ', $phrase) ) > 4) {
122 $phrase =
'"'.$phrase.
'"';
126 $phrase = preg_replace(
'/([\ ]+)/',
' ', $phrase);
129 $wordGroups = $this->_extractWordGroups($phrase);
132 if (!empty($wordGroups)) {
133 $phrase = str_replace(array_keys($wordGroups), $wordGroups, $phrase);
139 if (!$enableSearchOperators) {
141 $phrase = explode(
' ', $phrase);
142 $phrase = implode(
' '.$searchType.
' ', $phrase);
146 $tmpPhrase = explode(
' ', $phrase);
148 $keywordFlag =
false;
150 foreach ($tmpPhrase as $i => $word) {
151 if (in_array($word, array(
'AND',
'OR',
'NOT'))) {
157 if ($keywordFlag || $i == 0) {
158 $keywordFlag =
false;
161 $phrase[] = $searchType.
' '.$word;
165 $phrase = implode(
' ', $phrase);
171 if (!empty($wordGroups)) {
172 $phrase = str_replace($wordGroups, array_keys($wordGroups), $phrase);
176 $staticString = Yii::app()->getModule(
'search')->staticString;
177 if (trim($staticString)!=
'') {
178 $phrase .=
' '.$staticString;
181 $lang = substr(Yii::app()->language, 0, 2);
184 'metaresolvable' =>
'true',
185 'filter' =>
'languagecode:'.$lang,
188 if (!empty($this->
getRepositoryApi()->additionalRequestParameters[
'search'])) {
189 $additional = $this->
getRepositoryApi()->additionalRequestParameters[
'search'];
190 foreach ($additional as $item=>$val) {
191 $params[$item] = !empty($params[$item]) ? $params[$item].
' AND '.$val : $params[$item];
195 if ($whereToSearch !=
'all') {
196 $params[
'filter'] .=
' AND '.$whereToSearch.
':('.$phrase.
')';
198 $whereToSearchAll = Yii::app()->getModule(
'search')->whereToSearch[
'options'];
199 if (isset($whereToSearchAll[
'all'])) {
200 unset($whereToSearchAll[
'all']);
203 $aWhereToSearchAll = array();
205 foreach ($whereToSearchAll as $key=>$val) {
206 $aWhereToSearchAll[] = $key.
':('.$phrase.
')';
209 $params[
'filter'] .=
' AND ('.implode(
' OR ', $aWhereToSearchAll).
')';
212 if ($mimetype!=
'all') {
213 $params[
'filter'] .=
' AND mimetype:'.$mimetype;
216 if ($urlLimiter!=
'all') {
217 $params[
'filter'] .=
' AND url:'.$urlLimiter;
221 $params[
'start'] = $start;
225 $params[
'count'] = $count;
228 $cacheId = md5(serialize($params));
230 if (($data = Yii::app()->cache->get($cacheId)) ===
false) {
233 $data = unserialize($data);
236 Yii::app()->cache->set($cacheId, $data, Yii::app()->getModule(
'search')->cacheTime);
248 private function _checkPhrase($phrase)
250 if (trim($phrase) ==
'') {
256 private function _extractWordGroups($phrase, $quotes=array(
'"'))
258 if (empty($phrase)) {
262 $wordGroups = array();
264 foreach ($quotes as $quote) {
265 $reg = preg_quote($quote);
266 preg_match_all(
'/['.$quote.
'](.*)['.$quote.
']/sU', $phrase, $matches);
268 if(!empty($matches[1])) {
269 foreach ($matches[1] as $match) {
270 $wordGroups[$match] = str_replace(
' ',
'_', $match);