Change the SearchEngine interface around:
[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 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # http://www.gnu.org/copyleft/gpl.html
19
20 /**
21 * Search engine hook base class for MySQL.
22 * Specific bits for MySQL 3 and 4 variants are in child classes.
23 * @package MediaWiki
24 * @subpackage Search
25 */
26
27 /** */
28 require_once( 'SearchEngine.php' );
29
30 class SearchMySQL extends SearchEngine {
31 /**
32 * Perform a full text search query and return a result set.
33 *
34 * @param string $term - Raw search term
35 * @return MySQLSearchResultSet
36 * @access public
37 */
38 function searchText( $term ) {
39 $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), true ) ) );
40 return new MySQLSearchResultSet( $resultSet, $this->searchTerms );
41 }
42
43 /**
44 * Perform a title-only search query and return a result set.
45 *
46 * @param string $term - Raw search term
47 * @return MySQLSearchResultSet
48 * @access public
49 */
50 function searchTitle( $term ) {
51 $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), false ) ) );
52 return new MySQLSearchResultSet( $resultSet, $this->searchTerms );
53 }
54
55
56 /**
57 * Return a partial WHERE clause to exclude redirects, if so set
58 * @return string
59 * @access private
60 */
61 function queryRedirect() {
62 if( $this->showRedirects ) {
63 return 'AND cur_is_redirect=0';
64 } else {
65 return '';
66 }
67 }
68
69 /**
70 * Return a partial WHERE clause to limit the search to the given namespaces
71 * @return string
72 * @access private
73 */
74 function queryNamespaces() {
75 $namespaces = implode( ',', $this->namespaces );
76 if ($namespaces == '') {
77 $namespaces = '0';
78 }
79 return 'AND page_namespace IN (' . $namespaces . ')';
80 }
81
82 /**
83 * Return a LIMIT clause to limit results on the query.
84 * @return string
85 * @access private
86 */
87 function queryLimit() {
88 return $this->db->limitResult( $this->limit, $this->offset );
89 }
90
91 /**
92 * Does not do anything for generic search engine
93 * subclasses may define this though
94 * @return string
95 * @access private
96 */
97 function queryRanking( $filteredTerm, $fulltext ) {
98 return "";
99 }
100
101 /**
102 * Construct the full SQL query to do the search.
103 * The guts shoulds be constructed in queryMain()
104 * @param string $filteredTerm
105 * @param bool $fulltext
106 * @access private
107 */
108 function getQuery( $filteredTerm, $fulltext ) {
109 return $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
110 $this->queryRedirect() . ' ' .
111 $this->queryNamespaces() . ' ' .
112 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' .
113 $this->queryLimit();
114 }
115
116
117 /**
118 * Picks which field to index on, depending on what type of query.
119 * @param bool $fulltext
120 * @return string
121 */
122 function getIndexField( $fulltext ) {
123 return $fulltext ? 'si_text' : 'si_title';
124 }
125
126 /**
127 * Get the base part of the search query.
128 * The actual match syntax will depend on the server
129 * version; MySQL 3 and MySQL 4 have different capabilities
130 * in their fulltext search indexes.
131 *
132 * @param string $filteredTerm
133 * @param bool $fulltext
134 * @return string
135 * @access private
136 */
137 function queryMain( $filteredTerm, $fulltext ) {
138 $match = $this->parseQuery( $filteredTerm, $fulltext );
139 $page = $this->db->tableName( 'page' );
140 $searchindex = $this->db->tableName( 'searchindex' );
141 return 'SELECT page_id, page_namespace, page_title ' .
142 "FROM $page,$searchindex " .
143 'WHERE page_id=si_page AND ' . $match;
144 }
145
146 /**
147 * Create or update the search index record for the given page.
148 * Title and text should be pre-processed.
149 *
150 * @param int $id
151 * @param string $title
152 * @param string $text
153 */
154 function update( $id, $title, $text ) {
155 $dbw=& wfGetDB( DB_MASTER );
156 $dbw->replace( 'searchindex',
157 array( 'si_page' ),
158 array(
159 'si_page' => $id,
160 'si_title' => $title,
161 'si_text' => $text
162 ), 'SearchMySQL4::update' );
163 }
164
165 /**
166 * Update a search index record's title only.
167 * Title should be pre-processed.
168 *
169 * @param int $id
170 * @param string $title
171 */
172 function updateTitle( $id, $title ) {
173 $dbw =& wfGetDB( DB_MASTER );
174
175 $dbw->update( array( 'searchindex' ),
176 array( 'si_title' => $title ),
177 array( 'si_page' => $id ),
178 'SearchMySQL4::updateTitle',
179 $dbw->lowPriorityOption() );
180 }
181 }
182
183 class MySQLSearchResultSet extends SearchResultSet {
184 function MySQLSearchResultSet( $resultSet, $terms ) {
185 $this->mResultSet = $resultSet;
186 $this->mTerms = $terms;
187 }
188
189 function termMatches() {
190 return $this->mTerms;
191 }
192
193 function numRows() {
194 return $this->mResultSet->numRows();
195 }
196
197 function next() {
198 $row = $this->mResultSet->fetchObject();
199 if( $row === false ) {
200 return false;
201 } else {
202 return new SearchResult( $row );
203 }
204 }
205 }
206
207 ?>