2004-02-18 02:15:00 +00:00
|
|
|
<?php
|
2004-10-24 19:14:48 +00:00
|
|
|
/**
|
|
|
|
|
* Contain a class for special pages
|
|
|
|
|
* @package MediaWiki
|
2005-04-29 18:41:38 +00:00
|
|
|
* @subpackage Search
|
2004-10-24 19:14:48 +00:00
|
|
|
*/
|
2004-09-02 23:28:24 +00:00
|
|
|
|
2005-01-27 19:51:47 +00:00
|
|
|
/**
|
|
|
|
|
* @package MediaWiki
|
|
|
|
|
*/
|
2003-04-14 23:10:40 +00:00
|
|
|
class SearchEngine {
|
2004-10-20 09:56:34 +00:00
|
|
|
var $limit = 10;
|
|
|
|
|
var $offset = 0;
|
|
|
|
|
var $searchTerms = array();
|
|
|
|
|
var $namespaces = array( 0 );
|
|
|
|
|
var $showRedirects = false;
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2004-10-20 09:56:34 +00:00
|
|
|
* Perform a full text search query and return a result set.
|
2005-05-23 08:42:20 +00:00
|
|
|
* If title searches are not supported or disabled, return null.
|
2004-10-20 09:56:34 +00:00
|
|
|
*
|
|
|
|
|
* @param string $term - Raw search term
|
2005-05-23 08:42:20 +00:00
|
|
|
* @return SearchResultSet
|
2004-10-20 09:56:34 +00:00
|
|
|
* @access public
|
2005-05-23 08:42:20 +00:00
|
|
|
* @abstract
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2004-10-20 09:56:34 +00:00
|
|
|
function searchText( $term ) {
|
2005-05-23 08:42:20 +00:00
|
|
|
return null;
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2004-10-20 09:56:34 +00:00
|
|
|
* Perform a title-only search query and return a result set.
|
2005-05-23 08:42:20 +00:00
|
|
|
* If title searches are not supported or disabled, return null.
|
2004-10-20 09:56:34 +00:00
|
|
|
*
|
|
|
|
|
* @param string $term - Raw search term
|
2005-05-23 08:42:20 +00:00
|
|
|
* @return SearchResultSet
|
2004-10-20 09:56:34 +00:00
|
|
|
* @access public
|
2005-05-23 08:42:20 +00:00
|
|
|
* @abstract
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2004-10-20 09:56:34 +00:00
|
|
|
function searchTitle( $term ) {
|
2005-05-23 08:42:20 +00:00
|
|
|
return null;
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2004-10-20 09:56:34 +00:00
|
|
|
* If an exact title match can be find, or a very slightly close match,
|
|
|
|
|
* return the title. If no match, returns NULL.
|
|
|
|
|
*
|
2005-04-10 00:03:49 +00:00
|
|
|
* @static
|
2004-10-20 09:56:34 +00:00
|
|
|
* @param string $term
|
|
|
|
|
* @return Title
|
2004-09-02 23:28:24 +00:00
|
|
|
* @access private
|
|
|
|
|
*/
|
2004-10-20 09:56:34 +00:00
|
|
|
function getNearMatch( $term ) {
|
2004-04-11 05:28:11 +00:00
|
|
|
# Exact match? No need to look further.
|
2004-10-20 09:56:34 +00:00
|
|
|
$title = Title::newFromText( $term );
|
2005-06-28 17:42:47 +00:00
|
|
|
if (is_null($title))
|
|
|
|
|
return NULL;
|
|
|
|
|
|
2005-05-23 05:25:26 +00:00
|
|
|
if ( $title->getNamespace() == NS_SPECIAL || $title->exists() ) {
|
2004-08-15 08:23:19 +00:00
|
|
|
return $title;
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Now try all lower case (i.e. first letter capitalized)
|
|
|
|
|
#
|
2004-10-20 09:56:34 +00:00
|
|
|
$title = Title::newFromText( strtolower( $term ) );
|
2005-05-23 05:25:26 +00:00
|
|
|
if ( $title->exists() ) {
|
2004-08-15 08:23:19 +00:00
|
|
|
return $title;
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
2003-06-15 08:10:47 +00:00
|
|
|
|
2003-04-14 23:10:40 +00:00
|
|
|
# Now try capitalized string
|
|
|
|
|
#
|
2004-10-20 09:56:34 +00:00
|
|
|
$title = Title::newFromText( ucwords( strtolower( $term ) ) );
|
2005-05-23 05:25:26 +00:00
|
|
|
if ( $title->exists() ) {
|
2004-08-15 08:23:19 +00:00
|
|
|
return $title;
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
2003-06-15 08:10:47 +00:00
|
|
|
# Now try all upper case
|
|
|
|
|
#
|
2004-10-20 09:56:34 +00:00
|
|
|
$title = Title::newFromText( strtoupper( $term ) );
|
2005-05-23 05:25:26 +00:00
|
|
|
if ( $title->exists() ) {
|
2004-08-15 08:23:19 +00:00
|
|
|
return $title;
|
2003-06-15 08:10:47 +00:00
|
|
|
}
|
2005-07-13 06:47:17 +00:00
|
|
|
|
|
|
|
|
global $wgCapitalLinks, $wgContLang;
|
|
|
|
|
if( !$wgCapitalLinks ) {
|
|
|
|
|
// Catch differs-by-first-letter-case-only
|
|
|
|
|
$title = Title::newFromText( $wgContLang->ucfirst( $term ) );
|
|
|
|
|
if ( $title->exists() ) {
|
|
|
|
|
return $title;
|
|
|
|
|
}
|
|
|
|
|
$title = Title::newFromText( $wgContLang->lcfirst( $term ) );
|
|
|
|
|
if ( $title->exists() ) {
|
|
|
|
|
return $title;
|
|
|
|
|
}
|
|
|
|
|
}
|
2004-03-06 01:49:16 +00:00
|
|
|
|
2005-03-26 23:30:53 +00:00
|
|
|
$title = Title::newFromText( $term );
|
|
|
|
|
|
2004-05-04 14:36:42 +00:00
|
|
|
# Entering an IP address goes to the contributions page
|
2005-03-27 01:28:03 +00:00
|
|
|
if ( ( $title->getNamespace() == NS_USER && User::isIP($title->getText() ) )
|
|
|
|
|
|| User::isIP( trim( $term ) ) ) {
|
|
|
|
|
return Title::makeTitle( NS_SPECIAL, "Contributions/" . $title->getDbkey() );
|
2004-08-15 08:23:19 +00:00
|
|
|
}
|
2005-03-26 23:30:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# Entering a user goes to the user page whether it's there or not
|
2005-03-27 01:28:03 +00:00
|
|
|
if ( $title->getNamespace() == NS_USER ) {
|
|
|
|
|
return $title;
|
2005-03-27 01:23:40 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2005-04-10 00:03:49 +00:00
|
|
|
# Quoted term? Try without the quotes...
|
|
|
|
|
if( preg_match( '/^"([^"]+)"$/', $term, $matches ) ) {
|
|
|
|
|
return SearchEngine::getNearMatch( $matches[1] );
|
|
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2004-08-15 08:23:19 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2004-10-20 09:56:34 +00:00
|
|
|
function legalSearchChars() {
|
|
|
|
|
return "A-Za-z_'0-9\\x80-\\xFF\\-";
|
2004-02-11 01:48:42 +00:00
|
|
|
}
|
2003-06-15 08:10:47 +00:00
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2004-10-20 09:56:34 +00:00
|
|
|
* Set the maximum number of results to return
|
|
|
|
|
* and how many to skip before returning the first.
|
|
|
|
|
*
|
|
|
|
|
* @param int $limit
|
|
|
|
|
* @param int $offset
|
|
|
|
|
* @access public
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2004-10-20 09:56:34 +00:00
|
|
|
function setLimitOffset( $limit, $offset = 0 ) {
|
2005-08-16 23:36:16 +00:00
|
|
|
$this->limit = intval( $limit );
|
|
|
|
|
$this->offset = intval( $offset );
|
2004-02-11 01:48:42 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2004-10-20 09:56:34 +00:00
|
|
|
* Set which namespaces the search should include.
|
|
|
|
|
* Give an array of namespace index numbers.
|
|
|
|
|
*
|
|
|
|
|
* @param array $namespaces
|
|
|
|
|
* @access public
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2004-10-20 09:56:34 +00:00
|
|
|
function setNamespaces( $namespaces ) {
|
|
|
|
|
$this->namespaces = $namespaces;
|
|
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2004-10-20 09:56:34 +00:00
|
|
|
/**
|
|
|
|
|
* Make a list of searchable namespaces and their canonical names.
|
|
|
|
|
* @return array
|
|
|
|
|
* @access public
|
|
|
|
|
*/
|
|
|
|
|
function searchableNamespaces() {
|
|
|
|
|
global $wgContLang;
|
|
|
|
|
$arr = array();
|
|
|
|
|
foreach( $wgContLang->getNamespaces() as $ns => $name ) {
|
2005-01-28 05:10:05 +00:00
|
|
|
if( $ns >= NS_MAIN ) {
|
2004-10-20 09:56:34 +00:00
|
|
|
$arr[$ns] = $name;
|
2003-11-09 11:45:12 +00:00
|
|
|
}
|
2004-03-20 08:41:33 +00:00
|
|
|
}
|
2004-10-20 09:56:34 +00:00
|
|
|
return $arr;
|
2004-02-11 01:48:42 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2004-10-20 09:56:34 +00:00
|
|
|
/**
|
|
|
|
|
* Return a 'cleaned up' search string
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
* @access public
|
|
|
|
|
*/
|
|
|
|
|
function filter( $text ) {
|
|
|
|
|
$lc = $this->legalSearchChars();
|
|
|
|
|
return trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
|
|
|
|
|
}
|
2004-12-02 19:09:40 +00:00
|
|
|
/**
|
|
|
|
|
* Load up the appropriate search engine class for the currently
|
2005-05-23 05:25:26 +00:00
|
|
|
* active database backend, and return a configured instance.
|
|
|
|
|
*
|
|
|
|
|
* @return SearchEngine
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
2004-12-02 19:09:40 +00:00
|
|
|
function create() {
|
2005-08-26 23:02:54 +00:00
|
|
|
global $wgDBtype, $wgSearchType;
|
2005-05-23 08:42:20 +00:00
|
|
|
if( $wgSearchType ) {
|
|
|
|
|
$class = $wgSearchType;
|
|
|
|
|
} elseif( $wgDBtype == 'mysql' ) {
|
2005-08-26 23:02:54 +00:00
|
|
|
$class = 'SearchMySQL4';
|
|
|
|
|
require_once( 'SearchMySQL4.php' );
|
2005-05-23 05:25:26 +00:00
|
|
|
} else if ( $wgDBtype == 'PostgreSQL' ) {
|
|
|
|
|
$class = 'SearchTsearch2';
|
|
|
|
|
require_once( 'SearchTsearch2.php' );
|
|
|
|
|
} else {
|
|
|
|
|
$class = 'SearchEngineDummy';
|
|
|
|
|
}
|
|
|
|
|
$search = new $class( wfGetDB( DB_SLAVE ) );
|
|
|
|
|
$search->setLimitOffset(0,0);
|
|
|
|
|
return $search;
|
2004-12-02 19:09:40 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2005-05-23 08:42:20 +00:00
|
|
|
/**
|
|
|
|
|
* Create or update the search index record for the given page.
|
|
|
|
|
* Title and text should be pre-processed.
|
|
|
|
|
*
|
|
|
|
|
* @param int $id
|
|
|
|
|
* @param string $title
|
|
|
|
|
* @param string $text
|
|
|
|
|
* @abstract
|
|
|
|
|
*/
|
|
|
|
|
function update( $id, $title, $text ) {
|
|
|
|
|
// no-op
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update a search index record's title only.
|
|
|
|
|
* Title should be pre-processed.
|
|
|
|
|
*
|
|
|
|
|
* @param int $id
|
|
|
|
|
* @param string $title
|
|
|
|
|
* @abstract
|
|
|
|
|
*/
|
|
|
|
|
function updateTitle( $id, $title ) {
|
|
|
|
|
// no-op
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-07-05 21:22:25 +00:00
|
|
|
/** @package MediaWiki */
|
2005-05-23 08:42:20 +00:00
|
|
|
class SearchResultSet {
|
|
|
|
|
/**
|
|
|
|
|
* Fetch an array of regular expression fragments for matching
|
|
|
|
|
* the search terms as parsed by this engine in a text extract.
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
* @access public
|
|
|
|
|
* @abstract
|
|
|
|
|
*/
|
|
|
|
|
function termMatches() {
|
|
|
|
|
return array();
|
|
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2005-05-23 08:42:20 +00:00
|
|
|
function numRows() {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2005-05-23 08:42:20 +00:00
|
|
|
/**
|
|
|
|
|
* Return true if results are included in this result set.
|
|
|
|
|
* @return bool
|
|
|
|
|
* @abstract
|
|
|
|
|
*/
|
|
|
|
|
function hasResults() {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2005-05-23 08:42:20 +00:00
|
|
|
/**
|
|
|
|
|
* Some search modes return a total hit count for the query
|
|
|
|
|
* in the entire article database. This may include pages
|
|
|
|
|
* in namespaces that would not be matched on the given
|
|
|
|
|
* settings.
|
|
|
|
|
*
|
|
|
|
|
* Return null if no total hits number is supported.
|
|
|
|
|
*
|
|
|
|
|
* @return int
|
|
|
|
|
* @access public
|
|
|
|
|
*/
|
|
|
|
|
function getTotalHits() {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2005-05-23 08:42:20 +00:00
|
|
|
/**
|
|
|
|
|
* Some search modes return a suggested alternate term if there are
|
|
|
|
|
* no exact hits. Returns true if there is one on this set.
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
* @access public
|
|
|
|
|
*/
|
|
|
|
|
function hasSuggestion() {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2005-05-23 08:42:20 +00:00
|
|
|
/**
|
|
|
|
|
* Some search modes return a suggested alternate term if there are
|
|
|
|
|
* no exact hits. Check hasSuggestion() first.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
* @access public
|
|
|
|
|
*/
|
|
|
|
|
function getSuggestion() {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2005-05-23 08:42:20 +00:00
|
|
|
/**
|
|
|
|
|
* Fetches next search result, or false.
|
|
|
|
|
* @return SearchResult
|
|
|
|
|
* @access public
|
|
|
|
|
* @abstract
|
|
|
|
|
*/
|
|
|
|
|
function next() {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-07-05 21:22:25 +00:00
|
|
|
/** @package MediaWiki */
|
2005-05-23 08:42:20 +00:00
|
|
|
class SearchResult {
|
|
|
|
|
function SearchResult( $row ) {
|
|
|
|
|
$this->mTitle = Title::makeTitle( $row->page_namespace, $row->page_title );
|
|
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2005-05-23 08:42:20 +00:00
|
|
|
/**
|
|
|
|
|
* @return Title
|
|
|
|
|
* @access public
|
|
|
|
|
*/
|
|
|
|
|
function getTitle() {
|
|
|
|
|
return $this->mTitle;
|
|
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2005-05-23 08:42:20 +00:00
|
|
|
/**
|
|
|
|
|
* @return double or null if not supported
|
|
|
|
|
*/
|
|
|
|
|
function getScore() {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2004-10-20 09:56:34 +00:00
|
|
|
}
|
2004-02-11 01:48:42 +00:00
|
|
|
|
2005-01-27 19:51:47 +00:00
|
|
|
/**
|
|
|
|
|
* @package MediaWiki
|
|
|
|
|
*/
|
2004-10-20 09:56:34 +00:00
|
|
|
class SearchEngineDummy {
|
|
|
|
|
function search( $term ) {
|
|
|
|
|
return null;
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
function setLimitOffset($l, $o) {}
|
|
|
|
|
function legalSearchChars() {}
|
|
|
|
|
function update() {}
|
|
|
|
|
function setnamespaces() {}
|
|
|
|
|
function searchtitle() {}
|
|
|
|
|
function searchtext() {}
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
|
|
|
|
|