2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
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.
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.
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
21 * Search engine hook base class for MySQL.
22 * Specific bits for MySQL 3 and 4 variants are in child classes.
25 class SearchMySQL
extends SearchEngine
{
27 * Perform a full text search query and return a result set.
29 * @param string $term - Raw search term
30 * @return MySQLSearchResultSet
33 function searchText( $term ) {
34 $resultSet = $this->db
->resultObject( $this->db
->query( $this->getQuery( $this->filter( $term ), true ) ) );
35 return new MySQLSearchResultSet( $resultSet, $this->searchTerms
);
39 * Perform a title-only search query and return a result set.
41 * @param string $term - Raw search term
42 * @return MySQLSearchResultSet
45 function searchTitle( $term ) {
46 $resultSet = $this->db
->resultObject( $this->db
->query( $this->getQuery( $this->filter( $term ), false ) ) );
47 return new MySQLSearchResultSet( $resultSet, $this->searchTerms
);
52 * Return a partial WHERE clause to exclude redirects, if so set
56 function queryRedirect() {
57 if( $this->showRedirects
) {
60 return 'AND page_is_redirect=0';
65 * Return a partial WHERE clause to limit the search to the given namespaces
69 function queryNamespaces() {
70 $namespaces = implode( ',', $this->namespaces
);
71 if ($namespaces == '') {
74 return 'AND page_namespace IN (' . $namespaces . ')';
78 * Return a LIMIT clause to limit results on the query.
82 function queryLimit() {
83 return $this->db
->limitResult( '', $this->limit
, $this->offset
);
87 * Does not do anything for generic search engine
88 * subclasses may define this though
92 function queryRanking( $filteredTerm, $fulltext ) {
97 * Construct the full SQL query to do the search.
98 * The guts shoulds be constructed in queryMain()
99 * @param string $filteredTerm
100 * @param bool $fulltext
103 function getQuery( $filteredTerm, $fulltext ) {
104 return $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
105 $this->queryRedirect() . ' ' .
106 $this->queryNamespaces() . ' ' .
107 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' .
113 * Picks which field to index on, depending on what type of query.
114 * @param bool $fulltext
117 function getIndexField( $fulltext ) {
118 return $fulltext ?
'si_text' : 'si_title';
122 * Get the base part of the search query.
123 * The actual match syntax will depend on the server
124 * version; MySQL 3 and MySQL 4 have different capabilities
125 * in their fulltext search indexes.
127 * @param string $filteredTerm
128 * @param bool $fulltext
132 function queryMain( $filteredTerm, $fulltext ) {
133 $match = $this->parseQuery( $filteredTerm, $fulltext );
134 $page = $this->db
->tableName( 'page' );
135 $searchindex = $this->db
->tableName( 'searchindex' );
136 return 'SELECT page_id, page_namespace, page_title ' .
137 "FROM $page,$searchindex " .
138 'WHERE page_id=si_page AND ' . $match;
142 * Create or update the search index record for the given page.
143 * Title and text should be pre-processed.
146 * @param string $title
147 * @param string $text
149 function update( $id, $title, $text ) {
150 $dbw = wfGetDB( DB_MASTER
);
151 $dbw->replace( 'searchindex',
155 'si_title' => $title,
157 ), 'SearchMySQL4::update' );
161 * Update a search index record's title only.
162 * Title should be pre-processed.
165 * @param string $title
167 function updateTitle( $id, $title ) {
168 $dbw = wfGetDB( DB_MASTER
);
170 $dbw->update( 'searchindex',
171 array( 'si_title' => $title ),
172 array( 'si_page' => $id ),
173 'SearchMySQL4::updateTitle',
174 array( $dbw->lowPriorityOption() ) );
181 class MySQLSearchResultSet
extends SearchResultSet
{
182 function MySQLSearchResultSet( $resultSet, $terms ) {
183 $this->mResultSet
= $resultSet;
184 $this->mTerms
= $terms;
187 function termMatches() {
188 return $this->mTerms
;
192 return $this->mResultSet
->numRows();
196 $row = $this->mResultSet
->fetchObject();
197 if( $row === false ) {
200 return new SearchResult( $row );
205 $this->mResultSet
->free();