(bug 22339) Added srwhat=nearmatch to list=search to get a "go" result

Refactored SearchResult constructor to accept Titles. New static function newFromRow and newFromTitle can be used.
Added SearchNearMatchResultSet to wrap the output of SearchEngine::getNearMatch.
This commit is contained in:
Bryan Tong Minh 2010-07-09 11:08:18 +00:00
parent 55c96b6ca3
commit bf99865549
3 changed files with 93 additions and 5 deletions

View file

@ -263,6 +263,7 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN
* (bug 23473) Give description of properties on all modules
* (bug 24136) unknownerror when adding new section without summary, but forceditsummary
* (bug 16886) Sister projects box moves down the extract of the first result in IE 7.
* (bug 22339) Added srwhat=nearmatch to list=search to get a "go" result
=== Languages updated in 1.17 ===

View file

@ -73,6 +73,9 @@ class ApiQuerySearch extends ApiQueryGeneratorBase {
$matches = $search->searchText( $query );
} elseif ( $what == 'title' ) {
$matches = $search->searchTitle( $query );
} elseif ( $what == 'nearmatch' ) {
$query = str_replace( '_', ' ', $query );
$matches = SearchEngine::getNearMatchResultSet( $query );
} else {
// We default to title searches; this is a terrible legacy
// of the way we initially set up the MySQL fulltext-based
@ -175,6 +178,7 @@ class ApiQuerySearch extends ApiQueryGeneratorBase {
ApiBase::PARAM_TYPE => array(
'title',
'text',
'nearmatch',
)
),
'info' => array(

View file

@ -83,6 +83,17 @@ class SearchEngine {
wfRunHooks( 'SearchGetNearMatchComplete', array( $searchterm, &$title ) );
return $title;
}
/**
* Do a near match (see SearchEngine::getNearMatch) and wrap it into a
* SearchResultSet.
*
* @param $searchterm string
* @return SearchResultSet
*/
public static function getNearMatchResultSet( $searchterm ) {
return new SearchNearMatchResultSet( self::getNearMatch( $searchterm ) );
}
/**
* Really find the title match.
@ -573,7 +584,8 @@ class SqlSearchResultSet extends SearchResultSet {
$row = $this->mResultSet->fetchObject();
if ($row === false)
return false;
return new SearchResult($row);
return SearchResult::newFromRow( $row );
}
function free() {
@ -602,13 +614,59 @@ class SearchResult {
var $mRevision = null;
var $mImage = null;
function __construct( $row ) {
$this->mTitle = Title::makeTitle( $row->page_namespace, $row->page_title );
if( !is_null($this->mTitle) ){
/**
* Return a new SearchResult and initializes it with a title.
*
* @param $title Title
* @return SearchResult
*/
public static function newFromTitle( $title ) {
$result = new self();
$result->initFromTitle( $title );
return $result;
}
/**
* Return a new SearchResult and initializes it with a row.
*
* @param $row object
* @return SearchResult
*/
public static function newFromRow( $row ) {
$result = new self();
$result->initFromRow( $row );
return $result;
}
public function __construct( $row = null ) {
if ( !is_null( $row ) ) {
// Backwards compatibility with pre-1.17 callers
$this->initFromRow( $row );
}
}
/**
* Initialize from a database row. Makes a Title and passes that to
* initFromTitle.
*
* @param $row object
*/
protected function initFromRow( $row ) {
$this->initFromTitle( Title::makeTitle( $row->page_namespace, $row->page_title ) );
}
/**
* Initialize from a Title and if possible initializes a corresponding
* Revision and File.
*
* @param $title Title
*/
protected function initFromTitle( $title ) {
$this->mTitle = $title;
if( !is_null( $this->mTitle ) ){
$this->mRevision = Revision::newFromTitle( $this->mTitle );
if( $this->mTitle->getNamespace() === NS_FILE )
$this->mImage = wfFindFile( $this->mTitle );
}
}
}
/**
@ -751,6 +809,31 @@ class SearchResult {
return '';
}
}
/**
* A SearchResultSet wrapper for SearchEngine::getNearMatch
*/
class SearchNearMatchResultSet extends SearchResultSet {
private $fetched = false;
/**
* @param $match mixed Title if matched, else null
*/
public function __construct( $match ) {
$this->result = $match;
}
public function hasResult() {
return (bool)$this->result;
}
public function numRows() {
return $this->hasResults() ? 1 : 0;
}
public function next() {
if ( $this->fetched || !$this->result ) {
return false;
}
$this->fetched = true;
return SearchResult::newFromTitle( $this->result );
}
}
/**
* Highlight bits of wikitext