Removed action=print; I can't find anything using this
[mediawiki.git] / includes / search / SearchPostgres.php
blob0e5dc9bad624c0ded69f0f6b111eaff2b2f6bdba
1 <?php
2 /**
3 * PostgreSQL search engine
5 * Copyright © 2006-2007 Greg Sabino Mullane <greg@turnstep.com>
6 * http://www.mediawiki.org/
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
23 * @file
24 * @ingroup Search
27 /**
28 * Search engine hook base class for Postgres
29 * @ingroup Search
31 class SearchPostgres extends SearchEngine {
32 /**
33 * Creates an instance of this class
34 * @param $db DatabaseSqlite: database object
36 function __construct( $db ) {
37 parent::__construct( $db );
40 /**
41 * Perform a full text search query via tsearch2 and return a result set.
42 * Currently searches a page's current title (page.page_title) and
43 * latest revision article text (pagecontent.old_text)
45 * @param $term String: raw search term
46 * @return PostgresSearchResultSet
48 function searchTitle( $term ) {
49 $q = $this->searchQuery( $term , 'titlevector', 'page_title' );
50 $olderror = error_reporting(E_ERROR);
51 $resultSet = $this->db->resultObject( $this->db->query( $q, 'SearchPostgres', true ) );
52 error_reporting($olderror);
53 if (!$resultSet) {
54 // Needed for "Query requires full scan, GIN doesn't support it"
55 return new SearchResultTooMany();
57 return new PostgresSearchResultSet( $resultSet, $this->searchTerms );
59 function searchText( $term ) {
60 $q = $this->searchQuery( $term, 'textvector', 'old_text' );
61 $olderror = error_reporting(E_ERROR);
62 $resultSet = $this->db->resultObject( $this->db->query( $q, 'SearchPostgres', true ) );
63 error_reporting($olderror);
64 if (!$resultSet) {
65 return new SearchResultTooMany();
67 return new PostgresSearchResultSet( $resultSet, $this->searchTerms );
72 * Transform the user's search string into a better form for tsearch2
73 * Returns an SQL fragment consisting of quoted text to search for.
75 function parseQuery( $term ) {
77 wfDebug( "parseQuery received: $term \n" );
79 ## No backslashes allowed
80 $term = preg_replace('/\\\/', '', $term);
82 ## Collapse parens into nearby words:
83 $term = preg_replace('/\s*\(\s*/', ' (', $term);
84 $term = preg_replace('/\s*\)\s*/', ') ', $term);
86 ## Treat colons as word separators:
87 $term = preg_replace('/:/', ' ', $term);
89 $searchstring = '';
90 $m = array();
91 if( preg_match_all('/([-!]?)(\S+)\s*/', $term, $m, PREG_SET_ORDER ) ) {
92 foreach( $m as $terms ) {
93 if (strlen($terms[1])) {
94 $searchstring .= ' & !';
96 if (strtolower($terms[2]) === 'and') {
97 $searchstring .= ' & ';
99 else if (strtolower($terms[2]) === 'or' or $terms[2] === '|') {
100 $searchstring .= ' | ';
102 else if (strtolower($terms[2]) === 'not') {
103 $searchstring .= ' & !';
105 else {
106 $searchstring .= " & $terms[2]";
111 ## Strip out leading junk
112 $searchstring = preg_replace('/^[\s\&\|]+/', '', $searchstring);
114 ## Remove any doubled-up operators
115 $searchstring = preg_replace('/([\!\&\|]) +(?:[\&\|] +)+/', "$1 ", $searchstring);
117 ## Remove any non-spaced operators (e.g. "Zounds!")
118 $searchstring = preg_replace('/([^ ])[\!\&\|]/', "$1", $searchstring);
120 ## Remove any trailing whitespace or operators
121 $searchstring = preg_replace('/[\s\!\&\|]+$/', '', $searchstring);
123 ## Remove unnecessary quotes around everything
124 $searchstring = preg_replace('/^[\'"](.*)[\'"]$/', "$1", $searchstring);
126 ## Quote the whole thing
127 $searchstring = $this->db->addQuotes($searchstring);
129 wfDebug( "parseQuery returned: $searchstring \n" );
131 return $searchstring;
136 * Construct the full SQL query to do the search.
137 * @param $term String
138 * @param $fulltext String
139 * @param $colname
141 function searchQuery( $term, $fulltext, $colname ) {
142 # Get the SQL fragment for the given term
143 $searchstring = $this->parseQuery( $term );
145 ## We need a separate query here so gin does not complain about empty searches
146 $SQL = "SELECT to_tsquery($searchstring)";
147 $res = $this->db->query($SQL);
148 if (!$res) {
149 ## TODO: Better output (example to catch: one 'two)
150 die ("Sorry, that was not a valid search string. Please go back and try again");
152 $top = $res->fetchRow();
153 $top = $top[0];
155 if ($top === "") { ## e.g. if only stopwords are used XXX return something better
156 $query = "SELECT page_id, page_namespace, page_title, 0 AS score ".
157 "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
158 "AND r.rev_text_id = c.old_id AND 1=0";
160 else {
161 $m = array();
162 if( preg_match_all("/'([^']+)'/", $top, $m, PREG_SET_ORDER ) ) {
163 foreach( $m as $terms ) {
164 $this->searchTerms[$terms[1]] = $terms[1];
168 $query = "SELECT page_id, page_namespace, page_title, ".
169 "ts_rank($fulltext, to_tsquery($searchstring), 5) AS score ".
170 "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
171 "AND r.rev_text_id = c.old_id AND $fulltext @@ to_tsquery($searchstring)";
174 ## Redirects
175 if (! $this->showRedirects)
176 $query .= ' AND page_is_redirect = 0';
178 ## Namespaces - defaults to 0
179 if( !is_null($this->namespaces) ){ // null -> search all
180 if ( count($this->namespaces) < 1)
181 $query .= ' AND page_namespace = 0';
182 else {
183 $namespaces = $this->db->makeList( $this->namespaces );
184 $query .= " AND page_namespace IN ($namespaces)";
188 $query .= " ORDER BY score DESC, page_id DESC";
190 $query .= $this->db->limitResult( '', $this->limit, $this->offset );
192 wfDebug( "searchQuery returned: $query \n" );
194 return $query;
197 ## Most of the work of these two functions are done automatically via triggers
199 function update( $pageid, $title, $text ) {
200 ## We don't want to index older revisions
201 $SQL = "UPDATE pagecontent SET textvector = NULL WHERE old_id IN ".
202 "(SELECT rev_text_id FROM revision WHERE rev_page = " . intval( $pageid ) .
203 " ORDER BY rev_text_id DESC OFFSET 1)";
204 $this->db->query($SQL);
205 return true;
208 function updateTitle( $id, $title ) {
209 return true;
212 } ## end of the SearchPostgres class
215 * @ingroup Search
217 class PostgresSearchResult extends SearchResult {
218 function __construct( $row ) {
219 parent::__construct($row);
220 $this->score = $row->score;
222 function getScore() {
223 return $this->score;
228 * @ingroup Search
230 class PostgresSearchResultSet extends SqlSearchResultSet {
231 function __construct( $resultSet, $terms ) {
232 parent::__construct( $resultSet, $terms );
235 function next() {
236 $row = $this->mResultSet->fetchObject();
237 if( $row === false ) {
238 return false;
239 } else {
240 return new PostgresSearchResult( $row );