* Removed messages in English, they'll be inherited from the parent.
[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 /** */
27 require_once( 'SearchEngine.php' );
28
29 /**
30 * @package MediaWiki
31 * @subpackage Search
32 */
33 class SearchMySQL4 extends SearchEngine {
34 var $strictMatching = true;
35
36 /** @todo document */
37 function SearchMySQL4( &$db ) {
38 $this->db =& $db;
39 }
40
41 /** @todo document */
42 function getIndexField( $fulltext ) {
43 return $fulltext ? 'si_text' : 'si_title';
44 }
45
46 /** @todo document */
47 function parseQuery( $filteredText, $fulltext ) {
48 global $wgContLang;
49 $lc = SearchEngine::legalSearchChars();
50 $searchon = '';
51 $this->searchTerms = array();
52
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] == '') ) {
59 $terms[1] = '+';
60 }
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_]+";
65 } else {
66 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
67 }
68 $this->searchTerms[] = $regexp;
69 }
70 wfDebug( "Would search with '$searchon'\n" );
71 wfDebug( "Match with /\b" . implode( '\b|\b', $this->searchTerms ) . "\b/\n" );
72 } else {
73 wfDebug( "Can't understand search query '{$this->filteredText}'\n" );
74 }
75
76 $searchon = $this->db->strencode( $searchon );
77 $field = $this->getIndexField( $fulltext );
78 return " MATCH($field) AGAINST('$searchon' IN BOOLEAN MODE) ";
79 }
80
81 /** @todo document */
82 function queryMain( $filteredTerm, $fulltext ) {
83 $match = $this->parseQuery( $filteredTerm, $fulltext );
84 $page = $this->db->tableName( 'page' );
85 $revision = $this->db->tableName( 'revision' );
86 $text = $this->db->tableName( 'text' );
87 $searchindex = $this->db->tableName( 'searchindex' );
88 return 'SELECT page_id, page_namespace, page_title, old_flags, old_text ' .
89 "FROM $page,$revision,$text,$searchindex " .
90 'WHERE page_id=si_page AND page_latest=rev_id AND rev_text_id=old_id AND ' . $match;
91 }
92
93 /** @todo document */
94 function update( $id, $title, $text ) {
95 $dbw=& wfGetDB(DB_MASTER);
96 $dbw->replace( 'searchindex', array(array('si_page')),
97 array(
98 'si_page' => $id,
99 'si_title' => $title,
100 'si_text' => $text
101 ), 'SearchMySQL4::update' );
102 }
103
104 /** @todo document */
105 function updateTitle($id,$title) {
106 $dbw=& wfGetDB(DB_MASTER);
107 $lowpri=$dbw->lowPriorityOption();
108 $searchindex = $dbw->tableName( 'searchindex' );
109
110 $sql = "UPDATE $lowpri $searchindex SET si_title='" .
111 $dbw->strencode( $title ) .
112 "' WHERE si_page={$id}";
113
114 $dbw->query( $sql, "SearchMySQL4::updateTitle" );
115 }
116 }
117 ?>