wiki.techinc.nl/includes/SearchPostgres.php

256 lines
7.3 KiB
PHP
Raw Normal View History

2006-07-05 03:54:01 +00:00
<?php
# Copyright (C) 2006-2007 Greg Sabino Mullane <greg@turnstep.com>
2006-07-05 03:54:01 +00:00
# http://www.mediawiki.org/
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
# http://www.gnu.org/copyleft/gpl.html
/**
* @file
* @ingroup Search
*/
2006-07-05 03:54:01 +00:00
/**
* Search engine hook base class for Postgres
* @ingroup Search
2006-07-05 03:54:01 +00:00
*/
class SearchPostgres extends SearchEngine {
function SearchPostgres( $db ) {
$this->db = $db;
2006-07-05 03:54:01 +00:00
}
/**
* Perform a full text search query via tsearch2 and return a result set.
* Currently searches a page's current title (page.page_title) and
2007-04-04 00:14:03 +00:00
* latest revision article text (pagecontent.old_text)
2006-07-05 03:54:01 +00:00
*
* @param string $term - Raw search term
* @return PostgresSearchResultSet
* @access public
*/
2007-04-04 00:14:03 +00:00
function searchTitle( $term ) {
$q = $this->searchQuery( $term , 'titlevector', 'page_title' );
$olderror = error_reporting(E_ERROR);
$resultSet = $this->db->resultObject( $this->db->query( $q, 'SearchPostgres', true ) );
error_reporting($olderror);
if (!$resultSet) {
// Needed for "Query requires full scan, GIN doesn't support it"
return new SearchResultTooMany();
}
2006-07-05 03:54:01 +00:00
return new PostgresSearchResultSet( $resultSet, $this->searchTerms );
}
2007-04-04 00:14:03 +00:00
function searchText( $term ) {
$q = $this->searchQuery( $term, 'textvector', 'old_text' );
$olderror = error_reporting(E_ERROR);
$resultSet = $this->db->resultObject( $this->db->query( $q, 'SearchPostgres', true ) );
error_reporting($olderror);
if (!$resultSet) {
return new SearchResultTooMany();
}
2006-07-05 03:54:01 +00:00
return new PostgresSearchResultSet( $resultSet, $this->searchTerms );
}
/*
* Transform the user's search string into a better form for tsearch2
*/
2007-04-04 00:14:03 +00:00
function parseQuery( $term ) {
wfDebug( "parseQuery received: $term" );
## No backslashes allowed
$term = preg_replace('/\\\/', '', $term);
## Collapse parens into nearby words:
$term = preg_replace('/\s*\(\s*/', ' (', $term);
$term = preg_replace('/\s*\)\s*/', ') ', $term);
2006-07-05 03:54:01 +00:00
2007-04-05 13:51:16 +00:00
## Treat colons as word separators:
$term = preg_replace('/:/', ' ', $term);
2007-04-04 00:14:03 +00:00
$searchstring = '';
$m = array();
2007-04-04 00:14:03 +00:00
if( preg_match_all('/([-!]?)(\S+)\s*/', $term, $m, PREG_SET_ORDER ) ) {
2006-07-05 03:54:01 +00:00
foreach( $m as $terms ) {
2007-04-04 00:14:03 +00:00
if (strlen($terms[1])) {
$searchstring .= ' & !';
}
if (strtolower($terms[2]) === 'and') {
$searchstring .= ' & ';
}
else if (strtolower($terms[2]) === 'or' or $terms[2] === '|') {
$searchstring .= ' | ';
}
else if (strtolower($terms[2]) === 'not') {
$searchstring .= ' & !';
}
2007-04-04 00:14:03 +00:00
else {
$searchstring .= " & $terms[2]";
2006-07-05 03:54:01 +00:00
}
}
}
2007-04-04 00:14:03 +00:00
## Strip out leading junk
$searchstring = preg_replace('/^[\s\&\|]+/', '', $searchstring);
## Remove any doubled-up operators
$searchstring = preg_replace('/([\!\&\|]) +(?:[\&\|] +)+/', "$1 ", $searchstring);
2007-04-05 13:51:16 +00:00
## Remove any non-spaced operators (e.g. "Zounds!")
$searchstring = preg_replace('/([^ ])[\!\&\|]/', "$1", $searchstring);
2007-04-09 14:24:15 +00:00
## Remove any trailing whitespace or operators
$searchstring = preg_replace('/[\s\!\&\|]+$/', '', $searchstring);
2007-04-06 12:32:26 +00:00
2007-04-09 14:24:15 +00:00
## Remove unnecessary quotes around everything
$searchstring = preg_replace('/^[\'"](.*)[\'"]$/', "$1", $searchstring);
2007-04-05 13:51:16 +00:00
2007-04-04 00:14:03 +00:00
## Quote the whole thing
$searchstring = $this->db->addQuotes($searchstring);
wfDebug( "parseQuery returned: $searchstring" );
return $searchstring;
2006-07-05 03:54:01 +00:00
}
/**
* Construct the full SQL query to do the search.
* @param string $filteredTerm
* @param string $fulltext
* @private
*/
2007-04-04 00:14:03 +00:00
function searchQuery( $term, $fulltext, $colname ) {
global $wgDBversion;
if ( !isset( $wgDBversion ) ) {
$this->db->getServerVersion();
$wgDBversion = $this->db->numeric_version;
}
2008-02-10 15:48:06 +00:00
$prefix = $wgDBversion < 8.3 ? "'default'," : '';
2006-07-05 03:54:01 +00:00
2007-04-04 00:14:03 +00:00
$searchstring = $this->parseQuery( $term );
2006-07-05 03:54:01 +00:00
## We need a separate query here so gin does not complain about empty searches
2008-02-10 15:48:06 +00:00
$SQL = "SELECT to_tsquery($prefix $searchstring)";
2007-04-04 00:14:03 +00:00
$res = $this->db->doQuery($SQL);
if (!$res) {
2007-04-04 00:14:03 +00:00
## TODO: Better output (example to catch: one 'two)
die ("Sorry, that was not a valid search string. Please go back and try again");
}
2007-04-04 00:14:03 +00:00
$top = pg_fetch_result($res,0,0);
2007-04-04 00:14:03 +00:00
if ($top === "") { ## e.g. if only stopwords are used XXX return something better
$query = "SELECT page_id, page_namespace, page_title, 0 AS score ".
"FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
"AND r.rev_text_id = c.old_id AND 1=0";
}
2007-04-04 00:14:03 +00:00
else {
$m = array();
if( preg_match_all("/'([^']+)'/", $top, $m, PREG_SET_ORDER ) ) {
foreach( $m as $terms ) {
$this->searchTerms[$terms[1]] = $terms[1];
}
}
$rankscore = $wgDBversion > 8.2 ? 5 : 1;
2008-02-10 15:48:06 +00:00
$rank = $wgDBversion < 8.3 ? 'rank' : 'ts_rank';
2007-04-04 00:14:03 +00:00
$query = "SELECT page_id, page_namespace, page_title, ".
2008-02-10 15:48:06 +00:00
"$rank($fulltext, to_tsquery($prefix $searchstring), $rankscore) AS score ".
2007-04-04 00:14:03 +00:00
"FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
2008-02-10 15:48:06 +00:00
"AND r.rev_text_id = c.old_id AND $fulltext @@ to_tsquery($prefix $searchstring)";
2007-04-04 00:14:03 +00:00
}
2006-07-05 03:54:01 +00:00
## Redirects
if (! $this->showRedirects)
2008-02-12 21:41:40 +00:00
$query .= ' AND page_is_redirect = 0';
2006-07-05 03:54:01 +00:00
## Namespaces - defaults to 0
if( !is_null($this->namespaces) ){ // null -> search all
if ( count($this->namespaces) < 1)
$query .= ' AND page_namespace = 0';
else {
$namespaces = implode( ',', $this->namespaces );
$query .= " AND page_namespace IN ($namespaces)";
}
2006-07-05 03:54:01 +00:00
}
2007-04-04 00:14:03 +00:00
$query .= " ORDER BY score DESC, page_id DESC";
2006-07-05 03:54:01 +00:00
$query .= $this->db->limitResult( '', $this->limit, $this->offset );
2007-04-04 00:14:03 +00:00
wfDebug( "searchQuery returned: $query" );
2006-07-05 03:54:01 +00:00
return $query;
}
## Most of the work of these two functions are done automatically via triggers
2006-07-05 03:54:01 +00:00
2007-01-14 22:57:31 +00:00
function update( $pageid, $title, $text ) {
## We don't want to index older revisions
$SQL = "UPDATE pagecontent SET textvector = NULL WHERE old_id = ".
"(SELECT rev_text_id FROM revision WHERE rev_page = $pageid ".
"ORDER BY rev_text_id DESC LIMIT 1 OFFSET 1)";
2007-04-04 00:14:03 +00:00
$this->db->doQuery($SQL);
return true;
}
function updateTitle( $id, $title ) {
2007-01-14 22:57:31 +00:00
return true;
}
2006-07-05 03:54:01 +00:00
} ## end of the SearchPostgres class
/**
* @ingroup Search
*/
2007-04-04 00:14:03 +00:00
class PostgresSearchResult extends SearchResult {
function PostgresSearchResult( $row ) {
parent::SearchResult($row);
2007-04-04 00:14:03 +00:00
$this->score = $row->score;
}
function getScore() {
return $this->score;
}
}
/**
* @ingroup Search
*/
2006-07-05 03:54:01 +00:00
class PostgresSearchResultSet extends SearchResultSet {
function PostgresSearchResultSet( $resultSet, $terms ) {
$this->mResultSet = $resultSet;
$this->mTerms = $terms;
}
function termMatches() {
return $this->mTerms;
}
function numRows() {
return $this->mResultSet->numRows();
}
function next() {
$row = $this->mResultSet->fetchObject();
if( $row === false ) {
return false;
} else {
2007-04-04 00:14:03 +00:00
return new PostgresSearchResult( $row );
2006-07-05 03:54:01 +00:00
}
}
}