Index: trunk/phase3/includes/search/SearchEngine.php |
— | — | @@ -83,6 +83,17 @@ |
84 | 84 | wfRunHooks( 'SearchGetNearMatchComplete', array( $searchterm, &$title ) ); |
85 | 85 | return $title; |
86 | 86 | } |
| 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 | + } |
87 | 98 | |
88 | 99 | /** |
89 | 100 | * Really find the title match. |
— | — | @@ -573,7 +584,8 @@ |
574 | 585 | $row = $this->mResultSet->fetchObject(); |
575 | 586 | if ($row === false) |
576 | 587 | return false; |
577 | | - return new SearchResult($row); |
| 588 | + |
| 589 | + return SearchResult::newFromRow( $row ); |
578 | 590 | } |
579 | 591 | |
580 | 592 | function free() { |
— | — | @@ -602,13 +614,59 @@ |
603 | 615 | var $mRevision = null; |
604 | 616 | var $mImage = null; |
605 | 617 | |
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 ) ){ |
609 | 667 | $this->mRevision = Revision::newFromTitle( $this->mTitle ); |
610 | 668 | if( $this->mTitle->getNamespace() === NS_FILE ) |
611 | 669 | $this->mImage = wfFindFile( $this->mTitle ); |
612 | | - } |
| 670 | + } |
613 | 671 | } |
614 | 672 | |
615 | 673 | /** |
— | — | @@ -751,6 +809,31 @@ |
752 | 810 | return ''; |
753 | 811 | } |
754 | 812 | } |
| 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 | +} |
755 | 838 | |
756 | 839 | /** |
757 | 840 | * Highlight bits of wikitext |
Index: trunk/phase3/includes/api/ApiQuerySearch.php |
— | — | @@ -73,6 +73,9 @@ |
74 | 74 | $matches = $search->searchText( $query ); |
75 | 75 | } elseif ( $what == 'title' ) { |
76 | 76 | $matches = $search->searchTitle( $query ); |
| 77 | + } elseif ( $what == 'nearmatch' ) { |
| 78 | + $query = str_replace( '_', ' ', $query ); |
| 79 | + $matches = SearchEngine::getNearMatchResultSet( $query ); |
77 | 80 | } else { |
78 | 81 | // We default to title searches; this is a terrible legacy |
79 | 82 | // of the way we initially set up the MySQL fulltext-based |
— | — | @@ -175,6 +178,7 @@ |
176 | 179 | ApiBase::PARAM_TYPE => array( |
177 | 180 | 'title', |
178 | 181 | 'text', |
| 182 | + 'nearmatch', |
179 | 183 | ) |
180 | 184 | ), |
181 | 185 | 'info' => array( |
Index: trunk/phase3/RELEASE-NOTES |
— | — | @@ -263,6 +263,7 @@ |
264 | 264 | * (bug 23473) Give description of properties on all modules |
265 | 265 | * (bug 24136) unknownerror when adding new section without summary, but forceditsummary |
266 | 266 | * (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 |
267 | 268 | |
268 | 269 | === Languages updated in 1.17 === |
269 | 270 | |