]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/SearchMySQL.php
MediaWiki 1.15.5
[autoinstallsdev/mediawiki.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 string $term - Raw search term
90          * @return MySQLSearchResultSet
91          * @access public
92          */
93         function searchText( $term ) {
94                 $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), true ) ) );
95                 return new MySQLSearchResultSet( $resultSet, $this->searchTerms );
96         }
97
98         /**
99          * Perform a title-only search query and return a result set.
100          *
101          * @param string $term - Raw search term
102          * @return MySQLSearchResultSet
103          * @access public
104          */
105         function searchTitle( $term ) {
106                 $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), false ) ) );
107                 return new MySQLSearchResultSet( $resultSet, $this->searchTerms );
108         }
109
110
111         /**
112          * Return a partial WHERE clause to exclude redirects, if so set
113          * @return string
114          * @private
115          */
116         function queryRedirect() {
117                 if( $this->showRedirects ) {
118                         return '';
119                 } else {
120                         return 'AND page_is_redirect=0';
121                 }
122         }
123
124         /**
125          * Return a partial WHERE clause to limit the search to the given namespaces
126          * @return string
127          * @private
128          */
129         function queryNamespaces() {
130                 if( is_null($this->namespaces) )
131                         return '';  # search all
132                 if ( !count( $this->namespaces ) ) {
133                         $namespaces = '0';
134                 } else {
135                         $namespaces = $this->db->makeList( $this->namespaces );
136                 }
137                 return 'AND page_namespace IN (' . $namespaces . ')';
138         }
139
140         /**
141          * Return a LIMIT clause to limit results on the query.
142          * @return string
143          * @private
144          */
145         function queryLimit() {
146                 return $this->db->limitResult( '', $this->limit, $this->offset );
147         }
148
149         /**
150          * Does not do anything for generic search engine
151          * subclasses may define this though
152          * @return string
153          * @private
154          */
155         function queryRanking( $filteredTerm, $fulltext ) {
156                 return '';
157         }
158
159         /**
160          * Construct the full SQL query to do the search.
161          * The guts shoulds be constructed in queryMain()
162          * @param string $filteredTerm
163          * @param bool $fulltext
164          * @private
165          */
166         function getQuery( $filteredTerm, $fulltext ) {
167                 return $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
168                         $this->queryRedirect() . ' ' .
169                         $this->queryNamespaces() . ' ' .
170                         $this->queryRanking( $filteredTerm, $fulltext ) . ' ' .
171                         $this->queryLimit();
172         }
173
174
175         /**
176          * Picks which field to index on, depending on what type of query.
177          * @param bool $fulltext
178          * @return string
179          */
180         function getIndexField( $fulltext ) {
181                 return $fulltext ? 'si_text' : 'si_title';
182         }
183
184         /**
185          * Get the base part of the search query.
186          * The actual match syntax will depend on the server
187          * version; MySQL 3 and MySQL 4 have different capabilities
188          * in their fulltext search indexes.
189          *
190          * @param string $filteredTerm
191          * @param bool $fulltext
192          * @return string
193          * @private
194          */
195         function queryMain( $filteredTerm, $fulltext ) {
196                 $match = $this->parseQuery( $filteredTerm, $fulltext );
197                 $page        = $this->db->tableName( 'page' );
198                 $searchindex = $this->db->tableName( 'searchindex' );
199                 return 'SELECT page_id, page_namespace, page_title ' .
200                         "FROM $page,$searchindex " .
201                         'WHERE page_id=si_page AND ' . $match;
202         }
203
204         /**
205          * Create or update the search index record for the given page.
206          * Title and text should be pre-processed.
207          *
208          * @param int $id
209          * @param string $title
210          * @param string $text
211          */
212         function update( $id, $title, $text ) {
213                 $dbw = wfGetDB( DB_MASTER );
214                 $dbw->replace( 'searchindex',
215                         array( 'si_page' ),
216                         array(
217                                 'si_page' => $id,
218                                 'si_title' => $title,
219                                 'si_text' => $text
220                         ), __METHOD__ );
221         }
222
223         /**
224          * Update a search index record's title only.
225          * Title should be pre-processed.
226          *
227          * @param int $id
228          * @param string $title
229          */
230     function updateTitle( $id, $title ) {
231                 $dbw = wfGetDB( DB_MASTER );
232
233                 $dbw->update( 'searchindex',
234                         array( 'si_title' => $title ),
235                         array( 'si_page'  => $id ),
236                         __METHOD__,
237                         array( $dbw->lowPriorityOption() ) );
238         }
239 }
240
241 /**
242  * @ingroup Search
243  */
244 class MySQLSearchResultSet extends SearchResultSet {
245         function MySQLSearchResultSet( $resultSet, $terms ) {
246                 $this->mResultSet = $resultSet;
247                 $this->mTerms = $terms;
248         }
249
250         function termMatches() {
251                 return $this->mTerms;
252         }
253
254         function numRows() {
255                 return $this->mResultSet->numRows();
256         }
257
258         function next() {
259                 $row = $this->mResultSet->fetchObject();
260                 if( $row === false ) {
261                         return false;
262                 } else {
263                         return new SearchResult( $row );
264                 }
265         }
266
267         function free() {
268                 $this->mResultSet->free();
269         }
270 }