Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
SearchExactMatchRescorer
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 5
380
0.00% covered (danger)
0.00%
0 / 1
 rescore
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
110
 getReplacedRedirect
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 redirectTargetsToRedirect
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
30
 pullFront
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getRedirectTarget
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * Rescores results from a prefix search/opensearch to make sure the
4 * exact match is the first result.
5 *
6 * @license GPL-2.0-or-later
7 * @file
8 */
9
10namespace MediaWiki\Search;
11
12use MediaWiki\MediaWikiServices;
13use MediaWiki\Page\PageIdentity;
14use MediaWiki\Title\Title;
15
16/**
17 * An utility class to rescore search results by looking for an exact match
18 * in the db and add the page found to the first position.
19 *
20 * @ingroup Search
21 */
22class SearchExactMatchRescorer {
23    /**
24     * @var ?string set when a redirect returned from the engine is replaced by the exact match
25     */
26    private ?string $replacedRedirect;
27
28    /**
29     * Default search backend does proper prefix searching, but custom backends
30     * may sort based on other algorithms that may cause the exact title match
31     * to not be in the results or be lower down the list.
32     * @param string $search the query
33     * @param int[] $namespaces
34     * @param string[] $srchres results
35     * @param int $limit the max number of results to return
36     * @return string[] munged results
37     */
38    public function rescore( $search, $namespaces, $srchres, $limit ) {
39        $this->replacedRedirect = null;
40        // Pick namespace
41        $ns = in_array( NS_MAIN, $namespaces ) ? NS_MAIN : reset( $namespaces );
42        $t = Title::newFromText( $search, $ns );
43        if ( !$t || !$t->exists() ) {
44            // No exact match so just return the search results
45            return $srchres;
46        }
47        $string = $t->getPrefixedText();
48        $key = array_search( $string, $srchres );
49        if ( $key !== false ) {
50            // Exact match was in the results so just move it to the front
51            return $this->pullFront( $key, $srchres );
52        }
53        // Exact match not in the search results so check for some redirect handling cases
54        if ( $t->isRedirect() ) {
55            $target = $this->getRedirectTarget( $t );
56            $key = array_search( $target, $srchres );
57            if ( $key !== false ) {
58                // Exact match is a redirect to one of the returned matches so pull the
59                // returned match to the front.  This might look odd but the alternative
60                // is to put the redirect in front and drop the match.  The name of the
61                // found match is often more descriptive/better formed than the name of
62                // the redirect AND by definition they share a prefix.  Hopefully this
63                // choice is less confusing and more helpful.  But it might not be.  But
64                // it is the choice we're going with for now.
65                return $this->pullFront( $key, $srchres );
66            }
67            $redirectTargetsToRedirect = $this->redirectTargetsToRedirect( $srchres );
68            if ( isset( $redirectTargetsToRedirect[$target] ) ) {
69                // The exact match and something in the results list are both redirects
70                // to the same thing! In this case we prefer the match the user typed.
71                $this->replacedRedirect = array_splice( $srchres, $redirectTargetsToRedirect[$target], 1 )[0];
72                array_unshift( $srchres, $string );
73                return $srchres;
74            }
75        } else {
76            $redirectTargetsToRedirect = $this->redirectTargetsToRedirect( $srchres );
77            if ( isset( $redirectTargetsToRedirect[$string] ) ) {
78                // The exact match is the target of a redirect already in the results list so remove
79                // the redirect from the results list and push the exact match to the front
80                array_splice( $srchres, $redirectTargetsToRedirect[$string], 1 );
81                array_unshift( $srchres, $string );
82                return $srchres;
83            }
84        }
85
86        // Exact match is totally unique from the other results so just add it to the front
87        array_unshift( $srchres, $string );
88        // And roll one off the end if the results are too long
89        if ( count( $srchres ) > $limit ) {
90            array_pop( $srchres );
91        }
92        return $srchres;
93    }
94
95    /**
96     * Redirect initially returned by the search engine that got replaced by a better match:
97     * - exact match to a redirect to the same page
98     * - exact match to the target page
99     * @return string|null the replaced redirect or null if nothing was replaced
100     */
101    public function getReplacedRedirect(): ?string {
102        return $this->replacedRedirect;
103    }
104
105    /**
106     * @param string[] $titles
107     * @return array redirect target prefixedText to index of title in titles
108     *   that is a redirect to it.
109     */
110    private function redirectTargetsToRedirect( array $titles ) {
111        $result = [];
112        foreach ( $titles as $key => $titleText ) {
113            $title = Title::newFromText( $titleText );
114            if ( !$title || !$title->isRedirect() ) {
115                continue;
116            }
117            $target = $this->getRedirectTarget( $title );
118            if ( !$target ) {
119                continue;
120            }
121            $result[$target] = $key;
122        }
123        return $result;
124    }
125
126    /**
127     * Returns an array where the element of $array at index $key becomes
128     * the first element.
129     * @param int $key key to pull to the front
130     * @param array $array
131     * @return array $array with the item at $key pulled to the front
132     */
133    private function pullFront( $key, array $array ) {
134        $cut = array_splice( $array, $key, 1 );
135        array_unshift( $array, $cut[0] );
136        return $array;
137    }
138
139    /**
140     * Get a redirect's destination from a title
141     * @param PageIdentity $page A page to redirect. It may not redirect or even exist
142     * @return null|string If title exists and redirects, get the destination's prefixed name
143     */
144    private function getRedirectTarget( PageIdentity $page ) {
145        $redirectStore = MediaWikiServices::getInstance()->getRedirectStore();
146        $redir = $redirectStore->getRedirectTarget( $page );
147
148        // Needed to get the text needed for display.
149        $redir = Title::castFromLinkTarget( $redir );
150        return $redir ? $redir->getPrefixedText() : null;
151    }
152}
153
154/** @deprecated class alias since 1.46 */
155class_alias( SearchExactMatchRescorer::class, 'SearchExactMatchRescorer' );