00001 <?php
00002 # Copyright (C) 2006-2007 Greg Sabino Mullane <greg@turnstep.com>
00003 # http://www.mediawiki.org/
00004 #
00005 # This program is free software; you can redistribute it and/or modify
00006 # it under the terms of the GNU General Public License as published by
00007 # the Free Software Foundation; either version 2 of the License, or
00008 # (at your option) any later version.
00009 #
00010 # This program is distributed in the hope that it will be useful,
00011 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013 # GNU General Public License for more details.
00014 #
00015 # You should have received a copy of the GNU General Public License along
00016 # with this program; if not, write to the Free Software Foundation, Inc.,
00017 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018 # http://www.gnu.org/copyleft/gpl.html
00019
00029 class SearchPostgres extends SearchEngine {
00030
00031 function __construct( $db ) {
00032 $this->db = $db;
00033 }
00034
00044 function searchTitle( $term ) {
00045 $q = $this->searchQuery( $term , 'titlevector', 'page_title' );
00046 $olderror = error_reporting(E_ERROR);
00047 $resultSet = $this->db->resultObject( $this->db->query( $q, 'SearchPostgres', true ) );
00048 error_reporting($olderror);
00049 if (!$resultSet) {
00050
00051 return new SearchResultTooMany();
00052 }
00053 return new PostgresSearchResultSet( $resultSet, $this->searchTerms );
00054 }
00055 function searchText( $term ) {
00056 $q = $this->searchQuery( $term, 'textvector', 'old_text' );
00057 $olderror = error_reporting(E_ERROR);
00058 $resultSet = $this->db->resultObject( $this->db->query( $q, 'SearchPostgres', true ) );
00059 error_reporting($olderror);
00060 if (!$resultSet) {
00061 return new SearchResultTooMany();
00062 }
00063 return new PostgresSearchResultSet( $resultSet, $this->searchTerms );
00064 }
00065
00066
00067
00068
00069
00070
00071 function parseQuery( $term ) {
00072
00073 wfDebug( "parseQuery received: $term \n" );
00074
00075 ## No backslashes allowed
00076 $term = preg_replace('/\\\/', '', $term);
00077
00078 ## Collapse parens into nearby words:
00079 $term = preg_replace('/\s*\(\s*/', ' (', $term);
00080 $term = preg_replace('/\s*\)\s*/', ') ', $term);
00081
00082 ## Treat colons as word separators:
00083 $term = preg_replace('/:/', ' ', $term);
00084
00085 $searchstring = '';
00086 $m = array();
00087 if( preg_match_all('/([-!]?)(\S+)\s*/', $term, $m, PREG_SET_ORDER ) ) {
00088 foreach( $m as $terms ) {
00089 if (strlen($terms[1])) {
00090 $searchstring .= ' & !';
00091 }
00092 if (strtolower($terms[2]) === 'and') {
00093 $searchstring .= ' & ';
00094 }
00095 else if (strtolower($terms[2]) === 'or' or $terms[2] === '|') {
00096 $searchstring .= ' | ';
00097 }
00098 else if (strtolower($terms[2]) === 'not') {
00099 $searchstring .= ' & !';
00100 }
00101 else {
00102 $searchstring .= " & $terms[2]";
00103 }
00104 }
00105 }
00106
00107 ## Strip out leading junk
00108 $searchstring = preg_replace('/^[\s\&\|]+/', '', $searchstring);
00109
00110 ## Remove any doubled-up operators
00111 $searchstring = preg_replace('/([\!\&\|]) +(?:[\&\|] +)+/', "$1 ", $searchstring);
00112
00113 ## Remove any non-spaced operators (e.g. "Zounds!")
00114 $searchstring = preg_replace('/([^ ])[\!\&\|]/', "$1", $searchstring);
00115
00116 ## Remove any trailing whitespace or operators
00117 $searchstring = preg_replace('/[\s\!\&\|]+$/', '', $searchstring);
00118
00119 ## Remove unnecessary quotes around everything
00120 $searchstring = preg_replace('/^[\'"](.*)[\'"]$/', "$1", $searchstring);
00121
00122 ## Quote the whole thing
00123 $searchstring = $this->db->addQuotes($searchstring);
00124
00125 wfDebug( "parseQuery returned: $searchstring \n" );
00126
00127 return $searchstring;
00128
00129 }
00130
00137 function searchQuery( $term, $fulltext, $colname ) {
00138 global $wgDBversion;
00139
00140 if ( !isset( $wgDBversion ) ) {
00141 $this->db->getServerVersion();
00142 $wgDBversion = $this->db->numeric_version;
00143 }
00144 $prefix = $wgDBversion < 8.3 ? "'default'," : '';
00145
00146 # Get the SQL fragment for the given term
00147 $searchstring = $this->parseQuery( $term );
00148
00149 ## We need a separate query here so gin does not complain about empty searches
00150 $SQL = "SELECT to_tsquery($prefix $searchstring)";
00151 $res = $this->db->doQuery($SQL);
00152 if (!$res) {
00153 ## TODO: Better output (example to catch: one 'two)
00154 die ("Sorry, that was not a valid search string. Please go back and try again");
00155 }
00156 $top = pg_fetch_result($res,0,0);
00157
00158 if ($top === "") { ## e.g. if only stopwords are used XXX return something better
00159 $query = "SELECT page_id, page_namespace, page_title, 0 AS score ".
00160 "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
00161 "AND r.rev_text_id = c.old_id AND 1=0";
00162 }
00163 else {
00164 $m = array();
00165 if( preg_match_all("/'([^']+)'/", $top, $m, PREG_SET_ORDER ) ) {
00166 foreach( $m as $terms ) {
00167 $this->searchTerms[$terms[1]] = $terms[1];
00168 }
00169 }
00170
00171 $rankscore = $wgDBversion > 8.2 ? 5 : 1;
00172 $rank = $wgDBversion < 8.3 ? 'rank' : 'ts_rank';
00173 $query = "SELECT page_id, page_namespace, page_title, ".
00174 "$rank($fulltext, to_tsquery($prefix $searchstring), $rankscore) AS score ".
00175 "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
00176 "AND r.rev_text_id = c.old_id AND $fulltext @@ to_tsquery($prefix $searchstring)";
00177 }
00178
00179 ## Redirects
00180 if (! $this->showRedirects)
00181 $query .= ' AND page_is_redirect = 0';
00182
00183 ## Namespaces - defaults to 0
00184 if( !is_null($this->namespaces) ){
00185 if ( count($this->namespaces) < 1)
00186 $query .= ' AND page_namespace = 0';
00187 else {
00188 $namespaces = $this->db->makeList( $this->namespaces );
00189 $query .= " AND page_namespace IN ($namespaces)";
00190 }
00191 }
00192
00193 $query .= " ORDER BY score DESC, page_id DESC";
00194
00195 $query .= $this->db->limitResult( '', $this->limit, $this->offset );
00196
00197 wfDebug( "searchQuery returned: $query \n" );
00198
00199 return $query;
00200 }
00201
00202 ## Most of the work of these two functions are done automatically via triggers
00203
00204 function update( $pageid, $title, $text ) {
00205 ## We don't want to index older revisions
00206 $SQL = "UPDATE pagecontent SET textvector = NULL WHERE old_id IN ".
00207 "(SELECT rev_text_id FROM revision WHERE rev_page = " . intval( $pageid ) .
00208 " ORDER BY rev_text_id DESC OFFSET 1)";
00209 $this->db->doQuery($SQL);
00210 return true;
00211 }
00212
00213 function updateTitle( $id, $title ) {
00214 return true;
00215 }
00216
00217 } ## end of the SearchPostgres class
00218
00222 class PostgresSearchResult extends SearchResult {
00223 function __construct( $row ) {
00224 parent::__construct($row);
00225 $this->score = $row->score;
00226 }
00227 function getScore() {
00228 return $this->score;
00229 }
00230 }
00231
00235 class PostgresSearchResultSet extends SearchResultSet {
00236 function __construct( $resultSet, $terms ) {
00237 $this->mResultSet = $resultSet;
00238 $this->mTerms = $terms;
00239 }
00240
00241 function termMatches() {
00242 return $this->mTerms;
00243 }
00244
00245 function numRows() {
00246 return $this->mResultSet->numRows();
00247 }
00248
00249 function next() {
00250 $row = $this->mResultSet->fetchObject();
00251 if( $row === false ) {
00252 return false;
00253 } else {
00254 return new PostgresSearchResult( $row );
00255 }
00256 }
00257 }