2006-07-05 03:54:01 +00:00
|
|
|
<?php
|
2007-02-22 17:35:44 +00:00
|
|
|
# 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
|
|
|
|
|
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
/**
|
|
|
|
|
* @file
|
|
|
|
|
* @ingroup Search
|
|
|
|
|
*/
|
|
|
|
|
|
2006-07-05 03:54:01 +00:00
|
|
|
/**
|
|
|
|
|
* Search engine hook base class for Postgres
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup Search
|
2006-07-05 03:54:01 +00:00
|
|
|
*/
|
|
|
|
|
class SearchPostgres extends SearchEngine {
|
|
|
|
|
|
2008-07-02 02:02:26 +00:00
|
|
|
function __construct( $db ) {
|
2007-01-22 23:50:42 +00:00
|
|
|
$this->db = $db;
|
2006-07-05 03:54:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Perform a full text search query via tsearch2 and return a result set.
|
2008-04-14 07:45:50 +00:00
|
|
|
* 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 ) {
|
2008-02-17 14:11:55 +00:00
|
|
|
$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 ) {
|
2008-02-17 14:11:55 +00:00
|
|
|
$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
|
2008-12-22 12:31:15 +00:00
|
|
|
* Returns an SQL fragment consisting of quoted text to search for.
|
2006-07-05 03:54:01 +00:00
|
|
|
*/
|
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 = '';
|
2007-05-08 09:09:46 +00:00
|
|
|
$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-03-26 13:05:55 +00:00
|
|
|
}
|
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 ) {
|
2007-05-15 12:39:41 +00:00
|
|
|
global $wgDBversion;
|
|
|
|
|
|
2007-05-16 20:31:58 +00:00
|
|
|
if ( !isset( $wgDBversion ) ) {
|
2007-05-15 12:39:41 +00:00
|
|
|
$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
|
|
|
|
2008-12-22 12:31:15 +00:00
|
|
|
# Get the SQL fragment for the given term
|
2007-04-04 00:14:03 +00:00
|
|
|
$searchstring = $this->parseQuery( $term );
|
2006-07-05 03:54:01 +00:00
|
|
|
|
2007-03-26 13:05:55 +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);
|
2007-03-26 13:05:55 +00:00
|
|
|
if (!$res) {
|
2007-04-04 00:14:03 +00:00
|
|
|
## TODO: Better output (example to catch: one 'two)
|
2007-03-26 13:05:55 +00:00
|
|
|
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-03-26 13:05:55 +00:00
|
|
|
|
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 ".
|
2007-03-26 13:05:55 +00:00
|
|
|
"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 {
|
2007-04-27 03:45:43 +00:00
|
|
|
$m = array();
|
|
|
|
|
if( preg_match_all("/'([^']+)'/", $top, $m, PREG_SET_ORDER ) ) {
|
|
|
|
|
foreach( $m as $terms ) {
|
|
|
|
|
$this->searchTerms[$terms[1]] = $terms[1];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-15 12:39:41 +00:00
|
|
|
$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
|
2008-03-23 17:29:43 +00:00
|
|
|
if( !is_null($this->namespaces) ){ // null -> search all
|
|
|
|
|
if ( count($this->namespaces) < 1)
|
|
|
|
|
$query .= ' AND page_namespace = 0';
|
|
|
|
|
else {
|
2008-12-22 12:31:15 +00:00
|
|
|
$namespaces = $this->db->makeList( $this->namespaces );
|
2008-03-23 17:29:43 +00:00
|
|
|
$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;
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-26 13:05:55 +00:00
|
|
|
## 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
|
2008-07-29 14:14:10 +00:00
|
|
|
$SQL = "UPDATE pagecontent SET textvector = NULL WHERE old_id IN ".
|
2008-12-22 12:31:15 +00:00
|
|
|
"(SELECT rev_text_id FROM revision WHERE rev_page = " . intval( $pageid ) .
|
|
|
|
|
" ORDER BY rev_text_id DESC 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
|
|
|
|
|
|
2007-04-20 08:55:14 +00:00
|
|
|
/**
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup Search
|
2007-04-20 08:55:14 +00:00
|
|
|
*/
|
2007-04-04 00:14:03 +00:00
|
|
|
class PostgresSearchResult extends SearchResult {
|
2008-07-02 02:02:26 +00:00
|
|
|
function __construct( $row ) {
|
|
|
|
|
parent::__construct($row);
|
2007-04-04 00:14:03 +00:00
|
|
|
$this->score = $row->score;
|
|
|
|
|
}
|
|
|
|
|
function getScore() {
|
|
|
|
|
return $this->score;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-20 08:55:14 +00:00
|
|
|
/**
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup Search
|
2007-04-20 08:55:14 +00:00
|
|
|
*/
|
2006-07-05 03:54:01 +00:00
|
|
|
class PostgresSearchResultSet extends SearchResultSet {
|
2008-07-02 02:02:26 +00:00
|
|
|
function __construct( $resultSet, $terms ) {
|
2006-07-05 03:54:01 +00:00
|
|
|
$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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|