Making INNER JOIN implicit
[lhc/web/wiklou.git] / includes / SearchTsearch2.php
1 <?php
2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>, Domas Mituzas <domas.mituzas@gmail.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 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # http://www.gnu.org/copyleft/gpl.html
19
20 /**
21 * Search engine hook for PostgreSQL / Tsearch2
22 * @addtogroup Search
23 */
24
25 /**
26 * @todo document
27 * @addtogroup Search
28 */
29 class SearchTsearch2 extends SearchEngine {
30 var $strictMatching = false;
31
32 function __construct( $db ) {
33 $this->db = $db;
34 $this->mRanking = true;
35 }
36
37 function getIndexField( $fulltext ) {
38 return $fulltext ? 'si_text' : 'si_title';
39 }
40
41 function parseQuery( $filteredText, $fulltext ) {
42 global $wgContLang;
43 $lc = SearchEngine::legalSearchChars();
44 $searchon = '';
45 $this->searchTerms = array();
46
47 # FIXME: This doesn't handle parenthetical expressions.
48 $m = array();
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] == '') ) {
54 $terms[1] = '+';
55 }
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_]+";
60 } else {
61 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
62 }
63 $this->searchTerms[] = $regexp;
64 }
65 wfDebug( "Would search with '$searchon'\n" );
66 wfDebug( 'Match with /\b' . implode( '\b|\b', $this->searchTerms ) . "\b/\n" );
67 } else {
68 wfDebug( "Can't understand search query '{$this->filteredText}'\n" );
69 }
70
71 $searchon = preg_replace( '/(\s+)/', '&', $searchon );
72 $searchon = $this->db->strencode( $searchon );
73 return $searchon;
74 }
75
76 function queryRanking( $filteredTerm, $fulltext ) {
77 $field = $this->getIndexField( $fulltext );
78 $searchon = $this->parseQuery( $filteredTerm, $fulltext );
79 if ($this->mRanking)
80 return " ORDER BY rank($field,to_tsquery('$searchon')) DESC";
81 else
82 return "";
83 }
84
85
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') " ;
95 }
96
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, __METHOD__ );
102 $sql = "INSERT INTO $searchindex (si_page,si_title,si_text) ".
103 " VALUES ( $id, to_tsvector('".
104 $dbw->strencode($title).
105 "'),to_tsvector('".
106 $dbw->strencode( $text)."')) ";
107 $dbw->query($sql, __METHOD__ );
108 }
109
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}";
116
117 $dbw->query( $sql, __METHOD__ );
118 }
119 }