2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>, Domas Mituzas <domas.mituzas@gmail.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 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # http://www.gnu.org/copyleft/gpl.html
21 * Search engine hook for PostgreSQL / Tsearch2
29 class SearchTsearch2
extends SearchEngine
{
30 var $strictMatching = false;
32 function SearchTsearch2( &$db ) {
34 $this->mRanking
= true;
37 function getIndexField( $fulltext ) {
38 return $fulltext ?
'si_text' : 'si_title';
41 function parseQuery( $filteredText, $fulltext ) {
43 $lc = SearchEngine
::legalSearchChars();
45 $this->searchTerms
= array();
47 # FIXME: This doesn't handle parenthetical expressions.
49 if( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
50 $filteredText, $m, PREG_SET_ORDER
) ) {
51 foreach( $m as $terms ) {
52 if( $searchon !== '' ) $searchon .= ' ';
53 if( $this->strictMatching
&& ($terms[1] == '') ) {
56 $searchon .= $terms[1] . $wgContLang->stripForSearch( $terms[2] );
57 if( !empty( $terms[3] ) ) {
58 $regexp = preg_quote( $terms[3], '/' );
59 if( $terms[4] ) $regexp .= "[0-9A-Za-z_]+";
61 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
63 $this->searchTerms
[] = $regexp;
65 wfDebug( "Would search with '$searchon'\n" );
66 wfDebug( 'Match with /\b' . implode( '\b|\b', $this->searchTerms
) . "\b/\n" );
68 wfDebug( "Can't understand search query '{$this->filteredText}'\n" );
71 $searchon = preg_replace('/(\s+)/','&',$searchon);
72 $searchon = $this->db
->strencode( $searchon );
76 function queryRanking($filteredTerm, $fulltext) {
77 $field = $this->getIndexField( $fulltext );
78 $searchon = $this->parseQuery($filteredTerm,$fulltext);
80 return " ORDER BY rank($field,to_tsquery('$searchon')) DESC";
86 function queryMain( $filteredTerm, $fulltext ) {
87 $match = $this->parseQuery( $filteredTerm, $fulltext );
88 $field = $this->getIndexField( $fulltext );
89 $cur = $this->db
->tableName( 'cur' );
90 $searchindex = $this->db
->tableName( 'searchindex' );
91 return 'SELECT cur_id, cur_namespace, cur_title, cur_text ' .
92 "FROM $cur,$searchindex " .
93 'WHERE cur_id=si_page AND ' .
94 " $field @@ to_tsquery ('$match') " ;
97 function update( $id, $title, $text ) {
98 $dbw = wfGetDB(DB_MASTER
);
99 $searchindex = $dbw->tableName( 'searchindex' );
100 $sql = "DELETE FROM $searchindex WHERE si_page={$id}";
101 $dbw->query($sql,"SearchTsearch2:update");
102 $sql = "INSERT INTO $searchindex (si_page,si_title,si_text) ".
103 " VALUES ( $id, to_tsvector('".
104 $dbw->strencode($title).
106 $dbw->strencode( $text)."')) ";
107 $dbw->query($sql,"SearchTsearch2:update");
110 function updateTitle($id,$title) {
111 $dbw = wfGetDB(DB_MASTER
);
112 $searchindex = $dbw->tableName( 'searchindex' );
113 $sql = "UPDATE $searchindex SET si_title=to_tsvector('" .
114 $dbw->strencode( $title ) .
115 "') WHERE si_page={$id}";
117 $dbw->query( $sql, "SearchMySQL4::updateTitle" );