* (bug 6701) Kazakh language variants in MessagesEn.php
[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 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] == '') ) {
55 $terms[1] = '+';
56 }
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_]+";
61 } else {
62 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
63 }
64 $this->searchTerms[] = $regexp;
65 }
66 wfDebug( "Would search with '$searchon'\n" );
67 wfDebug( "Match with /\b" . implode( '\b|\b', $this->searchTerms ) . "\b/\n" );
68 } else {
69 wfDebug( "Can't understand search query '{$this->filteredText}'\n" );
70 }
71
72 $searchon = preg_replace('/(\s+)/','&',$searchon);
73 $searchon = $this->db->strencode( $searchon );
74 return $searchon;
75 }
76
77 function queryRanking($filteredTerm, $fulltext) {
78 $field = $this->getIndexField( $fulltext );
79 $searchon = $this->parseQuery($filteredTerm,$fulltext);
80 if ($this->mRanking)
81 return " ORDER BY rank($field,to_tsquery('$searchon')) DESC";
82 else
83 return "";
84 }
85
86
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') " ;
96 }
97
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,"SearchTsearch2:update");
103 $sql = "INSERT INTO $searchindex (si_page,si_title,si_text) ".
104 " VALUES ( $id, to_tsvector('".
105 $dbw->strencode($title).
106 "'),to_tsvector('".
107 $dbw->strencode( $text)."')) ";
108 $dbw->query($sql,"SearchTsearch2:update");
109 }
110
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 $db->strencode( $title ) .
116 "') WHERE si_page={$id}";
117
118 $dbw->query( $sql, "SearchMySQL4::updateTitle" );
119 }
120
121 }
122
123 ?>