bug 1004 Capitalized Norsk
[lhc/web/wiklou.git] / includes / SearchMySQL4.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 4+
22 * @package MediaWiki
23 * @subpackage Search
24 */
25
26 require_once( 'SearchEngine.php' );
27
28 class SearchMySQL4 extends SearchEngine {
29 var $strictMatching = false;
30
31 function SearchMySQL4( &$db ) {
32 $this->db =& $db;
33 }
34
35 function getIndexField( $fulltext ) {
36 return $fulltext ? 'si_text' : 'si_title';
37 }
38
39 function parseQuery( $filteredText, $fulltext ) {
40 global $wgContLang;
41 $lc = SearchEngine::legalSearchChars();
42 $searchon = '';
43 $this->searchTerms = array();
44
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] == '') ) {
51 $terms[1] = '+';
52 }
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_]+";
57 } else {
58 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
59 }
60 $this->searchTerms[] = $regexp;
61 }
62 wfDebug( "Would search with '$searchon'\n" );
63 wfDebug( "Match with /\b" . implode( '\b|\b', $this->searchTerms ) . "\b/\n" );
64 } else {
65 wfDebug( "Can't understand search query '{$this->filteredText}'\n" );
66 }
67
68 $searchon = $this->db->strencode( $searchon );
69 $field = $this->getIndexField( $fulltext );
70 return " MATCH($field) AGAINST('$searchon' IN BOOLEAN MODE) ";
71 }
72
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;
80 }
81
82 function update( $id, $title, $text ) {
83 $dbw=& wfGetDB(DB_MASTER);
84 $dbw->replace( 'searchindex', array(array('si_page')),
85 array(
86 'si_page' => $id,
87 'si_title' => $dbw->strencode($title),
88 'si_text' => $dbw->strencode( $text )
89 ), 'SearchMySQL4::update' );
90 }
91
92 function updateTitle($id,$title) {
93 $dbw=& wfGetDB(DB_MASTER);
94 $lowpri=$dbw->lowPriorityOption();
95 $searchindex = $dbw->tableName( 'searchindex' );
96
97 $sql = "UPDATE $lowpri $searchindex SET si_title='" .
98 $dbw->strencode( $title ) .
99 "' WHERE si_page={$id}";
100
101 $dbw->query( $sql, "SearchMySQL4::updateTitle" );
102 }
103
104 }
105
106 ?>