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