r69203 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r69202‎ | r69203 | r69204 >
Date:11:08, 9 July 2010
Author:btongminh
Status:ok (Comments)
Tags:
Comment:
(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.
Modified paths:
  • /trunk/phase3/RELEASE-NOTES (modified) (history)
  • /trunk/phase3/includes/api/ApiQuerySearch.php (modified) (history)
  • /trunk/phase3/includes/search/SearchEngine.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/search/SearchEngine.php
@@ -83,6 +83,17 @@
8484 wfRunHooks( 'SearchGetNearMatchComplete', array( $searchterm, &$title ) );
8585 return $title;
8686 }
 87+
 88+ /**
 89+ * Do a near match (see SearchEngine::getNearMatch) and wrap it into a
 90+ * SearchResultSet.
 91+ *
 92+ * @param $searchterm string
 93+ * @return SearchResultSet
 94+ */
 95+ public static function getNearMatchResultSet( $searchterm ) {
 96+ return new SearchNearMatchResultSet( self::getNearMatch( $searchterm ) );
 97+ }
8798
8899 /**
89100 * Really find the title match.
@@ -573,7 +584,8 @@
574585 $row = $this->mResultSet->fetchObject();
575586 if ($row === false)
576587 return false;
577 - return new SearchResult($row);
 588+
 589+ return SearchResult::newFromRow( $row );
578590 }
579591
580592 function free() {
@@ -602,13 +614,59 @@
603615 var $mRevision = null;
604616 var $mImage = null;
605617
606 - function __construct( $row ) {
607 - $this->mTitle = Title::makeTitle( $row->page_namespace, $row->page_title );
608 - if( !is_null($this->mTitle) ){
 618+ /**
 619+ * Return a new SearchResult and initializes it with a title.
 620+ *
 621+ * @param $title Title
 622+ * @return SearchResult
 623+ */
 624+ public static function newFromTitle( $title ) {
 625+ $result = new self();
 626+ $result->initFromTitle( $title );
 627+ return $result;
 628+ }
 629+ /**
 630+ * Return a new SearchResult and initializes it with a row.
 631+ *
 632+ * @param $row object
 633+ * @return SearchResult
 634+ */
 635+ public static function newFromRow( $row ) {
 636+ $result = new self();
 637+ $result->initFromRow( $row );
 638+ return $result;
 639+ }
 640+
 641+ public function __construct( $row = null ) {
 642+ if ( !is_null( $row ) ) {
 643+ // Backwards compatibility with pre-1.17 callers
 644+ $this->initFromRow( $row );
 645+ }
 646+ }
 647+
 648+ /**
 649+ * Initialize from a database row. Makes a Title and passes that to
 650+ * initFromTitle.
 651+ *
 652+ * @param $row object
 653+ */
 654+ protected function initFromRow( $row ) {
 655+ $this->initFromTitle( Title::makeTitle( $row->page_namespace, $row->page_title ) );
 656+ }
 657+
 658+ /**
 659+ * Initialize from a Title and if possible initializes a corresponding
 660+ * Revision and File.
 661+ *
 662+ * @param $title Title
 663+ */
 664+ protected function initFromTitle( $title ) {
 665+ $this->mTitle = $title;
 666+ if( !is_null( $this->mTitle ) ){
609667 $this->mRevision = Revision::newFromTitle( $this->mTitle );
610668 if( $this->mTitle->getNamespace() === NS_FILE )
611669 $this->mImage = wfFindFile( $this->mTitle );
612 - }
 670+ }
613671 }
614672
615673 /**
@@ -751,6 +809,31 @@
752810 return '';
753811 }
754812 }
 813+/**
 814+ * A SearchResultSet wrapper for SearchEngine::getNearMatch
 815+ */
 816+class SearchNearMatchResultSet extends SearchResultSet {
 817+ private $fetched = false;
 818+ /**
 819+ * @param $match mixed Title if matched, else null
 820+ */
 821+ public function __construct( $match ) {
 822+ $this->result = $match;
 823+ }
 824+ public function hasResult() {
 825+ return (bool)$this->result;
 826+ }
 827+ public function numRows() {
 828+ return $this->hasResults() ? 1 : 0;
 829+ }
 830+ public function next() {
 831+ if ( $this->fetched || !$this->result ) {
 832+ return false;
 833+ }
 834+ $this->fetched = true;
 835+ return SearchResult::newFromTitle( $this->result );
 836+ }
 837+}
755838
756839 /**
757840 * Highlight bits of wikitext
Index: trunk/phase3/includes/api/ApiQuerySearch.php
@@ -73,6 +73,9 @@
7474 $matches = $search->searchText( $query );
7575 } elseif ( $what == 'title' ) {
7676 $matches = $search->searchTitle( $query );
 77+ } elseif ( $what == 'nearmatch' ) {
 78+ $query = str_replace( '_', ' ', $query );
 79+ $matches = SearchEngine::getNearMatchResultSet( $query );
7780 } else {
7881 // We default to title searches; this is a terrible legacy
7982 // of the way we initially set up the MySQL fulltext-based
@@ -175,6 +178,7 @@
176179 ApiBase::PARAM_TYPE => array(
177180 'title',
178181 'text',
 182+ 'nearmatch',
179183 )
180184 ),
181185 'info' => array(
Index: trunk/phase3/RELEASE-NOTES
@@ -263,6 +263,7 @@
264264 * (bug 23473) Give description of properties on all modules
265265 * (bug 24136) unknownerror when adding new section without summary, but forceditsummary
266266 * (bug 16886) Sister projects box moves down the extract of the first result in IE 7.
 267+* (bug 22339) Added srwhat=nearmatch to list=search to get a "go" result
267268
268269 === Languages updated in 1.17 ===
269270

Follow-up revisions

RevisionCommit summaryAuthorDate
r69682Follow-up r69203: remove str_replace( '_', ' ', $query ); was only needed for...btongminh14:31, 21 July 2010

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r64436(bug 22339) "Go" result should be available through the APIgurch14:08, 31 March 2010

Comments

#Comment by Catrope (talk | contribs)   15:46, 19 July 2010
+			$query = str_replace( '_', ' ', $query );

This doesn't belong here. Either embed this logic into the SearchEngine::getNearMatchResultSet() code flow somewhere (preferred) or use proper title normalization functions.

#Comment by Bryan (talk | contribs)   14:32, 21 July 2010

Removed in r69682; inspection of SpecialSearch.php indicated that it was a normalization used for the Special:Search/$par case.

Status & tagging log