00001 <?php
00002 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
00003 # http://www.mediawiki.org/
00004 #
00005 # This program is free software; you can redistribute it and/or modify
00006 # it under the terms of the GNU General Public License as published by
00007 # the Free Software Foundation; either version 2 of the License, or
00008 # (at your option) any later version.
00009 #
00010 # This program is distributed in the hope that it will be useful,
00011 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013 # GNU General Public License for more details.
00014 #
00015 # You should have received a copy of the GNU General Public License along
00016 # with this program; if not, write to the Free Software Foundation, Inc.,
00017 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018 # http://www.gnu.org/copyleft/gpl.html
00019
00029 class SearchMySQL extends SearchEngine {
00030 var $strictMatching = true;
00031
00033 function __construct( $db ) {
00034 $this->db = $db;
00035 }
00036
00041 function parseQuery( $filteredText, $fulltext ) {
00042 global $wgContLang;
00043 $lc = SearchEngine::legalSearchChars();
00044 $searchon = '';
00045 $this->searchTerms = array();
00046
00047 # FIXME: This doesn't handle parenthetical expressions.
00048 $m = array();
00049 if( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
00050 $filteredText, $m, PREG_SET_ORDER ) ) {
00051 foreach( $m as $terms ) {
00052 if( $searchon !== '' ) $searchon .= ' ';
00053 if( $this->strictMatching && ($terms[1] == '') ) {
00054 $terms[1] = '+';
00055 }
00056 $searchon .= $terms[1] . $wgContLang->stripForSearch( $terms[2] );
00057 if( !empty( $terms[3] ) ) {
00058
00059 $regexp = preg_quote( $terms[3], '/' );
00060 if( $terms[4] ) {
00061 $regexp = "\b$regexp";
00062 } else {
00063 $regexp = "\b$regexp\b";
00064 }
00065 } else {
00066
00067 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
00068 }
00069 $this->searchTerms[] = $regexp;
00070 }
00071 wfDebug( "Would search with '$searchon'\n" );
00072 wfDebug( 'Match with /' . implode( '|', $this->searchTerms ) . "/\n" );
00073 } else {
00074 wfDebug( "Can't understand search query '{$filteredText}'\n" );
00075 }
00076
00077 $searchon = $this->db->strencode( $searchon );
00078 $field = $this->getIndexField( $fulltext );
00079 return " MATCH($field) AGAINST('$searchon' IN BOOLEAN MODE) ";
00080 }
00081
00082 public static function legalSearchChars() {
00083 return "\"*" . parent::legalSearchChars();
00084 }
00085
00093 function searchText( $term ) {
00094 $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), true ) ) );
00095 return new MySQLSearchResultSet( $resultSet, $this->searchTerms );
00096 }
00097
00105 function searchTitle( $term ) {
00106 $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), false ) ) );
00107 return new MySQLSearchResultSet( $resultSet, $this->searchTerms );
00108 }
00109
00110
00116 function queryRedirect() {
00117 if( $this->showRedirects ) {
00118 return '';
00119 } else {
00120 return 'AND page_is_redirect=0';
00121 }
00122 }
00123
00129 function queryNamespaces() {
00130 if( is_null($this->namespaces) )
00131 return ''; # search all
00132 if ( !count( $this->namespaces ) ) {
00133 $namespaces = '0';
00134 } else {
00135 $namespaces = $this->db->makeList( $this->namespaces );
00136 }
00137 return 'AND page_namespace IN (' . $namespaces . ')';
00138 }
00139
00145 function queryLimit() {
00146 return $this->db->limitResult( '', $this->limit, $this->offset );
00147 }
00148
00155 function queryRanking( $filteredTerm, $fulltext ) {
00156 return '';
00157 }
00158
00166 function getQuery( $filteredTerm, $fulltext ) {
00167 return $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
00168 $this->queryRedirect() . ' ' .
00169 $this->queryNamespaces() . ' ' .
00170 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' .
00171 $this->queryLimit();
00172 }
00173
00174
00180 function getIndexField( $fulltext ) {
00181 return $fulltext ? 'si_text' : 'si_title';
00182 }
00183
00195 function queryMain( $filteredTerm, $fulltext ) {
00196 $match = $this->parseQuery( $filteredTerm, $fulltext );
00197 $page = $this->db->tableName( 'page' );
00198 $searchindex = $this->db->tableName( 'searchindex' );
00199 return 'SELECT page_id, page_namespace, page_title ' .
00200 "FROM $page,$searchindex " .
00201 'WHERE page_id=si_page AND ' . $match;
00202 }
00203
00212 function update( $id, $title, $text ) {
00213 $dbw = wfGetDB( DB_MASTER );
00214 $dbw->replace( 'searchindex',
00215 array( 'si_page' ),
00216 array(
00217 'si_page' => $id,
00218 'si_title' => $title,
00219 'si_text' => $text
00220 ), __METHOD__ );
00221 }
00222
00230 function updateTitle( $id, $title ) {
00231 $dbw = wfGetDB( DB_MASTER );
00232
00233 $dbw->update( 'searchindex',
00234 array( 'si_title' => $title ),
00235 array( 'si_page' => $id ),
00236 __METHOD__,
00237 array( $dbw->lowPriorityOption() ) );
00238 }
00239 }
00240
00244 class MySQLSearchResultSet extends SearchResultSet {
00245 function MySQLSearchResultSet( $resultSet, $terms ) {
00246 $this->mResultSet = $resultSet;
00247 $this->mTerms = $terms;
00248 }
00249
00250 function termMatches() {
00251 return $this->mTerms;
00252 }
00253
00254 function numRows() {
00255 return $this->mResultSet->numRows();
00256 }
00257
00258 function next() {
00259 $row = $this->mResultSet->fetchObject();
00260 if( $row === false ) {
00261 return false;
00262 } else {
00263 return new SearchResult( $row );
00264 }
00265 }
00266
00267 function free() {
00268 $this->mResultSet->free();
00269 }
00270 }