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 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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.
28 require_once( 'SearchEngine.php' );
30 class SearchMySQL
extends SearchEngine
{
32 * Perform a full text search query and return a result set.
34 * @param string $term - Raw search term
35 * @return MySQLSearchResultSet
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
);
44 * Perform a title-only search query and return a result set.
46 * @param string $term - Raw search term
47 * @return MySQLSearchResultSet
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
);
57 * Return a partial WHERE clause to exclude redirects, if so set
61 function queryRedirect() {
62 if( $this->showRedirects
) {
63 return 'AND cur_is_redirect=0';
70 * Return a partial WHERE clause to limit the search to the given namespaces
74 function queryNamespaces() {
75 $namespaces = implode( ',', $this->namespaces
);
76 if ($namespaces == '') {
79 return 'AND page_namespace IN (' . $namespaces . ')';
83 * Return a LIMIT clause to limit results on the query.
87 function queryLimit() {
88 return $this->db
->limitResult( $this->limit
, $this->offset
);
92 * Does not do anything for generic search engine
93 * subclasses may define this though
97 function queryRanking( $filteredTerm, $fulltext ) {
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
108 function getQuery( $filteredTerm, $fulltext ) {
109 return $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
110 $this->queryRedirect() . ' ' .
111 $this->queryNamespaces() . ' ' .
112 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' .
118 * Picks which field to index on, depending on what type of query.
119 * @param bool $fulltext
122 function getIndexField( $fulltext ) {
123 return $fulltext ?
'si_text' : 'si_title';
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.
132 * @param string $filteredTerm
133 * @param bool $fulltext
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;
147 * Create or update the search index record for the given page.
148 * Title and text should be pre-processed.
151 * @param string $title
152 * @param string $text
154 function update( $id, $title, $text ) {
155 $dbw=& wfGetDB( DB_MASTER
);
156 $dbw->replace( 'searchindex',
160 'si_title' => $title,
162 ), 'SearchMySQL4::update' );
166 * Update a search index record's title only.
167 * Title should be pre-processed.
170 * @param string $title
172 function updateTitle( $id, $title ) {
173 $dbw =& wfGetDB( DB_MASTER
);
175 $dbw->update( 'searchindex',
176 array( 'si_title' => $title ),
177 array( 'si_page' => $id ),
178 'SearchMySQL4::updateTitle',
179 $dbw->lowPriorityOption() );
183 class MySQLSearchResultSet
extends SearchResultSet
{
184 function MySQLSearchResultSet( $resultSet, $terms ) {
185 $this->mResultSet
= $resultSet;
186 $this->mTerms
= $terms;
189 function termMatches() {
190 return $this->mTerms
;
194 return $this->mResultSet
->numRows();
198 $row = $this->mResultSet
->fetchObject();
199 if( $row === false ) {
202 return new SearchResult( $row );