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