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 * if we want to search for all the words or if only one is sufficient
67 * @var boolean $isExact
71 * sections to search in
73 * @var array $sections
75 var $sections = SEARCH__ALL_SECTIONS;
79 var $phrases = array();
81 // Something that's hopefully not going to end up in real data
82 var $field_separator = ' ioM0Thu6_fieldseparator_kaeph9Ee ';
87 * @param string $words words we are searching for
88 * @param int $offset offset
89 * @param boolean $isExact if we want to search for all the words or if only one is sufficient
90 * @param int $rowsPerPage number of rows per page
92 function __construct($words, $offset, $isExact, $rowsPerPage = SEARCH__DEFAULT_ROWS_PER_PAGE) {
94 $this->cleanSearchWords($words);
95 //We manual escap because every Query in Search escap parameters
96 $words = addslashes($words);
97 if (is_array ($this->words)){
98 $this->words = array_map ('addslashes',$this->words);
100 $this->words = array();
102 if (is_array ($this->phrases)){
103 $this->phrases = array_map ('addslashes',$this->phrases);
105 $this->phrases = array();
107 $this->rowsPerPage = $rowsPerPage;
108 $this->offset = $offset;
109 $this->isExact = $isExact;
110 $this->operator = $this->getOperator();
114 * cleanSearchWords - clean the words we are searching for
116 * @param string $words words we are searching for
118 function cleanSearchWords($words) {
119 $words = trim($words);
121 $this->setError(_('Error: criteria not specified'));
125 $words = preg_replace("/[ \t]+/", ' ', $words);
126 if(strlen($words) < 3) {
127 $this->setError(_('Error: search query too short'));
130 $words = htmlspecialchars($words);
131 $words = strtr($words, array('%' => '\%', '_' => '\_'));
134 foreach(explode(' ', quotemeta($words)) as $word) {
136 if(substr($word, -1) == "'") {
137 $word = substr($word, 0, -1);
139 $phrase .= ' '.$word;
140 $this->phrases[] = $phrase;
142 $phrase .= ' '.$word;
145 if(substr($word, 0, 1) == "'") {
146 $word = substr($word, 1);
148 if(substr($word, -1) == "'") {
149 // This is a special case where the phrase is just one word
150 $word = substr($word, 0, -1);
152 $this->words[] = $word;
157 $this->words[] = $word;
164 * executeQuery - execute the SQL query to get the results
166 function executeQuery() {
168 $this->result = db_query_qpa (
170 $this->rowsPerPage + 1,
174 $this->rowsTotalCount = db_numrows($this->result);
175 $this->rowsCount = min($this->rowsPerPage, $this->rowsTotalCount);
179 * getQuery - returns the query built to get the search results
180 * This is an abstract method. It _MUST_ be implemented in children classes.
182 * @return array query+params array
184 function getQuery() {
188 function addMatchCondition($qpa, $fieldName) {
190 if(!count($this->phrases)) {
191 $qpa = db_construct_qpa ($qpa, 'TRUE') ;
196 foreach ($this->phrases as $p) {
197 $regexs[] = strtolower (preg_replace ("/\s+/", "\s+", $p));
200 for ($i = 0; $i < count ($regexs); $i++) {
202 $qpa = db_construct_qpa ($qpa,
205 $qpa = db_construct_qpa ($qpa,
207 array ($regexs[$i])) ;
212 function addIlikeCondition($qpa, $fieldName) {
213 $wordArgs = array_map ('strtolower',
214 array_merge($this->words, $this->phrases));
216 for ($i = 0; $i < count ($wordArgs); $i++) {
218 $qpa = db_construct_qpa ($qpa,
221 $qpa = db_construct_qpa ($qpa,
222 'lower ('.$fieldName.') LIKE $1',
223 array ('%'.$wordArgs[$i].'%')) ;
229 * getOperator - get the operator we have to use in ILIKE condition
231 * @return string AND if it is an exact search, OR otherwise
233 function getOperator() {
242 * getResult - returns the result set
244 * @return resource result set
246 function & getResult() {
247 return $this->result;
251 * getRowsCount - returns number of rows for the current page
253 * @return int rows count for the current page
255 function getRowsCount() {
256 return $this->rowsCount;
260 * getRowsTotalCount - returns total number of rows
262 * @return int rows count
264 function getRowsTotalCount() {
265 return $this->rowsTotalCount;
269 * getOffset - returns the offset
273 function getOffset() {
274 return $this->offset;
278 * getRowsPerPage - returns number of rows per page
280 * @return int number of rows per page
282 function getRowsPerPage() {
283 return $this->rowsPerPage;
287 * getWords - returns the array containing words we are searching for
289 * @return array words we are searching for
291 function getWords() {
296 * getPhrases - returns the array containing phrases we are searching for
298 * @return array phrases we are searching for
300 function getPhrases() {
301 return $this->phrases;
305 * setSections - set the sections list
307 * @param $sections mixed array of sections or SEARCH__ALL_SECTIONS
309 function setSections($sections) {
310 if(is_array($sections)) {
311 $this->sections = array_values($sections) ;
313 $this->sections = $sections;
318 * getFTIwords - get words formatted in order to be used in the FTI stored procedures
320 * @return string words we are searching for, separated by
322 function getFTIwords() {
323 $bits = $this->words;
324 foreach ($this->phrases as $p) {
325 $bits[] = '('.implode ('&', explode (' ', $p)).')';
327 if ($this->isExact) {
328 $query = implode('&', $bits);
330 $query = implode('|', $bits);
338 // c-file-style: "bsd"