3 * SQLite search backend, based upon SearchMysql
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.
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.
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
25 * Search engine hook for SQLite
28 class SearchSqlite
extends SearchDatabase
{
30 * Whether fulltext search is supported by current schema
33 function fulltextSearchSupported() {
34 return $this->db
->checkForEnabledSearch();
38 * Parse the user's query and transform it into an SQL fragment which will
39 * become part of a WHERE clause
43 function parseQuery( $filteredText, $fulltext ) {
45 $lc = SearchEngine
::legalSearchChars(); // Minus format chars
47 $this->searchTerms
= array();
50 if ( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
51 $filteredText, $m, PREG_SET_ORDER
) ) {
52 foreach ( $m as $bits ) {
53 @list
( /* all */, $modifier, $term, $nonQuoted, $wildcard ) = $bits;
55 if ( $nonQuoted != '' ) {
59 $term = str_replace( '"', '', $term );
63 if ( $searchon !== '' ) {
67 // Some languages such as Serbian store the input form in the search index,
68 // so we may need to search for matches in multiple writing system variants.
69 $convertedVariants = $wgContLang->autoConvertToAllVariants( $term );
70 if ( is_array( $convertedVariants ) ) {
71 $variants = array_unique( array_values( $convertedVariants ) );
73 $variants = array( $term );
76 // The low-level search index does some processing on input to work
77 // around problems with minimum lengths and encoding in MySQL's
79 // For Chinese this also inserts spaces between adjacent Han characters.
80 $strippedVariants = array_map(
81 array( $wgContLang, 'normalizeForSearch' ),
84 // Some languages such as Chinese force all variants to a canonical
85 // form when stripping to the low-level search index, so to be sure
86 // let's check our variants list for unique items after stripping.
87 $strippedVariants = array_unique( $strippedVariants );
89 $searchon .= $modifier;
90 if ( count( $strippedVariants ) > 1 ) {
93 foreach ( $strippedVariants as $stripped ) {
94 if ( $nonQuoted && strpos( $stripped, ' ' ) !== false ) {
95 // Hack for Chinese: we need to toss in quotes for
96 // multiple-character phrases since normalizeForSearch()
97 // added spaces between them to make word breaks.
98 $stripped = '"' . trim( $stripped ) . '"';
100 $searchon .= "$quote$stripped$quote$wildcard ";
102 if ( count( $strippedVariants ) > 1 ) {
106 // Match individual terms or quoted phrase in result highlighting...
107 // Note that variants will be introduced in a later stage for highlighting!
108 $regexp = $this->regexTerm( $term, $wildcard );
109 $this->searchTerms
[] = $regexp;
113 wfDebug( __METHOD__
. ": Can't understand search query '{$filteredText}'\n" );
116 $searchon = $this->db
->strencode( $searchon );
117 $field = $this->getIndexField( $fulltext );
118 return " $field MATCH '$searchon' ";
121 function regexTerm( $string, $wildcard ) {
124 $regex = preg_quote( $string, '/' );
125 if ( $wgContLang->hasWordBreaks() ) {
127 // Don't cut off the final bit!
130 $regex = "\b$regex\b";
133 // For Chinese, words may legitimately abut other words in the text literal.
134 // Don't add \b boundary checks... note this could cause false positives
140 public static function legalSearchChars() {
141 return "\"*" . parent
::legalSearchChars();
145 * Perform a full text search query and return a result set.
147 * @param string $term Raw search term
148 * @return SqlSearchResultSet
150 function searchText( $term ) {
151 return $this->searchInternal( $term, true );
155 * Perform a title-only search query and return a result set.
157 * @param string $term Raw search term
158 * @return SqlSearchResultSet
160 function searchTitle( $term ) {
161 return $this->searchInternal( $term, false );
164 protected function searchInternal( $term, $fulltext ) {
165 global $wgCountTotalSearchHits, $wgContLang;
167 if ( !$this->fulltextSearchSupported() ) {
171 $filteredTerm = $this->filter( $wgContLang->lc( $term ) );
172 $resultSet = $this->db
->query( $this->getQuery( $filteredTerm, $fulltext ) );
175 if ( $wgCountTotalSearchHits ) {
176 $totalResult = $this->db
->query( $this->getCountQuery( $filteredTerm, $fulltext ) );
177 $row = $totalResult->fetchObject();
179 $total = intval( $row->c
);
181 $totalResult->free();
184 return new SqlSearchResultSet( $resultSet, $this->searchTerms
, $total );
188 * Return a partial WHERE clause to limit the search to the given namespaces
191 function queryNamespaces() {
192 if ( is_null( $this->namespaces
) ) {
193 return ''; # search all
195 if ( !count( $this->namespaces
) ) {
198 $namespaces = $this->db
->makeList( $this->namespaces
);
200 return 'AND page_namespace IN (' . $namespaces . ')';
204 * Returns a query with limit for number of results set.
208 function limitResult( $sql ) {
209 return $this->db
->limitResult( $sql, $this->limit
, $this->offset
);
213 * Construct the full SQL query to do the search.
214 * The guts shoulds be constructed in queryMain()
215 * @param string $filteredTerm
216 * @param bool $fulltext
219 function getQuery( $filteredTerm, $fulltext ) {
220 return $this->limitResult(
221 $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
222 $this->queryNamespaces()
227 * Picks which field to index on, depending on what type of query.
228 * @param bool $fulltext
231 function getIndexField( $fulltext ) {
232 return $fulltext ?
'si_text' : 'si_title';
236 * Get the base part of the search query.
238 * @param string $filteredTerm
239 * @param bool $fulltext
242 function queryMain( $filteredTerm, $fulltext ) {
243 $match = $this->parseQuery( $filteredTerm, $fulltext );
244 $page = $this->db
->tableName( 'page' );
245 $searchindex = $this->db
->tableName( 'searchindex' );
246 return "SELECT $searchindex.rowid, page_namespace, page_title " .
247 "FROM $page,$searchindex " .
248 "WHERE page_id=$searchindex.rowid AND $match";
251 function getCountQuery( $filteredTerm, $fulltext ) {
252 $match = $this->parseQuery( $filteredTerm, $fulltext );
253 $page = $this->db
->tableName( 'page' );
254 $searchindex = $this->db
->tableName( 'searchindex' );
255 return "SELECT COUNT(*) AS c " .
256 "FROM $page,$searchindex " .
257 "WHERE page_id=$searchindex.rowid AND $match " .
258 $this->queryNamespaces();
262 * Create or update the search index record for the given page.
263 * Title and text should be pre-processed.
266 * @param string $title
267 * @param string $text
269 function update( $id, $title, $text ) {
270 if ( !$this->fulltextSearchSupported() ) {
273 // @todo find a method to do it in a single request,
274 // couldn't do it so far due to typelessness of FTS3 tables.
275 $dbw = wfGetDB( DB_MASTER
);
277 $dbw->delete( 'searchindex', array( 'rowid' => $id ), __METHOD__
);
279 $dbw->insert( 'searchindex',
282 'si_title' => $title,
288 * Update a search index record's title only.
289 * Title should be pre-processed.
292 * @param string $title
294 function updateTitle( $id, $title ) {
295 if ( !$this->fulltextSearchSupported() ) {
298 $dbw = wfGetDB( DB_MASTER
);
300 $dbw->update( 'searchindex',
301 array( 'si_title' => $title ),
302 array( 'rowid' => $id ),