Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00035 class ilQueryParser
00036 {
00037 var $lng = null;
00038
00039
00040 var $query_str;
00041 var $quoted_words = array();
00042 var $message;
00043 var $combination;
00044
00049 function ilQueryParser($a_query_str)
00050 {
00051 global $lng;
00052
00053 $this->lng =& $lng;
00054
00055 $this->query_str = $a_query_str;
00056 $this->message = '';
00057 }
00058
00059
00060 function setMessage($a_msg)
00061 {
00062 $this->message = $a_msg;
00063 }
00064 function getMessage()
00065 {
00066 return $this->message;
00067 }
00068 function appendMessage($a_msg)
00069 {
00070 if(strlen($this->getMessage()))
00071 {
00072 $this->message .= '<br />';
00073 }
00074 $this->message .= $a_msg;
00075 }
00076
00077 function setCombination($a_combination)
00078 {
00079 $this->combination = $a_combination;
00080 }
00081 function getCombination()
00082 {
00083 return $this->combination;
00084 }
00085
00086 function getQueryString()
00087 {
00088 return trim($this->query_str);
00089 }
00090 function getWords()
00091 {
00092 return $this->words ? $this->words : array();
00093 }
00094
00095 function getQuotedWords($with_quotation = false)
00096 {
00097 if($with_quotation)
00098 {
00099 return $this->quoted_words ? $this->quoted_words : array();
00100 }
00101 else
00102 {
00103 foreach($this->quoted_words as $word)
00104 {
00105 $tmp_word[] = str_replace('\"','',$word);
00106 }
00107 return $tmp_word ? $tmp_word : array();
00108 }
00109 }
00110
00111 function getLuceneQueryString()
00112 {
00113 $counter = 0;
00114 $tmp_str = "";
00115 foreach($this->getQuotedWords(true) as $word) {
00116 if($counter++)
00117 {
00118 $tmp_str .= (" ".strtoupper($this->getCombination())." ");
00119 }
00120 $tmp_str .= $word;
00121 }
00122 return $tmp_str;
00123 }
00124 function parse()
00125 {
00126 $this->words = array();
00127
00128 if(!strlen($this->getQueryString()))
00129 {
00130 return false;
00131 }
00132
00133 $words = explode(' ',trim($this->getQueryString()));
00134 foreach($words as $word)
00135 {
00136 if(!strlen(trim($word)))
00137 {
00138 continue;
00139 }
00140 if(strlen(trim($word)) < 3)
00141 {
00142 $this->setMessage($this->lng->txt('search_minimum_three'));
00143 continue;
00144 }
00145 $this->words[] = ilUtil::prepareDBString($word);
00146 }
00147
00148
00149
00150 $this->__parseQuotation();
00151
00152 return true;
00153 }
00154
00155 function __parseQuotation()
00156 {
00157 if(!strlen($this->getQueryString()))
00158 {
00159 return false;
00160 }
00161 $query_str = $this->getQueryString();
00162 while(preg_match("/\".*?\"/",$query_str,$matches))
00163 {
00164 $query_str = str_replace($matches[0],'',$query_str);
00165 $this->quoted_words[] = ilUtil::prepareDBString($matches[0]);
00166 }
00167
00168
00169 $words = explode(' ',trim($query_str));
00170 foreach($words as $word)
00171 {
00172 if(!strlen(trim($word)))
00173 {
00174 continue;
00175 }
00176 $this->quoted_words[] = ilUtil::prepareDBString($word);
00177 }
00178 }
00179
00180 function validate()
00181 {
00182
00183 if(strlen($this->getMessage()))
00184 {
00185 return false;
00186 }
00187
00188 if(!count($this->getWords()))
00189 {
00190 $this->setMessage($this->lng->txt('msg_no_search_string'));
00191 return false;
00192 }
00193
00194 return true;
00195 }
00196 }
00197 ?>