2 # Copyright (C) 2006-2007 Greg Sabino Mullane <greg@turnstep.com>
3 # http://www.mediawiki.org/
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.
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.
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
21 * Search engine hook base class for Postgres
25 class SearchPostgres
extends SearchEngine
{
27 function SearchPostgres( $db ) {
32 * Perform a full text search query via tsearch2 and return a result set.
33 * Currently searches a page's current title (page.page_title) and
34 * latest revision article text (pagecontent.old_text)
36 * @param string $term - Raw search term
37 * @return PostgresSearchResultSet
40 function searchTitle( $term ) {
41 $resultSet = $this->db
->resultObject( $this->db
->query( $this->searchQuery( $term , 'titlevector', 'page_title' )));
42 return new PostgresSearchResultSet( $resultSet, $this->searchTerms
);
44 function searchText( $term ) {
45 $resultSet = $this->db
->resultObject( $this->db
->query( $this->searchQuery( $term, 'textvector', 'old_text' )));
46 return new PostgresSearchResultSet( $resultSet, $this->searchTerms
);
51 * Transform the user's search string into a better form for tsearch2
53 function parseQuery( $term ) {
55 wfDebug( "parseQuery received: $term" );
57 ## No backslashes allowed
58 $term = preg_replace('/\\\/', '', $term);
60 ## Collapse parens into nearby words:
61 $term = preg_replace('/\s*\(\s*/', ' (', $term);
62 $term = preg_replace('/\s*\)\s*/', ') ', $term);
64 ## Treat colons as word separators:
65 $term = preg_replace('/:/', ' ', $term);
67 $this->searchTerms
= array();
70 if( preg_match_all('/([-!]?)(\S+)\s*/', $term, $m, PREG_SET_ORDER
) ) {
71 foreach( $m as $terms ) {
72 if (strlen($terms[1])) {
73 $searchstring .= ' & !';
75 if (strtolower($terms[2]) === 'and') {
76 $searchstring .= ' & ';
78 else if (strtolower($terms[2]) === 'or' or $terms[2] === '|') {
79 $searchstring .= ' | ';
81 else if (strtolower($terms[2]) === 'not') {
82 $searchstring .= ' & !';
85 $searchstring .= " & $terms[2]";
86 $safeterm = preg_replace('/\W+/', '', $terms[2]);
87 $this->searchTerms
[$safeterm] = $safeterm;
92 ## Strip out leading junk
93 $searchstring = preg_replace('/^[\s\&\|]+/', '', $searchstring);
95 ## Remove any doubled-up operators
96 $searchstring = preg_replace('/([\!\&\|]) +(?:[\&\|] +)+/', "$1 ", $searchstring);
98 ## Remove any non-spaced operators (e.g. "Zounds!")
99 $searchstring = preg_replace('/([^ ])[\!\&\|]/', "$1", $searchstring);
101 ## Remove any trailing operators
102 $searchstring = preg_replace('/(?: [\!\&\|])*$/', '', $searchstring);
104 ## Quote the whole thing
105 $searchstring = $this->db
->addQuotes($searchstring);
107 wfDebug( "parseQuery returned: $searchstring" );
109 return $searchstring;
114 * Construct the full SQL query to do the search.
115 * @param string $filteredTerm
116 * @param string $fulltext
119 function searchQuery( $term, $fulltext, $colname ) {
121 $searchstring = $this->parseQuery( $term );
123 ## We need a separate query here so gin does not complain about empty searches
124 $SQL = "SELECT to_tsquery('default',$searchstring)";
125 $res = $this->db
->doQuery($SQL);
127 ## TODO: Better output (example to catch: one 'two)
128 die ("Sorry, that was not a valid search string. Please go back and try again");
130 $top = pg_fetch_result($res,0,0);
132 if ($top === "") { ## e.g. if only stopwords are used XXX return something better
133 $query = "SELECT page_id, page_namespace, page_title, 0 AS score ".
134 "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
135 "AND r.rev_text_id = c.old_id AND 1=0";
138 $query = "SELECT page_id, page_namespace, page_title, ".
139 "rank($fulltext, to_tsquery('default',$searchstring),5) AS score ".
140 "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
141 "AND r.rev_text_id = c.old_id AND $fulltext @@ to_tsquery('default',$searchstring)";
145 if (! $this->showRedirects
)
146 $query .= ' AND page_is_redirect = 0'; ## IS FALSE
148 ## Namespaces - defaults to 0
149 if ( count($this->namespaces
) < 1)
150 $query .= ' AND page_namespace = 0';
152 $namespaces = implode( ',', $this->namespaces
);
153 $query .= " AND page_namespace IN ($namespaces)";
156 $query .= " ORDER BY score DESC, page_id DESC";
158 $query .= $this->db
->limitResult( '', $this->limit
, $this->offset
);
160 wfDebug( "searchQuery returned: $query" );
165 ## Most of the work of these two functions are done automatically via triggers
167 function update( $pageid, $title, $text ) {
168 ## We don't want to index older revisions
169 $SQL = "UPDATE pagecontent SET textvector = NULL WHERE old_id = ".
170 "(SELECT rev_text_id FROM revision WHERE rev_page = $pageid ".
171 "ORDER BY rev_text_id DESC LIMIT 1 OFFSET 1)";
172 $this->db
->doQuery($SQL);
176 function updateTitle( $id, $title ) {
180 } ## end of the SearchPostgres class
183 class PostgresSearchResult
extends SearchResult
{
184 function PostgresSearchResult( $row ) {
185 $this->mTitle
= Title
::makeTitle( $row->page_namespace
, $row->page_title
);
186 $this->score
= $row->score
;
188 function getScore() {
193 class PostgresSearchResultSet
extends SearchResultSet
{
194 function PostgresSearchResultSet( $resultSet, $terms ) {
195 $this->mResultSet
= $resultSet;
196 $this->mTerms
= $terms;
199 function termMatches() {
200 return $this->mTerms
;
204 return $this->mResultSet
->numRows();
208 $row = $this->mResultSet
->fetchObject();
209 if( $row === false ) {
212 return new PostgresSearchResult( $row );