• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

Services/Search/classes/class.ilQueryParser.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003         +-----------------------------------------------------------------------------+
00004         | ILIAS open source                                                           |
00005         +-----------------------------------------------------------------------------+
00006         | Copyright (c) 1998-2001 ILIAS open source, University of Cologne            |
00007         |                                                                             |
00008         | This program is free software; you can redistribute it and/or               |
00009         | modify it under the terms of the GNU General Public License                 |
00010         | as published by the Free Software Foundation; either version 2              |
00011         | of the License, or (at your option) any later version.                      |
00012         |                                                                             |
00013         | This program is distributed in the hope that it will be useful,             |
00014         | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00015         | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
00016         | GNU General Public License for more details.                                |
00017         |                                                                             |
00018         | You should have received a copy of the GNU General Public License           |
00019         | along with this program; if not, write to the Free Software                 |
00020         | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
00021         +-----------------------------------------------------------------------------+
00022 */
00023 
00035 define('QP_COMBINATION_AND','and');
00036 define('QP_COMBINATION_OR','or');
00037 
00038 class ilQueryParser
00039 {
00040         var $lng = null;
00041 
00042         var $min_word_length = 0;
00043 
00044 
00045         var $query_str;
00046         var $quoted_words = array();
00047         var $message; // Translated error message
00048         var $combination; // combiniation of search words e.g 'and' or 'or'
00049 
00054         function ilQueryParser($a_query_str)
00055         {
00056                 global $lng;
00057 
00058                 define(MIN_WORD_LENGTH,3);
00059 
00060                 $this->lng =& $lng;
00061 
00062                 $this->query_str = $a_query_str;
00063                 $this->message = '';
00064 
00065                 $this->min_word_length = MIN_WORD_LENGTH;
00066         }
00067 
00068         function setMinWordLength($a_length)
00069         {
00070                 $this->min_word_length = $a_length;
00071         }
00072         function getMinWordLength()
00073         {
00074                 return $this->min_word_length;
00075         }
00076 
00077         function setMessage($a_msg)
00078         {
00079                 $this->message = $a_msg;
00080         }
00081         function getMessage()
00082         {
00083                 return $this->message;
00084         }
00085         function appendMessage($a_msg)
00086         {
00087                 if(strlen($this->getMessage()))
00088                 {
00089                         $this->message .= '<br />';
00090                 }
00091                 $this->message .= $a_msg;
00092         }
00093 
00094         function setCombination($a_combination)
00095         {
00096                 $this->combination = $a_combination;
00097         }
00098         function getCombination()
00099         {
00100                 return $this->combination;
00101         }
00102 
00103         function getQueryString()
00104         {
00105                 return trim($this->query_str);
00106         }
00107         function getWords()
00108         {
00109                 return $this->words ? $this->words : array();
00110         }
00111 
00112         function getQuotedWords($with_quotation = false)
00113         {
00114                 if($with_quotation)
00115                 {
00116                         return $this->quoted_words ? $this->quoted_words : array();
00117                 }
00118                 else
00119                 {
00120                         foreach($this->quoted_words as $word)
00121                         {
00122                                 $tmp_word[] = str_replace('\"','',$word);
00123                         }
00124                         return $tmp_word ? $tmp_word : array();
00125                 }
00126         }
00127 
00128         function getLuceneQueryString()
00129         {
00130                 $counter = 0;
00131                 $tmp_str = "";
00132                 foreach($this->getQuotedWords(true) as $word) {
00133                         if($counter++)
00134                         {
00135                                 $tmp_str .= (" ".strtoupper($this->getCombination())." ");
00136                         }
00137                         $tmp_str .= $word;
00138                 }
00139                 return $tmp_str;
00140         }
00141         function parse()
00142         {
00143                 $this->words = array();
00144 
00145                 #if(!strlen($this->getQueryString()))
00146                 #{
00147                 #       return false;
00148                 #}
00149 
00150                 $words = explode(' ',trim($this->getQueryString()));
00151                 foreach($words as $word)
00152                 {
00153                         if(!strlen(trim($word)))
00154                         {
00155                                 continue;
00156                         }
00157                         
00158                         #if(strlen(trim($word)) < $this->getMinWordLength())
00159                         #{
00160                         #       $this->setMessage($this->lng->txt('search_minimum_three'));
00161                         #       continue;
00162                         #}
00163                         $this->words[] = ilUtil::prepareDBString($word);
00164                 }
00165 
00166                 // Parse strings like && 'A "B C D" E' as 'A' && 'B C D' && 'E'
00167                 // Can be used in LIKE search or fulltext search > MYSQL 4.0
00168                 $this->__parseQuotation();
00169 
00170                 return true;
00171         }
00172 
00173         function __parseQuotation()
00174         {
00175                 if(!strlen($this->getQueryString()))
00176                 {
00177                         $this->quoted_words[] = '';
00178                         return false;
00179                 }
00180                 $query_str = $this->getQueryString();
00181                 while(preg_match("/\".*?\"/",$query_str,$matches))
00182                 {
00183                         $query_str = str_replace($matches[0],'',$query_str);
00184                         $this->quoted_words[] = ilUtil::prepareDBString($matches[0]);
00185                 }
00186 
00187                 // Parse the rest
00188                 $words = explode(' ',trim($query_str));
00189                 foreach($words as $word)
00190                 {
00191                         if(!strlen(trim($word)))
00192                         {
00193                                 continue;
00194                         }
00195                         $this->quoted_words[] = ilUtil::prepareDBString($word);
00196                 }
00197                 
00198         }
00199 
00200         function validate()
00201         {
00202                 // Words with less than 3 characters
00203                 if(strlen($this->getMessage()))
00204                 {
00205                         return false;
00206                 }
00207                 // No search string given
00208                 if($this->getMinWordLength() and !count($this->getWords()))
00209                 {
00210                         $this->setMessage($this->lng->txt('msg_no_search_string'));
00211                         return false;
00212                 }
00213 
00214                 return true;
00215         }
00216 }
00217 ?>

Generated on Fri Dec 13 2013 17:57:01 for ILIAS Release_3_9_x_branch .rev 46835 by  doxygen 1.7.1