• 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 class ilQueryParser
00036 {
00037         var $lng = null;
00038 
00039 
00040         var $query_str;
00041         var $quoted_words = array();
00042         var $message; // Translated error message
00043         var $combination; // combiniation of search words e.g 'and' or 'or'
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                 // Parse strings like && 'A "B C D" E' as 'A' && 'B C D' && 'E'
00149                 // Can be used in LIKE search or fulltext search > MYSQL 4.0
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                 // Parse the rest
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                 // Words with less than 3 characters
00183                 if(strlen($this->getMessage()))
00184                 {
00185                         return false;
00186                 }
00187                 // No search string given
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 ?>

Generated on Fri Dec 13 2013 10:18:31 for ILIAS Release_3_5_x_branch .rev 46805 by  doxygen 1.7.1