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
30 class SearchTsearch2
extends SearchEngine
{
31 var $strictMatching = false;
33 function __construct( $db ) {
35 $this->mRanking
= true;
38 function getIndexField( $fulltext ) {
39 return $fulltext ?
'si_text' : 'si_title';
42 function parseQuery( $filteredText, $fulltext ) {
44 $lc = SearchEngine
::legalSearchChars();
46 $this->searchTerms
= array();
48 # FIXME: This doesn't handle parenthetical expressions.
50 if( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
51 $filteredText, $m, PREG_SET_ORDER
) ) {
52 foreach( $m as $terms ) {
53 if( $searchon !== '' ) $searchon .= ' ';
54 if( $this->strictMatching
&& ($terms[1] == '') ) {
57 $searchon .= $terms[1] . $wgContLang->stripForSearch( $terms[2] );
58 if( !empty( $terms[3] ) ) {
59 $regexp = preg_quote( $terms[3], '/' );
60 if( $terms[4] ) $regexp .= "[0-9A-Za-z_]+";
62 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
64 $this->searchTerms
[] = $regexp;
66 wfDebug( "Would search with '$searchon'\n" );
67 wfDebug( 'Match with /\b' . implode( '\b|\b', $this->searchTerms
) . "\b/\n" );
69 wfDebug( "Can't understand search query '{$this->filteredText}'\n" );
72 $searchon = preg_replace( '/(\s+)/', '&', $searchon );
73 $searchon = $this->db
->strencode( $searchon );
77 function queryRanking( $filteredTerm, $fulltext ) {
78 $field = $this->getIndexField( $fulltext );
79 $searchon = $this->parseQuery( $filteredTerm, $fulltext );
81 return " ORDER BY rank($field,to_tsquery('$searchon')) DESC";
87 function queryMain( $filteredTerm, $fulltext ) {
88 $match = $this->parseQuery( $filteredTerm, $fulltext );
89 $field = $this->getIndexField( $fulltext );
90 $cur = $this->db
->tableName( 'cur' );
91 $searchindex = $this->db
->tableName( 'searchindex' );
92 return 'SELECT cur_id, cur_namespace, cur_title, cur_text ' .
93 "FROM $cur,$searchindex " .
94 'WHERE cur_id=si_page AND ' .
95 " $field @@ to_tsquery ('$match') " ;
98 function update( $id, $title, $text ) {
99 $dbw = wfGetDB( DB_MASTER
);
100 $searchindex = $dbw->tableName( 'searchindex' );
101 $sql = "DELETE FROM $searchindex WHERE si_page={$id}";
102 $dbw->query( $sql, __METHOD__
);
103 $sql = "INSERT INTO $searchindex (si_page,si_title,si_text) ".
104 " VALUES ( $id, to_tsvector('".
105 $dbw->strencode($title).
107 $dbw->strencode( $text)."')) ";
108 $dbw->query($sql, __METHOD__
);
111 function updateTitle($id,$title) {
112 $dbw = wfGetDB(DB_MASTER
);
113 $searchindex = $dbw->tableName( 'searchindex' );
114 $sql = "UPDATE $searchindex SET si_title=to_tsvector('" .
115 $dbw->strencode( $title ) .
116 "') WHERE si_page={$id}";
118 $dbw->query( $sql, __METHOD__
);