a1fa5b38fb4bbbbfbd9cc2e8eff0c7904a141912
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+
26 require_once( 'SearchEngine.php' );
28 class SearchMySQL4
extends SearchEngine
{
29 var $strictMatching = false;
31 function SearchMySQL4( &$db ) {
35 function getIndexField( $fulltext ) {
36 return $fulltext ?
'si_text' : 'si_title';
39 function parseQuery( $filteredText, $fulltext ) {
41 $lc = SearchEngine
::legalSearchChars();
43 $this->searchTerms
= array();
45 # FIXME: This doesn't handle parenthetical expressions.
46 if( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
47 $filteredText, $m, PREG_SET_ORDER
) ) {
48 foreach( $m as $terms ) {
49 if( $searchon !== '' ) $searchon .= ' ';
50 if( $this->strictMatching
&& ($terms[1] == '') ) {
53 $searchon .= $terms[1] . $wgContLang->stripForSearch( $terms[2] );
54 if( !empty( $terms[3] ) ) {
55 $regexp = preg_quote( $terms[3], '/' );
56 if( $terms[4] ) $regexp .= "[0-9A-Za-z_]+";
58 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
60 $this->searchTerms
[] = $regexp;
62 wfDebug( "Would search with '$searchon'\n" );
63 wfDebug( "Match with /\b" . implode( '\b|\b', $this->searchTerms
) . "\b/\n" );
65 wfDebug( "Can't understand search query '{$this->filteredText}'\n" );
68 $searchon = $this->db
->strencode( $searchon );
69 $field = $this->getIndexField( $fulltext );
70 return " MATCH($field) AGAINST('$searchon' IN BOOLEAN MODE) ";
73 function queryMain( $filteredTerm, $fulltext ) {
74 $match = $this->parseQuery( $filteredTerm, $fulltext );
75 $cur = $this->db
->tableName( 'cur' );
76 $searchindex = $this->db
->tableName( 'searchindex' );
77 return 'SELECT cur_id, cur_namespace, cur_title, cur_text ' .
78 "FROM $cur,$searchindex " .
79 'WHERE cur_id=si_page AND ' . $match;
82 function update( $id, $title, $text ) {
83 $dbw=& wfGetDB(DB_MASTER
);
84 $dbw->replace( 'searchindex', array(array('si_page')),
87 'si_title' => $dbw->strencode($title),
88 'si_text' => $dbw->strencode( $text )
89 ), 'SearchMySQL4::update' );
92 function updateTitle($id,$title) {
93 $dbw=& wfGetDB(DB_MASTER
);
94 $lowpri=$dbw->lowPriorityOption();
95 $searchindex = $dbw->tableName( 'searchindex' );
97 $sql = "UPDATE $lowpri $searchindex SET si_title='" .
98 $dbw->strencode( $title ) .
99 "' WHERE si_page={$id}";
101 $dbw->query( $sql, "SearchMySQL4::updateTitle" );