* move SpecialSearch::getSearchEngines() to SearchEngine::create()
[lhc/web/wiklou.git] / includes / SearchMySQL3.php
1 <?php
2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
4 #
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.
9 #
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.
14 #
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
19
20 /**
21 * Search engine hook for MySQL 3.23.x
22 * @package MediaWiki
23 * @subpackage Search
24 */
25
26 require_once( 'SearchEngine.php' );
27
28 class SearchMySQL3 extends SearchEngine {
29 function SearchMySQL3( &$db ) {
30 $this->db =& $db;
31 }
32
33 function getIndexField( $fulltext ) {
34 return $fulltext ? 'si_text' : 'si_title';
35 }
36
37 function parseQuery( $filteredText, $fulltext ) {
38 global $wgDBminWordLen, $wgContLang;
39
40 $field = $this->getIndexField( $fulltext );
41
42 # on non mysql4 database: get list of words we don't want to search for
43 require_once( 'FulltextStoplist.php' );
44
45 $lc = SearchEngine::legalSearchChars() . '()';
46 $q = preg_replace( "/([()])/", " \\1 ", $filteredText );
47 $q = preg_replace( "/\\s+/", " ", $q );
48 $w = explode( ' ', trim( $q ) );
49
50 $last = $cond = '';
51 foreach ( $w as $word ) {
52 $word = $wgContLang->stripForSearch( $word );
53 if ( 'and' == $word || 'or' == $word || 'not' == $word
54 || '(' == $word || ')' == $word ) {
55 $cond .= ' ' . strtoupper( $word );
56 $last = '';
57 } else if ( strlen( $word ) < $wgDBminWordLen ) {
58 continue;
59 } else if ( FulltextStoplist::inList( $word ) ) {
60 continue;
61 } else {
62 if ( '' != $last ) { $cond .= ' AND'; }
63 $cond .= " (MATCH ($field) AGAINST ('" .
64 $this->db->strencode( $word ). "'))";
65 $last = $word;
66 $this->searchTerms[] = "\\b" . preg_quote( $word, '/' ) . "\\b";
67 }
68 }
69 if ( 0 == count( $this->searchTerms ) ) {
70 return null;
71 }
72
73 return '(' . $cond . ' )';
74 }
75
76 function queryMain( $filteredTerm, $fulltext ) {
77 $match = $this->parseQuery( $filteredTerm, $fulltext );
78 $cur = $this->db->tableName( 'cur' );
79 $searchindex = $this->db->tableName( 'searchindex' );
80 return 'SELECT cur_id, cur_namespace, cur_title, cur_text ' .
81 "FROM $cur,$searchindex " .
82 'WHERE cur_id=si_page AND ' . $match;
83 }
84
85 function update( $id, $title, $text ) {
86 $dbw=& wfGetDB(DB_MASTER);
87 $dbw->replace( 'searchindex', array(array('si_page')),
88 array(
89 'si_page' => $id,
90 'si_title' => $dbw->strencode($title),
91 'si_text' => $dbw->strencode( $text )
92 ), 'SearchMySQL3::update' );
93 }
94
95 function updateTitle($id,$title) {
96 $dbw=& wfGetDB(DB_MASTER);
97 $lowpri=$dbw->lowPriorityOption();
98 $searchindex = $dbw->tableName( 'searchindex' );
99
100 $sql = "UPDATE $lowpri $searchindex SET si_title='" .
101 $db->strencode( $title ) .
102 "' WHERE si_page={$id}";
103
104 $dbw->query( $sql, "SearchMySQL3::updateTitle" );
105 }
106 }
107
108 ?>