Use formatNum for counts on category page.
[lhc/web/wiklou.git] / includes / SearchMySQL.php
1 <?php
2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # http://www.gnu.org/copyleft/gpl.html
19
20 /**
21 * Search engine hook for MySQL 4+
22 * @addtogroup Search
23 */
24 class SearchMySQL extends SearchEngine {
25 var $strictMatching = true;
26
27 /** @todo document */
28 function __construct( $db ) {
29 $this->db = $db;
30 }
31
32 /** @todo document */
33 function parseQuery( $filteredText, $fulltext ) {
34 global $wgContLang;
35 $lc = SearchEngine::legalSearchChars(); // Minus format chars
36 $searchon = '';
37 $this->searchTerms = array();
38
39 # FIXME: This doesn't handle parenthetical expressions.
40 $m = array();
41 if( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
42 $filteredText, $m, PREG_SET_ORDER ) ) {
43 foreach( $m as $terms ) {
44 if( $searchon !== '' ) $searchon .= ' ';
45 if( $this->strictMatching && ($terms[1] == '') ) {
46 $terms[1] = '+';
47 }
48 $searchon .= $terms[1] . $wgContLang->stripForSearch( $terms[2] );
49 if( !empty( $terms[3] ) ) {
50 // Match individual terms in result highlighting...
51 $regexp = preg_quote( $terms[3], '/' );
52 if( $terms[4] ) $regexp .= "[0-9A-Za-z_]+";
53 } else {
54 // Match the quoted term in result highlighting...
55 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
56 }
57 $this->searchTerms[] = "\b$regexp\b";
58 }
59 wfDebug( "Would search with '$searchon'\n" );
60 wfDebug( 'Match with /\b' . implode( '\b|\b', $this->searchTerms ) . "\b/\n" );
61 } else {
62 wfDebug( "Can't understand search query '{$filteredText}'\n" );
63 }
64
65 $searchon = $this->db->strencode( $searchon );
66 $field = $this->getIndexField( $fulltext );
67 return " MATCH($field) AGAINST('$searchon' IN BOOLEAN MODE) ";
68 }
69
70 public static function legalSearchChars() {
71 return "\"*" . parent::legalSearchChars();
72 }
73
74 /**
75 * Perform a full text search query and return a result set.
76 *
77 * @param string $term - Raw search term
78 * @return MySQLSearchResultSet
79 * @access public
80 */
81 function searchText( $term ) {
82 $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), true ) ) );
83 return new MySQLSearchResultSet( $resultSet, $this->searchTerms );
84 }
85
86 /**
87 * Perform a title-only search query and return a result set.
88 *
89 * @param string $term - Raw search term
90 * @return MySQLSearchResultSet
91 * @access public
92 */
93 function searchTitle( $term ) {
94 $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), false ) ) );
95 return new MySQLSearchResultSet( $resultSet, $this->searchTerms );
96 }
97
98
99 /**
100 * Return a partial WHERE clause to exclude redirects, if so set
101 * @return string
102 * @private
103 */
104 function queryRedirect() {
105 if( $this->showRedirects ) {
106 return '';
107 } else {
108 return 'AND page_is_redirect=0';
109 }
110 }
111
112 /**
113 * Return a partial WHERE clause to limit the search to the given namespaces
114 * @return string
115 * @private
116 */
117 function queryNamespaces() {
118 $namespaces = implode( ',', $this->namespaces );
119 if ($namespaces == '') {
120 $namespaces = '0';
121 }
122 return 'AND page_namespace IN (' . $namespaces . ')';
123 }
124
125 /**
126 * Return a LIMIT clause to limit results on the query.
127 * @return string
128 * @private
129 */
130 function queryLimit() {
131 return $this->db->limitResult( '', $this->limit, $this->offset );
132 }
133
134 /**
135 * Does not do anything for generic search engine
136 * subclasses may define this though
137 * @return string
138 * @private
139 */
140 function queryRanking( $filteredTerm, $fulltext ) {
141 return '';
142 }
143
144 /**
145 * Construct the full SQL query to do the search.
146 * The guts shoulds be constructed in queryMain()
147 * @param string $filteredTerm
148 * @param bool $fulltext
149 * @private
150 */
151 function getQuery( $filteredTerm, $fulltext ) {
152 return $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
153 $this->queryRedirect() . ' ' .
154 $this->queryNamespaces() . ' ' .
155 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' .
156 $this->queryLimit();
157 }
158
159
160 /**
161 * Picks which field to index on, depending on what type of query.
162 * @param bool $fulltext
163 * @return string
164 */
165 function getIndexField( $fulltext ) {
166 return $fulltext ? 'si_text' : 'si_title';
167 }
168
169 /**
170 * Get the base part of the search query.
171 * The actual match syntax will depend on the server
172 * version; MySQL 3 and MySQL 4 have different capabilities
173 * in their fulltext search indexes.
174 *
175 * @param string $filteredTerm
176 * @param bool $fulltext
177 * @return string
178 * @private
179 */
180 function queryMain( $filteredTerm, $fulltext ) {
181 $match = $this->parseQuery( $filteredTerm, $fulltext );
182 $page = $this->db->tableName( 'page' );
183 $searchindex = $this->db->tableName( 'searchindex' );
184 return 'SELECT page_id, page_namespace, page_title ' .
185 "FROM $page,$searchindex " .
186 'WHERE page_id=si_page AND ' . $match;
187 }
188
189 /**
190 * Create or update the search index record for the given page.
191 * Title and text should be pre-processed.
192 *
193 * @param int $id
194 * @param string $title
195 * @param string $text
196 */
197 function update( $id, $title, $text ) {
198 $dbw = wfGetDB( DB_MASTER );
199 $dbw->replace( 'searchindex',
200 array( 'si_page' ),
201 array(
202 'si_page' => $id,
203 'si_title' => $title,
204 'si_text' => $text
205 ), __METHOD__ );
206 }
207
208 /**
209 * Update a search index record's title only.
210 * Title should be pre-processed.
211 *
212 * @param int $id
213 * @param string $title
214 */
215 function updateTitle( $id, $title ) {
216 $dbw = wfGetDB( DB_MASTER );
217
218 $dbw->update( 'searchindex',
219 array( 'si_title' => $title ),
220 array( 'si_page' => $id ),
221 __METHOD__,
222 array( $dbw->lowPriorityOption() ) );
223 }
224 }
225
226 /**
227 * @addtogroup Search
228 */
229 class MySQLSearchResultSet extends SearchResultSet {
230 function MySQLSearchResultSet( $resultSet, $terms ) {
231 $this->mResultSet = $resultSet;
232 $this->mTerms = $terms;
233 }
234
235 function termMatches() {
236 return $this->mTerms;
237 }
238
239 function numRows() {
240 return $this->mResultSet->numRows();
241 }
242
243 function next() {
244 $row = $this->mResultSet->fetchObject();
245 if( $row === false ) {
246 return false;
247 } else {
248 return new SearchResult( $row );
249 }
250 }
251
252 function free() {
253 $this->mResultSet->free();
254 }
255 }
256
257