3 * FusionForge search engine
5 * Copyright 1999-2001, VA Linux Systems, Inc
6 * Copyright 2004, Guillaume Smet/Open Wide
7 * Copyright 2009, Roland Mas
8 * Copyright 2010-2011, Franck Villaume - Capgemini
9 * Copyright (C) 2012 Alain Peyrat - Alcatel-Lucent
11 * This file is part of FusionForge. FusionForge is free software;
12 * you can redistribute it and/or modify it under the terms of the
13 * GNU General Public License as published by the Free Software
14 * Foundation; either version 2 of the Licence, or (at your option)
17 * FusionForge is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 class SearchQuery extends Error {
29 * the operator between each part of the query. Can be AND or OR.
31 * @var string $operator
35 * Number of rows per page
37 * @var int $rowsPerPage
41 * Number of rows we will display on the page
47 * Number of rows returned by the query
49 * @var int $rowsTotalCount
51 var $rowsTotalCount = 0;
61 * @var resource $result
65 * When search by id is enabled, the id to search for
69 var $searchId = false;
71 * if we want to search for all the words or if only one is sufficient
73 * @var boolean $isExact
77 * sections to search in
79 * @var array $sections
81 var $sections = SEARCH__ALL_SECTIONS;
85 var $phrases = array();
87 // Something that's hopefully not going to end up in real data
88 var $field_separator = ' ioM0Thu6_fieldseparator_kaeph9Ee ';
93 * @param string $words words we are searching for
94 * @param int $offset offset
95 * @param boolean $isExact if we want to search for all the words or if only one is sufficient
96 * @param int $rowsPerPage number of rows per page
98 function __construct($words, $offset, $isExact, $rowsPerPage = SEARCH__DEFAULT_ROWS_PER_PAGE) {
99 $this->field_separator = ' ioM0Thu6_fieldseparator_kaeph9Ee ';
101 $this->cleanSearchWords($words);
102 //We manual escap because every Query in Search escap parameters
103 $words = addslashes($words);
104 if (is_array ($this->words)){
105 $this->words = array_map ('addslashes',$this->words);
107 $this->words = array();
109 if (is_array ($this->phrases)){
110 $this->phrases = array_map ('addslashes',$this->phrases);
112 $this->phrases = array();
114 $this->rowsPerPage = $rowsPerPage;
115 $this->offset = $offset;
116 $this->isExact = $isExact;
117 $this->operator = $this->getOperator();
121 * cleanSearchWords - clean the words we are searching for
123 * @param string $words words we are searching for
125 function cleanSearchWords($words) {
126 $words = trim($words);
128 $this->setError(_('Error: criteria not specified'));
131 if(is_numeric($words) && $this->implementsSearchById()) {
132 $this->searchId = (int) $words;
134 $words = preg_replace("/[ \t]+/", ' ', $words);
135 if(strlen($words) < 3) {
136 $this->setError(_('Error: search query too short'));
139 $words = htmlspecialchars($words);
140 $words = strtr($words, array('%' => '\%', '_' => '\_'));
143 foreach(explode(' ', quotemeta($words)) as $word) {
145 if(substr($word, -1) == "'") {
146 $word = substr($word, 0, -1);
148 $phrase .= ' '.$word;
149 $this->phrases[] = $phrase;
151 $phrase .= ' '.$word;
154 if(substr($word, 0, 1) == "'") {
155 $word = substr($word, 1);
157 if(substr($word, -1) == "'") {
158 // This is a special case where the phrase is just one word
159 $word = substr($word, 0, -1);
161 $this->words[] = $word;
166 $this->words[] = $word;
174 * executeQuery - execute the SQL query to get the results
176 function executeQuery() {
178 if($this->searchId) {
179 $qpa = $this->getSearchByIdQuery();
181 $qpa = $this->getQuery();
184 $this->result = db_query_qpa (
186 $this->rowsPerPage + 1,
190 $this->rowsTotalCount = db_numrows($this->result);
191 $this->rowsCount = min($this->rowsPerPage, $this->rowsTotalCount);
195 * getQuery - returns the query built to get the search results
196 * This is an abstract method. It _MUST_ be implemented in children classes.
198 * @return array query+params array
200 function getQuery() {
204 function addMatchCondition($qpa, $fieldName) {
205 if(!count($this->phrases)) {
206 $qpa = db_construct_qpa ($qpa, 'TRUE') ;
210 $regexs = array_map ('strtolower',
211 array_merge ($this->phrases,
212 str_replace(' ', "\s+", $this->phrases)));
214 for ($i = 0; $i < count ($regexs); $i++) {
216 $qpa = db_construct_qpa ($qpa,
219 $qpa = db_construct_qpa ($qpa,
221 array ($regexs[$i])) ;
226 function addIlikeCondition($qpa, $fieldName) {
227 $wordArgs = array_map ('strtolower',
228 array_merge($this->words, $this->phrases));
230 for ($i = 0; $i < count ($wordArgs); $i++) {
232 $qpa = db_construct_qpa ($qpa,
235 $qpa = db_construct_qpa ($qpa,
236 'lower ('.$fieldName.') LIKE $1',
237 array ('%'.$wordArgs[$i].'%')) ;
243 * getOperator - get the operator we have to use in ILIKE condition
245 * @return string AND if it is an exact search, OR otherwise
247 function getOperator() {
256 * implementsSearchById - check if the current object implements the search by id feature by having a getSearchByIdQuery method
258 * @return boolean true if our object implements search by id, false otherwise.
260 function implementsSearchById() {
261 return method_exists($this, 'getSearchByIdQuery');
265 * getResult - returns the result set
267 * @return resource result set
269 function & getResult() {
270 return $this->result;
274 * getRowsCount - returns number of rows for the current page
276 * @return int rows count for the current page
278 function getRowsCount() {
279 return $this->rowsCount;
283 * getRowsTotalCount - returns total number of rows
285 * @return int rows count
287 function getRowsTotalCount() {
288 return $this->rowsTotalCount;
292 * getOffset - returns the offset
296 function getOffset() {
297 return $this->offset;
301 * getRowsPerPage - returns number of rows per page
303 * @return int number of rows per page
305 function getRowsPerPage() {
306 return $this->rowsPerPage;
310 * getWords - returns the array containing words we are searching for
312 * @return array words we are searching for
314 function getWords() {
319 * getPhrases - returns the array containing phrases we are searching for
321 * @return array phrases we are searching for
323 function getPhrases() {
324 return $this->phrases;
328 * setSections - set the sections list
330 * @param $sections mixed array of sections or SEARCH__ALL_SECTIONS
332 function setSections($sections) {
333 if(is_array($sections)) {
334 $this->sections = array_values($sections) ;
336 $this->sections = $sections;
341 * getFTIwords - get words formatted in order to be used in the FTI stored procedures
343 * @return string words we are searching for, separated by
345 function getFTIwords() {
346 $bits = $this->words;
347 foreach ($this->phrases as $p) {
348 $bits[] = '('.implode ('&', explode (' ', $p)).')';
350 if ($this->isExact) {
351 $query = implode('&', $bits);
353 $query = implode('|', $bits);
361 // c-file-style: "bsd"