3 * Rescores results from a prefix search/opensearch to make sure the
4 * exact match is the first result.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
25 * An utility class to rescore search results by looking for an exact match
26 * in the db and add the page found to the first position.
28 * NOTE: extracted from TitlePrefixSearch
31 class SearchExactMatchRescorer
{
33 * Default search backend does proper prefix searching, but custom backends
34 * may sort based on other algorithms that may cause the exact title match
35 * to not be in the results or be lower down the list.
36 * @param string $search the query
37 * @param int[] $namespaces the namespaces
38 * @param int $limit the max number of results to return
39 * @param string[] $srchres results
40 * @return string[] munged results
42 public function rescore( $search, $namespaces, $srchres, $limit ) {
43 // Pick namespace (based on PrefixSearch::defaultSearchBackend)
44 $ns = in_array( NS_MAIN
, $namespaces ) ? NS_MAIN
: $namespaces[0];
45 $t = Title
::newFromText( $search, $ns );
46 if ( !$t ||
!$t->exists() ) {
47 // No exact match so just return the search results
50 $string = $t->getPrefixedText();
51 $key = array_search( $string, $srchres );
52 if ( $key !== false ) {
53 // Exact match was in the results so just move it to the front
54 return $this->pullFront( $key, $srchres );
56 // Exact match not in the search results so check for some redirect handling cases
57 if ( $t->isRedirect() ) {
58 $target = $this->getRedirectTarget( $t );
59 $key = array_search( $target, $srchres );
60 if ( $key !== false ) {
61 // Exact match is a redirect to one of the returned matches so pull the
62 // returned match to the front. This might look odd but the alternative
63 // is to put the redirect in front and drop the match. The name of the
64 // found match is often more descriptive/better formed than the name of
65 // the redirect AND by definition they share a prefix. Hopefully this
66 // choice is less confusing and more helpful. But it might not be. But
67 // it is the choice we're going with for now.
68 return $this->pullFront( $key, $srchres );
70 $redirectTargetsToRedirect = $this->redirectTargetsToRedirect( $srchres );
71 if ( isset( $redirectTargetsToRedirect[$target] ) ) {
72 // The exact match and something in the results list are both redirects
73 // to the same thing! In this case we'll pull the returned match to the
74 // top following the same logic above. Again, it might not be a perfect
75 // choice but it'll do.
76 return $this->pullFront( $redirectTargetsToRedirect[$target], $srchres );
79 $redirectTargetsToRedirect = $this->redirectTargetsToRedirect( $srchres );
80 if ( isset( $redirectTargetsToRedirect[$string] ) ) {
81 // The exact match is the target of a redirect already in the results list so remove
82 // the redirect from the results list and push the exact match to the front
83 array_splice( $srchres, $redirectTargetsToRedirect[$string], 1 );
84 array_unshift( $srchres, $string );
89 // Exact match is totally unique from the other results so just add it to the front
90 array_unshift( $srchres, $string );
91 // And roll one off the end if the results are too long
92 if ( count( $srchres ) > $limit ) {
93 array_pop( $srchres );
99 * @param string[] $titles as strings
100 * @return array redirect target prefixedText to index of title in titles
101 * that is a redirect to it.
103 private function redirectTargetsToRedirect( $titles ) {
105 foreach ( $titles as $key => $titleText ) {
106 $title = Title
::newFromText( $titleText );
107 if ( !$title ||
!$title->isRedirect() ) {
110 $target = $this->getRedirectTarget( $title );
114 $result[$target] = $key;
120 * Returns an array where the element of $array at index $key becomes
122 * @param int $key key to pull to the front
123 * @return array $array with the item at $key pulled to the front
125 private function pullFront( $key, $array ) {
126 $cut = array_splice( $array, $key, 1 );
127 array_unshift( $array, $cut[0] );
132 * Get a redirect's destination from a title
133 * @param Title $title A title to redirect. It may not redirect or even exist
134 * @return null|string If title exists and redirects, get the destination's prefixed name
136 private function getRedirectTarget( $title ) {
137 $page = WikiPage
::factory( $title );
138 if ( !$page->exists() ) {
141 $redir = $page->getRedirectTarget();
142 return $redir ?
$redir->getPrefixedText() : null;