1fca9899630b3c15c30ed862fac201245505cad8
[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 * @package MediaWiki
23 * @subpackage Search
24 */
25
26 /**
27 * @todo document
28 * @package MediaWiki
29 * @subpackage Search
30 */
31 class SearchTsearch2 extends SearchEngine {
32 var $strictMatching = false;
33
34 function SearchTsearch2( &$db ) {
35 $this->db =& $db;
36 $this->mRanking = true;
37 }
38
39 function getIndexField( $fulltext ) {
40 return $fulltext ? 'si_text' : 'si_title';
41 }
42
43 function parseQuery( $filteredText, $fulltext ) {
44 global $wgContLang;
45 $lc = SearchEngine::legalSearchChars();
46 $searchon = '';
47 $this->searchTerms = array();
48
49 # FIXME: This doesn't handle parenthetical expressions.
50 $m = array();
51 if( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
52 $filteredText, $m, PREG_SET_ORDER ) ) {
53 foreach( $m as $terms ) {
54 if( $searchon !== '' ) $searchon .= ' ';
55 if( $this->strictMatching && ($terms[1] == '') ) {
56 $terms[1] = '+';
57 }
58 $searchon .= $terms[1] . $wgContLang->stripForSearch( $terms[2] );
59 if( !empty( $terms[3] ) ) {
60 $regexp = preg_quote( $terms[3], '/' );
61 if( $terms[4] ) $regexp .= "[0-9A-Za-z_]+";
62 } else {
63 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
64 }
65 $this->searchTerms[] = $regexp;
66 }
67 wfDebug( "Would search with '$searchon'\n" );
68 wfDebug( 'Match with /\b' . implode( '\b|\b', $this->searchTerms ) . "\b/\n" );
69 } else {
70 wfDebug( "Can't understand search query '{$this->filteredText}'\n" );
71 }
72
73 $searchon = preg_replace('/(\s+)/','&',$searchon);
74 $searchon = $this->db->strencode( $searchon );
75 return $searchon;
76 }
77
78 function queryRanking($filteredTerm, $fulltext) {
79 $field = $this->getIndexField( $fulltext );
80 $searchon = $this->parseQuery($filteredTerm,$fulltext);
81 if ($this->mRanking)
82 return " ORDER BY rank($field,to_tsquery('$searchon')) DESC";
83 else
84 return "";
85 }
86
87
88 function queryMain( $filteredTerm, $fulltext ) {
89 $match = $this->parseQuery( $filteredTerm, $fulltext );
90 $field = $this->getIndexField( $fulltext );
91 $cur = $this->db->tableName( 'cur' );
92 $searchindex = $this->db->tableName( 'searchindex' );
93 return 'SELECT cur_id, cur_namespace, cur_title, cur_text ' .
94 "FROM $cur,$searchindex " .
95 'WHERE cur_id=si_page AND ' .
96 " $field @@ to_tsquery ('$match') " ;
97 }
98
99 function update( $id, $title, $text ) {
100 $dbw=& wfGetDB(DB_MASTER);
101 $searchindex = $dbw->tableName( 'searchindex' );
102 $sql = "DELETE FROM $searchindex WHERE si_page={$id}";
103 $dbw->query($sql,"SearchTsearch2:update");
104 $sql = "INSERT INTO $searchindex (si_page,si_title,si_text) ".
105 " VALUES ( $id, to_tsvector('".
106 $dbw->strencode($title).
107 "'),to_tsvector('".
108 $dbw->strencode( $text)."')) ";
109 $dbw->query($sql,"SearchTsearch2:update");
110 }
111
112 function updateTitle($id,$title) {
113 $dbw=& wfGetDB(DB_MASTER);
114 $searchindex = $dbw->tableName( 'searchindex' );
115 $sql = "UPDATE $searchindex SET si_title=to_tsvector('" .
116 $dbw->strencode( $title ) .
117 "') WHERE si_page={$id}";
118
119 $dbw->query( $sql, "SearchMySQL4::updateTitle" );
120 }
121
122 }
123
124 ?>