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
00043 function searchTitle( $term ) {
00044 $q = $this->searchQuery( $term , 'titlevector', 'page_title' );
00045 $olderror = error_reporting(E_ERROR);
00046 $resultSet = $this->db->resultObject( $this->db->query( $q, 'SearchPostgres', true ) );
00047 error_reporting($olderror);
00048 if (!$resultSet) {
00049
00050 return new SearchResultTooMany();
00051 }
00052 return new PostgresSearchResultSet( $resultSet, $this->searchTerms );
00053 }
00054 function searchText( $term ) {
00055 $q = $this->searchQuery( $term, 'textvector', 'old_text' );
00056 $olderror = error_reporting(E_ERROR);
00057 $resultSet = $this->db->resultObject( $this->db->query( $q, 'SearchPostgres', true ) );
00058 error_reporting($olderror);
00059 if (!$resultSet) {
00060 return new SearchResultTooMany();
00061 }
00062 return new PostgresSearchResultSet( $resultSet, $this->searchTerms );
00063 }
00064
00065
00066
00067
00068
00069
00070 function parseQuery( $term ) {
00071
00072 wfDebug( "parseQuery received: $term \n" );
00073
00074 ## No backslashes allowed
00075 $term = preg_replace('/\\\/', '', $term);
00076
00077 ## Collapse parens into nearby words:
00078 $term = preg_replace('/\s*\(\s*/', ' (', $term);
00079 $term = preg_replace('/\s*\)\s*/', ') ', $term);
00080
00081 ## Treat colons as word separators:
00082 $term = preg_replace('/:/', ' ', $term);
00083
00084 $searchstring = '';
00085 $m = array();
00086 if( preg_match_all('/([-!]?)(\S+)\s*/', $term, $m, PREG_SET_ORDER ) ) {
00087 foreach( $m as $terms ) {
00088 if (strlen($terms[1])) {
00089 $searchstring .= ' & !';
00090 }
00091 if (strtolower($terms[2]) === 'and') {
00092 $searchstring .= ' & ';
00093 }
00094 else if (strtolower($terms[2]) === 'or' or $terms[2] === '|') {
00095 $searchstring .= ' | ';
00096 }
00097 else if (strtolower($terms[2]) === 'not') {
00098 $searchstring .= ' & !';
00099 }
00100 else {
00101 $searchstring .= " & $terms[2]";
00102 }
00103 }
00104 }
00105
00106 ## Strip out leading junk
00107 $searchstring = preg_replace('/^[\s\&\|]+/', '', $searchstring);
00108
00109 ## Remove any doubled-up operators
00110 $searchstring = preg_replace('/([\!\&\|]) +(?:[\&\|] +)+/', "$1 ", $searchstring);
00111
00112 ## Remove any non-spaced operators (e.g. "Zounds!")
00113 $searchstring = preg_replace('/([^ ])[\!\&\|]/', "$1", $searchstring);
00114
00115 ## Remove any trailing whitespace or operators
00116 $searchstring = preg_replace('/[\s\!\&\|]+$/', '', $searchstring);
00117
00118 ## Remove unnecessary quotes around everything
00119 $searchstring = preg_replace('/^[\'"](.*)[\'"]$/', "$1", $searchstring);
00120
00121 ## Quote the whole thing
00122 $searchstring = $this->db->addQuotes($searchstring);
00123
00124 wfDebug( "parseQuery returned: $searchstring \n" );
00125
00126 return $searchstring;
00127
00128 }
00129
00135 function searchQuery( $term, $fulltext, $colname ) {
00136 global $wgDBversion;
00137
00138 if ( !isset( $wgDBversion ) ) {
00139 $this->db->getServerVersion();
00140 $wgDBversion = $this->db->numeric_version;
00141 }
00142 $prefix = $wgDBversion < 8.3 ? "'default'," : '';
00143
00144 # Get the SQL fragment for the given term
00145 $searchstring = $this->parseQuery( $term );
00146
00147 ## We need a separate query here so gin does not complain about empty searches
00148 $SQL = "SELECT to_tsquery($prefix $searchstring)";
00149 $res = $this->db->doQuery($SQL);
00150 if (!$res) {
00151 ## TODO: Better output (example to catch: one 'two)
00152 die ("Sorry, that was not a valid search string. Please go back and try again");
00153 }
00154 $top = pg_fetch_result($res,0,0);
00155
00156 if ($top === "") { ## e.g. if only stopwords are used XXX return something better
00157 $query = "SELECT page_id, page_namespace, page_title, 0 AS score ".
00158 "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
00159 "AND r.rev_text_id = c.old_id AND 1=0";
00160 }
00161 else {
00162 $m = array();
00163 if( preg_match_all("/'([^']+)'/", $top, $m, PREG_SET_ORDER ) ) {
00164 foreach( $m as $terms ) {
00165 $this->searchTerms[$terms[1]] = $terms[1];
00166 }
00167 }
00168
00169 $rankscore = $wgDBversion > 8.2 ? 5 : 1;
00170 $rank = $wgDBversion < 8.3 ? 'rank' : 'ts_rank';
00171 $query = "SELECT page_id, page_namespace, page_title, ".
00172 "$rank($fulltext, to_tsquery($prefix $searchstring), $rankscore) AS score ".
00173 "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
00174 "AND r.rev_text_id = c.old_id AND $fulltext @@ to_tsquery($prefix $searchstring)";
00175 }
00176
00177 ## Redirects
00178 if (! $this->showRedirects)
00179 $query .= ' AND page_is_redirect = 0';
00180
00181 ## Namespaces - defaults to 0
00182 if( !is_null($this->namespaces) ){
00183 if ( count($this->namespaces) < 1)
00184 $query .= ' AND page_namespace = 0';
00185 else {
00186 $namespaces = $this->db->makeList( $this->namespaces );
00187 $query .= " AND page_namespace IN ($namespaces)";
00188 }
00189 }
00190
00191 $query .= " ORDER BY score DESC, page_id DESC";
00192
00193 $query .= $this->db->limitResult( '', $this->limit, $this->offset );
00194
00195 wfDebug( "searchQuery returned: $query \n" );
00196
00197 return $query;
00198 }
00199
00200 ## Most of the work of these two functions are done automatically via triggers
00201
00202 function update( $pageid, $title, $text ) {
00203 ## We don't want to index older revisions
00204 $SQL = "UPDATE pagecontent SET textvector = NULL WHERE old_id IN ".
00205 "(SELECT rev_text_id FROM revision WHERE rev_page = " . intval( $pageid ) .
00206 " ORDER BY rev_text_id DESC OFFSET 1)";
00207 $this->db->doQuery($SQL);
00208 return true;
00209 }
00210
00211 function updateTitle( $id, $title ) {
00212 return true;
00213 }
00214
00215 } ## end of the SearchPostgres class
00216
00220 class PostgresSearchResult extends SearchResult {
00221 function __construct( $row ) {
00222 parent::__construct($row);
00223 $this->score = $row->score;
00224 }
00225 function getScore() {
00226 return $this->score;
00227 }
00228 }
00229
00233 class PostgresSearchResultSet extends SqlSearchResultSet {
00234 function __construct( $resultSet, $terms ) {
00235 parent::__construct( $resultSet, $terms );
00236 }
00237
00238 function next() {
00239 $row = $this->mResultSet->fetchObject();
00240 if( $row === false ) {
00241 return false;
00242 } else {
00243 return new PostgresSearchResult( $row );
00244 }
00245 }
00246 }