• Main Page
  • Related Pages
  • 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                         if(strlen(trim($word)) < $this->getMinWordLength())
00158                         {
00159                                 $this->setMessage($this->lng->txt('search_minimum_three'));
00160                                 continue;
00161                         }
00162                         $this->words[] = ilUtil::prepareDBString($word);
00163                 }
00164 
00165                 // Parse strings like && 'A "B C D" E' as 'A' && 'B C D' && 'E'
00166                 // Can be used in LIKE search or fulltext search > MYSQL 4.0
00167                 $this->__parseQuotation();
00168 
00169                 return true;
00170         }
00171 
00172         function __parseQuotation()
00173         {
00174                 if(!strlen($this->getQueryString()))
00175                 {
00176                         $this->quoted_words[] = '';
00177                         return false;
00178                 }
00179                 $query_str = $this->getQueryString();
00180                 while(preg_match("/\".*?\"/",$query_str,$matches))
00181                 {
00182                         $query_str = str_replace($matches[0],'',$query_str);
00183                         $this->quoted_words[] = ilUtil::prepareDBString($matches[0]);
00184                 }
00185 
00186                 // Parse the rest
00187                 $words = explode(' ',trim($query_str));
00188                 foreach($words as $word)
00189                 {
00190                         if(!strlen(trim($word)))
00191                         {
00192                                 continue;
00193                         }
00194                         $this->quoted_words[] = ilUtil::prepareDBString($word);
00195                 }
00196         }
00197 
00198         function validate()
00199         {
00200                 // Words with less than 3 characters
00201                 if(strlen($this->getMessage()))
00202                 {
00203                         return false;
00204                 }
00205                 // No search string given
00206                 if($this->getMinWordLength() and !count($this->getWords()))
00207                 {
00208                         $this->setMessage($this->lng->txt('msg_no_search_string'));
00209                         return false;
00210                 }
00211 
00212                 return true;
00213         }
00214 }
00215 ?>

Generated on Fri Dec 13 2013 11:57:59 for ILIAS Release_3_6_x_branch .rev 46809 by  doxygen 1.7.1