MediaWiki 1.41.2
SearchExactMatchRescorer.php
Go to the documentation of this file.
1<?php
26
45 public function rescore( $search, $namespaces, $srchres, $limit ) {
46 // Pick namespace (based on PrefixSearch::defaultSearchBackend)
47 $ns = in_array( NS_MAIN, $namespaces ) ? NS_MAIN : reset( $namespaces );
48 $t = Title::newFromText( $search, $ns );
49 if ( !$t || !$t->exists() ) {
50 // No exact match so just return the search results
51 return $srchres;
52 }
53 $string = $t->getPrefixedText();
54 $key = array_search( $string, $srchres );
55 if ( $key !== false ) {
56 // Exact match was in the results so just move it to the front
57 return $this->pullFront( $key, $srchres );
58 }
59 // Exact match not in the search results so check for some redirect handling cases
60 if ( $t->isRedirect() ) {
61 $target = $this->getRedirectTarget( $t );
62 $key = array_search( $target, $srchres );
63 if ( $key !== false ) {
64 // Exact match is a redirect to one of the returned matches so pull the
65 // returned match to the front. This might look odd but the alternative
66 // is to put the redirect in front and drop the match. The name of the
67 // found match is often more descriptive/better formed than the name of
68 // the redirect AND by definition they share a prefix. Hopefully this
69 // choice is less confusing and more helpful. But it might not be. But
70 // it is the choice we're going with for now.
71 return $this->pullFront( $key, $srchres );
72 }
73 $redirectTargetsToRedirect = $this->redirectTargetsToRedirect( $srchres );
74 if ( isset( $redirectTargetsToRedirect[$target] ) ) {
75 // The exact match and something in the results list are both redirects
76 // to the same thing! In this case we'll pull the returned match to the
77 // top following the same logic above. Again, it might not be a perfect
78 // choice but it'll do.
79 return $this->pullFront( $redirectTargetsToRedirect[$target], $srchres );
80 }
81 } else {
82 $redirectTargetsToRedirect = $this->redirectTargetsToRedirect( $srchres );
83 if ( isset( $redirectTargetsToRedirect[$string] ) ) {
84 // The exact match is the target of a redirect already in the results list so remove
85 // the redirect from the results list and push the exact match to the front
86 array_splice( $srchres, $redirectTargetsToRedirect[$string], 1 );
87 array_unshift( $srchres, $string );
88 return $srchres;
89 }
90 }
91
92 // Exact match is totally unique from the other results so just add it to the front
93 array_unshift( $srchres, $string );
94 // And roll one off the end if the results are too long
95 if ( count( $srchres ) > $limit ) {
96 array_pop( $srchres );
97 }
98 return $srchres;
99 }
100
106 private function redirectTargetsToRedirect( array $titles ) {
107 $result = [];
108 foreach ( $titles as $key => $titleText ) {
109 $title = Title::newFromText( $titleText );
110 if ( !$title || !$title->isRedirect() ) {
111 continue;
112 }
113 $target = $this->getRedirectTarget( $title );
114 if ( !$target ) {
115 continue;
116 }
117 $result[$target] = $key;
118 }
119 return $result;
120 }
121
129 private function pullFront( $key, array $array ) {
130 $cut = array_splice( $array, $key, 1 );
131 array_unshift( $array, $cut[0] );
132 return $array;
133 }
134
140 private function getRedirectTarget( $title ) {
141 $redirectStore = MediaWikiServices::getInstance()->getRedirectStore();
142 $redir = $redirectStore->getRedirectTarget( $title );
143
144 // Needed to get the text needed for display.
145 $redir = Title::castFromLinkTarget( $redir );
146 return $redir ? $redir->getPrefixedText() : null;
147 }
148}
const NS_MAIN
Definition Defines.php:64
Service locator for MediaWiki core services.
Represents a title within MediaWiki.
Definition Title.php:76
An utility class to rescore search results by looking for an exact match in the db and add the page f...
rescore( $search, $namespaces, $srchres, $limit)
Default search backend does proper prefix searching, but custom backends may sort based on other algo...