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
|
|
|
|
|
*/
|
2004-09-02 23:28:24 +00:00
|
|
|
|
2004-10-24 19:14:48 +00:00
|
|
|
/** */
|
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;
|
|
|
|
|
|
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.
|
|
|
|
|
*
|
|
|
|
|
* @param string $term - Raw search term
|
|
|
|
|
* @param array $namespaces - List of namespaces to search
|
|
|
|
|
* @return ResultWrapper
|
|
|
|
|
* @access public
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2004-10-20 09:56:34 +00:00
|
|
|
function searchText( $term ) {
|
|
|
|
|
return $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), true ) ) );
|
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.
|
|
|
|
|
*
|
|
|
|
|
* @param string $term - Raw search term
|
|
|
|
|
* @param array $namespaces - List of namespaces to search
|
|
|
|
|
* @return ResultWrapper
|
|
|
|
|
* @access public
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2004-10-20 09:56:34 +00:00
|
|
|
function searchTitle( $term ) {
|
|
|
|
|
return $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), false ) ) );
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
2004-10-20 09:56:34 +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.
|
|
|
|
|
*
|
|
|
|
|
* @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 );
|
2004-08-15 08:23:19 +00:00
|
|
|
if ( $title->getNamespace() == NS_SPECIAL || 0 != $title->getArticleID() ) {
|
|
|
|
|
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 ) );
|
2004-08-15 08:23:19 +00:00
|
|
|
if ( 0 != $title->getArticleID() ) {
|
|
|
|
|
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 ) ) );
|
2004-08-15 08:23:19 +00:00
|
|
|
if ( 0 != $title->getArticleID() ) {
|
|
|
|
|
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 ) );
|
2004-08-15 08:23:19 +00:00
|
|
|
if ( 0 != $title->getArticleID() ) {
|
|
|
|
|
return $title;
|
2003-06-15 08:10:47 +00:00
|
|
|
}
|
2004-03-06 01:49:16 +00:00
|
|
|
|
2004-05-04 14:36:42 +00:00
|
|
|
# Entering an IP address goes to the contributions page
|
2004-10-20 09:56:34 +00:00
|
|
|
if ( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/', $term ) ) {
|
|
|
|
|
$title = Title::makeTitle( NS_SPECIAL, "Contributions/" . $term );
|
2004-08-15 08:23:19 +00:00
|
|
|
return $title;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
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 ) {
|
|
|
|
|
$this->limit = IntVal( $limit );
|
|
|
|
|
$this->offset = IntVal( $offset );
|
2004-02-11 01:48:42 +00:00
|
|
|
}
|
2004-10-20 09:56:34 +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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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 ) {
|
|
|
|
|
if( $ns >= 0 ) {
|
|
|
|
|
$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
|
|
|
}
|
2004-10-20 09:56:34 +00:00
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2004-10-20 09:56:34 +00:00
|
|
|
* 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
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2004-10-20 09:56:34 +00:00
|
|
|
function termMatches() {
|
|
|
|
|
return $this->searchTerms;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return a 'cleaned up' search string
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
* @access public
|
|
|
|
|
*/
|
|
|
|
|
function filter( $text ) {
|
|
|
|
|
$lc = $this->legalSearchChars();
|
|
|
|
|
return trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return a partial WHERE clause to exclude redirects, if so set
|
|
|
|
|
* @return string
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
|
|
|
|
function queryRedirect() {
|
|
|
|
|
if( $this->showRedirects ) {
|
|
|
|
|
return 'AND cur_is_redirect=0';
|
|
|
|
|
} else {
|
|
|
|
|
return '';
|
2004-02-11 12:03:01 +00:00
|
|
|
}
|
2004-10-20 09:56:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return a partial WHERE clause to limit the search to the given namespaces
|
|
|
|
|
* @return string
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
|
|
|
|
function queryNamespaces() {
|
|
|
|
|
$namespaces = implode( ',', $this->namespaces );
|
|
|
|
|
if ($namespaces == '') {
|
|
|
|
|
$namespaces = '0';
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
2004-10-20 09:56:34 +00:00
|
|
|
return 'AND cur_namespace IN (' . $namespaces . ')';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return a LIMIT clause to limit results on the query.
|
|
|
|
|
* @return string
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
|
|
|
|
function queryLimit() {
|
|
|
|
|
return $this->db->limitResult( $this->limit, $this->offset );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Construct the full SQL query to do the search.
|
|
|
|
|
* The guts shoulds be constructed in queryMain()
|
|
|
|
|
* @param string $filteredTerm
|
|
|
|
|
* @param bool $fulltext
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
|
|
|
|
function getQuery( $filteredTerm, $fulltext ) {
|
|
|
|
|
return $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
|
|
|
|
|
$this->queryRedirect() . ' ' .
|
|
|
|
|
$this->queryNamespaces() . ' ' .
|
|
|
|
|
$this->queryLimit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2004-02-11 01:48:42 +00:00
|
|
|
|
2004-10-24 19:14:48 +00:00
|
|
|
/** */
|
2004-10-20 09:56:34 +00:00
|
|
|
class SearchEngineDummy {
|
|
|
|
|
function search( $term ) {
|
|
|
|
|
return null;
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-02-11 01:48:42 +00:00
|
|
|
|
2004-10-24 19:14:48 +00:00
|
|
|
?>
|