Revert r49669, r49670 "extract text layer from djvu file (see bug 18046)"
[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 * @file
22 * @ingroup Search
23 */
24
25 /**
26 * Search engine hook for MySQL 4+
27 * @ingroup Search
28 */
29 class SearchMySQL extends SearchEngine {
30 var $strictMatching = true;
31
32 /** @todo document */
33 function __construct( $db ) {
34 $this->db = $db;
35 }
36
37 /**
38 * Parse the user's query and transform it into an SQL fragment which will
39 * become part of a WHERE clause
40 */
41 function parseQuery( $filteredText, $fulltext ) {
42 global $wgContLang;
43 $lc = SearchEngine::legalSearchChars(); // Minus format chars
44 $searchon = '';
45 $this->searchTerms = array();
46
47 # FIXME: This doesn't handle parenthetical expressions.
48 $m = array();
49 if( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
50 $filteredText, $m, PREG_SET_ORDER ) ) {
51 foreach( $m as $terms ) {
52 if( $searchon !== '' ) $searchon .= ' ';
53 if( $this->strictMatching && ($terms[1] == '') ) {
54 $terms[1] = '+';
55 }
56 $searchon .= $terms[1] . $wgContLang->stripForSearch( $terms[2] );
57 if( !empty( $terms[3] ) ) {
58 // Match individual terms in result highlighting...
59 $regexp = preg_quote( $terms[3], '/' );
60 if( $terms[4] ) {
61 $regexp = "\b$regexp"; // foo*
62 } else {
63 $regexp = "\b$regexp\b";
64 }
65 } else {
66 // Match the quoted term in result highlighting...
67 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
68 }
69 $this->searchTerms[] = $regexp;
70 }
71 wfDebug( "Would search with '$searchon'\n" );
72 wfDebug( 'Match with /' . implode( '|', $this->searchTerms ) . "/\n" );
73 } else {
74 wfDebug( "Can't understand search query '{$filteredText}'\n" );
75 }
76
77 $searchon = $this->db->strencode( $searchon );
78 $field = $this->getIndexField( $fulltext );
79 return " MATCH($field) AGAINST('$searchon' IN BOOLEAN MODE) ";
80 }
81
82 public static function legalSearchChars() {
83 return "\"*" . parent::legalSearchChars();
84 }
85
86 /**
87 * Perform a full text search query and return a result set.
88 *
89 * @param $term String: raw search term
90 * @return MySQLSearchResultSet
91 */
92 function searchText( $term ) {
93 $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), true ) ) );
94 return new MySQLSearchResultSet( $resultSet, $this->searchTerms );
95 }
96
97 /**
98 * Perform a title-only search query and return a result set.
99 *
100 * @param $term String: raw search term
101 * @return MySQLSearchResultSet
102 */
103 function searchTitle( $term ) {
104 $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), false ) ) );
105 return new MySQLSearchResultSet( $resultSet, $this->searchTerms );
106 }
107
108
109 /**
110 * Return a partial WHERE clause to exclude redirects, if so set
111 * @return String
112 */
113 function queryRedirect() {
114 if( $this->showRedirects ) {
115 return '';
116 } else {
117 return 'AND page_is_redirect=0';
118 }
119 }
120
121 /**
122 * Return a partial WHERE clause to limit the search to the given namespaces
123 * @return String
124 */
125 function queryNamespaces() {
126 if( is_null($this->namespaces) )
127 return ''; # search all
128 if ( !count( $this->namespaces ) ) {
129 $namespaces = '0';
130 } else {
131 $namespaces = $this->db->makeList( $this->namespaces );
132 }
133 return 'AND page_namespace IN (' . $namespaces . ')';
134 }
135
136 /**
137 * Return a LIMIT clause to limit results on the query.
138 * @return String
139 */
140 function queryLimit() {
141 return $this->db->limitResult( '', $this->limit, $this->offset );
142 }
143
144 /**
145 * Does not do anything for generic search engine
146 * subclasses may define this though
147 * @return String
148 */
149 function queryRanking( $filteredTerm, $fulltext ) {
150 return '';
151 }
152
153 /**
154 * Construct the full SQL query to do the search.
155 * The guts shoulds be constructed in queryMain()
156 * @param $filteredTerm String
157 * @param $fulltext Boolean
158 */
159 function getQuery( $filteredTerm, $fulltext ) {
160 return $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
161 $this->queryRedirect() . ' ' .
162 $this->queryNamespaces() . ' ' .
163 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' .
164 $this->queryLimit();
165 }
166
167
168 /**
169 * Picks which field to index on, depending on what type of query.
170 * @param $fulltext Boolean
171 * @return String
172 */
173 function getIndexField( $fulltext ) {
174 return $fulltext ? 'si_text' : 'si_title';
175 }
176
177 /**
178 * Get the base part of the search query.
179 * The actual match syntax will depend on the server
180 * version; MySQL 3 and MySQL 4 have different capabilities
181 * in their fulltext search indexes.
182 *
183 * @param $filteredTerm String
184 * @param $fulltext Boolean
185 * @return String
186 */
187 function queryMain( $filteredTerm, $fulltext ) {
188 $match = $this->parseQuery( $filteredTerm, $fulltext );
189 $page = $this->db->tableName( 'page' );
190 $searchindex = $this->db->tableName( 'searchindex' );
191 return 'SELECT page_id, page_namespace, page_title ' .
192 "FROM $page,$searchindex " .
193 'WHERE page_id=si_page AND ' . $match;
194 }
195
196 /**
197 * Create or update the search index record for the given page.
198 * Title and text should be pre-processed.
199 *
200 * @param $id Integer
201 * @param $title String
202 * @param $text String
203 */
204 function update( $id, $title, $text ) {
205 $dbw = wfGetDB( DB_MASTER );
206 $dbw->replace( 'searchindex',
207 array( 'si_page' ),
208 array(
209 'si_page' => $id,
210 'si_title' => $title,
211 'si_text' => $text
212 ), __METHOD__ );
213 }
214
215 /**
216 * Update a search index record's title only.
217 * Title should be pre-processed.
218 *
219 * @param $id Integer
220 * @param $title String
221 */
222 function updateTitle( $id, $title ) {
223 $dbw = wfGetDB( DB_MASTER );
224
225 $dbw->update( 'searchindex',
226 array( 'si_title' => $title ),
227 array( 'si_page' => $id ),
228 __METHOD__,
229 array( $dbw->lowPriorityOption() ) );
230 }
231 }
232
233 /**
234 * @ingroup Search
235 */
236 class MySQLSearchResultSet extends SearchResultSet {
237 function MySQLSearchResultSet( $resultSet, $terms ) {
238 $this->mResultSet = $resultSet;
239 $this->mTerms = $terms;
240 }
241
242 function termMatches() {
243 return $this->mTerms;
244 }
245
246 function numRows() {
247 return $this->mResultSet->numRows();
248 }
249
250 function next() {
251 $row = $this->mResultSet->fetchObject();
252 if( $row === false ) {
253 return false;
254 } else {
255 return new SearchResult( $row );
256 }
257 }
258
259 function free() {
260 $this->mResultSet->free();
261 }
262 }