Fix IE freezing rendering whilst waiting for CSS with MonoBook (bug 624) (take 3)
[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 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 require_once( 'SearchEngine.php' );
27
28 class SearchTsearch2 extends SearchEngine {
29 var $strictMatching = false;
30
31 function SearchTsearch2( &$db ) {
32 $this->db =& $db;
33 $this->db->setSchema('tsearch');
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 if( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
49 $filteredText, $m, PREG_SET_ORDER ) ) {
50 foreach( $m as $terms ) {
51 if( $searchon !== '' ) $searchon .= ' ';
52 if( $this->strictMatching && ($terms[1] == '') ) {
53 $terms[1] = '+';
54 }
55 $searchon .= $terms[1] . $wgContLang->stripForSearch( $terms[2] );
56 if( !empty( $terms[3] ) ) {
57 $regexp = preg_quote( $terms[3], '/' );
58 if( $terms[4] ) $regexp .= "[0-9A-Za-z_]+";
59 } else {
60 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
61 }
62 $this->searchTerms[] = $regexp;
63 }
64 wfDebug( "Would search with '$searchon'\n" );
65 wfDebug( "Match with /\b" . implode( '\b|\b', $this->searchTerms ) . "\b/\n" );
66 } else {
67 wfDebug( "Can't understand search query '{$this->filteredText}'\n" );
68 }
69
70 $searchon = preg_replace('/(\s+)/','&',$searchon);
71 $searchon = $this->db->strencode( $searchon );
72 return $searchon;
73 }
74
75 function queryRanking($filteredTerm, $fulltext) {
76 $field = $this->getIndexField( $fulltext );
77 $searchon = $this->parseQuery($filteredTerm,$fulltext);
78 if ($this->mRanking)
79 return " ORDER BY rank($field,to_tsquery('$searchon')) DESC";
80 else
81 return "";
82 }
83
84
85 function queryMain( $filteredTerm, $fulltext ) {
86 $match = $this->parseQuery( $filteredTerm, $fulltext );
87 $field = $this->getIndexField( $fulltext );
88 $cur = $this->db->tableName( 'cur' );
89 $searchindex = $this->db->tableName( 'searchindex' );
90 return 'SELECT cur_id, cur_namespace, cur_title, cur_text ' .
91 "FROM $cur,$searchindex " .
92 'WHERE cur_id=si_page AND ' .
93 " $field @@ to_tsquery ('$match') " ;
94 }
95
96 function update( $id, $title, $text ) {
97 $dbw=& wfGetDB(DB_MASTER);
98 $searchindex = $dbw->tableName( 'searchindex' );
99 $sql = "DELETE FROM $searchindex WHERE si_page={$id}";
100 $dbw->query($sql,"SearchTsearch2:update");
101 $sql = "INSERT INTO $searchindex (si_page,si_title,si_text) ".
102 " VALUES ( $id, to_tsvector('".
103 $dbw->strencode($title).
104 "'),to_tsvector('".
105 $dbw->strencode( $text)."')) ";
106 $dbw->query($sql,"SearchTsearch2:update");
107 }
108
109 function updateTitle($id,$title) {
110 $dbw=& wfGetDB(DB_MASTER);
111 $searchindex = $dbw->tableName( 'searchindex' );
112 $sql = "UPDATE $searchindex SET si_title=to_tsvector('" .
113 $db->strencode( $title ) .
114 "') WHERE si_page={$id}";
115
116 $dbw->query( $sql, "SearchMySQL4::updateTitle" );
117 }
118
119 }
120
121 ?>