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.
28 require_once( 'SearchEngine.php' );
30 /** @package MediaWiki */
31 class SearchMySQL
extends SearchEngine
{
33 * Perform a full text search query and return a result set.
35 * @param string $term - Raw search term
36 * @return MySQLSearchResultSet
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
);
45 * Perform a title-only search query and return a result set.
47 * @param string $term - Raw search term
48 * @return MySQLSearchResultSet
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
);
58 * Return a partial WHERE clause to exclude redirects, if so set
62 function queryRedirect() {
63 if( $this->showRedirects
) {
66 return 'AND page_is_redirect=0';
71 * Return a partial WHERE clause to limit the search to the given namespaces
75 function queryNamespaces() {
76 $namespaces = implode( ',', $this->namespaces
);
77 if ($namespaces == '') {
80 return 'AND page_namespace IN (' . $namespaces . ')';
84 * Return a LIMIT clause to limit results on the query.
88 function queryLimit() {
89 return $this->db
->limitResult( '', $this->limit
, $this->offset
);
93 * Does not do anything for generic search engine
94 * subclasses may define this though
98 function queryRanking( $filteredTerm, $fulltext ) {
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
109 function getQuery( $filteredTerm, $fulltext ) {
110 return $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
111 $this->queryRedirect() . ' ' .
112 $this->queryNamespaces() . ' ' .
113 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' .
119 * Picks which field to index on, depending on what type of query.
120 * @param bool $fulltext
123 function getIndexField( $fulltext ) {
124 return $fulltext ?
'si_text' : 'si_title';
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.
133 * @param string $filteredTerm
134 * @param bool $fulltext
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;
148 * Create or update the search index record for the given page.
149 * Title and text should be pre-processed.
152 * @param string $title
153 * @param string $text
155 function update( $id, $title, $text ) {
156 $dbw=& wfGetDB( DB_MASTER
);
157 $dbw->replace( 'searchindex',
161 'si_title' => $title,
163 ), 'SearchMySQL4::update' );
167 * Update a search index record's title only.
168 * Title should be pre-processed.
171 * @param string $title
173 function updateTitle( $id, $title ) {
174 $dbw =& wfGetDB( DB_MASTER
);
176 $dbw->update( 'searchindex',
177 array( 'si_title' => $title ),
178 array( 'si_page' => $id ),
179 'SearchMySQL4::updateTitle',
180 array( $dbw->lowPriorityOption() ) );
184 /** @package MediaWiki */
185 class MySQLSearchResultSet
extends SearchResultSet
{
186 function MySQLSearchResultSet( $resultSet, $terms ) {
187 $this->mResultSet
= $resultSet;
188 $this->mTerms
= $terms;
191 function termMatches() {
192 return $this->mTerms
;
196 return $this->mResultSet
->numRows();
200 $row = $this->mResultSet
->fetchObject();
201 if( $row === false ) {
204 return new SearchResult( $row );