* Added a test for a link with multiple pipes
[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 /** */
27 require_once( 'SearchEngine.php' );
28
29 /**
30 * @todo document
31 * @package MediaWiki
32 * @subpackage Search
33 */
34 class SearchTsearch2 extends SearchEngine {
35 var $strictMatching = false;
36
37 function SearchTsearch2( &$db ) {
38 $this->db =& $db;
39 $this->db->setSchema('tsearch');
40 $this->mRanking = true;
41 }
42
43 function getIndexField( $fulltext ) {
44 return $fulltext ? 'si_text' : 'si_title';
45 }
46
47 function parseQuery( $filteredText, $fulltext ) {
48 global $wgContLang;
49 $lc = SearchEngine::legalSearchChars();
50 $searchon = '';
51 $this->searchTerms = array();
52
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] == '') ) {
59 $terms[1] = '+';
60 }
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_]+";
65 } else {
66 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
67 }
68 $this->searchTerms[] = $regexp;
69 }
70 wfDebug( "Would search with '$searchon'\n" );
71 wfDebug( "Match with /\b" . implode( '\b|\b', $this->searchTerms ) . "\b/\n" );
72 } else {
73 wfDebug( "Can't understand search query '{$this->filteredText}'\n" );
74 }
75
76 $searchon = preg_replace('/(\s+)/','&',$searchon);
77 $searchon = $this->db->strencode( $searchon );
78 return $searchon;
79 }
80
81 function queryRanking($filteredTerm, $fulltext) {
82 $field = $this->getIndexField( $fulltext );
83 $searchon = $this->parseQuery($filteredTerm,$fulltext);
84 if ($this->mRanking)
85 return " ORDER BY rank($field,to_tsquery('$searchon')) DESC";
86 else
87 return "";
88 }
89
90
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') " ;
100 }
101
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).
110 "'),to_tsvector('".
111 $dbw->strencode( $text)."')) ";
112 $dbw->query($sql,"SearchTsearch2:update");
113 }
114
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}";
121
122 $dbw->query( $sql, "SearchMySQL4::updateTitle" );
123 }
124
125 }
126
127 ?>