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 for MySQL 4+
27 require_once( 'SearchEngine.php' );
33 class SearchMySQL4
extends SearchEngine
{
34 var $strictMatching = true;
37 function SearchMySQL4( &$db ) {
42 function getIndexField( $fulltext ) {
43 return $fulltext ?
'si_text' : 'si_title';
47 function parseQuery( $filteredText, $fulltext ) {
49 $lc = SearchEngine
::legalSearchChars();
51 $this->searchTerms
= array();
53 # FIXME: This doesn't handle parenthetical expressions.
54 if( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
55 $filteredText, $m, PREG_SET_ORDER
) ) {
56 foreach( $m as $terms ) {
57 if( $searchon !== '' ) $searchon .= ' ';
58 if( $this->strictMatching
&& ($terms[1] == '') ) {
61 $searchon .= $terms[1] . $wgContLang->stripForSearch( $terms[2] );
62 if( !empty( $terms[3] ) ) {
63 $regexp = preg_quote( $terms[3], '/' );
64 if( $terms[4] ) $regexp .= "[0-9A-Za-z_]+";
66 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
68 $this->searchTerms
[] = $regexp;
70 wfDebug( "Would search with '$searchon'\n" );
71 wfDebug( "Match with /\b" . implode( '\b|\b', $this->searchTerms
) . "\b/\n" );
73 wfDebug( "Can't understand search query '{$this->filteredText}'\n" );
76 $searchon = $this->db
->strencode( $searchon );
77 $field = $this->getIndexField( $fulltext );
78 return " MATCH($field) AGAINST('$searchon' IN BOOLEAN MODE) ";
82 function queryMain( $filteredTerm, $fulltext ) {
83 $match = $this->parseQuery( $filteredTerm, $fulltext );
84 $page = $this->db
->tableName( 'page' );
85 $text = $this->db
->tableName( 'text' );
86 $searchindex = $this->db
->tableName( 'searchindex' );
87 return 'SELECT page_id, page_namespace, page_title, old_flags, old_text ' .
88 "FROM $page,$text,$searchindex " .
89 'WHERE page_id=si_page AND page_latest=old_id AND ' . $match;
93 function update( $id, $title, $text ) {
94 $dbw=& wfGetDB(DB_MASTER
);
95 $dbw->replace( 'searchindex', array(array('si_page')),
100 ), 'SearchMySQL4::update' );
103 /** @todo document */
104 function updateTitle($id,$title) {
105 $dbw=& wfGetDB(DB_MASTER
);
106 $lowpri=$dbw->lowPriorityOption();
107 $searchindex = $dbw->tableName( 'searchindex' );
109 $sql = "UPDATE $lowpri $searchindex SET si_title='" .
110 $dbw->strencode( $title ) .
111 "' WHERE si_page={$id}";
113 $dbw->query( $sql, "SearchMySQL4::updateTitle" );