655a48e514fa0d35ae9f150f9ca014fc12c837a0
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 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # http://www.gnu.org/copyleft/gpl.html
21 * Search engine hook for PostgreSQL / Tsearch2
27 require_once( 'SearchEngine.php' );
34 class SearchTsearch2
extends SearchEngine
{
35 var $strictMatching = false;
37 function SearchTsearch2( &$db ) {
39 $this->db
->setSchema('tsearch');
40 $this->mRanking
= true;
43 function getIndexField( $fulltext ) {
44 return $fulltext ?
'si_text' : 'si_title';
47 function parseQuery( $filteredText, $fulltext ) {
49 $lc = SearchEngine
::legalSearchChars();
51 $this->searchTerms
= array();
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] == '') ) {
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_]+";
66 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
68 $this->searchTerms
[] = $regexp;
70 wfDebug( "Would search with '$searchon'\n" );
71 wfDebug( "Match with /\b" . implode( '\b|\b', $this->searchTerms
) . "\b/\n" );
73 wfDebug( "Can't understand search query '{$this->filteredText}'\n" );
76 $searchon = preg_replace('/(\s+)/','&',$searchon);
77 $searchon = $this->db
->strencode( $searchon );
81 function queryRanking($filteredTerm, $fulltext) {
82 $field = $this->getIndexField( $fulltext );
83 $searchon = $this->parseQuery($filteredTerm,$fulltext);
85 return " ORDER BY rank($field,to_tsquery('$searchon')) DESC";
91 function queryMain( $filteredTerm, $fulltext ) {
92 $match = $this->parseQuery( $filteredTerm, $fulltext );
93 $field = $this->getIndexField( $fulltext );
94 $cur = $this->db
->tableName( 'cur' );
95 $searchindex = $this->db
->tableName( 'searchindex' );
96 return 'SELECT cur_id, cur_namespace, cur_title, cur_text ' .
97 "FROM $cur,$searchindex " .
98 'WHERE cur_id=si_page AND ' .
99 " $field @@ to_tsquery ('$match') " ;
102 function update( $id, $title, $text ) {
103 $dbw=& wfGetDB(DB_MASTER
);
104 $searchindex = $dbw->tableName( 'searchindex' );
105 $sql = "DELETE FROM $searchindex WHERE si_page={$id}";
106 $dbw->query($sql,"SearchTsearch2:update");
107 $sql = "INSERT INTO $searchindex (si_page,si_title,si_text) ".
108 " VALUES ( $id, to_tsvector('".
109 $dbw->strencode($title).
111 $dbw->strencode( $text)."')) ";
112 $dbw->query($sql,"SearchTsearch2:update");
115 function updateTitle($id,$title) {
116 $dbw=& wfGetDB(DB_MASTER
);
117 $searchindex = $dbw->tableName( 'searchindex' );
118 $sql = "UPDATE $searchindex SET si_title=to_tsvector('" .
119 $db->strencode( $title ) .
120 "') WHERE si_page={$id}";
122 $dbw->query( $sql, "SearchMySQL4::updateTitle" );