3 * Prefix search of page names.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 use MediaWiki\MediaWikiServices
;
26 * Handles searching prefixes of titles and finding any page
27 * names that match. Used largely by the OpenSearch implementation.
28 * @deprecated Since 1.27, Use SearchEngine::defaultPrefixSearch or SearchEngine::completionSearch
32 abstract class PrefixSearch
{
34 * Do a prefix search of titles and return a list of matching page names.
36 * @param string $search
38 * @param array $namespaces Used if query is not explicitly prefixed
39 * @param int $offset How many results to offset from the beginning
40 * @return array Array of strings or Title objects
42 public function search( $search, $limit, $namespaces = [], $offset = 0 ) {
43 $search = trim( $search );
44 if ( $search == '' ) {
45 return []; // Return empty result
48 $hasNamespace = SearchEngine
::parseNamespacePrefixes( $search, false, true );
49 if ( $hasNamespace !== false ) {
50 list( $search, $namespaces ) = $hasNamespace;
53 return $this->searchBackend( $namespaces, $search, $limit, $offset );
57 * Do a prefix search for all possible variants of the prefix
58 * @param string $search
60 * @param array $namespaces
61 * @param int $offset How many results to offset from the beginning
65 public function searchWithVariants( $search, $limit, array $namespaces, $offset = 0 ) {
66 $searches = $this->search( $search, $limit, $namespaces, $offset );
68 // if the content language has variants, try to retrieve fallback results
69 $fallbackLimit = $limit - count( $searches );
70 if ( $fallbackLimit > 0 ) {
71 $fallbackSearches = MediaWikiServices
::getInstance()->getContentLanguage()->
72 autoConvertToAllVariants( $search );
73 $fallbackSearches = array_diff( array_unique( $fallbackSearches ), [ $search ] );
75 foreach ( $fallbackSearches as $fbs ) {
76 $fallbackSearchResult = $this->search( $fbs, $fallbackLimit, $namespaces );
77 $searches = array_merge( $searches, $fallbackSearchResult );
78 $fallbackLimit -= count( $fallbackSearchResult );
80 if ( $fallbackLimit == 0 ) {
89 * When implemented in a descendant class, receives an array of Title objects and returns
90 * either an unmodified array or an array of strings corresponding to titles passed to it.
92 * @param array $titles
95 abstract protected function titles( array $titles );
98 * When implemented in a descendant class, receives an array of titles as strings and returns
99 * either an unmodified array or an array of Title objects corresponding to strings received.
101 * @param array $strings
105 abstract protected function strings( array $strings );
108 * Do a prefix search of titles and return a list of matching page names.
109 * @param array $namespaces
110 * @param string $search
112 * @param int $offset How many results to offset from the beginning
113 * @return array Array of strings
115 protected function searchBackend( $namespaces, $search, $limit, $offset ) {
116 if ( count( $namespaces ) == 1 ) {
117 $ns = $namespaces[0];
118 if ( $ns == NS_MEDIA
) {
119 $namespaces = [ NS_FILE
];
120 } elseif ( $ns == NS_SPECIAL
) {
121 return $this->titles( $this->specialSearch( $search, $limit, $offset ) );
126 'PrefixSearchBackend',
127 [ $namespaces, $search, $limit, &$srchres, $offset ]
129 return $this->titles( $this->defaultSearchBackend( $namespaces, $search, $limit, $offset ) );
131 return $this->strings(
132 $this->handleResultFromHook( $srchres, $namespaces, $search, $limit, $offset ) );
135 private function handleResultFromHook( $srchres, $namespaces, $search, $limit, $offset ) {
136 if ( $offset === 0 ) {
137 // Only perform exact db match if offset === 0
138 // This is still far from perfect but at least we avoid returning the
139 // same title afain and again when the user is scrolling with a query
140 // that matches a title in the db.
141 $rescorer = new SearchExactMatchRescorer();
142 $srchres = $rescorer->rescore( $search, $namespaces, $srchres, $limit );
148 * Prefix search special-case for Special: namespace.
150 * @param string $search Term
151 * @param int $limit Max number of items to return
152 * @param int $offset Number of items to offset
155 protected function specialSearch( $search, $limit, $offset ) {
156 $searchParts = explode( '/', $search, 2 );
157 $searchKey = $searchParts[0];
158 $subpageSearch = $searchParts[1] ??
null;
160 // Handle subpage search separately.
161 $spFactory = MediaWikiServices
::getInstance()->getSpecialPageFactory();
162 if ( $subpageSearch !== null ) {
163 // Try matching the full search string as a page name
164 $specialTitle = Title
::makeTitleSafe( NS_SPECIAL
, $searchKey );
165 if ( !$specialTitle ) {
168 $special = $spFactory->getPage( $specialTitle->getText() );
170 $subpages = $special->prefixSearchSubpages( $subpageSearch, $limit, $offset );
171 return array_map( function ( $sub ) use ( $specialTitle ) {
172 return $specialTitle->getSubpage( $sub );
179 # normalize searchKey, so aliases with spaces can be found - T27675
180 $contLang = MediaWikiServices
::getInstance()->getContentLanguage();
181 $searchKey = str_replace( ' ', '_', $searchKey );
182 $searchKey = $contLang->caseFold( $searchKey );
184 // Unlike SpecialPage itself, we want the canonical forms of both
185 // canonical and alias title forms...
187 foreach ( $spFactory->getNames() as $page ) {
188 $keys[$contLang->caseFold( $page )] = [ 'page' => $page, 'rank' => 0 ];
191 foreach ( $contLang->getSpecialPageAliases() as $page => $aliases ) {
192 if ( !in_array( $page, $spFactory->getNames() ) ) {# T22885
196 foreach ( $aliases as $key => $alias ) {
197 $keys[$contLang->caseFold( $alias )] = [ 'page' => $alias, 'rank' => $key ];
203 foreach ( $keys as $pageKey => $page ) {
204 if ( $searchKey === '' ||
strpos( $pageKey, $searchKey ) === 0 ) {
205 // T29671: Don't use SpecialPage::getTitleFor() here because it
206 // localizes its input leading to searches for e.g. Special:All
207 // returning Spezial:MediaWiki-Systemnachrichten and returning
208 // Spezial:Alle_Seiten twice when $wgLanguageCode == 'de'
209 $matches[$page['rank']][] = Title
::makeTitleSafe( NS_SPECIAL
, $page['page'] );
211 if ( isset( $matches[0] ) && count( $matches[0] ) >= $limit +
$offset ) {
212 // We have enough items in primary rank, no use to continue
219 // Ensure keys are in order
222 $matches = array_reduce( $matches, 'array_merge', [] );
224 return array_slice( $matches, $offset, $limit );
228 * Unless overridden by PrefixSearchBackend hook...
229 * This is case-sensitive (First character may
230 * be automatically capitalized by Title::secureAndSpit()
231 * later on depending on $wgCapitalLinks)
233 * @param array|null $namespaces Namespaces to search in
234 * @param string $search Term
235 * @param int $limit Max number of items to return
236 * @param int $offset Number of items to skip
237 * @return Title[] Array of Title objects
239 public function defaultSearchBackend( $namespaces, $search, $limit, $offset ) {
240 // Backwards compatability with old code. Default to NS_MAIN if no namespaces provided.
241 if ( $namespaces === null ) {
244 if ( !$namespaces ) {
245 $namespaces[] = NS_MAIN
;
248 // Construct suitable prefix for each namespace. They differ in cases where
249 // some namespaces always capitalize and some don't.
251 foreach ( $namespaces as $namespace ) {
252 // For now, if special is included, ignore the other namespaces
253 if ( $namespace == NS_SPECIAL
) {
254 return $this->specialSearch( $search, $limit, $offset );
257 $title = Title
::makeTitleSafe( $namespace, $search );
258 // Why does the prefix default to empty?
259 $prefix = $title ?
$title->getDBkey() : '';
260 $prefixes[$prefix][] = $namespace;
263 $dbr = wfGetDB( DB_REPLICA
);
264 // Often there is only one prefix that applies to all requested namespaces,
265 // but sometimes there are two if some namespaces do not always capitalize.
267 foreach ( $prefixes as $prefix => $namespaces ) {
269 'page_namespace' => $namespaces,
270 'page_title' . $dbr->buildLike( $prefix, $dbr->anyString() ),
272 $conds[] = $dbr->makeList( $condition, LIST_AND
);
276 $fields = [ 'page_id', 'page_namespace', 'page_title' ];
277 $conds = $dbr->makeList( $conds, LIST_OR
);
280 'ORDER BY' => [ 'page_title', 'page_namespace' ],
284 $res = $dbr->select( $table, $fields, $conds, __METHOD__
, $options );
286 return iterator_to_array( TitleArray
::newFromResult( $res ) );
290 * Validate an array of numerical namespace indexes
292 * @param array $namespaces
293 * @return array (default: contains only NS_MAIN)
295 protected function validateNamespaces( $namespaces ) {
296 // We will look at each given namespace against content language namespaces
297 $validNamespaces = MediaWikiServices
::getInstance()->getContentLanguage()->getNamespaces();
298 if ( is_array( $namespaces ) && count( $namespaces ) > 0 ) {
300 foreach ( $namespaces as $ns ) {
301 if ( is_numeric( $ns ) && array_key_exists( $ns, $validNamespaces ) ) {
305 if ( count( $valid ) > 0 ) {