2007-03-11 04:41:02 +00:00
|
|
|
<?php
|
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
|
|
|
/**
|
2010-08-08 10:44:59 +00:00
|
|
|
* Oracle search engine
|
|
|
|
|
*
|
|
|
|
|
* Copyright © 2004 Brion Vibber <brion@pobox.com>
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
|
|
|
|
|
2007-03-11 04:41:02 +00:00
|
|
|
/**
|
|
|
|
|
* Search engine hook base class for Oracle (ConText).
|
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-03-11 04:41:02 +00:00
|
|
|
*/
|
|
|
|
|
class SearchOracle extends SearchEngine {
|
2010-01-08 07:52:51 +00:00
|
|
|
|
|
|
|
|
private $reservedWords = array ('ABOUT' => 1,
|
|
|
|
|
'ACCUM' => 1,
|
|
|
|
|
'AND' => 1,
|
|
|
|
|
'BT' => 1,
|
|
|
|
|
'BTG' => 1,
|
|
|
|
|
'BTI' => 1,
|
|
|
|
|
'BTP' => 1,
|
|
|
|
|
'FUZZY' => 1,
|
|
|
|
|
'HASPATH' => 1,
|
|
|
|
|
'INPATH' => 1,
|
|
|
|
|
'MINUS' => 1,
|
|
|
|
|
'NEAR' => 1,
|
|
|
|
|
'NOT' => 1,
|
|
|
|
|
'NT' => 1,
|
|
|
|
|
'NTG' => 1,
|
|
|
|
|
'NTI' => 1,
|
|
|
|
|
'NTP' => 1,
|
|
|
|
|
'OR' => 1,
|
|
|
|
|
'PT' => 1,
|
|
|
|
|
'RT' => 1,
|
|
|
|
|
'SQE' => 1,
|
|
|
|
|
'SYN' => 1,
|
|
|
|
|
'TR' => 1,
|
|
|
|
|
'TRSYN' => 1,
|
|
|
|
|
'TT' => 1,
|
|
|
|
|
'WITHIN' => 1);
|
2010-11-22 16:08:12 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates an instance of this class
|
|
|
|
|
* @param $db DatabasePostgres: database object
|
|
|
|
|
*/
|
2007-03-11 04:41:02 +00:00
|
|
|
function __construct($db) {
|
2010-11-22 16:08:12 +00:00
|
|
|
parent::__construct( $db );
|
2007-03-11 04:41:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Perform a full text search query and return a result set.
|
|
|
|
|
*
|
2009-04-23 10:28:05 +00:00
|
|
|
* @param $term String: raw search term
|
2010-01-09 14:30:55 +00:00
|
|
|
* @return SqlSearchResultSet
|
2007-03-11 04:41:02 +00:00
|
|
|
*/
|
|
|
|
|
function searchText( $term ) {
|
2009-06-05 11:45:32 +00:00
|
|
|
if ($term == '')
|
2010-01-09 14:30:55 +00:00
|
|
|
return new SqlSearchResultSet(false, '');
|
2009-12-28 14:10:00 +00:00
|
|
|
|
2007-03-11 04:41:02 +00:00
|
|
|
$resultSet = $this->db->resultObject($this->db->query($this->getQuery($this->filter($term), true)));
|
2010-01-09 14:30:55 +00:00
|
|
|
return new SqlSearchResultSet($resultSet, $this->searchTerms);
|
2007-03-11 04:41:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Perform a title-only search query and return a result set.
|
|
|
|
|
*
|
2009-04-23 10:28:05 +00:00
|
|
|
* @param $term String: raw search term
|
2010-01-09 14:30:55 +00:00
|
|
|
* @return SqlSearchResultSet
|
2007-03-11 04:41:02 +00:00
|
|
|
*/
|
|
|
|
|
function searchTitle($term) {
|
2009-06-05 11:45:32 +00:00
|
|
|
if ($term == '')
|
2010-01-09 14:30:55 +00:00
|
|
|
return new SqlSearchResultSet(false, '');
|
2009-12-28 14:10:00 +00:00
|
|
|
|
2007-03-11 04:41:02 +00:00
|
|
|
$resultSet = $this->db->resultObject($this->db->query($this->getQuery($this->filter($term), false)));
|
|
|
|
|
return new MySQLSearchResultSet($resultSet, $this->searchTerms);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return a partial WHERE clause to exclude redirects, if so set
|
2009-04-23 10:28:05 +00:00
|
|
|
* @return String
|
2007-03-11 04:41:02 +00:00
|
|
|
*/
|
|
|
|
|
function queryRedirect() {
|
|
|
|
|
if ($this->showRedirects) {
|
|
|
|
|
return '';
|
|
|
|
|
} else {
|
|
|
|
|
return 'AND page_is_redirect=0';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return a partial WHERE clause to limit the search to the given namespaces
|
2009-04-23 10:28:05 +00:00
|
|
|
* @return String
|
2007-03-11 04:41:02 +00:00
|
|
|
*/
|
|
|
|
|
function queryNamespaces() {
|
2008-03-23 18:08:23 +00:00
|
|
|
if( is_null($this->namespaces) )
|
|
|
|
|
return '';
|
2008-12-22 12:31:15 +00:00
|
|
|
if ( !count( $this->namespaces ) ) {
|
2007-03-11 04:41:02 +00:00
|
|
|
$namespaces = '0';
|
2008-12-22 12:31:15 +00:00
|
|
|
} else {
|
|
|
|
|
$namespaces = $this->db->makeList( $this->namespaces );
|
2007-03-11 04:41:02 +00:00
|
|
|
}
|
|
|
|
|
return 'AND page_namespace IN (' . $namespaces . ')';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return a LIMIT clause to limit results on the query.
|
2011-05-28 19:00:01 +00:00
|
|
|
*
|
2012-07-15 20:32:48 +00:00
|
|
|
* @param $sql string
|
2011-05-28 19:00:01 +00:00
|
|
|
*
|
2009-04-23 10:28:05 +00:00
|
|
|
* @return String
|
2007-03-11 04:41:02 +00:00
|
|
|
*/
|
2011-05-28 19:00:01 +00:00
|
|
|
function queryLimit( $sql ) {
|
2007-03-11 04:41:02 +00:00
|
|
|
return $this->db->limitResult($sql, $this->limit, $this->offset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Does not do anything for generic search engine
|
|
|
|
|
* subclasses may define this though
|
2011-05-28 19:00:01 +00:00
|
|
|
*
|
2009-04-23 10:28:05 +00:00
|
|
|
* @return String
|
2007-03-11 04:41:02 +00:00
|
|
|
*/
|
2011-05-28 19:00:01 +00:00
|
|
|
function queryRanking( $filteredTerm, $fulltext ) {
|
2007-03-11 04:53:58 +00:00
|
|
|
return ' ORDER BY score(1)';
|
2007-03-11 04:41:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Construct the full SQL query to do the search.
|
|
|
|
|
* The guts shoulds be constructed in queryMain()
|
2009-04-23 10:28:05 +00:00
|
|
|
* @param $filteredTerm String
|
|
|
|
|
* @param $fulltext Boolean
|
2012-02-09 21:36:14 +00:00
|
|
|
* @return String
|
2007-03-11 04:41:02 +00:00
|
|
|
*/
|
|
|
|
|
function getQuery( $filteredTerm, $fulltext ) {
|
|
|
|
|
return $this->queryLimit($this->queryMain($filteredTerm, $fulltext) . ' ' .
|
|
|
|
|
$this->queryRedirect() . ' ' .
|
|
|
|
|
$this->queryNamespaces() . ' ' .
|
|
|
|
|
$this->queryRanking( $filteredTerm, $fulltext ) . ' ');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Picks which field to index on, depending on what type of query.
|
2009-04-23 10:28:05 +00:00
|
|
|
* @param $fulltext Boolean
|
|
|
|
|
* @return String
|
2007-03-11 04:41:02 +00:00
|
|
|
*/
|
|
|
|
|
function getIndexField($fulltext) {
|
|
|
|
|
return $fulltext ? 'si_text' : 'si_title';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the base part of the search query.
|
|
|
|
|
*
|
2009-04-23 10:28:05 +00:00
|
|
|
* @param $filteredTerm String
|
|
|
|
|
* @param $fulltext Boolean
|
|
|
|
|
* @return String
|
2007-03-11 04:41:02 +00:00
|
|
|
*/
|
|
|
|
|
function queryMain( $filteredTerm, $fulltext ) {
|
|
|
|
|
$match = $this->parseQuery($filteredTerm, $fulltext);
|
|
|
|
|
$page = $this->db->tableName('page');
|
|
|
|
|
$searchindex = $this->db->tableName('searchindex');
|
|
|
|
|
return 'SELECT page_id, page_namespace, page_title ' .
|
|
|
|
|
"FROM $page,$searchindex " .
|
|
|
|
|
'WHERE page_id=si_page AND ' . $match;
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-28 14:10:00 +00:00
|
|
|
/**
|
|
|
|
|
* Parse a user input search string, and return an SQL fragment to be used
|
2008-12-22 12:31:15 +00:00
|
|
|
* as part of a WHERE clause
|
2012-02-09 21:36:14 +00:00
|
|
|
* @return string
|
2008-12-22 12:31:15 +00:00
|
|
|
*/
|
2007-03-11 04:53:58 +00:00
|
|
|
function parseQuery($filteredText, $fulltext) {
|
2007-03-11 04:41:02 +00:00
|
|
|
global $wgContLang;
|
|
|
|
|
$lc = SearchEngine::legalSearchChars();
|
|
|
|
|
$this->searchTerms = array();
|
|
|
|
|
|
2011-05-17 22:03:20 +00:00
|
|
|
# @todo FIXME: This doesn't handle parenthetical expressions.
|
2007-03-11 04:41:02 +00:00
|
|
|
$m = array();
|
2009-12-28 14:10:00 +00:00
|
|
|
$searchon = '';
|
2007-03-11 04:53:58 +00:00
|
|
|
if (preg_match_all('/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
|
|
|
|
|
$filteredText, $m, PREG_SET_ORDER)) {
|
|
|
|
|
foreach($m as $terms) {
|
2009-06-09 19:19:14 +00:00
|
|
|
// Search terms in all variant forms, only
|
|
|
|
|
// apply on wiki with LanguageConverter
|
|
|
|
|
$temp_terms = $wgContLang->autoConvertToAllVariants( $terms[2] );
|
|
|
|
|
if( is_array( $temp_terms )) {
|
|
|
|
|
$temp_terms = array_unique( array_values( $temp_terms ));
|
2009-12-28 14:10:00 +00:00
|
|
|
foreach( $temp_terms as $t ) {
|
|
|
|
|
$searchon .= ($terms[1] == '-' ? ' ~' : ' & ') . $this->escapeTerm( $t );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$searchon .= ($terms[1] == '-' ? ' ~' : ' & ') . $this->escapeTerm( $terms[2] );
|
2009-06-09 19:19:14 +00:00
|
|
|
}
|
2007-03-11 04:53:58 +00:00
|
|
|
if (!empty($terms[3])) {
|
2007-03-11 04:41:02 +00:00
|
|
|
$regexp = preg_quote( $terms[3], '/' );
|
2007-03-11 04:53:58 +00:00
|
|
|
if ($terms[4])
|
|
|
|
|
$regexp .= "[0-9A-Za-z_]+";
|
2007-03-11 04:41:02 +00:00
|
|
|
} else {
|
2007-03-11 04:53:58 +00:00
|
|
|
$regexp = preg_quote(str_replace('"', '', $terms[2]), '/');
|
2007-03-11 04:41:02 +00:00
|
|
|
}
|
|
|
|
|
$this->searchTerms[] = $regexp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-28 14:10:00 +00:00
|
|
|
|
|
|
|
|
$searchon = $this->db->addQuotes(ltrim($searchon, ' &'));
|
2007-03-11 04:53:58 +00:00
|
|
|
$field = $this->getIndexField($fulltext);
|
2008-12-22 12:31:15 +00:00
|
|
|
return " CONTAINS($field, $searchon, 1) > 0 ";
|
2007-03-11 04:41:02 +00:00
|
|
|
}
|
|
|
|
|
|
2009-12-28 14:10:00 +00:00
|
|
|
private function escapeTerm($t) {
|
|
|
|
|
global $wgContLang;
|
2010-02-02 15:09:01 +00:00
|
|
|
$t = $wgContLang->normalizeForSearch($t);
|
2010-01-08 07:52:51 +00:00
|
|
|
$t = isset($this->reservedWords[strtoupper($t)]) ? '{'.$t.'}' : $t;
|
2009-12-28 14:10:00 +00:00
|
|
|
$t = preg_replace('/^"(.*)"$/', '($1)', $t);
|
|
|
|
|
$t = preg_replace('/([-&|])/', '\\\\$1', $t);
|
|
|
|
|
return $t;
|
|
|
|
|
}
|
2007-03-11 04:41:02 +00:00
|
|
|
/**
|
|
|
|
|
* Create or update the search index record for the given page.
|
|
|
|
|
* Title and text should be pre-processed.
|
|
|
|
|
*
|
2009-04-23 10:28:05 +00:00
|
|
|
* @param $id Integer
|
|
|
|
|
* @param $title String
|
|
|
|
|
* @param $text String
|
2007-03-11 04:41:02 +00:00
|
|
|
*/
|
|
|
|
|
function update($id, $title, $text) {
|
|
|
|
|
$dbw = wfGetDB(DB_MASTER);
|
|
|
|
|
$dbw->replace('searchindex',
|
|
|
|
|
array('si_page'),
|
|
|
|
|
array(
|
|
|
|
|
'si_page' => $id,
|
|
|
|
|
'si_title' => $title,
|
|
|
|
|
'si_text' => $text
|
|
|
|
|
), 'SearchOracle::update' );
|
2011-01-27 08:25:48 +00:00
|
|
|
|
|
|
|
|
// Sync the index
|
|
|
|
|
// We need to specify the DB name (i.e. user/schema) here so that
|
|
|
|
|
// it can work from the installer, where
|
|
|
|
|
// ALTER SESSION SET CURRENT_SCHEMA = ...
|
|
|
|
|
// was used.
|
|
|
|
|
$dbw->query( "CALL ctx_ddl.sync_index(" .
|
2011-09-06 20:51:10 +00:00
|
|
|
$dbw->addQuotes( $dbw->getDBname() . '.' . $dbw->tableName( 'si_text_idx', 'raw' ) ) . ")" );
|
2011-01-27 08:25:48 +00:00
|
|
|
$dbw->query( "CALL ctx_ddl.sync_index(" .
|
2011-09-06 20:51:10 +00:00
|
|
|
$dbw->addQuotes( $dbw->getDBname() . '.' . $dbw->tableName( 'si_title_idx', 'raw' ) ) . ")" );
|
2007-03-11 04:41:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update a search index record's title only.
|
|
|
|
|
* Title should be pre-processed.
|
|
|
|
|
*
|
2010-05-14 20:51:16 +00:00
|
|
|
* @param $id Integer
|
|
|
|
|
* @param $title String
|
2007-03-11 04:41:02 +00:00
|
|
|
*/
|
2007-03-11 05:09:35 +00:00
|
|
|
function updateTitle($id, $title) {
|
2007-03-11 04:41:02 +00:00
|
|
|
$dbw = wfGetDB(DB_MASTER);
|
|
|
|
|
|
|
|
|
|
$dbw->update('searchindex',
|
|
|
|
|
array('si_title' => $title),
|
|
|
|
|
array('si_page' => $id),
|
|
|
|
|
'SearchOracle::updateTitle',
|
|
|
|
|
array());
|
|
|
|
|
}
|
2009-12-28 14:10:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
public static function legalSearchChars() {
|
|
|
|
|
return "\"" . parent::legalSearchChars();
|
|
|
|
|
}
|
2007-03-11 04:41:02 +00:00
|
|
|
}
|