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