From: Bryan Tong Minh Date: Fri, 9 Jul 2010 11:08:18 +0000 (+0000) Subject: (bug 22339) Added srwhat=nearmatch to list=search to get a "go" result X-Git-Tag: 1.31.0-rc.0~36196 X-Git-Url: http://git.cyclocoop.org/%28?a=commitdiff_plain;h=bf9986554925aaea6a2f7ba64a26dd983bc4dfbb;p=lhc%2Fweb%2Fwiklou.git (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. --- diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 805ee6e96d..4973b34b8a 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -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 === diff --git a/includes/api/ApiQuerySearch.php b/includes/api/ApiQuerySearch.php index eb38b8207b..b1608fde3a 100644 --- a/includes/api/ApiQuerySearch.php +++ b/includes/api/ApiQuerySearch.php @@ -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( diff --git a/includes/search/SearchEngine.php b/includes/search/SearchEngine.php index b8db2ec349..ce7457485f 100644 --- a/includes/search/SearchEngine.php +++ b/includes/search/SearchEngine.php @@ -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