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)
119 if ($searchType ==
'EXACT') {
120 $phrase =
'"'.$phrase.
'"';
122 $phrase = preg_replace(
'/([\ ]+)/',
' ', $phrase);
125 $wordGroups = $this->_extractWordGroups($phrase);
128 if (!empty($wordGroups)) {
129 $phrase = str_replace(array_keys($wordGroups), $wordGroups, $phrase);
135 if(!$enableSearchOperators){
137 $phrase = explode(
' ', $phrase);
138 $phrase = implode(
' '.$searchType.
' ', $phrase);
142 $tmpPhrase = explode(
' ', $phrase);
144 $keywordFlag =
false;
146 foreach ($tmpPhrase as $i => $word) {
147 if (in_array($word, array(
'AND',
'OR',
'NOT'))) {
153 if ($keywordFlag || $i == 0) {
154 $keywordFlag =
false;
157 $phrase[] = $searchType.
' '.$word;
161 $phrase = implode(
' ', $phrase);
167 if (!empty($wordGroups)) {
168 $phrase = str_replace($wordGroups, array_keys($wordGroups), $phrase);
172 $staticString = Yii::app()->getModule(
'search')->staticString;
173 if (trim($staticString)!=
'') {
174 $phrase .=
' '.$staticString;
177 $lang = substr(Yii::app()->language, 0, 2);
180 'metaresolvable' =>
'true',
181 'filter' =>
'languagecode:'.$lang,
184 if (!empty($this->
getRepositoryApi()->additionalRequestParameters[
'search'])) {
185 $additional = $this->
getRepositoryApi()->additionalRequestParameters[
'search'];
186 foreach ($additional as $item=>$val) {
187 $params[$item] = !empty($params[$item]) ? $params[$item].
' AND '.$val : $params[$item];
191 if ($whereToSearch !=
'all') {
192 $params[
'filter'] .=
' AND '.$whereToSearch.
':('.$phrase.
')';
194 $whereToSearchAll = Yii::app()->getModule(
'search')->whereToSearch[
'options'];
195 if (isset($whereToSearchAll[
'all'])) {
196 unset($whereToSearchAll[
'all']);
199 $aWhereToSearchAll = array();
201 foreach ($whereToSearchAll as $key=>$val) {
202 $aWhereToSearchAll[] = $key.
':('.$phrase.
')';
205 $params[
'filter'] .=
' AND ('.implode(
' OR ', $aWhereToSearchAll).
')';
208 if ($mimetype!=
'all') {
209 $params[
'filter'] .=
' AND mimetype:'.$mimetype;
212 if ($urlLimiter!=
'all') {
213 $params[
'filter'] .=
' AND url:'.$urlLimiter;
217 $params[
'start'] = $start;
221 $params[
'count'] = $count;
224 $cacheId = md5(serialize($params));
226 if (($data = Yii::app()->cache->get($cacheId)) ===
false) {
229 $data = unserialize($data);
232 Yii::app()->cache->set($cacheId, $data, Yii::app()->getModule(
'search')->cacheTime);
244 private function _checkPhrase($phrase)
246 if (trim($phrase) ==
'') {
252 private function _extractWordGroups($phrase, $quotes=array(
'"'))
254 if (empty($phrase)) {
257 $wordGroups = array();
258 foreach ($quotes as $quote) {
259 $reg = preg_quote($quote);
260 preg_match_all(
'/['.$quote.
'](.*)['.$quote.
']/sU', $phrase, $matches);
262 if(!empty($matches[1])) {
263 foreach ($matches[1] as $match) {
264 $wordGroups[$match] = str_replace(
' ',
'_', $match);