/
Mysql.php
171 lines (154 loc) · 5.16 KB
/
Mysql.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
* phpMyFAQ MySQL (ext/mysql) search classes
*
* PHP Version 5.2
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* @category phpMyFAQ
* @package PMF_Search_Database
* @author Thorsten Rinne <thorsten@phpmyfaq.de>
* @copyright 2010-2013 phpMyFAQ Team
* @license http://www.mozilla.org/MPL/MPL-1.1.html Mozilla Public License Version 1.1
* @link http://www.phpmyfaq.de
* @since 2010-06-06
*/
if (!defined('IS_VALID_PHPMYFAQ')) {
exit();
}
/**
* PMF_Search_Database_Mysql
*
* @category phpMyFAQ
* @package PMF_Search_Database
* @author Thorsten Rinne <thorsten@phpmyfaq.de>
* @copyright 2010-2013 phpMyFAQ Team
* @license http://www.mozilla.org/MPL/MPL-1.1.html Mozilla Public License Version 1.1
* @link http://www.phpmyfaq.de
* @since 2010-06-06
*/
class PMF_Search_Database_Mysql extends PMF_Search_Database
{
/**
* Constructor
*
* @param PMF_Language $language Language
*
* @return PMF_Search_Abstract
*/
public function __construct(PMF_Language $language)
{
parent::__construct($language);
}
/**
* Prepares the search and executes it
*
* @param string $searchTerm Search term
*
* @return resource
*
* @throws PMF_Search_Exception
*/
public function search($searchTerm)
{
if (is_numeric($searchTerm)) {
parent::search($searchTerm);
} else {
$enableRelevance = PMF_Configuration::getInstance()->get('search.enableRelevance');
$columns = $this->getResultColumns();
$columns .= ($enableRelevance) ? $this->getMatchingColumnsAsResult($searchTerm) : '';
$orderBy = ($enableRelevance) ? 'ORDER BY ' . $this->getMatchingOrder() . ' DESC' : '';
$chars = array (chr(150), chr(147), chr(148), chr(146), chr(34), '"', '"');
$replace = array ("-", "\"", "\"", "'", "\"" , "\"", "\"");
$searchTerm = str_replace ($chars, $replace, $searchTerm);
$query = sprintf("
SELECT
%s
FROM
%s %s %s
WHERE
MATCH (%s) AGAINST ('%s' IN BOOLEAN MODE)
%s
%s",
$columns,
$this->getTable(),
$this->getJoinedTable(),
$this->getJoinedColumns(),
$this->getMatchingColumns(),
$this->dbHandle->escape_string($searchTerm),
$this->getConditions(),
$orderBy
);
$this->resultSet = $this->dbHandle->query($query);
// Fallback for searches with less than three characters
if (0 == $this->dbHandle->num_rows($this->resultSet)) {
$query = sprintf("
SELECT
%s
FROM
%s %s %s
WHERE
%s
%s",
$this->getResultColumns(),
$this->getTable(),
$this->getJoinedTable(),
$this->getJoinedColumns(),
$this->getMatchClause($searchTerm),
$this->getConditions());
}
$this->resultSet = $this->dbHandle->query($query);
}
return $this->resultSet;
}
/**
* Add the matching columns into the columns for the resultset
*
* @return PMF_Search_Database
*/
public function getMatchingColumnsAsResult($searchterm)
{
$resultColumns = '';
foreach ($this->matchingColumns as $matchColumn) {
$column = sprintf("MATCH (%s) AGAINST ('*%s*' IN BOOLEAN MODE) AS rel_%s",
$matchColumn,
$this->dbHandle->escape_string($searchterm),
substr(strstr($matchColumn, '.'), 1));
$resultColumns .= ', ' . $column;
}
return $resultColumns;
}
/**
* Returns the part of the SQL query with the order by
*
* The order is calculate by weight depend on the search.relevance order
*
* @return string
*/
public function getMatchingOrder()
{
$config = PMF_Configuration::getInstance()->get('search.relevance');
$list = explode(",", $config);
$count = count($list);
$order = '';
foreach ($list as $field) {
$string = '(rel_' . $field . '*' . $count .')';
if (empty($order)) {
$order .= $string;
} else {
$order .= '+' . $string;
}
$count--;
}
return $order;
}
}